-
Notifications
You must be signed in to change notification settings - Fork 1
/
N3Parser.js
434 lines (389 loc) · 16 KB
/
N3Parser.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
/**
* Created by joachimvh on 8/12/2015.
*/
var N3Lexer = require('./N3Lexer');
var Util = require('./Util');
var uuid = require('uuid');
var _ = require('lodash');
function N3Parser () {}
N3Parser.BASE = Util.BASE;
N3Parser.prototype.toJSONLD = function (input)
{
var lexer = new N3Lexer();
var lex = lexer.parse(input);
var unsafe = this._unsafePrefixes(lex, {});
var jsonld = this._parse(lex, null, {}, unsafe);
jsonld = this._simplify(jsonld);
// do this before removing default graph so everything has at least 1 reference
this._compact(jsonld);
this._cleanTypes(jsonld);
// default graph is not necessary if there is only 1 root node
if (jsonld['@graph'] && jsonld['@graph'].length === 1 && _.every(jsonld, function (val, key) { return key === '@context' || key === '@graph'; }))
{
var child = jsonld['@graph'][0];
delete jsonld['@graph'];
jsonld = _.extend(jsonld, child);
}
return jsonld;
};
N3Parser.prototype._parse = function (lex, root, context, unsafe)
{
if (Util.isLiteral(lex) || _.isArray(lex))
throw 'Input should be an object.';
var result, i;
if (lex.type === 'Document' || lex.type === 'Formula')
{
result = {'@context': {}, '@graph': []};
var newContext = {};
for (var c in context)
newContext[c] = context[c];
for (i = 0; i < lex.val.length; ++i)
{
var statement = lex.val[i];
if (statement.type === 'Prefix')
{
var uri = statement.val[1].substring(1, statement.val[1].length - 1);
result['@context'][statement.val[0]] = uri;
newContext[statement.val[0]] = uri;
}
else
result['@graph'].push(this._parse(statement, result, newContext, unsafe));
}
if (result['@context'][''])
{
result['@context'][N3Parser.BASE] = result['@context'][''];
delete result['@context']['']
}
for (var key in unsafe)
delete result['@context'][key]; // delete unsafe keys from context to prevent confusion
if (_.isEmpty(result['@context']))
delete result['@context'];
}
else if (lex.type === 'TripleData' || lex.type === 'BlankTripleData')
{
var predicateObjects;
if (lex.type === 'TripleData')
{
predicateObjects = lex.val[1];
result = this._parse(lex.val[0], root, context, unsafe);
}
else
{
predicateObjects = lex.val;
result = {};
}
for (i = 0; i < predicateObjects.length; ++i)
{
var po = predicateObjects[i];
var predicate = this._handlePredicate(po.val[0], root, context, unsafe);
if (!_.isString(predicate))
{
if ('@id' in predicate)
throw "Predicate shouldn't have an ID yet.";
predicate['@id'] = '_:b_' + uuid.v4();
if (Object.keys(predicate).length > 1) // no use adding it if it's just a blank node
root['@graph'].push(predicate);
predicate = predicate['@id'];
}
var objects = _.map(po.val[1], function (thingy) { return this._parse(thingy, root, context, unsafe); }.bind(this));
// Literals would use @type for both datatype and rdf:type, this gets around that, sort of
if (result['@value'] !== undefined && predicate === '@type')
predicate = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type';
if (!(predicate in result))
result[predicate] = objects;
else
result[predicate].push.apply(result[predicate], objects);
}
}
else if (lex.type === 'List')
result = { '@list': _.map(lex.val, function (thingy) { return this._parse(thingy, root, context, unsafe); }.bind(this)) };
else if (lex.type === 'RDFLiteral')
{
var str = lex.val[0];
var type = lex.val[1];
var lang = lex.val[2];
var tick = str[0];
str = str[1] === tick ? str.substring(3, str.length-3) : str.substring(1, str.length-1);
str = this._numericEscape(this._stringEscape(str));
result = { '@value': str };
if (type) result['@type'] = [this._parse(type, root, context, unsafe)]; // array to stay consistent with rest of jsonld generation
else if (lang) result['@language'] = lang;
}
else if (lex.type === 'BooleanLiteral')
result = { '@value': lex.val == 'true' || lex.val === '@true' };
else if (lex.type === 'NumericLiteral')
result = { '@value': parseFloat(lex.val) };
else if (lex.type === 'Variable')
result = { '@id' : lex.val };
else if (lex.type === 'ExplicitIRI')
result = { '@id': this._numericEscape(lex.val.substring(1, lex.val.length-1)) }; // remove < >
else if (lex.type === 'PrefixedIRI')
{
var prefixIdx = lex.val.indexOf(':');
var prefix = lex.val.substring(0, prefixIdx);
if (prefix === '')
result = { '@id': N3Parser.BASE + lex.val };
else if (prefix === '_')
result = { '@id': lex.val };
else if (unsafe[prefix] && context[prefix])
result = { '@id': context[prefix] + lex.val.substring(prefixIdx + 1) };
else
{
if (!context[prefix])
throw new Error('unknown prefix ' + prefix);
result = { '@id': lex.val}
}
}
else throw 'Unsupported type or should have been handled in one of the other cases: ' + lex.type;
return result;
};
N3Parser.prototype._handlePredicate = function (lex, root, context, unsafe)
{
if (Util.isLiteral(lex) || _.isArray(lex))
throw 'Input should be an object.';
var result;
if (lex.type === 'ExplicitIRI' || lex.type === 'PrefixedIRI' || lex.type ==='Variable')
result = this._parse(lex, root, context, unsafe)['@id'];
else if (lex.type === 'SymbolicIRI')
{
switch (lex.val)
{
case '=': result = 'http://www.w3.org/2002/07/owl#equivalentTo'; break;
case '=>': result = 'http://www.w3.org/2000/10/swap/log#implies'; break;
case '<=': result = { '@reverse': 'http://www.w3.org/2000/10/swap/log#implies' }; break;
case '@a':
case 'a': result = '@type'; break;
default: throw 'Unsupported symbolic IRI: ' + lex.val;
}
}
else
result = this._parse(lex, root, context, unsafe);
return result;
};
// http://www.w3.org/TR/turtle/#sec-escapes
N3Parser.prototype._stringEscape = function (str)
{
var regex = /((?:\\\\)*)\\([tbnrf"'\\])/g;
return str.replace(regex, function (match, p1, p2)
{
var slashes = p1.substr(0, p1.length/2);
var c;
switch (p2)
{
case 't': c = '\t'; break;
case 'b': c = '\b'; break;
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 'f': c = '\f'; break;
case '"':
case "'":
case '\\': c = p2; break;
default: c = '';
}
return slashes + c;
});
};
N3Parser.prototype._numericEscape = function (str)
{
var regex = /\\[uU]([A-fa-f0-9]{4,6})/g;
return str.replace(regex, function (match, unicode)
{
return String.fromCharCode(unicode);
});
};
// Warning: will modify the context object
N3Parser.prototype._unsafePrefixes = function (lex, result, context)
{
result = result || {};
context = context || {};
if (Util.isLiteral(lex) || !lex) return {};
if (_.isArray(lex))
{
for (var i = 0; i < lex.length; ++i)
this._unsafePrefixes(lex[i], result, context);
return result;
}
if (lex.type === 'Prefix')
context[lex.val[0]] = lex.val[1].substring(1, lex.val[1].length - 1);
this._unsafePrefixes(lex.val, result, context);
if (lex.type === 'ExplicitIRI')
{
var prefixIdx = lex.val.indexOf(':');
var prefix = lex.val.substring(1, prefixIdx); // 1, since '<' is still there
if (prefix in context && lex.val.substr(prefixIdx, 3) !== '://')
result[prefix] = context[prefix];
}
return result;
};
// TODO: reserved escape
N3Parser.prototype._simplify = function (jsonld)
{
if (Util.isLiteral(jsonld))
return jsonld;
if (_.isArray(jsonld))
{
for (var i = 0; i < jsonld.length; ++i)
jsonld[i] = this._simplify(jsonld[i]);
return jsonld;
}
var keys = Object.keys(jsonld);
if (keys.length === 1 && keys[0] === '@value')
return jsonld['@value'];
for (var key in jsonld)
{
if (key === '@context')
{
if (Object.keys(jsonld[key]).length > 0)
jsonld[key] = this._simplify(jsonld[key]);
}
else
{
var objects = this._simplify(jsonld[key]);
if (!_.isArray(objects))
objects = [objects];
if (key === '@type')
objects = _.map(objects, '@id');
if (objects.length === 1 && key !== '@graph' && key !== '@list')
objects = objects[0];
jsonld[key] = objects;
}
}
// this is a special case where we have literals as triples without predicates in the graph root
if ('@graph' in jsonld)
jsonld['@graph'] = _.map(jsonld['@graph'], function (thingy) { if (Util.isLiteral(thingy)) return { '@value': thingy}; return thingy; });
return jsonld;
};
N3Parser.prototype._compact = function (jsonld, references)
{
if (Util.isLiteral(jsonld) || !jsonld)
return;
if (_.isArray(jsonld))
return _.each(jsonld, function (thingy) { this._compact(thingy, references); }.bind(this));
var key;
if ('@id' in jsonld)
{
var id = jsonld['@id'];
if (id in references)
{
this._mergeNodes(references[id], jsonld);
jsonld['@type'] = null;
}
else
references[id] = jsonld;
}
// adding the predicates would only be necessary if we delete the blank node @ids
for (key in jsonld)
{
this._compact(jsonld[key], key === '@graph' ? {} : references);
if (key === '@list')
{
jsonld[key] = _.map(jsonld[key], function (node)
{
if (_.isObject(node) && node['@type'] === null)
return { '@id': node['@id']};
return node;
});
}
else if (_.isArray(jsonld[key]))
{
var acc = {};
var list = _.filter(_.map(jsonld[key], function (node)
{
// can't delete if it's the only reference
if (node['@id'] && !acc[node['@id']] && key !== '@graph')
{
acc[node['@id']] = true;
if (node['@type'] === null)
return { '@id': node['@id']};
}
return node['@type'] === null ? null : node;
}));
if (list.length === 0 && key !== '@graph') delete jsonld[key];
else if (list.length === 1 && key !== '@graph') jsonld[key] = list[0]; // @graph always expects a list for its parameters
else jsonld[key] = list;
}
else if (_.isObject(jsonld[key]) && jsonld[key]['@type'] === null)
// need to modify object in place to make sure all references are correct
for (var subKey in jsonld[key])
if (subKey !== '@id')
delete jsonld[key][subKey];
}
};
N3Parser.prototype._mergeNodes = function (objectA, objectB)
{
var i;
if (_.isString(objectA) && _.isString(objectB))
return [objectA, objectB];
if (_.isArray(objectA) !== _.isArray(objectB))
{
objectA = _.isArray(objectA) ? objectA : [objectA];
objectB = _.isArray(objectB) ? objectB : [objectB];
}
if (_.isArray(objectA) && _.isArray(objectB))
{
Array.prototype.push.apply(objectA, objectB);
return objectA;
}
var idA = objectA['@id'];
var idB = objectB['@id'];
if (idA !== idB || (idA === undefined && idB === undefined))
return [objectA, objectB];
// 2 objects
var keys = Object.keys(objectB);
for (i = 0; i < keys.length; ++i)
{
var key = keys[i];
if (key === '@id')
continue;
if (objectA[key] !== undefined && objectB[key] !== undefined)
objectA[key] = this._mergeNodes(objectA[key], objectB[key]);
else if (objectB[key] !== undefined)
objectA[key] = objectB[key];
}
return objectA;
};
// Workaround for the fact that @type: null can remain when parsing for example '<a> <b> <c>. <a> <b> <d>. <c> <e> <d>, <f>.'
// The reason is that the references can get modified afterwards, accidentally introducing @type: null.
// A better solution would be a better implementation of _compact.
N3Parser.prototype._cleanTypes = function (jsonld)
{
if (Util.isLiteral(jsonld)) {
return jsonld;
}
for (const key in jsonld)
{
if (key === '@type' && !jsonld[key]) {
delete jsonld[key];
} else {
this._cleanTypes(jsonld[key]);
}
}
}
module.exports = N3Parser;
// :a :b :c.a:a :b :c.
// :a :b :5.E3:a :b :c.
//var parser = new N3Parser();
//var jsonld = parser.toJSONLD('_:request http:methodName "GET"; tmpl:requestURI ("http://skillp.tho.f4w.l0g.in/api/operator_skills/" ?id); http:resp [ http:body _:body ]. _:body :contains {[ :name _:name; :desc _:desc; :role _:role; :skills {[ :machine _:m; :tool _:t; :computer _:c]} ]}. ?operator :machineSkills _:m; :toolSkills _:t; :computerSkills _:c. ?operator :name _:name; :desc _:desc; :role _:role.');
//var jsonld = parser.toJSONLD('() {() () ()} ().');
//var jsonld = parser.toJSONLD('@prefix : <http://f4w.restdesc.org/demo#>. @prefix tmpl: <http://purl.org/restdesc/http-template#> . @prefix http: <http://www.w3.org/2011/http#> ._:sk15_1 http:methodName "POST". _:sk15_1 tmpl:requestURI ("http://defects.tho.f4w.l0g.in/api/reports"). _:sk15_1 http:body {_:sk16_1 :event_id 174 . _:sk16_1 :operator_id 3 . _:sk16_1 :solution_id 3 . _:sk16_1 :success false. _:sk16_1 :comment "solved!"}. :firstTry :triedAndReported _:sk17_1. :firstTry :tryNewSolution true.');
//var jsonld = parser.toJSONLD('"a"^^<xsd:int> :a _:a.');
//var jsonld = parser.toJSONLD(':a :tolerances ( {[ :min :min1; :max :max1 ]} {[ :min :min2; :max :max2 ]} ).');
//var jsonld = parser.toJSONLD('{ :a }.');
//var jsonld = parser.toJSONLD(':a :b 0, 1.');
//var jsonld = parser.toJSONLD(':toJSONLDa :b :c. :c :b :a.');
//var jsonld = parser.toJSONLD('# comment " test \n <http://test#stuff> :b "str#ing". :a :b """line 1\n#line2\nline3""". # comment about this thing');
//var jsonld = parser.toJSONLD(':a :b "a\n\rb\\"c"@nl-de.');
//var jsonld = parser.toJSONLD(':Plato :says { :Socrates :is :mortal }.');
//var jsonld = parser.toJSONLD('{ :Plato :is :immortal } :says { :Socrates :is { :person :is :mortal } . :Donald a :Duck }.');
//var jsonld = parser.toJSONLD('[:a :b]^<test> [:c :d]!<test2> [:e :f]!<test3>.');
//var jsonld = parser.toJSONLD('[:a :b] :c [:e :f].');
//var jsonld = parser.toJSONLD(':a :b 5.E3.a:a :b :c.');
//var jsonld = parser.toJSONLD('@prefix gr: <http://purl.org/goodrelations/v1#> . <http://www.acme.com/#store> a gr:Location; gr:hasOpeningHoursSpecification [ a gr:OpeningHoursSpecification; gr:opens "08:00:00"; gr:closes "20:00:00"; gr:hasOpeningHoursDayOfWeek gr:Friday, gr:Monday, gr:Thursday, gr:Tuesday, gr:Wednesday ]; gr:name "Hepp\'s Happy Burger Restaurant" .');
//var jsonld = parser.toJSONLD('@prefix ex:<http://ex.org/>. <:a> <ex:b> ex:c.');
//console.log(JSON.stringify(jsonld, null, 4));
//var fs = require('fs');
//var data = fs.readFileSync('n3/secondUseCase/proof.n3', 'utf8');
//var jsonld = parser.parse(data);
//var JSONLDParser = require('./JSONLDParser');
//var jp = new JSONLDParser();
//console.log(jp.toN3(jsonld, 'http://www.example.org/'));