-
Notifications
You must be signed in to change notification settings - Fork 3
/
updatehelper.js
213 lines (177 loc) · 5.08 KB
/
updatehelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* copyright: IBM Confidential
* copyright: OCO Source Materials
* copyright: © IBM Corp. All Rights Reserved
* date: 2020
*
* IBM Certificate of Originality
*/
"use strict";
const Utils = require("./utils");
const Serializer = require("./serializer");
const GraphFactory = require("./graphfactory");
const HK = require("./hk");
const HKTypes = require("hklib").Types;
function serializeToUpdate(entities, oldEntities, conversionOptions)
{
let changedParents = {};
let changeEntities = {};
for(let k in entities)
{
let entity = entities[k];
if(!entity.id)
{
continue;
}
if(entity.properties || entity.metaProperties || (entity.interfaces || entity.interfaces === null) || entity.className)
{
let triples = [];
let propertiesSet = new Set();
let metaProperties = {};
// Collect properties and metaproperties that are being modified
let metaAndProperties = Object.keys(entity.properties || {});
metaAndProperties = metaAndProperties.concat(Object.keys(entity.metaProperties || {}) || []);
propertiesSet = new Set(metaAndProperties);
let interfaceChanges = _evaluateInterfacesChanges(entity);
if(propertiesSet.size > 0 || interfaceChanges.interfacesKeys.length > 0 || entity.className)
{
triples = _convertEntityToTriples(entity, conversionOptions, interfaceChanges.interfacesToUpdate, propertiesSet);
}
else if(entity.interfaces === null && oldEntities.hasOwnProperty(entity.id))
{
entity.interfaces = oldEntities[entity.id].interfaces || {};
for(let interfaceKey in entity.interfaces)
{
entity.interfaces[interfaceKey] = null;
}
interfaceChanges = _evaluateInterfacesChanges(entity);
triples = _convertEntityToTriples(entity, conversionOptions, interfaceChanges.interfacesToUpdate, propertiesSet);
}
let changes = {properties: Array.from(propertiesSet),
interfacesToUpdate: interfaceChanges.interfacesToUpdate,
interfacesToRemove: interfaceChanges.interfacesToRemove,
interfacesPropertites: interfaceChanges.interfacesProperties,
metaProperties: metaProperties,
triples: triples,};
changeEntities[entity.id] = changes;
}
// Collect entities that being reparent
if(entity.hasOwnProperty("parent"))
{
changedParents[entity.id] = entity.parent;
}
}
return {changedEntities: changeEntities, changedParents: changedParents};
}
function _convertEntityToTriples(entity, conversionOptions, interfacesToUpdate, propertiesSet = new Set())
{
let obj = {id: entity.id,
properties: entity.properties || {},
metaProperties: entity.metaProperties || {},
interfaces: interfacesToUpdate};
if(entity.type === HKTypes.REFERENCE)
{
obj.type = HKTypes.REFERENCE;
obj.ref = entity.ref;
}
if(entity.type === HKTypes.CONNECTOR)
{
propertiesSet.add(HK.CLASSNAME_URI);
obj.className = entity.className;
obj.type = HKTypes.CONNECTOR;
obj.roles = entity.roles;
}
if(entity.hasOwnProperty("parent"))
{
obj.parent = entity.parent;
}
let graph = GraphFactory.createGraph("application/json",true);
Serializer.serialize([obj], conversionOptions, graph);
let triples = [];
// Escape literal, this is for sparql
graph.forEachStatement((s, p, o, g) =>
{
// let t = triples[i];
let t = [s, p, o, g];
if(!Utils.isUriOrBlankNode(t[2]))
{
let info = {};
let v = Utils.getValueFromLiteral(t[2], info);
// Escape string literal
let hasBreakLine = v.search("\n");
let escapedQuote = hasBreakLine > 0 ? "\\\"\"\"" : "\\\"";
let q = hasBreakLine > 0 ? "\"\"\"" : "\"";
if(! conversionOptions.noEscape)
{
v = JSON.stringify(v).slice(1, -1);
}
if(hasBreakLine >= 0)
{
v = v.replace(new RegExp(q, "ig"), escapedQuote);
}
if(info.type)
{
let type = Utils.isUri(info.type) ? info.type : `<${Utils.generateResourceFromId(info.type)}>`;
t[2] = `${q}${v}${q}^^${type}`;
}
else if(info.lang)
{
t[2] = `${q}${v}${q}@${info.lang}`;
}
else
{
if( ! conversionOptions.noEscape)
{
t[2] = `${q}${v}${q}`;
}
else
{
let literal = v.split("^^");
if(literal[1])
{
t[2] = `${literal[0]}^^<${Utils.generateResourceFromId(literal[1])}>`;
}
else
{
t[2] = `${q}${v}${q}`;
}
}
}
}
triples.push(t);
});
return triples;
}
function _evaluateInterfacesChanges(entity)
{
// Collect interfaces
let interfs = entity.interfaces || {};
let interfacesKeys = Object.keys(interfs);
let interfacesToRemove = [];
let interfacesToUpdate = {};
let interfacesProperties = {};
for(let k in interfs)
{
let interf = interfs[k];
if(interf)
{
interfacesToUpdate[interf.key || k] = interf ;
let properties = Object.keys(interf.properties || {});
if(properties.length > 0)
{
interfacesProperties[interf.key || k] = properties;
}
}
else
{
interfacesToRemove.push(k);
}
}
return {
interfacesKeys: interfacesKeys,
interfacesToRemove: interfacesToRemove,
interfacesToUpdate: interfacesToUpdate,
interfacesProperties: interfacesProperties
};
}
exports.serializeToUpdate = serializeToUpdate;