Skip to content

Commit afe6288

Browse files
Updated WOQL syntax tests passing (aside from TripleBuilder)
1 parent 5430903 commit afe6288

15 files changed

+229
-228
lines changed

lib/query/woqlCore.js

Lines changed: 116 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,15 @@ WOQLQuery.prototype.rawVar = function(varb) {
110110
return varb
111111
}
112112

113+
WOQLQuery.prototype.rawVarList = function(vl) {
114+
let ret = []
115+
for (var i = 0; i < vl.length; i++) {
116+
let co = this.rawVar(vl[i])
117+
ret.push(co)
118+
}
119+
return ret
120+
}
121+
113122
/**
114123
* Transforms a javascript representation of a query into a json object if needs be
115124
*/
@@ -181,19 +190,64 @@ WOQLQuery.prototype.arop = function(arg) {
181190
if (typeof arg == 'object') {
182191
return arg.json ? arg.json() : arg
183192
}
184-
return this.cleanArithmeticObject(arg, 'xsd:decimal')
193+
return this.cleanArithmeticValue(arg, 'xsd:decimal')
194+
}
195+
196+
/**
197+
* takes input that can be either a string (variable name)
198+
* or an array - each element of the array is a member of the list
199+
*/
200+
WOQLQuery.prototype.wlist = function(wvar, string_only) {
201+
if (typeof wvar == 'string') return this.expandDataVariable(wvar, true)
202+
if (Array.isArray(wvar)) {
203+
let ret = []
204+
for (var i = 0; i < wvar.length; i++) {
205+
let co = this.cleanDataValue(wvar[i])
206+
if (typeof co == 'string') co = {node: co}
207+
ret.push(co)
208+
}
209+
return ret
210+
}
211+
}
212+
213+
/**
214+
* takes a list of input that can be any value
215+
*/
216+
WOQLQuery.prototype.valueList = function(wvar, string_only) {
217+
if (typeof wvar == 'string') return this.expandValueVariable(wvar, true)
218+
if (Array.isArray(wvar)) {
219+
let ret = []
220+
for (var i = 0; i < wvar.length; i++) {
221+
let co = this.cleanObject(wvar[i])
222+
if (typeof co == 'string') co = {node: co}
223+
ret.push(co)
224+
}
225+
return ret
226+
}
185227
}
186228

187229
/**
188-
* Wraps value lists in the appropriate json-ld
230+
* creates an unadorned variable name list
189231
*/
190232
WOQLQuery.prototype.vlist = function(list) {
191-
vlist = []
192-
for(i = 0; i++; i < list.length){
193-
v = this.expandVariable(list[i])
194-
vlist.push(v)
233+
let vl = []
234+
for (var i = 0; i < list.length; i++){
235+
let v = this.expandValueVariable(list[i])
236+
vl.push(v['variable'])
195237
}
196-
return vlist
238+
return vl
239+
}
240+
241+
/**
242+
* Wraps data values
243+
*/
244+
WOQLQuery.prototype.dataValueList = function(list) {
245+
let dvl = []
246+
for (var i = 0; i < list.length; i++){
247+
let o = this.cleanDataValue(list[i])
248+
dvl.push(o)
249+
}
250+
return dvl
197251
}
198252

199253
/**
@@ -208,8 +262,7 @@ WOQLQuery.prototype.cleanSubject = function(s) {
208262
return s
209263
} else if (typeof s === 'string') {
210264
if (s.indexOf('v:') !== -1) subj = s
211-
//else if (this.vocab && this.vocab[s]) subj = this.vocab[s]
212-
else subj = s //'doc:' + s
265+
else subj = s
213266
return this.expandNodeVariable(subj)
214267
}
215268
this.parameterError('Subject must be a URI string')
@@ -265,6 +318,8 @@ WOQLQuery.prototype.cleanObject = function(o, t) {
265318
return this.cleanClass(o)
266319
} else if (this.vocab && this.vocab[o]) {
267320
return this.cleanClass(this.vocab[o])
321+
} else if (s.indexOf('v:') !== -1) {
322+
return this.expandValueVariable(o)
268323
} else {
269324
obj['data'] = this.jlt(o, t)
270325
}
@@ -278,10 +333,14 @@ WOQLQuery.prototype.cleanObject = function(o, t) {
278333
return obj
279334
}
280335

281-
WOQLQuery.prototype.cleanDataObject = function(o, t) {
336+
WOQLQuery.prototype.cleanDataValue = function(o, t) {
282337
let obj = {'@type': 'DataValue'}
283338
if (typeof o === 'string') {
284-
obj['data'] = this.jlt(o, t)
339+
if (o.indexOf('v:') !== -1) {
340+
return this.expandDataVariable(o)
341+
}else{
342+
obj['data'] = this.jlt(o, t)
343+
}
285344
} else if (typeof o == 'number') {
286345
t = t || 'xsd:decimal'
287346
obj['data'] = this.jlt(o, t)
@@ -292,10 +351,14 @@ WOQLQuery.prototype.cleanDataObject = function(o, t) {
292351
return obj
293352
}
294353

295-
WOQLQuery.prototype.cleanArithmeticObject = function(o, t) {
354+
WOQLQuery.prototype.cleanArithmeticValue = function(o, t) {
296355
let obj = {'@type': 'ArithmeticValue'}
297356
if (typeof o === 'string') {
298-
obj['data'] = this.jlt(o, t)
357+
if (o.indexOf('v:') !== -1) {
358+
return this.expandArithmeticVariable(o)
359+
}else{
360+
obj['data'] = this.jlt(o, t)
361+
}
299362
} else if (typeof o == 'number') {
300363
t = t || 'xsd:decimal'
301364
obj['data'] = this.jlt(o, t)
@@ -306,12 +369,27 @@ WOQLQuery.prototype.cleanArithmeticObject = function(o, t) {
306369
return obj
307370
}
308371

372+
WOQLQuery.prototype.cleanNodeValue = function(o, t) {
373+
let obj = {'@type': 'NodeValue'}
374+
if (typeof o === 'string') {
375+
if (o.indexOf('v:') !== -1) {
376+
return this.expandNodeVariable(o)
377+
} else {
378+
obj['node'] = o
379+
}
380+
} else if (typeof o == 'object' && o) {
381+
if (o['@value']) obj['data'] = o
382+
else return o
383+
}
384+
return obj
385+
}
386+
309387
/*
310388
* check if can be a class in the object
311389
*/
312390
WOQLQuery.prototype.looksLikeClass = function(o) {
313391
//if it is like User or Person it is a class
314-
if (o.indexOf('v:') === -1) return true
392+
if (o.indexOf('v:') === -1) return true
315393

316394
let pref = o.split(':')[0]
317395
if (
@@ -339,61 +417,45 @@ WOQLQuery.prototype.cleanGraph = function(g) {
339417
* Transforms strings that start with v: into variable json-ld structures
340418
* @param varname - will be transformed if it starts with v:
341419
*/
342-
WOQLQuery.prototype.expandVariable = function(varname, always) {
420+
WOQLQuery.prototype.expandVariable = function(varname,type,always) {
343421
if (varname.substring(0, 2) == 'v:' || always) {
344422
if (varname.substring(0, 2) == 'v:') varname = varname.substring(2)
345423
return {
346-
'@type': 'Value',
424+
'@type': type,
347425
'variable': varname
348426
}
349427
} else {
350428
return {
351-
'@type': 'Value',
429+
'@type': type,
352430
'node': varname,
353431
}
354432
}
355-
//return varname
433+
}
434+
435+
WOQLQuery.prototype.expandValueVariable = function(varname, always) {
436+
return this.expandVariable(varname, 'Value', always)
356437
}
357438

358439
WOQLQuery.prototype.expandNodeVariable = function(varname, always) {
359-
if (varname.substring(0, 2) == 'v:' || always) {
360-
if (varname.substring(0, 2) == 'v:') varname = varname.substring(2)
361-
return {
362-
'@type': 'NodeValue',
363-
'variable': varname,
364-
}
365-
} else {
366-
return {
367-
'@type': 'NodeValue',
368-
'node': varname,
369-
}
370-
}
371-
//return varname
440+
return this.expandVariable(varname, 'NodeValue', always)
372441
}
373442

374443
WOQLQuery.prototype.expandDataVariable = function(varname, always) {
375-
if (varname.substring(0, 2) == 'v:' || always) {
376-
if (varname.substring(0, 2) == 'v:') varname = varname.substring(2)
377-
return {
378-
'@type': 'Value',
379-
'variable': varname
380-
}
381-
} else {
382-
return {
383-
'@type': 'Value',
384-
'node': varname,
385-
}
386-
}
387-
//return varname
444+
return this.expandVariable(varname, 'DataValue', always)
388445
}
389446

447+
WOQLQuery.prototype.expandArithmeticVariable = function(varname, always) {
448+
return this.expandVariable(varname, 'ArithmeticValue', always)
449+
}
450+
451+
390452
WOQLQuery.prototype.cleanClass = function(c, stringonly) {
391453
if (typeof c !== 'string') return ''
392454
if (c.indexOf(':') === -1) {
393455
if (this.vocab && this.vocab[c]) c = this.vocab[c]
394456
else c = c //'scm:' + c
395457
}
396-
return stringonly ? c : this.expandVariable(c)
458+
return stringonly ? c : this.expandValueVariable(c)
397459
}
398460

399461
WOQLQuery.prototype.cleanType = function(t, stringonly) {
@@ -724,11 +786,16 @@ function tokensToJSON(seq, q) {
724786
function copyJSON(orig, rollup) {
725787
if (Array.isArray(orig)) return orig
726788
if (rollup) {
727-
if (['And', 'Or'].indexOf(orig['@type']) != -1) {
728-
if (!orig['query_list'] || !orig['query_list'].length) return {}
729-
if (orig['query_list'].length == 1)
730-
return copyJSON(orig['query_list'][0]['query'], rollup)
789+
if (orig['@type'] == 'And') {
790+
if (!orig['and'] || !orig['and'].length) return {}
791+
if (orig['and'].length == 1)
792+
return copyJSON(orig['and'][0], rollup)
793+
} else if (orig['@type'] == 'Or') {
794+
if (!orig['or'] || !orig['or'].length) return {}
795+
if (orig['or'].length == 1)
796+
return copyJSON(orig['or'][0], rollup)
731797
}
798+
732799
if (typeof orig['query'] != 'undefined' && orig['@type'] != 'Comment') {
733800
if (!orig['query']['@type']) return {}
734801
} else if (orig['@type'] == 'Comment' && orig['comment']) {

0 commit comments

Comments
 (0)