Newer
Older
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
var SparqlGenerator = require('sparqljs').Generator;
// var SparqlParser = require('sparqljs').Parser;
var Config = require('./SparnaturalConfig.js');
class DefaultQueryGenerator {
constructor(addDistinct, typePredicate, specProvider) {
this.WIDGETS_REQUIRING_VALUES = [
Config.SEARCH_PROPERTY,
Config.TIME_PROPERTY_PERIOD,
Config.TIME_PROPERTY_YEAR,
Config.TIME_PROPERTY_DATE
] ;
this.addDistinct = addDistinct;
this.typePredicate = typePredicate;
this.specProvider = specProvider;
this.additionnalPrefixes = {};
}
// add a new prefix to the generated query
addPrefix(prefix, uri) {
this.additionnalPrefixes[prefix] = uri;
}
setPrefixes(prefixes) {
this.additionnalPrefixes = prefixes;
}
/**
* Generates the query and notifies the callback
**/
generateQuery(formObject) {
var jsonQuery = this.newQueryJson() ;
var ArrayLiIndex = [] ;
$(formObject._this).find('ul.componentsListe li.groupe').each(function(i) {
var data_id = $(this).attr('data-index') ;
ArrayLiIndex[data_id] = ArrayLiIndex.length ;
}) ;
if(this.hasEnoughCriteria(formObject)) {
for (var i = 0; i < formObject.components.length; i++) {
jsonQuery = this.processQueryComponent(
jsonQuery,
formObject,
ArrayLiIndex,
formObject.components[i],
i
);
} ;
var generator = new SparqlGenerator();
var generatedQuery = generator.stringify(jsonQuery);
return { "generatedQuery" : generatedQuery, "jsonQuery" : jsonQuery } ;
} else {
return null;
}
}
hasEnoughCriteria(formObject) {
for (var i = 0; i < formObject.components.length; i++) {
// if there is no value selected and the widget required one
// do not process this component
if(
($.inArray(formObject.components[i].CriteriaGroup.EndClassWidgetGroup.inputTypeComponent.widgetType, this.WIDGETS_REQUIRING_VALUES) > -1)
&&
(formObject.components[i].CriteriaGroup.EndClassWidgetGroup.selectedValues.length === 0)
) {
continue;
} else {
// we will have at least one component with a criteria
return true;
}
} ;
// not enough criteria found.
return false;
}
processQueryComponent(jsonQuery, formObject, ArrayLiIndex, component, index) {
var VALUE_SELECTION_WIDGETS = [
Config.LIST_PROPERTY,
Config.AUTOCOMPLETE_PROPERTY,
Config.NON_SELECTABLE_PROPERTY // Pas de valeur selectionné mais sera forcement utilisé pour une Class
];
var SPARQL_GRAPHDB_SEARCH_PROPERTY = 'sparql:GraphDBSearchProperty';
var domainClass = component.CriteriaGroup.StartClassGroup.value_selected ;
var property = component.CriteriaGroup.ObjectPropertyGroup.value_selected ;
var rangeClass = component.CriteriaGroup.EndClassGroup.value_selected ;
var dependantDe = this.GetDependantCriteria(formObject, index) ;
// get index of subject and object variables
var subjectVariableIndex;
var objectVarIndex;
if ((dependantDe != null) && (dependantDe.type == 'parent')){
subjectVariableIndex = ArrayLiIndex[dependantDe.element.id] + 1;
objectVarIndex = ArrayLiIndex[index] + 1;
addStartClass = false ;
} else {
subjectVariableIndex = 0 ;
objectVarIndex = ArrayLiIndex[index] + 1 ;
}
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
// name start and end variables
// dashes should be replaced
if (subjectVariableIndex == 0) {
subjectVariable = "?this";
} else {
subjectVariable = '?'+this.localName(domainClass).replace("-", "_")+''+subjectVariableIndex ;
}
if (rangeClass != null) {
var objectVariable = '?'+this.localName(rangeClass).replace("-", "_")+''+objectVarIndex ;
} else {
var objectVariable = null ;
}
// whether to add class criteria for subject or not
var addStartClass = true ;
if ((dependantDe != null) && (dependantDe.type == 'parent')){
addStartClass = false ;
}
if ((dependantDe != null) && (dependantDe.type == 'sibling')){
addStartClass = false ;
}
// list of triples to be inserted in the query
var newBasicGraphPattern = this.initBasicGraphPattern() ;
if (addStartClass) {
newBasicGraphPattern.triples.push(this.buildTriple(subjectVariable, this.typePredicate, domainClass)) ;
}
var _WidgetType = component.CriteriaGroup.EndClassWidgetGroup.inputTypeComponent.widgetType ;
if ( VALUE_SELECTION_WIDGETS.indexOf(_WidgetType) !== -1 ) {
if (component.CriteriaGroup.EndClassWidgetGroup.selectedValues.length == 1) {
// if we are in a value selection widget and we have a single value selected
// then insert the value directly as the object of the triple
newBasicGraphPattern.triples.push(this.buildTriple(subjectVariable, property, component.CriteriaGroup.EndClassWidgetGroup.selectedValues[0])) ;
} else {
// otherwise use a variable name as the object of the triple
newBasicGraphPattern.triples.push(this.buildTriple(subjectVariable, property, objectVariable)) ;
}
// if no value is selected add a type criteria for the object
if (
component.CriteriaGroup.EndClassWidgetGroup.selectedValues.length == 0
&&
(
!this.specProvider.isRemoteClass(component.CriteriaGroup.EndClassGroup.value_selected)
&&
!this.specProvider.isLiteralClass(component.CriteriaGroup.EndClassGroup.value_selected)
)
) {
newBasicGraphPattern.triples.push(this.buildTriple(objectVariable, this.typePredicate, component.CriteriaGroup.EndClassGroup.value_selected)) ;
}
} else {
if (
objectVariable !== null
&&
// don't add the triple if we are on a fulltext search since this will be part of the
// search clause
// this.specProvider.getObjectPropertyType(property).indexOf(SPARQL_GRAPHDB_SEARCH_PROPERTY) == -1
_WidgetType != Config.GRAPHDB_SEARCH_PROPERTY
) {
newBasicGraphPattern.triples.push(this.buildTriple(subjectVariable, property, objectVariable)) ;
}
}
jsonQuery.where.push(newBasicGraphPattern) ;
if(component.CriteriaGroup.EndClassWidgetGroup.selectedValues.length > 0 ) {
var __this = this ;
switch (_WidgetType) {
case Config.LIST_PROPERTY:
if (component.CriteriaGroup.EndClassWidgetGroup.selectedValues.length > 1) {
// add values clause if we have more than 1 values
var jsonValues = this.initValues() ;
jsonValues = this.addVariable(jsonValues, objectVariable, component.CriteriaGroup.EndClassWidgetGroup.selectedValues)
jsonQuery.where.push(jsonValues) ;
}
break;
case Config.AUTOCOMPLETE_PROPERTY:
if (component.CriteriaGroup.EndClassWidgetGroup.selectedValues.length > 1) {
// add values clause if we have more than 1 values
var jsonValues = this.initValues() ;
jsonValues = this.addVariable(jsonValues, objectVariable, component.CriteriaGroup.EndClassWidgetGroup.selectedValues)
jsonQuery.where.push(jsonValues) ;
}
break;
case Config.TIME_PROPERTY_PERIOD:
case Config.TIME_PROPERTY_YEAR:
case Config.TIME_PROPERTY_DATE:
for (var key in component.CriteriaGroup.EndClassWidgetGroup.selectedValues) {
var value = component.CriteriaGroup.EndClassWidgetGroup.selectedValues[key];
jsonQuery.where.push(
this.initFilterTime(value.start, value.stop, objectVariable)
) ;
}
break;
case Config.SEARCH_PROPERTY:
var searchKey = component.CriteriaGroup.EndClassWidgetGroup.selectedValues[0] ;
jsonFilter = this.initFilterSearch(searchKey, objectVariable) ;
jsonQuery.where.push(jsonFilter) ;
break;
case Config.GRAPHDB_SEARCH_PROPERTY:
var searchKey = component.CriteriaGroup.EndClassWidgetGroup.selectedValues[0] ;
jsonQuery = this.updateGraphDbPrefixes(jsonQuery);
var connectorName = this.localName(rangeClass);
var fieldName = this.localName(property);
var searchVariable = subjectVariable+"Search";
newBasicGraphPattern.triples.push(this.buildTriple(searchVariable, this.typePredicate, "http://www.ontotext.com/connectors/lucene/instance#"+connectorName)) ;
// add literal triple
newBasicGraphPattern.triples.push(this.buildTriple(searchVariable, "http://www.ontotext.com/connectors/lucene#query", fieldName+":"+searchKey, true)) ;
newBasicGraphPattern.triples.push(this.buildTriple(searchVariable, "http://www.ontotext.com/connectors/lucene#entities", subjectVariable)) ;
break;
default:
console.log('Unknown widget type when generating SPARQL : '+_WidgetType);
}
}
return jsonQuery;
}
GetDependantCriteria(thisForm_, id) {
var dependant = null ;
var dep_id = null ;
var element = thisForm_._this.find('li[data-index="'+id+'"]') ;
if ($(element).parents('li').length > 0) {
dep_id = $($(element).parents('li')[0]).attr('data-index') ;
dependant = {type : 'parent'} ;
} else {
if ($(element).prev().length > 0) {
dep_id = $(element).prev().attr('data-index') ;
dependant = {type : 'sibling'} ;
}
}
$(thisForm_.components).each(function(index) {
if (this.index == dep_id) {
dependant.element = this.CriteriaGroup ;
}
}) ;
return dependant ;
}
newQueryJson() {
var jsonQuery = {
"queryType": "SELECT"+((this.addDistinct)?' DISTINCT':'')+"",
"variables": [
"?this"
],
"where": [],
"type": "query",
"prefixes": {
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"xsd": "http://www.w3.org/2001/XMLSchema#"
}
};
// add additionnal prefixes
for (var key in this.additionnalPrefixes) {
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
jsonQuery.prefixes[key] = this.additionnalPrefixes[key];
}
return jsonQuery;
}
initBasicGraphPattern() {
return {
"type": "bgp",
"triples": []
} ;
}
initValues() {
return {
"type": "values",
"values": []
} ;
}
initFilterTime(StartYear, EndYear, index) {
var filters = new Array ;
if (StartYear != null) {
filters.push( {
"type": "operation",
"operator": ">=",
"args": [
""+index+"",
"\""+StartYear+"\"^^http://www.w3.org/2001/XMLSchema#date"
]
}) ;
}
if (EndYear != null) {
filters.push( {
"type": "operation",
"operator": "<=",
"args": [
""+index+"",
"\""+EndYear+"\"^^http://www.w3.org/2001/XMLSchema#date"
]
}) ;
}
if (filters.length == 2 ) {
return {
"type": "filter",
"expression": {
"type": 'operation',
"operator": "&&",
"args": filters
}
} ;
} else {
return {
"type": "filter",
"expression": filters[0]
} ;
}
}
initFilterSearch(texte, variable) {
return {
"type": "filter",
"expression": {
"type": "operation",
"operator": "regex",
"args": [
""+variable+"",
"\""+texte+"\"",
"\"i\""
]
}
} ;
}
buildTriple(subjet, predicate, object, literalObject=false) {
// encapsulates the object in quotes so that it is interpreted as a literal
var objectValue = (literalObject)?"\""+object+"\"":object;
var triple = {
"subject": subjet,
"predicate": predicate,
"object": objectValue,
} ;
return triple;
}
addVariable(jsonValues, name, valueUrl) {
$.each(valueUrl, function( index, value ) {
var newValue = { } ;
newValue[name] = value ;
jsonValues.values.push(newValue) ;
});
return jsonValues ;
}
addVariableDate(json, name, valueUrl) {
var newValue = { };
newValue[name] = valueUrl ;
json.where[1].values.push(newValue) ;
return json ;
}
localName(uri) {
if (uri.indexOf("#") > -1) {
return uri.split("#")[1] ;
} else {
var components = uri.split("/") ;
return components[components.length - 1] ;
}
}
updateGraphDbPrefixes(jsonQuery) {
jsonQuery.prefixes.inst = "http://www.ontotext.com/connectors/lucene/instance#";
jsonQuery.prefixes.lucene = "http://www.ontotext.com/connectors/lucene#";
return jsonQuery ;
}
}
module.exports = {
DefaultQueryGenerator: DefaultQueryGenerator
}