forked from shexjs/shex.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shex-extension-map.js
443 lines (394 loc) · 14.4 KB
/
shex-extension-map.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
/*
* TODO
* templates: @<foo> %map:{ my:specimen.container.code=.1.code, my:specimen.container.disp=.1.display %}
* node identifiers: @foo> %map:{ foo.id=substr(20) %}
* multiplicity: ...
*/
const ShExMapCjsModule = function (config) {
const extensions = require("./lib/extensions");
const N3Util = require("n3/lib/N3Util");
const N3DataFactory = require("n3/lib/N3DataFactory").default;
const materializer = require("./lib/ShExMaterializer")(config);
const MapExt = "http://shex.io/extensions/Map/#";
const pattern = /^ *(?:<([^>]*)>|([^:]*):([^ ]*)) *$/;
const UNBOUNDED = -1;
const MAX_MAX_CARD = 50; // @@ don't repeat forever during dev experiments.
function register (validator, api) {
if (api === undefined || !('ShExTerm' in api))
throw Error('SemAct extensions must be called with register(validator, {ShExTerm, ...)')
const prefixes = "_prefixes" in validator.schema ?
validator.schema._prefixes :
{};
validator.semActHandler.results[MapExt] = {};
validator.semActHandler.register(
MapExt,
{
/**
* Callback for extension invocation.
*
* @param {string} code - text of the semantic action.
* @param {object} ctx - matched triple or results subset.
* @param {object} extensionStorage - place where the extension writes into the result structure.
* @return {bool} false if the extension failed or did not accept the ctx object.
*/
dispatch: function (code, ctx, extensionStorage) {
function fail (msg) { const e = Error(msg); Error.captureStackTrace(e, fail); throw e; }
function getPrefixedName(bindingName) {
// already have the fully prefixed binding name ready to go
if (typeof bindingName === "string") return bindingName;
// bindingName is from a pattern match - need to get & expand it with prefix
const prefixedName = bindingName[1] ? bindingName[1] :
bindingName[2] in prefixes ? (prefixes[bindingName[2]] + bindingName[3]) :
fail("unknown prefix " + bindingName[2] + " in \"" + code + "\".");
return prefixedName;
}
const update = function(bindingName, value) {
if (!bindingName) {
throw Error("Invocation error: " + MapExt + " code \"" + code + "\" didn't match " + pattern);
}
const prefixedName = getPrefixedName(bindingName);
const quotedValue = value; // value.match(/"(.+)"/) === null ? '"' + value + '"' : value;
validator.semActHandler.results[MapExt][prefixedName] = quotedValue;
extensionStorage[prefixedName] = quotedValue;
};
// Do we have a map extension function?
if (/.*[(].*[)].*$/.test(code)) {
const results = extensions.lift(code, ctx.object, prefixes);
for (key in results)
update(key, results[key])
} else {
const bindingName = code.match(pattern);
update(bindingName, ctx.node || ctx.object);
}
return true;
}
}
);
return {
results: validator.semActHandler.results[MapExt],
binder,
trivialMaterializer,
visitTripleConstraint
}
function visitTripleConstraint (expr, curSubjectx, nextBNode, target, visitor, schema, bindings, recurse, direct, checkValueExpr) {
function P (pname) { return expandPrefixedName(pname, schema._prefixes); }
function L (value, modifier) { return N3Util.createLiteral(value, modifier); }
function B () { return nextBNode(); }
// utility functions for e.g. s = add(B(), P(":value"), L("70", P("xsd:float")))
function add (s, p, o) {
target.addQuad(api.ShExTerm.externalTriple({
subject: s,
predicate: p,
object: o
}, N3DataFactory));
return s;
}
const mapExts = (expr.semActs || []).filter(function (ext) { return ext.name === MapExt; });
if (mapExts.length) {
mapExts.forEach(function (ext) {
const code = ext.code;
const m = code.match(pattern);
let tripleObject;
if (m) {
const arg = m[1] ? m[1] : P(m[2] + ":" + m[3]);
const val = n3ify(bindings.get(arg));
if (val !== undefined) {
tripleObject = val;
}
}
// Is the arg a function? Check if it has parentheses and ends with a closing one
if (tripleObject === undefined) {
if (/[ a-zA-Z0-9]+\(/.test(code))
tripleObject = extensions.lower(code, bindings, schema.prefixes);
}
if (tripleObject === undefined)
; // console.warn('Not in bindings: ',code);
else if (expr.inverse)
//add(tripleObject, expr.predicate, curSubject);
add(tripleObject, expr.predicate, curSubjectx.cs);
else
//add(curSubject , expr.predicate, tripleObject);
add(curSubjectx.cs, expr.predicate, tripleObject);
});
} else if (typeof expr.valueExpr !== "string" && "values" in expr.valueExpr && expr.valueExpr.values.length === 1) {
if (expr.inverse)
add(expr.valueExpr.values[0], expr.predicate, curSubjectx.cs);
else
add(curSubjectx.cs, expr.predicate, n3ify(expr.valueExpr.values[0]));
} else {
const oldSubject = curSubjectx.cs;
let maxAdd = "max" in expr ? expr.max === UNBOUNDED ? Infinity : expr.max : 1;
if (maxAdd > MAX_MAX_CARD)
maxAdd = MAX_MAX_CARD;
if (!recurse)
maxAdd = 1; // no grounds to know how much to repeat.
for (let repetition = 0; repetition < maxAdd; ++repetition) {
curSubjectx.cs = B();
if (recurse) {
const res = checkValueExpr(curSubjectx.cs, expr.valueExpr, recurse, direct)
if ("errors" in res)
break;
}
if (expr.inverse)
add(curSubjectx.cs, expr.predicate, oldSubject);
else
add(oldSubject, expr.predicate, curSubjectx.cs);
}
visitor._maybeSet(expr, { type: "TripleConstraint" }, "TripleConstraint",
["inverse", "negated", "predicate", "valueExpr",
"min", "max", "annotations", "semActs"])
curSubjectx.cs = oldSubject;
}
}
function trivialMaterializer (schema, nextBNode) {
let blankNodeCount = 0;
const index = schema._index || api.ShExUtil.index(schema)
nextBNode = nextBNode || function () {
return '_:b' + blankNodeCount++;
};
return {
materialize: function (bindings, createRoot, shape, target) {
shape = !shape || shape === validator.start ? schema.start : shape;
target = target || new config.rdfjs.Store();
// target.addPrefixes(schema.prefixes); // not used, but seems polite
// utility functions for e.g. s = add(B(), P(":value"), L("70", P("xsd:float")))
function P (pname) { return expandPrefixedName(pname, schema.prefixes); }
function L (value, modifier) { return N3Util.createLiteral(value, modifier); }
function B () { return nextBNode(); }
function add (s, p, o) { target.addTriple({ subject: s, predicate: p, object: n3ify(o) }); return s; }
const curSubject = createRoot || B();
const curSubjectx = {cs: curSubject};
const v = api.ShExUtil.Visitor();
const oldVisitShapeRef = v.visitShapeRef;
v.visitShapeRef = function (shapeRef) {
this.visitShapeExpr(index.shapeExprs[shapeRef], shapeRef);
return oldVisitShapeRef.call(v, shapeRef);
};
v.visitValueRef = function (r) {
this.visitExpression(schema.shapes[r], r);
return this._visitValue(r);
};
v.visitTripleConstraint = function (expr) {
visitTripleConstraint(expr, curSubjectx, nextBNode, target, this, schema, bindings);
};
v.visitShapeExpr(shape, "_: -start-");
return target;
}
};
}
function binder (tree) {
let stack = []; // e.g. [2, 1] for v="http://shex.io/extensions/Map/#BPDAM-XXX"
const globals = {}; // !! delme
//
/**
* returns: { const->count }
*/
function _mults (obj) {
const rays = [];
const objs = [];
const counts = Object.keys(obj).reduce((r, k) => {
let toAdd = null;
if (typeof obj[k] === "object" && !("value" in obj[k])) {
toAdd = _mults(obj[k]);
if (Array.isArray(obj[k]))
rays.push(k);
else
objs.push(k);
} else {
// variable name.
toAdd = _make(k, 1);
}
return _add(r, toAdd);
}, {});
if (rays.length > 0) {
objs.forEach(i => {
const novel = Object.keys(obj[i]).filter(k => {
return counts[k] === 1;
});
if (novel.length) {
const n2 = novel.reduce((r, k) => {
r[k] = obj[i][k];
return r;
}, {});
rays.forEach(l => {
_cross(obj[l], n2);
});
}
});
objs.reverse();
objs.forEach(i => {
obj.splice(i, 1); // remove object from tree
});
}
return counts;
}
function _add (l, r) {
const ret = Object.assign({}, l);
return Object.keys(r).reduce((ret, k) => {
const add = k in r ? r[k] : 1;
ret[k] = k in ret ? ret[k] + add : add;
return ret;
}, ret);
}
function _make (k, v) {
const ret = {};
ret[k] = v;
return ret;
}
function _cross (list, map) {
for (let listIndex in list) {
if (Array.isArray(list[listIndex])) {
_cross(list[listIndex], map);
} else {
Object.keys(map).forEach(mapKey => {
if (mapKey in list[listIndex])
throw Error("unexpected duplicate key: " + mapKey + " in " + JSON.stringify(list[listIndex]));
list[listIndex][mapKey] = map[mapKey];
});
}
};
}
_mults(tree);
function _simplify (list) {
const ret = list.reduce((r, elt) => {
return r.concat(
Array.isArray(elt) ?
_simplify(elt) :
elt
);
}, []);
return ret.length === 1 ? ret[0] : ret;
}
tree = Array.isArray(tree) ? _simplify(tree) : [tree]; // expects an array
// const globals = tree.reduce((r, e, idx) => {
// if (!Array.isArray(e)) {
// Object.keys(e).forEach(k => {
// r[k] = e[k];
// });
// removables.unshift(idx); // higher indexes at the left
// }
// return r;
// }, {});
function getter (v) {
// work with copy of stack while trying to grok this problem...
if (stack === null)
return undefined;
if (v in globals)
return globals[v];
const nextStack = stack.slice();
let next = diveIntoObj(nextStack); // no effect if in obj
while (!(v in next)) {
let last;
while(!Array.isArray(next)) {
last = nextStack.pop();
next = getObj(nextStack);
}
if (next.length === last+1) {
stack = null;
return undefined;
}
nextStack.push(last+1);
next = diveIntoObj(nextStack);
// console.log("advanced to " + nextStack);
// throw Error ("can't advance to find " + v + " in " + JSON.stringify(next));
}
stack = nextStack.slice();
const ret = next[v];
delete next[v];
return ret;
function getObj (s) {
return s.reduce(function (res, elt) {
return res[elt];
}, tree);
}
function diveIntoObj (s) {
while (Array.isArray(getObj(s)))
s.push(0);
return getObj(s);
}
};
return {get: getter};
}
}
function done (validator) {
if (Object.keys(validator.semActHandler.results[MapExt]).length === 0)
delete validator.semActHandler.results[MapExt];
}
function n3ify (ldterm) {
if (typeof ldterm !== "object")
return ldterm;
const ret = "\"" + ldterm.value + "\"";
if ("language" in ldterm)
return ret + "@" + ldterm.language;
if ("type" in ldterm)
return ret + "^^" + ldterm.type;
return ret;
}
// Expands the prefixed name to a full IRI (also when it occurs as a literal's type)
function expandPrefixedName (prefixedName, prefixes) {
const match = /(?:^|"\^\^)([^:\/#"'\^_]*):[^\/]*$/.exec(prefixedName);
let prefix, base, index;
if (match)
prefix = match[1], base = prefixes[prefix], index = match.index;
if (base === undefined)
return prefixedName;
// The match index is non-zero when expanding a literal's type
return index === 0 ? base + prefixedName.substr(prefix.length + 1)
: prefixedName.substr(0, index + 3) +
base + prefixedName.substr(index + prefix.length + 4);
}
function extractBindingsDelMe (soln, min, max, depth) {
if ("min" in soln && soln.min < min)
min = soln.min
const myMax = "max" in soln ?
(soln.max === UNBOUNDED ?
Infinity :
soln.max) :
1;
if (myMax > max)
max = myMax
function walkExpressions (s) {
return s.expressions.reduce((inner, e) => {
return inner.concat(extractBindingsDelMe(e, min, max, depth+1));
}, []);
}
function walkTriple (s) {
const fromTriple = "extensions" in s && MapExt in s.extensions ?
[{ depth: depth, min: min, max: max, obj: s.extensions[MapExt] }] :
[];
return "referenced" in s ?
fromTriple.concat(extractBindingsDelMe(s.referenced.solution, min, max, depth+1)) :
fromTriple;
}
function structuralError (msg) { throw Error(msg); }
const walk = // function to explore each solution
soln.type === "someOfSolutions" ||
soln.type === "eachOfSolutions" ? walkExpressions :
soln.type === "tripleConstraintSolutions" ? walkTriple :
structuralError("unknown type: " + soln.type);
if (myMax > 1) // preserve important associations:
// map: e.g. [[1,2],[3,4]]
// [walk(soln.solutions[0]), walk(soln.solutions[1]),...]
return soln.solutions.map(walk);
else // hide unimportant nesting:
// flatmap: e.g. [1,2,3,4]
// [].concat(walk(soln.solutions[0])).concat(walk(soln.solutions[1]))...
return [].concat.apply([], soln.solutions.map(walk));
}
return {
register: register,
done: done,
materializer: materializer,
// binder: binder,
url: MapExt,
// visitTripleConstraint: myvisitTripleConstraint
extension: {
hashmap: require("./lib/hashmap_extension"),
regex: require("./lib/regex_extension")
},
extensions: require("./lib/extensions"),
utils: require("./lib/extension-utils"),
};
};
if (typeof require !== 'undefined' && typeof exports !== 'undefined')
module.exports = ShExMapCjsModule;