-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser.js
534 lines (449 loc) · 16.3 KB
/
parser.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*
* Copyright (c) 2016-present, IBM Research
* Licensed under The MIT License [see LICENSE for details]
*/
"use strict";
const { Node, Connector, Link, Context, Reference, ConnectorClass, RoleTypes } = require("hklib");
const Constants = require("./constants");
const Utils = require("./utils");
const owl = require("./owl");
const rdfs = require("./rdfs");
const wikidata = require("./wikidata");
const hk = require("./hk");
const uuidv1 = require('uuid/v1');
const RELATION_QUALIFIER_URIS = new Set();
RELATION_QUALIFIER_URIS.add(owl.INVERSE_OF_URI);
RELATION_QUALIFIER_URIS.add(rdfs.SUBPROPERTYOF_URI);
const HK_NULL_URI = `<${Constants.HK_NULL}>`;
const isUriOrBlankNode = Utils.isUriOrBlankNode;
/**
* Parse rdf to Hyperknowledge entities.
*
* @param {object} graph The graph (quads, if it contains named graph) to be parsed and converted to Hyperknowledge entities.
* @param {boolean|object} [options] Parsing options, if it is a boolean, is equivalent to {createContext: true} which means it will generate context for each named graph.
* @param {boolean} [options.createContext] Create the context entity for each named graph. Default is false.
* @param {boolean} [options.namespaceContext] Contextualize entities based on their namespace.
* @param {boolean} [options.subjectLabel] Set the subject role name `subject`
* @param {boolean} [options.objectLabel] Set the object role name `object`
* @param {boolean} [options.convertOwl] EXPERIMENTAL OWL rules. Default is false.
* @param {boolean} [options.convertOwlTime] EXPERIMENTAL OWL Time rules. Default is false.
* @param {boolean} [options.customRdfParser] Use the customizable parser. Default is false.
* @param {boolean} [options.timeContext] Context for OWL Time entities and relationships, if convertOwlTime is true.
* @param {boolean} [options.preserveBlankNodes] Preserve the blank node ids if true, otherwise replace it by a uuid inteded to be unique in the database. Default is false.
* @param {boolean} [options.serialize] Serialize output, i. e. remove unnecessary methods and fields from the intances.
* @param {boolean} [options.convertHK] If set, it will read the Hyperknowledge vocabulary and make special conversion. Default is true.
* @param {boolean} [options.onlyHK] If set, it will ONLY read the Hyperknowledge vocabulary and convert those entities, this options override `convertHK`. Default is false.
* @param {boolean} [options.textLiteralAsNode] If true, string literals will be converted to content nodes, which will be linked to subject using a link whose connector is the predicate.
* @param {boolean} [options.textLiteralAsNodeEncoding] If 'property', textLiteralAsNode encoding will be made using node and link properties. If 'metaproperty' encoding will be made using node and link metaproperties. Default is 'metaproperty'.
* @param {string} [options.strategy] "pre-existing-context", "new-context" or "automatically."
* @param {array} [options.hierarchyConnectorIds] "List of predicates that should become hierarchy connectors."
* @param {object|undefined} [customizableOptions] A dictionary of customizable options while parsing.
* @param {array|undefined} [customizableOptions.contextualize] indicates the predicates that should create contexts based on the object.
* @param {string|undefined} [customizableOptions.contextualize.p] a predicate that should create a context relation.
* @param {string|undefined} [customizableOptions.contextualize.o] the object that should become a context. When undefined, it indicates that any object with that predicate should become context.
*/
function parseGraph(graph, options, customizableOptions)
{
if (typeof options === "boolean")
{
options = {
createContext: options
};
}
else if (typeof options === Array)
{
// options = {ids: options};
}
else if (!options)
{
options = {};
}
let namespaceContext = options.namespaceContext || false;
let createContext = options.createContext || namespaceContext;
let strategy = options.strategy;
const preserveBlankNodes = options.preserveBlankNodes || false;
let setNodeContext = options.setNodeContext || false;
let rootContext = options.context;
let convertHK = options.convertHK && true;
let onlyHK = options.onlyHK || false;
let textLiteralAsNode = options.textLiteralAsNode || false;
let textLiteralAsNodeEncoding = options.textLiteralAsNodeEncoding || 'metaproperty';
convertHK = convertHK || onlyHK;
let serialize = options.serialize || false;
const subjectLabel = options.subjectLabel || Constants.DEFAULT_SUBJECT_ROLE;
const objectLabel = options.objectLabel || Constants.DEFAULT_OBJECT_ROLE;
const hierarchyConnectorIds = options.hierarchyConnectorIds || [rdfs.TYPE_URI, rdfs.SUBCLASSOF_URI, rdfs.SUBPROPERTYOF_URI, wikidata.INSTANCE_OF_URI, wikidata.SUBCLASS_OF_URI];
let entities = {};
let connectors = {};
let blankNodesMap = {};
let refNodesMap = {};
// instantiate new parsers
const parsers = [];
registeredParsers.forEach(parser => {
try {
const instantiatedParser = new parser(entities, connectors, blankNodesMap, refNodesMap, options, customizableOptions);
parsers.push(instantiatedParser);
// console.log(`new instantiated parser ${instantiatedParser}`);
}
catch(err) {
console.error(`There was an error while instatianting the parser ${parser}`);
throw err;
}
});
let getParent = (iri, g) =>
{
if (namespaceContext) {
if (iri.includes("#")) {
return `<${iri.split('#')[0].replace('<', '')}>`
}
}
return ((g === HK_NULL_URI || g === null) && rootContext) ? rootContext : g;
}
let createReference = (s, g) =>
{
const parent = getParent(s, g);
let ref = new Reference();
ref.id = Utils.createRefUri(s, parent);
ref.ref = s;
ref.parent = parent;
entities[ref.id] = ref;
refNodesMap[ref.id] = ref;
return ref;
}
// FIRST LOOP
// Collect basic connectors
// Collect contexts
graph.forEachStatement((s, p, o, g) =>
{
const parent = getParent(s, g);
for (let i = 0; i < parsers.length; i++) {
const parser = parsers[i];
if (parser.firstLoopShouldConvert(s, p, o, parent)) {
let shouldContinue = parser.firstLoopCallback(s, p, o, parent);
if (!shouldContinue) {
return;
}
}
}
// Create connector?
if (Utils.isUri(p) && Utils.isUriOrBlankNode(o))
{
let connectorId = Utils.getIdFromResource(p);
if (!connectors.hasOwnProperty(connectorId))
{
let connector = new Connector();
connector.id = connectorId;
connector.className = hierarchyConnectorIds.includes(p) ? ConnectorClass.HIERARCHY : ConnectorClass.FACTS;
connector.addRole(subjectLabel, RoleTypes.SUBJECT);
connector.addRole(objectLabel, RoleTypes.OBJECT);
connectors[connectorId] = connector;
entities[connectorId] = connector;
}
}
const isPreExistingContext = strategy === 'pre-existing-context' && parent === rootContext;
if (createContext && parent && parent !== HK_NULL_URI && !isPreExistingContext)
{
// Create context
if (!entities.hasOwnProperty(parent))
{
let context = new Context();
context.id = parent;
entities[parent] = context;
}
}
});
// SECOND LOOP
// Create nodes
graph.forEachStatement((s, p, o, g) =>
{
const parent = getParent(s, g);
// console.log(s, p, o);
// Replace the blank node identitier to uuid
// In order to make this id more robust along the base
if (!preserveBlankNodes)
{
if (Utils.isBlankNode(s) && !blankNodesMap.hasOwnProperty(s))
{
blankNodesMap[s] = `_:${uuidv1()}`;
}
if (Utils.isBlankNode(o) && !blankNodesMap.hasOwnProperty(o))
{
blankNodesMap[o] = `_:${uuidv1()}`;
}
}
for(let i = 0; i < parsers.length; i++) {
const parser = parsers[i];
if (parser.secondLoopShouldConvert(s, p, o, parent)) {
let shouldContinue = parser.secondLoopCallback(s, p, o, parent);
if (!shouldContinue) {
return;
}
}
}
let subjectId = Utils.getIdFromResource(s);
if ( isUriOrBlankNode(s) && !entities.hasOwnProperty(subjectId))
{
let node = new Node();
node.id = blankNodesMap.hasOwnProperty(s) ? blankNodesMap[s] : subjectId;
entities[node.id] = node;
node.parent = undefined;
// Set the context to the graph name
if (setNodeContext && parent)
{
node.parent = Utils.getIdFromResource(parent);
}
}
let objectId = Utils.getIdFromResource(o);
if ( isUriOrBlankNode(o) && !entities.hasOwnProperty(objectId))
{
let node = new Node();
node.id = blankNodesMap.hasOwnProperty(o) ? blankNodesMap[o] : objectId;
entities[node.id] = node;
node.parent = undefined;
// Set the context to the graph name
if (setNodeContext && parent)
{
const parentId = Utils.getIdFromResource(parent);
node.parent = parentId !== node.id ? parentId : null;
}
}
});
// LAST LOOP
// Create attributes, relations and ref nodes if needed
graph.forEachStatement((s, p, o, g) =>
{
const parent = getParent(s, g);
const parentIdFromResource = Utils.getIdFromResource(parent);
for(let i = 0; i < parsers.length; i++) {
const parser = parsers[i];
if (parser.lastLoopShouldConvert(s, p, o, parent)) {
let shouldContinue = parser.lastLoopCallback(s, p, o, parent);
if (!shouldContinue) {
return;
}
}
}
// Set relationship
if (isUriOrBlankNode(o))
{
let connectorId = Utils.getIdFromResource(p);
if (connectors.hasOwnProperty(connectorId))
{
let connector = connectors[connectorId];
let link = new Link();
let roles = connector.getRoles();
for (let i = 0; i < roles.length; i++)
{
let role = roles[i];
let roleType = connector.getRoleType(role);
if (roleType === RoleTypes.SUBJECT || roleType === RoleTypes.CHILD)
{
let subjId = blankNodesMap.hasOwnProperty(s) ? blankNodesMap[s] : s;
subjId = Utils.getIdFromResource(subjId);
link.addBind(subjectLabel, subjId);
}
else if (roleType === RoleTypes.OBJECT || roleType === RoleTypes.PARENT)
{
let objId = blankNodesMap.hasOwnProperty(o) ? blankNodesMap[o] : o;
objId = Utils.getIdFromResource(objId);
link.addBind(objectLabel, objId);
}
}
link.id = Utils.createSpoUri(s, p, o, parent);
link.connector = connectorId;
if (g)
{
link.parent = parentIdFromResource;
}
entities[link.id] = link;
}
}
else
{
// Since it is a literal the it become a property
let entity = null;
// Get maped blank node
if (!preserveBlankNodes && blankNodesMap.hasOwnProperty(s))
{
s = blankNodesMap[s];
}
let subjectId = Utils.getIdFromResource(s);
if (!parentIdFromResource)
{
entity = entities[subjectId]; // we assume the entity must have been created
}
else
{
entity = entities[subjectId] || null;
if(entity !== null)
{
if (entity.type !== Connector.type && entity.parent !==parentIdFromResource)
{
// The node already exists and it belongs to another context
// This assign will force to look for a reference node
entity = null;
}
}
// Check if there is a reference to the resource
if (!entity)
{
let refId = Utils.createRefUri(s, parent);
entity = entities[refId] || null;
}
}
// If at this point the entity was not set
// create a reference to it
if (!entity)
{
if (onlyHK)
{
// Do not create entities by inference
// when conversion is to only convert
// hyperknowledge entities
return;
}
entity = createReference(s, parent);
}
// Convert the literal
_setPropertyFromLiteral(entity, p, o, entities, connectors, subjectLabel, objectLabel, textLiteralAsNode, textLiteralAsNodeEncoding);
}
});
// Finish conversion
// Add connectors
for (let c in connectors)
{
entities[c] = connectors[c];
}
parsers.forEach(parser => {
if (parser.mustConvert) {
parser.finish(entities);
}
});
// Serialize entities
if (serialize)
{
for (let k in entities)
{
entities[k] = entities[k].serialize();
}
}
return entities;
}
function _setPropertyFromLiteral(entity, p, o, entities, connectors, subjectLabel, objectLabel, textLiteralAsNode = false, textLiteralAsNodeEncoding = 'property')
{
let typeInfo = {};
let value = Utils.getValueFromLiteral(o, typeInfo, true);
let propertyName = Utils.getIdFromResource(p);
if (typeInfo.lang)
{
value = `"${value}"@${typeInfo.lang}`;
}
if(typeof value === "string")
{
let literalSlices = value.split(`^^`);
if (literalSlices[0] === `"${HK_NULL_URI}"`)
{
if (literalSlices[1] !== null)
{
entity.setMetaProperty(Utils.getIdFromResource(p), Utils.getIdFromResource(literalSlices[1]));
}
return;
}
if(textLiteralAsNode)
{
// add property or metaproperty in subject node
const literalTypeId = Utils.getIdFromResource(hk.DATA_LITERAL_URI);
const predicateId = Utils.getIdFromResource(p);
if(textLiteralAsNodeEncoding === 'property')
{
entity.setProperty(literalTypeId, predicateId);
}
else if(textLiteralAsNodeEncoding === 'metaproperty')
{
entity.setMetaProperty(literalTypeId, predicateId);
}
// create content node with literal as data, if needed
const contentNodeUri = Utils.createContentNodeUri(value);
if(!entities.hasOwnProperty(contentNodeUri))
{
const contentNode = new Node(contentNodeUri, entity.parent);
contentNode.setProperty('mimeType', 'plain/text');
contentNode.setProperty('data', value);
entities[contentNodeUri] = contentNode;
}
// create predicate connector, if needed
const connectorId = Utils.getIdFromResource(p);
if(!connectors.hasOwnProperty(connectorId))
{
const contentConnector = new Connector(connectorId, ConnectorClass.FACTS);
contentConnector.addRole(subjectLabel, RoleTypes.SUBJECT);
contentConnector.addRole(objectLabel, RoleTypes.OBJECT);
connectors[contentConnector.id] = contentConnector;
entities[contentConnector.id] = contentConnector;
}
// create spo link between subject and content node
const linkUri = Utils.createSpoUri(entity.id, p, value, entity.parent);
const contentLink = new Link(linkUri, p, entity.parent);
contentLink.addBind(subjectLabel, entity.id);
contentLink.addBind(objectLabel, contentNodeUri);
if(textLiteralAsNodeEncoding === 'property')
{
contentLink.setProperty(literalTypeId, predicateId);
}
else if(textLiteralAsNodeEncoding === 'metaproperty')
{
contentLink.setMetaProperty(literalTypeId, predicateId);
}
entities[linkUri] = contentLink;
// create hierarchical connector, if needed
const typeConnectorId = Utils.getIdFromResource(rdfs.TYPE_URI);
if(!connectors.hasOwnProperty(typeConnectorId))
{
const typeConnector = new Connector(typeConnectorId, ConnectorClass.HIERARCHY);
typeConnector.addRole(subjectLabel, RoleTypes.SUBJECT);
typeConnector.addRole(objectLabel, RoleTypes.OBJECT);
connectors[typeConnector.id] = typeConnector;
entities[typeConnector.id] = typeConnector;
}
// add literal node to body, if needed
let typeNode = entities[literalTypeId];
if(!typeNode)
{
typeNode = new Node(literalTypeId, null);
entities[literalTypeId] = typeNode;
}
// add reference to literal node within context, if needed
if(entity.parent && entity.parent !== "null" && entity.parent !== HK_NULL_URI)
{
const typeReferenceUri = Utils.createRefUri(literalTypeId, entity.parent);
if(!entities.hasOwnProperty(typeReferenceUri))
{
typeNode = new Reference(typeReferenceUri, literalTypeId, entity.parent);
entities[typeReferenceUri] = typeNode;
}
else
{
typeNode = entities[typeReferenceUri];
}
}
// create hierarchical link between content node and literal type
const typeLinkUri = Utils.createSpoUri(contentNodeUri, rdfs.TYPE_URI, hk.DATA_LITERAL_URI, entity.parent);
const typeLink = new Link(typeLinkUri, rdfs.TYPE_URI, entity.parent);
typeLink.addBind(subjectLabel, contentNodeUri);
typeLink.addBind(objectLabel, typeNode.id);
entities[typeLinkUri] = typeLink;
return;
}
}
entity.setOrAppendToProperty(propertyName, value);
if (typeInfo.type)
{
entity.setOrAppendToMetaProperty(propertyName, Utils.getIdFromResource(typeInfo.type));
}
}
const registeredParsers = new Set();
function registerParser(parser) {
registeredParsers.add(parser);
}
exports.registerParser = registerParser;
exports.parseGraph = parseGraph;