Skip to content

Commit 05ba853

Browse files
committed
review woql and tests
1 parent b492429 commit 05ba853

10 files changed

+34
-1974
lines changed

lib/connectionConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ ConnectionConfig.prototype.apiURL = function() {
119119
* @returns {string | boolean}
120120
*/
121121
ConnectionConfig.prototype.db = function() {
122-
if(!this.dbid) return false//throw new Error('Invalid database name');
122+
if(!this.dbid) throw new Error('Invalid database name');
123123
return this.dbid
124124
}
125125

lib/query/woqlBuilder.js

Lines changed: 25 additions & 278 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//WOQLQuery
22
/**
33
* module WOQLQuery
4+
*
45
*/
5-
const WOQLQuery = require('./woqlSchema')
6-
//const WOQLQuery = require('./woqlCore')
7-
//const WOQLSchema = require('./woqlSchema')
6+
7+
const WOQLQuery = require('./woqlQuery')
88
const WOQLLibrary = require('./woqlLibrary')
99

1010
/*class WOQLQuery extends WOQLQueryImp {
@@ -36,11 +36,6 @@ WOQLQuery.prototype.lib = function() {
3636
return new WOQLLibrary()
3737
}
3838

39-
WOQLQuery.prototype.abstract = function(graph, subj) {
40-
this._add_partial(subj, 'system:tag', 'system:abstract', graph)
41-
return this
42-
}
43-
4439
WOQLQuery.prototype.string = function(s) {
4540
return {'@type': 'xsd:string', '@value': String(s)}
4641
}
@@ -111,36 +106,6 @@ WOQLQuery.prototype.nuke = function(g) {
111106
}
112107
}
113108

114-
/**
115-
* Add a property at the current class/document
116-
*
117-
* @param {string} proId - property ID
118-
* @param {string} type - property type (range) (on class inserts) property value on data inserts
119-
* @returns WOQLQuery object
120-
*
121-
* A range could be another class/document or an "xsd":"http://www.w3.org/2001/XMLSchema#" type
122-
* like string|integer|datatime|nonNegativeInteger|positiveInteger etc ..
123-
* (you don't need the prefix xsd for specific a type)
124-
*/
125-
126-
WOQLQuery.prototype.property = function(proId, type_or_value) {
127-
//get the name of the last class it used only here!!!
128-
//the value is setted in add_class (woqlSchema)
129-
if (this._adding_class()) {
130-
let part = this.findLastSubject(this.cursor)
131-
let g = false
132-
let gpart
133-
if (part) gpart = part['graph_filter'] || part['graph']
134-
if (gpart) g = gpart['@value']
135-
let nprop = new WOQLQuery()
136-
.add_property(proId, type_or_value, g)
137-
.domain(this._adding_class())
138-
this.and(nprop)
139-
} else {
140-
this._add_partial(false, proId, type_or_value)
141-
}
142-
return this
143-
}
144109

145110
WOQLQuery.prototype.node = function(node, type) {
146111
type = type || false
@@ -157,6 +122,28 @@ WOQLQuery.prototype.node = function(node, type) {
157122
return this
158123
}
159124

125+
/**
126+
* set the graph type schema or instance
127+
* for the query we point at the instance graph so you use this only
128+
* if you would like to query the schema graph
129+
*/
130+
//do not remove
131+
WOQLQuery.prototype.graph = function (g) {
132+
return this._set_context({
133+
graph: g
134+
});
135+
};
136+
//do not remove
137+
WOQLQuery.prototype._set_context = function (ctxt) {
138+
if (!this.triple_builder_context) this.triple_builder_context = {};
139+
140+
for (var k in ctxt) {
141+
this.triple_builder_context[k] = ctxt[k];
142+
}
143+
144+
return this;
145+
};
146+
160147
WOQLQuery.prototype.insert = function(id, type, refGraph) {
161148
refGraph = refGraph || (this.triple_builder_context ? this.triple_builder_context.graph : false)
162149
if (refGraph) {
@@ -165,24 +152,6 @@ WOQLQuery.prototype.insert = function(id, type, refGraph) {
165152
return this.add_triple(id, 'rdf:type', '@schema:'+type)
166153
}
167154

168-
WOQLQuery.prototype.insert_data = function(data, refGraph) {
169-
if (data.type && data.id) {
170-
this.insert(data.id, type, refGraph)
171-
if (data.label) {
172-
this.label(data.label)
173-
}
174-
if (data.description) {
175-
this.description(data.description)
176-
}
177-
for (var k in data) {
178-
if (['id', 'label', 'type', 'description'].indexOf(k) == -1) {
179-
this.property(k, data[k])
180-
}
181-
}
182-
}
183-
return this
184-
}
185-
186155
/**
187156
* @description Creates a pattern matching rule for a quad [Subject, Predicate, Object, Graph] or for a triple [Subject, Predicate, Object]
188157
* add extra information about the type of the value object
@@ -217,226 +186,4 @@ WOQLQuery.prototype.link = function(a, b, c, g) {
217186
}
218187
}
219188

220-
WOQLQuery.prototype.graph = function(g) {
221-
return this._set_context({graph: g})
222-
}
223-
224-
WOQLQuery.prototype.domain = function(d) {
225-
return this._add_partial(false, 'rdfs:domain', d)
226-
}
227-
/**
228-
* @param {string} labelVar - the label variable name or string "v:Label" or "My Label"
229-
* @param {string} [lang] - the label language
230-
*/
231-
232-
WOQLQuery.prototype.label = function(labelVar, lang = 'en') {
233-
if (labelVar.substring(0, 2) == 'v:') {
234-
var d = labelVar
235-
} else {
236-
var d = {'@value': labelVar, '@type': 'xsd:string', '@language': lang}
237-
}
238-
return this._add_partial(false, 'rdfs:label', d)
239-
}
240-
241-
WOQLQuery.prototype.description = function(c, lang) {
242-
lang = lang ? lang : 'en'
243-
if (c.substring(0, 2) == 'v:') {
244-
var d = c
245-
} else {
246-
var d = {'@value': c, '@type': 'xsd:string', '@language': lang}
247-
}
248-
return this._add_partial(false, 'rdfs:comment', d)
249-
}
250-
251-
/**
252-
* Specifies that a new class should have parents class
253-
* @param {array} parentList the list of parent class []
254-
*
255-
*/
256-
WOQLQuery.prototype.parent = function(...parentList) {
257-
for (var i = 0; i < parentList.length; i++) {
258-
var pn = parentList[i]
259-
this._add_partial(false, 'rdfs:subClassOf', pn)
260-
}
261-
return this
262-
}
263-
264-
WOQLQuery.prototype.max = function(m) {
265-
this._card(m, 'max')
266-
return this
267-
}
268-
269-
WOQLQuery.prototype.cardinality = function(m) {
270-
this._card(m, 'cardinality')
271-
return this
272-
}
273-
274-
WOQLQuery.prototype.min = function(m) {
275-
this._card(m, 'min')
276-
return this
277-
}
278-
279-
/**
280-
* Adds partially specified triples / quads as used in add_class, add_property, property, label, etc
281-
*/
282-
WOQLQuery.prototype._add_partial = function(s, p, o, g) {
283-
let ctxt = this.triple_builder_context || {}
284-
s = s || ctxt.subject
285-
g = g || ctxt.graph
286-
let lastsubj = this.findLastSubject(this.cursor)
287-
//if the last subject is a cardinality restriction quad
288-
//I'm looking for the last property
289-
if (
290-
lastsubj['predicate'] &&
291-
lastsubj['predicate']['node'] &&
292-
lastsubj['predicate']['node'].indexOf('Cardinality') > -1
293-
) {
294-
lastsubj = this.findLastProperty(this.cursor)
295-
}
296-
if (lastsubj && !s) s = lastsubj['subject']
297-
let t = ctxt.action || lastsubj['@type']
298-
if (!g) {
299-
const gobj = lastsubj['graph_filter'] || lastsubj['graph']
300-
g = gobj ? gobj['@value'] : 'schema'
301-
}
302-
if (t == 'AddTriple') this.and(new WOQLQuery().add_quad(s, p, o,g))
303-
else if (t == 'DeleteTriple') this.and(new WOQLQuery().delete_quad(s, p, o,g))
304-
else if (t == 'AddTriple') this.and(new WOQLQuery().add_quad(s, p, o, g))
305-
else if (t == 'DeleteTriple') this.and(new WOQLQuery().delete_quad(s, p, o, g))
306-
else if (t == 'Triple') this.and(new WOQLQuery().quad(s, p, o, g))
307-
else this.and(new WOQLQuery().quad(s, p, o, g))
308-
return this
309-
}
310-
311-
/**
312-
* Called to indicate that a add class chain has started
313-
* needed to ensure that we maintain a second state (class) as well as a property state
314-
*/
315-
WOQLQuery.prototype._adding_class = function(string_only) {
316-
if (this.triple_builder_context) {
317-
let x = this.triple_builder_context['adding_class']
318-
if (x && string_only && typeof x == 'object') return x['node']
319-
return x
320-
}
321-
return false
322-
}
323-
324-
WOQLQuery.prototype._set_adding_class = function(c) {
325-
if (!this.triple_builder_context) this.triple_builder_context = {}
326-
this.triple_builder_context['adding_class'] = c
327-
return this
328-
}
329-
330-
/**
331-
* Sets the internal context for triple building functions
332-
*/
333-
WOQLQuery.prototype._set_context = function(ctxt) {
334-
if (!this.triple_builder_context) this.triple_builder_context = {}
335-
for (var k in ctxt) {
336-
this.triple_builder_context[k] = ctxt[k]
337-
}
338-
return this
339-
}
340-
341-
/**
342-
* gets the object (value) for a triple earlier in the query that has a specific subject and predicate
343-
* used to find the class that a property cardinality restriction is applied to
344-
*/
345-
WOQLQuery.prototype._get_object = function(s, p) {
346-
if (this.cursor['@type'] == 'And') {
347-
for (var i = 0; i < this.cursor['query_list'].length; i++) {
348-
let subq = this.cursor['query_list'][i]['query']
349-
if (
350-
this._same_entry(subq['subject'], s) &&
351-
this._same_entry(subq['predicate'], p)
352-
)
353-
return subq['object']
354-
}
355-
}
356-
return false
357-
}
358-
359-
WOQLQuery.prototype._same_entry = function(a, b) {
360-
if (a == b) return true
361-
if (typeof a == 'object' && typeof b == 'string') {
362-
return this._string_matches_object(b, a)
363-
}
364-
if (typeof b == 'object' && typeof a == 'string') {
365-
return this._string_matches_object(a, b)
366-
}
367-
if (typeof a == 'object' && typeof b == 'object') {
368-
for (var k in a) {
369-
if (!b[k] || a[k] != b[k]) return false
370-
}
371-
for (var k in b) {
372-
if (!a[k] || a[k] != b[k]) return false
373-
}
374-
return true
375-
}
376-
}
377-
378-
/**
379-
* Checks to see if a string and a node / datatype / variable are in fact the same
380-
* @param {string} str
381-
* @param {object} node
382-
* @returns {string|boolean}
383-
*/
384-
WOQLQuery.prototype._string_matches_object = function(str, node) {
385-
if (node['node']) return str == node['node']
386-
if (node['@value']) return str == node['@value']
387-
if (node['variable_name']) return str == 'v:' + node['variable_name']['@value']
388-
return false
389-
}
390-
391-
/**
392-
*
393-
* @param {number} cardValue
394-
* @param {string} which
395-
* @returns {WOQLQuery}
396-
*/
397-
398-
WOQLQuery.prototype._card = function(cardValue, which) {
399-
//need to generate a new id for the cardinality restriction object
400-
//and point it at the property in question via the onProperty function
401-
let ctxt = this.triple_builder_context || {}
402-
let subject = ctxt.subject
403-
let graph = ctxt.graph
404-
let lastsubj = this.findLastProperty(this.cursor)
405-
if (lastsubj && !subject) subject = lastsubj['subject']
406-
if (typeof subject === 'object') {
407-
if (subject['node']) subject = subject['node']
408-
else return this
409-
}
410-
411-
if (lastsubj && !graph) {
412-
const gobj = lastsubj['graph_filter'] || lastsubj['graph']
413-
graph = gobj ? gobj['@value'] : false
414-
}
415-
let cardId = subject + '_' + which + '_' + cardValue
416-
417-
//important!! this add_quad have to stay in this position
418-
//then make the domain of the property into a subclass of the restriction
419-
let classDomain = this._get_object(subject, 'rdfs:domain')
420-
if (classDomain) {
421-
this.and(new WOQLQuery().add_quad(classDomain, 'subClassOf', cardId, graph))
422-
}
423-
this.and(
424-
new WOQLQuery()
425-
.add_quad(cardId, 'type', 'owl:Restriction', graph)
426-
.add_quad(cardId, 'owl:onProperty', subject, graph),
427-
)
428-
const cardObj = {min: 'owl:minCardinality', max: 'owl:maxCardinality'}
429-
let cardType = cardObj[which] ? cardObj[which] : 'owl:Cardinality'
430-
431-
this.and(
432-
new WOQLQuery().add_quad(
433-
cardId,
434-
cardType,
435-
{'@value': cardValue, '@type': 'xsd:nonNegativeInteger'},
436-
graph,
437-
),
438-
)
439-
return this
440-
}
441-
442189
module.exports = WOQLQuery

0 commit comments

Comments
 (0)