rdflib 2.2.20 → 2.2.21-23b2ead1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/rdflib.min.js +1 -1
- package/dist/rdflib.min.js.LICENSE.txt +9 -1
- package/dist/rdflib.min.js.map +1 -1
- package/esm/blank-node.js +61 -114
- package/esm/class-order.js +1 -1
- package/esm/collection.js +70 -126
- package/esm/default-graph.js +14 -48
- package/esm/empty.js +8 -39
- package/esm/factories/canonical-data-factory.js +33 -65
- package/esm/factories/extended-term-factory.js +18 -25
- package/esm/factories/factory-types.js +3 -2
- package/esm/factories/rdflib-data-factory.js +9 -19
- package/esm/fetcher.js +1341 -1854
- package/esm/formula.js +639 -846
- package/esm/index.js +40 -77
- package/esm/jsonldparser.js +34 -50
- package/esm/jsonparser.js +1 -8
- package/esm/lists.js +105 -0
- package/esm/literal.js +120 -189
- package/esm/log.js +7 -7
- package/esm/n3parser.js +1015 -1412
- package/esm/named-node.js +70 -119
- package/esm/namespace.js +2 -5
- package/esm/node-internal.js +73 -110
- package/esm/node.js +2 -7
- package/esm/parse.js +12 -19
- package/esm/patch-parser.js +10 -30
- package/esm/query-to-sparql.js +0 -18
- package/esm/query.js +63 -147
- package/esm/rdfaparser.js +794 -997
- package/esm/rdfxmlparser.js +347 -461
- package/esm/serialize.js +7 -33
- package/esm/serializer.js +886 -1036
- package/esm/sparql-to-query.js +44 -134
- package/esm/statement.js +54 -78
- package/esm/store.js +831 -1102
- package/esm/types.js +22 -21
- package/esm/update-manager.js +869 -1104
- package/esm/updates-via.js +104 -161
- package/esm/uri.js +9 -53
- package/esm/utils/default-graph-uri.js +3 -2
- package/esm/utils/termValue.js +0 -1
- package/esm/utils/terms.js +28 -19
- package/esm/utils-js.js +20 -61
- package/esm/utils.js +10 -21
- package/esm/variable.js +32 -78
- package/esm/xsd.js +2 -2
- package/lib/blank-node.js +60 -113
- package/lib/class-order.js +1 -2
- package/lib/collection.js +69 -129
- package/lib/default-graph.js +13 -52
- package/lib/empty.js +8 -43
- package/lib/factories/canonical-data-factory.js +35 -79
- package/lib/factories/extended-term-factory.js +18 -32
- package/lib/factories/factory-types.d.ts +6 -6
- package/lib/factories/factory-types.js +1 -4
- package/lib/factories/rdflib-data-factory.js +9 -23
- package/lib/fetcher.d.ts +6 -6
- package/lib/fetcher.js +1370 -1843
- package/lib/formula.d.ts +1 -1
- package/lib/formula.js +640 -855
- package/lib/index.d.ts +1 -2
- package/lib/index.js +67 -156
- package/lib/jsonldparser.js +33 -54
- package/lib/jsonparser.js +1 -10
- package/lib/lists.d.ts +13 -0
- package/lib/lists.js +113 -0
- package/lib/literal.js +120 -195
- package/lib/log.d.ts +0 -6
- package/lib/log.js +7 -8
- package/lib/n3parser.js +1030 -1436
- package/lib/named-node.js +69 -126
- package/lib/namespace.js +2 -7
- package/lib/node-internal.js +74 -107
- package/lib/node.js +2 -12
- package/lib/parse.d.ts +1 -1
- package/lib/parse.js +12 -32
- package/lib/patch-parser.js +11 -34
- package/lib/query-to-sparql.js +0 -23
- package/lib/query.js +62 -167
- package/lib/rdfaparser.js +796 -1009
- package/lib/rdfxmlparser.js +349 -466
- package/lib/serialize.d.ts +1 -1
- package/lib/serialize.js +7 -43
- package/lib/serializer.d.ts +1 -0
- package/lib/serializer.js +889 -1051
- package/lib/sparql-to-query.js +42 -167
- package/lib/statement.js +55 -84
- package/lib/store.d.ts +2 -2
- package/lib/store.js +850 -1112
- package/lib/tf-types.d.ts +4 -4
- package/lib/types.d.ts +8 -8
- package/lib/types.js +23 -23
- package/lib/update-manager.d.ts +1 -1
- package/lib/update-manager.js +865 -1101
- package/lib/updates-via.js +105 -164
- package/lib/uri.js +8 -61
- package/lib/utils/default-graph-uri.js +3 -5
- package/lib/utils/termValue.js +0 -2
- package/lib/utils/terms.d.ts +6 -1
- package/lib/utils/terms.js +29 -35
- package/lib/utils-js.js +23 -88
- package/lib/utils.js +10 -27
- package/lib/variable.js +34 -85
- package/lib/xsd-internal.js +0 -3
- package/lib/xsd.js +2 -6
- package/package.json +38 -40
- package/src/collection.ts +2 -0
- package/src/fetcher.ts +2 -2
- package/src/index.ts +0 -2
- package/src/jsonldparser.js +13 -4
- package/src/lists.ts +121 -0
- package/src/serialize.ts +2 -10
- package/src/serializer.js +78 -0
- package/src/statement.ts +7 -0
- package/src/store.ts +7 -7
- package/src/update-manager.ts +15 -9
- package/src/utils/terms.ts +23 -1
- package/changes.txt +0 -59
- package/esm/convert.js +0 -61
- package/lib/convert.d.ts +0 -2
- package/lib/convert.js +0 -77
- package/src/convert.js +0 -70
package/esm/index.js
CHANGED
|
@@ -1,18 +1,6 @@
|
|
|
1
|
-
import _createClass from "@babel/runtime/helpers/createClass";
|
|
2
|
-
import _classCallCheck from "@babel/runtime/helpers/classCallCheck";
|
|
3
|
-
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
|
|
4
|
-
import _inherits from "@babel/runtime/helpers/inherits";
|
|
5
|
-
import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn";
|
|
6
|
-
import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf";
|
|
7
1
|
import _defineProperty from "@babel/runtime/helpers/defineProperty";
|
|
8
|
-
|
|
9
|
-
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
|
|
10
|
-
|
|
11
|
-
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
|
|
12
|
-
|
|
13
2
|
import BlankNode from './blank-node';
|
|
14
3
|
import Collection from './collection';
|
|
15
|
-
import * as convert from './convert';
|
|
16
4
|
import Empty from './empty';
|
|
17
5
|
import Fetcher from './fetcher';
|
|
18
6
|
import Formula from './formula';
|
|
@@ -40,77 +28,52 @@ import { UpdatesVia } from './updates-via';
|
|
|
40
28
|
import * as uri from './uri';
|
|
41
29
|
import * as Util from './utils-js';
|
|
42
30
|
import Variable from './variable';
|
|
43
|
-
import DataFactory from './factories/rdflib-data-factory';
|
|
31
|
+
import DataFactory from './factories/rdflib-data-factory';
|
|
44
32
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
for (
|
|
33
|
+
// Prepare bound versions of data factory methods for export
|
|
34
|
+
const boundDataFactory = {};
|
|
35
|
+
for (const name in DataFactory) {
|
|
48
36
|
if (typeof DataFactory[name] === 'function') boundDataFactory[name] = DataFactory[name].bind(DataFactory);
|
|
49
37
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
var term = Node.fromValue; // TODO: this export is broken;
|
|
38
|
+
const {
|
|
39
|
+
fetcher,
|
|
40
|
+
graph,
|
|
41
|
+
lit,
|
|
42
|
+
st,
|
|
43
|
+
namedNode,
|
|
44
|
+
variable,
|
|
45
|
+
blankNode,
|
|
46
|
+
defaultGraph,
|
|
47
|
+
literal,
|
|
48
|
+
quad,
|
|
49
|
+
triple
|
|
50
|
+
} = boundDataFactory;
|
|
51
|
+
const formula = new Formula();
|
|
52
|
+
const fromNT = str => formula.fromNT(str);
|
|
53
|
+
const term = Node.fromValue;
|
|
54
|
+
|
|
55
|
+
// TODO: this export is broken;
|
|
69
56
|
// it exports the _current_ value of nextId, which is always 0
|
|
70
|
-
|
|
71
|
-
var NextId = BlankNode.nextId;
|
|
57
|
+
const NextId = BlankNode.nextId;
|
|
72
58
|
export * from './utils/terms';
|
|
73
|
-
export { BlankNode, Collection,
|
|
74
|
-
|
|
59
|
+
export { BlankNode, Collection, DataFactory, Empty, Fetcher, Formula, Store, jsonParser, Literal, log, N3Parser, NamedNode, Namespace, Node, parse, Query, queryToSPARQL, RDFaProcessor, RDFParser, serialize, Serializer, SPARQLToQuery, sparqlUpdateParser, Statement, term, UpdateManager, UpdatesSocket, UpdatesVia, uri, Util, Variable, Store as IndexedFormula,
|
|
60
|
+
// Alias
|
|
61
|
+
|
|
62
|
+
NextId, fromNT, fetcher, graph, lit, st, namedNode as sym,
|
|
63
|
+
// RDFJS DataFactory interface
|
|
75
64
|
blankNode, defaultGraph, literal, namedNode, quad, triple, variable };
|
|
76
65
|
export { termValue } from './utils/termValue';
|
|
77
|
-
export
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
function ConnectedStore(features) {
|
|
83
|
-
var _this;
|
|
84
|
-
|
|
85
|
-
_classCallCheck(this, ConnectedStore);
|
|
86
|
-
|
|
87
|
-
_this = _super.call(this, features);
|
|
88
|
-
|
|
89
|
-
_defineProperty(_assertThisInitialized(_this), "fetcher", void 0);
|
|
90
|
-
|
|
91
|
-
_this.fetcher = new Fetcher(_assertThisInitialized(_this), {});
|
|
92
|
-
return _this;
|
|
66
|
+
export class ConnectedStore extends Store {
|
|
67
|
+
constructor(features) {
|
|
68
|
+
super(features);
|
|
69
|
+
_defineProperty(this, "fetcher", void 0);
|
|
70
|
+
this.fetcher = new Fetcher(this, {});
|
|
93
71
|
}
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
var _super2 = _createSuper(LiveStore);
|
|
101
|
-
|
|
102
|
-
function LiveStore(features) {
|
|
103
|
-
var _this2;
|
|
104
|
-
|
|
105
|
-
_classCallCheck(this, LiveStore);
|
|
106
|
-
|
|
107
|
-
_this2 = _super2.call(this, features);
|
|
108
|
-
|
|
109
|
-
_defineProperty(_assertThisInitialized(_this2), "updater", void 0);
|
|
110
|
-
|
|
111
|
-
_this2.updater = new UpdateManager(_assertThisInitialized(_this2));
|
|
112
|
-
return _this2;
|
|
72
|
+
}
|
|
73
|
+
export class LiveStore extends ConnectedStore {
|
|
74
|
+
constructor(features) {
|
|
75
|
+
super(features);
|
|
76
|
+
_defineProperty(this, "updater", void 0);
|
|
77
|
+
this.updater = new UpdateManager(this);
|
|
113
78
|
}
|
|
114
|
-
|
|
115
|
-
return _createClass(LiveStore);
|
|
116
|
-
}(ConnectedStore);
|
|
79
|
+
}
|
package/esm/jsonldparser.js
CHANGED
|
@@ -1,118 +1,106 @@
|
|
|
1
1
|
import jsonld from 'jsonld';
|
|
2
2
|
import { arrayToStatements } from './utils';
|
|
3
|
+
|
|
3
4
|
/**
|
|
4
5
|
* Parses json-ld formatted JS objects to a rdf Term.
|
|
5
6
|
* @param kb - The DataFactory to use.
|
|
6
7
|
* @param obj - The json-ld object to process.
|
|
7
8
|
* @return {Literal|NamedNode|BlankNode|Collection}
|
|
8
9
|
*/
|
|
9
|
-
|
|
10
10
|
export function jsonldObjectToTerm(kb, obj) {
|
|
11
11
|
if (typeof obj === 'string') {
|
|
12
12
|
return kb.rdfFactory.literal(obj);
|
|
13
13
|
}
|
|
14
|
-
|
|
15
14
|
if (Object.prototype.hasOwnProperty.call(obj, '@list')) {
|
|
16
15
|
if (kb.rdfFactory.supports["COLLECTIONS"] === true) {
|
|
17
16
|
return listToCollection(kb, obj['@list']);
|
|
18
17
|
}
|
|
19
|
-
|
|
20
18
|
return listToStatements(kb, obj);
|
|
21
19
|
}
|
|
22
|
-
|
|
23
20
|
if (Object.prototype.hasOwnProperty.call(obj, '@id')) {
|
|
24
|
-
return kb
|
|
21
|
+
return nodeType(kb, obj);
|
|
25
22
|
}
|
|
26
|
-
|
|
27
23
|
if (Object.prototype.hasOwnProperty.call(obj, '@language')) {
|
|
28
24
|
return kb.rdfFactory.literal(obj['@value'], obj['@language']);
|
|
29
25
|
}
|
|
30
|
-
|
|
31
26
|
if (Object.prototype.hasOwnProperty.call(obj, '@type')) {
|
|
32
27
|
return kb.rdfFactory.literal(obj['@value'], kb.rdfFactory.namedNode(obj['@type']));
|
|
33
28
|
}
|
|
34
|
-
|
|
35
29
|
if (Object.prototype.hasOwnProperty.call(obj, '@value')) {
|
|
36
30
|
return kb.rdfFactory.literal(obj['@value']);
|
|
37
31
|
}
|
|
38
|
-
|
|
39
32
|
return kb.rdfFactory.literal(obj);
|
|
40
33
|
}
|
|
34
|
+
|
|
41
35
|
/**
|
|
42
36
|
* Adds the statements in a json-ld list object to {kb}.
|
|
43
37
|
*/
|
|
44
|
-
|
|
45
38
|
function listToStatements(kb, obj) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
});
|
|
50
|
-
var statements = arrayToStatements(kb.rdfFactory, listId, items);
|
|
39
|
+
const listId = obj['@id'] ? nodeType(kb, obj) : kb.rdfFactory.blankNode();
|
|
40
|
+
const items = obj['@list'].map(listItem => jsonldObjectToTerm(kb, listItem));
|
|
41
|
+
const statements = arrayToStatements(kb.rdfFactory, listId, items);
|
|
51
42
|
kb.addAll(statements);
|
|
52
43
|
return listId;
|
|
53
44
|
}
|
|
54
|
-
|
|
55
45
|
function listToCollection(kb, obj) {
|
|
56
46
|
if (!Array.isArray(obj)) {
|
|
57
47
|
throw new TypeError("Object must be an array");
|
|
58
48
|
}
|
|
59
|
-
|
|
60
|
-
return kb.rdfFactory.collection(obj.map(function (o) {
|
|
61
|
-
return jsonldObjectToTerm(kb, o);
|
|
62
|
-
}));
|
|
49
|
+
return kb.rdfFactory.collection(obj.map(o => jsonldObjectToTerm(kb, o)));
|
|
63
50
|
}
|
|
51
|
+
|
|
64
52
|
/**
|
|
65
53
|
* Takes a json-ld formatted string {str} and adds its statements to {kb}.
|
|
66
54
|
*
|
|
67
55
|
* Ensure that {kb.rdfFactory} is a DataFactory.
|
|
68
56
|
*/
|
|
69
|
-
|
|
70
|
-
|
|
71
57
|
export default function jsonldParser(str, kb, base, callback) {
|
|
72
|
-
|
|
58
|
+
const baseString = base && Object.prototype.hasOwnProperty.call(base, 'termType') ? base.value : base;
|
|
73
59
|
return jsonld.flatten(JSON.parse(str), null, {
|
|
74
60
|
base: baseString
|
|
75
|
-
}).then(
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
61
|
+
}).then(flattened => flattened.reduce((store, flatResource) => {
|
|
62
|
+
kb = processResource(kb, base, flatResource);
|
|
63
|
+
return kb;
|
|
64
|
+
}, kb)).then(callback).catch(callback);
|
|
65
|
+
}
|
|
66
|
+
function nodeType(kb, obj) {
|
|
67
|
+
if (obj['@id'].startsWith('_:')) {
|
|
68
|
+
// This object is a Blank Node.. pass the id without the preceding _:
|
|
69
|
+
return kb.rdfFactory.blankNode(obj['@id'].substring(2));
|
|
70
|
+
} else {
|
|
71
|
+
// This object is a Named Node
|
|
72
|
+
return kb.rdfFactory.namedNode(obj['@id']);
|
|
73
|
+
}
|
|
81
74
|
}
|
|
82
|
-
|
|
83
75
|
function processResource(kb, base, flatResource) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
for (var _i = 0, _Object$keys = Object.keys(flatResource); _i < _Object$keys.length; _i++) {
|
|
87
|
-
var property = _Object$keys[_i];
|
|
88
|
-
|
|
76
|
+
const id = flatResource['@id'] ? nodeType(kb, flatResource) : kb.rdfFactory.blankNode();
|
|
77
|
+
for (const property of Object.keys(flatResource)) {
|
|
89
78
|
if (property === '@id') {
|
|
90
79
|
continue;
|
|
91
80
|
} else if (property == '@graph') {
|
|
92
81
|
// the JSON-LD flattened structure may contain nested graphs
|
|
93
82
|
// the id value for this object is the new base (named graph id) for all nested flat resources
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
83
|
+
const graphId = id;
|
|
84
|
+
// this is an array of resources
|
|
85
|
+
const nestedFlatResources = flatResource[property];
|
|
97
86
|
|
|
98
|
-
|
|
87
|
+
// recursively process all flat resources in the array, but with the graphId as base.
|
|
88
|
+
for (let i = 0; i < nestedFlatResources.length; i++) {
|
|
99
89
|
kb = processResource(kb, graphId, nestedFlatResources[i]);
|
|
100
90
|
}
|
|
101
91
|
}
|
|
102
|
-
|
|
103
|
-
var value = flatResource[property];
|
|
104
|
-
|
|
92
|
+
const value = flatResource[property];
|
|
105
93
|
if (Array.isArray(value)) {
|
|
106
|
-
for (
|
|
107
|
-
kb.addStatement(createStatement(kb, id, property, value[
|
|
94
|
+
for (let i = 0; i < value.length; i++) {
|
|
95
|
+
kb.addStatement(createStatement(kb, id, property, value[i], base));
|
|
108
96
|
}
|
|
109
97
|
} else {
|
|
110
98
|
kb.addStatement(createStatement(kb, id, property, value, base));
|
|
111
99
|
}
|
|
112
100
|
}
|
|
113
|
-
|
|
114
101
|
return kb;
|
|
115
102
|
}
|
|
103
|
+
|
|
116
104
|
/**
|
|
117
105
|
* Create statement quad depending on @type being a type node
|
|
118
106
|
* @param kb
|
|
@@ -121,11 +109,8 @@ function processResource(kb, base, flatResource) {
|
|
|
121
109
|
* @param value
|
|
122
110
|
* @return quad statement
|
|
123
111
|
*/
|
|
124
|
-
|
|
125
|
-
|
|
126
112
|
function createStatement(kb, id, property, value, base) {
|
|
127
|
-
|
|
128
|
-
|
|
113
|
+
let predicate, object;
|
|
129
114
|
if (property === "@type") {
|
|
130
115
|
predicate = kb.rdfFactory.namedNode("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
|
|
131
116
|
object = kb.rdfFactory.namedNode(value);
|
|
@@ -133,6 +118,5 @@ function createStatement(kb, id, property, value, base) {
|
|
|
133
118
|
predicate = kb.rdfFactory.namedNode(property);
|
|
134
119
|
object = jsonldObjectToTerm(kb, value);
|
|
135
120
|
}
|
|
136
|
-
|
|
137
121
|
return kb.rdfFactory.quad(id, predicate, object, kb.rdfFactory.namedNode(base));
|
|
138
122
|
}
|
package/esm/jsonparser.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
export default (function () {
|
|
2
2
|
return {
|
|
3
|
-
parseJSON: function
|
|
3
|
+
parseJSON: function (data, source, store) {
|
|
4
4
|
var subject, predicate, object;
|
|
5
5
|
var bnodes = {};
|
|
6
6
|
var why = store.sym(source);
|
|
7
|
-
|
|
8
7
|
for (var x in data) {
|
|
9
8
|
if (x.indexOf('_:') === 0) {
|
|
10
9
|
if (bnodes[x]) {
|
|
@@ -16,16 +15,12 @@ export default (function () {
|
|
|
16
15
|
} else {
|
|
17
16
|
subject = store.sym(x);
|
|
18
17
|
}
|
|
19
|
-
|
|
20
18
|
var preds = data[x];
|
|
21
|
-
|
|
22
19
|
for (var y in preds) {
|
|
23
20
|
var objects = preds[y];
|
|
24
21
|
predicate = store.sym(y);
|
|
25
|
-
|
|
26
22
|
for (var z in objects) {
|
|
27
23
|
var obj = objects[z];
|
|
28
|
-
|
|
29
24
|
if (obj.type === 'uri') {
|
|
30
25
|
object = store.sym(obj.value);
|
|
31
26
|
store.add(subject, predicate, object, why);
|
|
@@ -36,7 +31,6 @@ export default (function () {
|
|
|
36
31
|
object = store.bnode(obj.value);
|
|
37
32
|
bnodes[obj.value] = object;
|
|
38
33
|
}
|
|
39
|
-
|
|
40
34
|
store.add(subject, predicate, object, why);
|
|
41
35
|
} else if (obj.type === 'Literal') {
|
|
42
36
|
// var datatype
|
|
@@ -47,7 +41,6 @@ export default (function () {
|
|
|
47
41
|
} else {
|
|
48
42
|
object = store.literal(obj.value);
|
|
49
43
|
}
|
|
50
|
-
|
|
51
44
|
store.add(subject, predicate, object, why);
|
|
52
45
|
} else {
|
|
53
46
|
throw new Error('error: unexpected termtype: ' + z.type);
|
package/esm/lists.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/* Lists form conversion
|
|
2
|
+
*/
|
|
3
|
+
|
|
4
|
+
// import DataFactory from './factories/extended-term-factory'
|
|
5
|
+
// import jsonldParser from './jsonldparser'
|
|
6
|
+
// @ts-ignore is this injected?
|
|
7
|
+
// @@ Goal: remove this dependency
|
|
8
|
+
// import N3Parser from './n3parser'
|
|
9
|
+
// import { parseRDFaDOM } from './rdfaparser'
|
|
10
|
+
// import RDFParser from './rdfxmlparser'
|
|
11
|
+
// import sparqlUpdateParser from './patch-parser'
|
|
12
|
+
// import * as Util from './utils-js'
|
|
13
|
+
|
|
14
|
+
// import BlankNode from './blank-node'
|
|
15
|
+
// import NamedNode from './named-node'
|
|
16
|
+
import Collection from './collection';
|
|
17
|
+
import Statement from './statement';
|
|
18
|
+
// import Formula from './formula'
|
|
19
|
+
|
|
20
|
+
import Namespace from './namespace';
|
|
21
|
+
const RDF = Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
|
|
22
|
+
|
|
23
|
+
/* Replace a given node with another node throughout a given document
|
|
24
|
+
*
|
|
25
|
+
* we do the predicate as well for complenesss though we don't expect Collections to use it
|
|
26
|
+
*/
|
|
27
|
+
export function substituteInDoc(store, x, y, doc) {
|
|
28
|
+
// console.log(`substituteInDoc put ${x} for ${y} in ${doc}}`)
|
|
29
|
+
for (const quad of store.statementsMatching(y, null, null, doc)) {
|
|
30
|
+
const newStatement = new Statement(x, quad.predicate, quad.object, doc);
|
|
31
|
+
store.remove(quad);
|
|
32
|
+
store.add(newStatement);
|
|
33
|
+
}
|
|
34
|
+
for (const quad of store.statementsMatching(null, y, null, doc)) {
|
|
35
|
+
store.remove(quad);
|
|
36
|
+
// console.log(` substituteInDoc predicate ${x} in ${quad}}`)
|
|
37
|
+
store.add(new Statement(quad.subject, x, quad.object, doc));
|
|
38
|
+
}
|
|
39
|
+
for (const quad of store.statementsMatching(null, null, y, doc)) {
|
|
40
|
+
store.remove(quad);
|
|
41
|
+
store.add(new Statement(quad.subject, quad.predicate, x, doc));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/* Change all lone rdf:nil nodes into empty Collections
|
|
46
|
+
*/
|
|
47
|
+
export function substituteNillsInDoc(store, doc) {
|
|
48
|
+
const x = RDF('nil');
|
|
49
|
+
for (const quad of store.statementsMatching(x, null, null, doc)) {
|
|
50
|
+
store.remove(quad);
|
|
51
|
+
const y = new Collection();
|
|
52
|
+
store.add(new Statement(y, quad.predicate, quad.object, doc));
|
|
53
|
+
}
|
|
54
|
+
for (const quad of store.statementsMatching(null, null, x, doc)) {
|
|
55
|
+
if (!quad.predicate.sameTerm(RDF('rest'))) {
|
|
56
|
+
// If not a tail
|
|
57
|
+
store.remove(quad);
|
|
58
|
+
const y = new Collection();
|
|
59
|
+
store.add(new Statement(quad.subject, quad.predicate, y, doc));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Convert lists reified as rdf:first, rest
|
|
65
|
+
* Normal method is sync.
|
|
66
|
+
* Unfortunately jsdonld is currently written to need to be called async.
|
|
67
|
+
* Hence the mess below with executeCallback.
|
|
68
|
+
* @param store - The quadstore
|
|
69
|
+
* @param doc - The document in which the conversion is done
|
|
70
|
+
*/
|
|
71
|
+
|
|
72
|
+
export function convertFirstRestNil(store, doc // Do whole store?
|
|
73
|
+
) {
|
|
74
|
+
function preceding(ele, listSoFar, trash) {
|
|
75
|
+
const rests = store.statementsMatching(ele, RDF('rest'), null, doc);
|
|
76
|
+
if (rests.length !== 1) throw new Error(`Bad list structure: no rest at ${ele}`);
|
|
77
|
+
const firsts = store.statementsMatching(ele, RDF('first'), null, doc);
|
|
78
|
+
if (firsts.length !== 1) throw new Error(`Bad list structure: rest but ${firsts.length} firsts at ${ele}`);
|
|
79
|
+
const value = firsts[0].object;
|
|
80
|
+
const total = [value].concat(listSoFar);
|
|
81
|
+
// console.log(' List now is: ', total)
|
|
82
|
+
const totalTrash = trash.concat(rests).concat(firsts);
|
|
83
|
+
const pres = store.statementsMatching(null, RDF('rest'), ele, doc);
|
|
84
|
+
if (pres.length === 0) {
|
|
85
|
+
// Head of the list
|
|
86
|
+
const newList = new Collection(total);
|
|
87
|
+
store.remove(totalTrash);
|
|
88
|
+
// Replace old list with new list:
|
|
89
|
+
substituteInDoc(store, newList, ele, doc);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (pres.length !== 1) throw new Error(`Bad list structure: ${pres.length} pres at ${ele}`);
|
|
93
|
+
const pre = pres[0].subject;
|
|
94
|
+
if (pre.termType !== 'BlankNode') throw new Error(`Bad list element node ${pre} type: ${pre.termType} `);
|
|
95
|
+
preceding(pre, total, totalTrash);
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
substituteNillsInDoc(store, doc); // lone ones only
|
|
99
|
+
|
|
100
|
+
const tails = store.statementsMatching(null, RDF('rest'), RDF('nil'), doc);
|
|
101
|
+
tails.forEach(tail => {
|
|
102
|
+
if (tail.subject.termType !== 'BlankNode') throw new Error(`Bad list element node ${tail.subject} type: ${tail.subject.termType} `);
|
|
103
|
+
preceding(tail.subject, [], []);
|
|
104
|
+
});
|
|
105
|
+
}
|