-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathwoqlBuilder.js
236 lines (208 loc) · 7.44 KB
/
woqlBuilder.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
/* eslint-disable guard-for-in */
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-underscore-dangle */
/* eslint-disable no-self-assign */
/* eslint-disable camelcase */
/* eslint-disable no-param-reassign */
// WOQLQuery
/**
* module WOQLQuery
*
*/
/**
* defines the internal functions of the woql query object - the
* language API is defined in WOQLQuery
* @module WOQLQuery
* @constructor
* @param {object} [query] json-ld query for initialisation
* @returns {WOQLQuery}
*/
const WOQLQueryExt = require('./woqlQuery');
// eslint-disable-next-line no-unused-vars
const typedef = require('../typedef');
// const WOQLLibrary = require('./woqlLibrary');
class WOQLQuery extends WOQLQueryExt {
// eslint-disable-next-line no-useless-constructor
constructor(query) {
super(query);
}
}
// WOQLQuery.prototype.counter = 1;
/**
* @param {typedef.GraphRef} [Graph] - the resource identifier of a graph possible
* @param {string|Var} [Subj] - The IRI of a triple’s subject or a variable
* @param {string|Var} [Pred] - The IRI of a property or a variable
* @param {string|Var} [Obj] - The IRI of a node or a variable, or a literal
* @returns {WOQLQuery} - A WOQLQuery which contains the pattern matching expression
*/
/**
* Simple composite functions which produce WOQL queries
*/
WOQLQuery.prototype.star = function (Graph, Subj, Pred, Obj) {
Subj = Subj || 'v:Subject';
Pred = Pred || 'v:Predicate';
Obj = Obj || 'v:Object';
Graph = Graph || false;
if (Graph) {
return this.quad(Subj, Pred, Obj, Graph);
}
return this.triple(Subj, Pred, Obj);
};
/**
* @param {string|Var} [Subj] - The IRI of a triple’s subject or a variable
* @param {string|Var} [Pred] - The IRI of a property or a variable
* @param {string|Var} [Obj] - The IRI of a node or a variable, or a literal
* @param {typedef.GraphRef} [Graph] - the resource identifier of a graph possible
* @returns {WOQLQuery} - A WOQLQuery which contains the pattern matching expression
*/
WOQLQuery.prototype.all = function (Subj, Pred, Obj, Graph) {
return this.star(Graph, Subj, Pred, Obj);
};
/**
* @param {string} s
* @returns {object}
*/
WOQLQuery.prototype.string = function (s) {
return { '@type': 'xsd:string', '@value': String(s) };
};
/**
* @param {boolean} tf
* @returns {object}
*/
WOQLQuery.prototype.boolean = function (tf) {
tf = tf || false;
return this.literal(tf, 'boolean');
};
/**
* @param {any} s
* @param {string} t
* @returns {object}
*/
WOQLQuery.prototype.literal = function (s, t) {
t = t.indexOf(':') === -1 ? `xsd:${t}` : t;
return { '@type': t, '@value': s };
};
/**
* @param {string} s
* @returns {object}
*/
WOQLQuery.prototype.iri = function (s) {
return {
'@type': 'NodeValue',
node: s,
};
};
/**
* Update a pattern matching rule for the triple (Subject, Predicate, oldObjValue) with the
* new one (Subject, Predicate, newObjValue)
* @param {string|Var} subject - The IRI of a triple’s subject or a variable
* @param {string|Var} predicate - The IRI of a property or a variable
* @param {string|Var} newObjValue - The value to update or a literal
* @param {string|Var} oldObjValue - The old value of the object
* @returns {WOQLQuery} A WOQLQuery which contains the a Update Triple Statement
*/
WOQLQuery.prototype.update_triple = function (subject, predicate, newObjValue, oldObjValue) {
const tmp_name = oldObjValue || `v:AnyObject__${this.counter += 1}`;
return this.and(
new WOQLQuery().opt(
new WOQLQuery()
.triple(subject, predicate, tmp_name)
.delete_triple(subject, predicate, tmp_name)
.not()
.triple(subject, predicate, newObjValue),
),
new WOQLQuery().add_triple(subject, predicate, newObjValue),
);
};
/**
* @description Update a pattern matching rule for the quad [S, P, O, G]
* (Subject, Predicate, Object, Graph)
* @param {string} subject - The IRI of a triple’s subject or a variable
* @param {string} predicate - The IRI of a property or a variable
* @param {string} newObject - The value to update or a literal
* @param {string} graph - the resource identifier of a graph possible value are
* schema/{main - myschema - *} | instance/{main - myschema - *} | inference/{main - myschema - *}
* @returns {WOQLQuery} A WOQLQuery which contains the a Update Quad Statement
*/
WOQLQuery.prototype.update_quad = function (subject, predicate, newObject, graph) {
const tmp_name = `v:AnyObject__${this.counter += 1}`;
return this.and(
new WOQLQuery().opt(
new WOQLQuery()
.quad(subject, predicate, tmp_name, graph)
.delete_quad(subject, predicate, tmp_name, graph)
.not()
.quad(subject, predicate, newObject, graph),
),
new WOQLQuery().add_quad(subject, predicate, newObject, graph),
);
};
/**
* Deletes all triples in the passed graph (defaults to instance/main)
* @param {typedef.GraphRef} [graphRef] - Resource String identifying the graph from
* which all triples will be removed
* @returns {WOQLQuery} - A WOQLQuery which contains the deletion expression
*/
WOQLQuery.prototype.nuke = function (graphRef) {
if (graphRef) {
return this.quad('v:A', 'v:B', 'v:C', graphRef).delete_quad('v:A', 'v:B', 'v:C', graphRef);
}
return this.triple('v:A', 'v:B', 'v:C').delete_triple('v:A', 'v:B', 'v:C');
};
/**
*
* @param {string|Var} node - The IRI of a node or a variable containing an IRI which will
* be the subject of the builder functions
* @param {typedef.FuntionType} [type] - Optional type of builder function to build
* (default is triple)
* @returns {WOQLQuery} - A WOQLQuery which contains the partial Node pattern matching expression
* @example
*/
WOQLQuery.prototype.node = function (node, type) {
type = type || false;
if (type === 'add_quad') type = 'AddTriple';
else if (type === 'delete_quad') type = 'DeleteTriple';
else if (type === 'add_triple') type = 'AddTriple';
else if (type === 'delete_triple') type = 'DeleteTriple';
else if (type === 'quad') type = 'Triple';
else if (type === 'triple') type = 'Triple';
if (type && type.indexOf(':') === -1) type = type;
const ctxt = { subject: node };
if (type) ctxt.action = type;
this._set_context(ctxt);
return this;
};
// do not remove
/**
* Sets the graph resource ID that will be used for subsequent chained function calls
* @param {typedef.GraphRef} [graphRef] Resource String identifying the graph which will
* be used for subsequent chained schema calls
* @returns {WOQLQuery} A WOQLQuery which contains the partial Graph pattern matching expression
*/
WOQLQuery.prototype.graph = function (graphRef) {
return this._set_context({
graph: graphRef,
});
};
// do not remove
WOQLQuery.prototype._set_context = function (ctxt) {
if (!this.triple_builder_context) this.triple_builder_context = {};
for (const k in ctxt) {
this.triple_builder_context[k] = ctxt[k];
}
return this;
};
/**
* @param {string|Var} id - IRI string or variable containing
* @param {string|Var} type - IRI string or variable containing the IRI of the
* @param {typedef.GraphRef} [refGraph] - Optional Graph resource identifier
* @returns {WOQLQuery} A WOQLQuery which contains the insert expression
*/
WOQLQuery.prototype.insert = function (id, type, refGraph) {
refGraph = refGraph || (this.triple_builder_context ? this.triple_builder_context.graph : false);
if (refGraph) {
return this.add_quad(id, 'rdf:type', `@schema:${type}`, refGraph);
}
return this.add_triple(id, 'rdf:type', `@schema:${type}`);
};
module.exports = WOQLQuery;