-
Notifications
You must be signed in to change notification settings - Fork 3
/
serializer.js
384 lines (336 loc) · 11.4 KB
/
serializer.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/*
* Copyright (c) 2016-present, IBM Research
* Licensed under The MIT License [see LICENSE for details]
*/
"use strict";
const { HKTypes, HKEntity, Connector, Link, Reference, RoleTypes } = require("hklib");
const Utils = require("./utils");
const Constants = require("./constants");
const TriGGraph = require("./triggraph");
const HKSerializer = require("./hkserializer");
const OWLSerializer = require("./simpleowlserializer");
const OLWTimeSerializer = require("./owltimeserializer");
const hk = require("./hk");
/**
* Convert Hyperknowledge entities to a rdflib.js graph.
*
* @param {Array} entities Entities to convert.
* @param {IndexedFormula} graph Graph to update, if it is null create a new graph
* @param {object} options Dictionary with parameters
* @param {boolean} [options.subjectLabel] Set the subject role name `subject`, can be null
* @param {boolean} [options.objectLabel] Set the object role name `object`, can be null
* @param {boolean} [options.convertHK] Generate additional triples to build hyperknowledge entities
* @param {boolean} [options.convertOwl] Uses owl rules to convert entities
* @param {boolean} [options.convertOwlTime] Uses owl Time rules to convert entities
* @param {boolean} [options.timeContext] Context for owl Time conversion generated entities and relationships.
* @param {boolean} [options.skipRefNodes] Skip extra reference nodes in case convertHK is true. Default is false.
* @param {boolean} [options.inverseRefNode] When convertHK is true, the triple of ref nodes are inversed. That is, generates 'uri isReferenceBy refId' instead of 'refId references uri', . This promotes rdf view of data.
* @param {boolean} [options.compressReification] If convertHK is true, links will be reificated, the compress mode will generate the minimum of triples, default is false
* @param {boolean} [options.convertNumber] Convert the number/boolean to xsd schema
* @param {boolean} [options.reifyArray] Reify the arrays, default is false.
* @param {boolean} [options.defaultGraph] The uri to set when the parent of a entity is null
* @param {boolean} [options.suppressDuplicates] If true, duplicate quads will be suppressed from the output graph. Otherwise, they will not be suppressed. Defaults to true.
* @param {object} referenceMap A string indexed map. Where the index is a refnode id and the value is the refnode itself.
* @returns An instance of rdflib.js graph.
*/
function serialize(entities, options = {}, graph = new TriGGraph(), referenceMap = {})
{
if (!entities)
{
return graph;
}
let connectors = {};
let literalAsNodeTriples = {};
let defaultGraph = options.defaultGraph || null;
let suppressDuplicates = options.hasOwnProperty('suppressDuplicates') ? options.suppressDuplicates : true;
if (options.convertHK)
{
if (!options.hasOwnProperty("reifyArray"))
{
options.reifyArray = true;
// compressArray = true;
}
}
let subjectLabel = null;
let objectLabel = null;
options.owlSerializer = new OWLSerializer(graph, options);
options.owlTimeSerializer = new OLWTimeSerializer(graph, options);
let hkSerializer = new HKSerializer(graph, options);
// We can set subjectLabel and objectLabel as null
if (options.hasOwnProperty("subjectLabel"))
{
subjectLabel = options.subjectLabel;
}
else
{
subjectLabel = Constants.DEFAULT_SUBJECT_ROLE;
}
if (options.hasOwnProperty("objectLabel"))
{
objectLabel = options.objectLabel;
}
else
{
objectLabel = Constants.DEFAULT_OBJECT_ROLE;
}
// Collect connectors, references and literal links
for (let k in entities)
{
let entity = entities[k];
if (entity.type === Connector.type)
{
connectors[entity.id] = entity;
_collectProperties(entity, graph, options);
}
else if (entity.type === Reference.type)
{
referenceMap[entity.id] = entity;
}
else if (entity.type === Link.type)
{
const literalTypeId = hk.DATA_LITERAL_URI;
const hasLiteralProperty = entity.properties && entity.properties.hasOwnProperty(literalTypeId);
const hasLiteralMetaProperty = entity.metaProperties && entity.metaProperties.hasOwnProperty(literalTypeId);
if (hasLiteralProperty || hasLiteralMetaProperty)
{
const subject = Object.keys(entity.binds[subjectLabel])[0];
const predicate = entity.connector;
const object = entities[Object.keys(entity.binds[objectLabel])[0]].getProperty('data');
const graph = entity.parent;
literalAsNodeTriples[entity.id] = { subject, predicate, object, graph };
}
}
}
for (let k in entities)
{
let entity = entities[k];
switch (entity.type)
{
case HKTypes.CONTEXT:
case HKTypes.NODE:
case HKTypes.VIRTUAL_CONTEXT:
case HKTypes.VIRTUAL_NODE:
{
// Convert literals
_collectProperties(entity, graph, options);
break;
}
case HKTypes.REFERENCE:
{
// Convert literals
if (options.convertHK && !options.skipRefNodes)
{
_collectProperties(entity, graph, options);
}
// Generate triples to the resource from the ref
if (options.convertOwl || ((!options.convertHK || options.compressReification) && entity.parent))
{
let refObj = {
id: entity.ref,
properties: entity.properties,
metaProperties: entity.metaProperties,
parent: entity.parent
};
_collectProperties(refObj, graph, options);
}
break;
}
case HKTypes.LINK:
{
let connector = connectors[entity.connector];
let roles = null;
if (connector)
{
let connectorRoles = Connector.prototype.getRoles.call(connector);
let tempRole = { s: null, o: null };
for (let j = 0; j < connectorRoles.length; j++)
{
let role = connectorRoles[j];
let t = Connector.prototype.getRoleType.call(connector, role);
if (t === RoleTypes.CHILD || t === RoleTypes.SUBJECT)
{
tempRole.s = role;
}
else if (t === RoleTypes.PARENT || t === RoleTypes.OBJECT)
{
tempRole.o = role;
}
}
if (tempRole.s && tempRole.o)
{
roles = [tempRole.s, tempRole.o];
}
}
else
{
roles = [subjectLabel, objectLabel];
}
if (roles)
{
Link.prototype.forEachCrossBind.call(entity, roles, (s, o) =>
{
let subjId = s;
let objId = o;
//in case we are still using reference nodes
const cachedSubjectRef = referenceMap[s] || referenceMap[Utils.getIdFromResource(s)] || null;
if (cachedSubjectRef)
{
subjId = cachedSubjectRef.ref;
}
const cachedObjectRef = referenceMap[o] || referenceMap[Utils.getIdFromResource(o)] || null;
if (cachedObjectRef)
{
objId = cachedObjectRef.ref;
}
let context = undefined;
if (entity.parent)
{
context = entity.parent;
}
else if (defaultGraph)
{
context = defaultGraph;
}
if (options.convertOwlTime)
{
options.owlTimeSerializer.serializeTemporalAnchorBind(entity, entities, subjectLabel, objectLabel, subjId, objId, defaultGraph, context);
}
else
{
graph.add(subjId, entity.connector, objId, context);
}
});
}
if (options.convertHK)
{
_collectProperties(entity, graph, options);
}
break;
}
case HKTypes.CONNECTOR:
break;
case HKTypes.TRAIL:
break;
default:
{
if (entity.properties)
{
_collectProperties(entity, graph, options);
}
break;
}
}
if (entity && options.convertHK)
{
hkSerializer.serialize(entity);
}
}
// reify literal as node triples
for (let k in literalAsNodeTriples)
{
const triple = literalAsNodeTriples[k];
const literal = _buildLiteralObject(triple.object);
graph.add(triple.subject, triple.predicate, literal, triple.graph);
}
if(suppressDuplicates && typeof graph.suppressDuplicates == 'function') graph.suppressDuplicates();
return graph;
}
function _addLiteral(entity, graph, predicate, value, metaProperty, graphName)
{
let literal = _buildLiteralObject(value, metaProperty);
if (entity.hasOwnProperty('type'))
{
if (entity.type === Reference.type && entity.parent)
{
//we are dealing with a ref node, use reference for the triple
graph.add(entity.ref, predicate, literal, graphName);
}
}
graph.add(entity.id, predicate, literal, graphName);
}
function _buildLiteralObject(value, metaProperty)
{
let typeInfo = {};
let v = null;
if (value !== null)
{
v = Utils.getValueFromLiteral(value, typeInfo) || value;
}
else
{
// Entity with only metaproperties
v = `<${Constants.HK_NULL}>`;
}
let lang = undefined;
let type = typeInfo.type || metaProperty;
if (typeInfo.lang)
{
lang = typeInfo.lang;
}
let literal = Utils.createLiteralObject(v, lang, type);
return literal;
}
function _collectProperties(entity, graph, options)
{
let convertNumber = options.convertNumber || false;
let reifyArray = options.reifyArray || false;
let defaultGraph = options.defaultGraph || null;
HKEntity.prototype.foreachProperty.call(entity, (key, value, metaProperty) =>
{
let graphName = null;
if (entity.parent)
{
graphName = entity.parent;
}
else if (defaultGraph)
{
graphName = defaultGraph;
}
if (value === null || value === undefined)
{
if (metaProperty !== null)
{
// Update only metaproperty
_addLiteral(entity, graph, key, null, metaProperty, graphName);
}
return;
}
if (options.convertOwl && options.owlSerializer.shouldConvertProperty(entity.id, key, value))
{
options.owlSerializer.convertProperty(entity.id, key, value, metaProperty, graphName);
return;
}
if (reifyArray && Array.isArray(value))
{
let literal = Utils.createLiteralObject(JSON.stringify(value), null, hk.DATA_LIST_URI);
graph.add(entity.id, key, literal, graphName);
}
// else
if (Array.isArray(value))
{
value = Array.from(new Set(value));
for (let i = 0; i < value.length; i++)
{
let currentMetaProperty = null;
if (metaProperty)
{
currentMetaProperty = Array.isArray(metaProperty) && metaProperty.length == value.length ? metaProperty[i] : metaProperty;
}
else
{
currentMetaProperty = Utils.getTypeIfNumberOrBoolean(value[i]);
}
_addLiteral(entity, graph, key, value[i], currentMetaProperty, graphName);
}
}
else
{
if (!metaProperty && convertNumber)
{
metaProperty = Utils.getTypeIfNumberOrBoolean(value);
}
_addLiteral(entity, graph, key, value, metaProperty, graphName);
}
});
}
exports.serialize = serialize;