forked from ad-freiburg/qlever-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sparql-hint.js
executable file
·1514 lines (1268 loc) · 50.6 KB
/
sparql-hint.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
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
273
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var lastUrl; // remark url for autocomplete call
var requestExtension = false; // append autocompletion or create new widget
var lastSize = 0; // size of last auto completion call (increases over the time)
var size = 40; // size for next auto completion call
var resultSize = 0; // result size for counter badge
var lastWidget = undefined; // last auto completion widget instance
var activeLine; // the current active line that holds loader / counter badge
var activeLineBadgeLine; // the bade holder in the current active line
var activeLineNumber; // the line number of the active line (replaced by loader)
var sparqlCallback;
var sparqlFrom;
var sparqlTo;
var sparqlTimeout;
var sparqlRequest;
var suggestions;
(function (mod) {
if (typeof exports == "object" && typeof module == "object") // CommonJS
mod(require("../../lib/codemirror"), require("../../mode/sparql/sparql"));
else if (typeof define == "function" && define.amd) // AMD
define(["../../lib/codemirror", "../../mode/sparql/sparql"], mod);
else // Plain browser env
mod(CodeMirror);
})(function (CodeMirror) {
"use strict";
var timeoutCompletion; // holds the window.timeout of the completion - needed to stop requests
var sparqlQuery; // holds the sparql query that is executed
var Pos = CodeMirror.Pos,
cmpPos = CodeMirror.cmpPos;
// helper to detect arrays
function isArray(val) {
return Object.prototype.toString.call(val) == "[object Array]"
}
// get language specific keywords
function getKeywords(editor) {
var mode = editor.doc.modeOption;
return CodeMirror.resolveMode(mode).keywords;
}
// add matches to result
function addMatches(result, addedSuggestions, context) {
log('Found ' + addedSuggestions.length + ' suggestions for this position', 'suggestions');
// current line
var cursor = editor.getCursor();
var line = editor.getLine(cursor.line).slice(0, cursor.ch);
var curChar = line[line.length - 1];
var lineTokens = [];
var token = "";
var types = getAvailableTypes(context);
// split line by white spaces
var nextToken = undefined;
do {
nextToken = getLastLineToken(line);
if (!(nextToken.string.match(/^\?[\w\d]*$/) && nextToken.endsInWhitespace)) {
lineTokens.unshift(nextToken);
}
line = line.slice(0, nextToken.start)
} while (nextToken == undefined || nextToken.start != 0);
// remove tokens one by one until there are suggestions
var foundSuggestions = false;
var allSuggestions = [];
for (var j in lineTokens) {
if (foundSuggestions) {
break;
}
var currentTokens = lineTokens.slice(j);
var token = "";
// Rebuild the token we just typed
for (var subToken of currentTokens) {
token += subToken.string;
if (subToken.endsInWhitespace) {
token += " ";
}
}
for (var suggestion of addedSuggestions) {
var word = suggestion.word;
var type = types[suggestion.type] || {};
var alreadyExists = 0;
var fullLineContent = editor.getLine(cursor.line).trim();
if (type.requiresEmptyLine == true && (fullLineContent != "" && !word.startsWith(fullLineContent))) {
continue;
}
// check if the type already exists
if (type.onlyOnce == true) {
// get content to test with
var content = (context) ? context['content'] : editor.getValue();
if (type.definition) {
type.definition.lastIndex = 0;
var match = content.match(type.definition) || [];
alreadyExists = match.length;
}
}
if (j == 0 && type.suggestOnlyWhenMatch != true && alreadyExists == 0) {
allSuggestions.push(suggestion.word);
}
if (word.toLowerCase().startsWith(token.toLowerCase()) && token.trim().length > 0 && word != token) {
// if the type already exists but it is within the token we just typed: continue suggesting it
// if it is outside of what we typed: don't suggest it
if (alreadyExists == 1) {
type.definition.lastIndex = 0;
var match = type.definition.exec(token);
if (!match) {
continue;
}
} else if (alreadyExists > 1) {
continue;
}
for (var subToken of currentTokens) {
if (subToken.endsInWhitespace) {
word = word.replace(RegExp(subToken.string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), "i"), "").replace(/^\s*/, "");
}
}
result.push(word);
foundSuggestions = true;
}
}
}
line = editor.getLine(cursor.line).slice(0, cursor.ch);
// suggest everything if we didn't find any suggestion and didn't start typing a word
if (!foundSuggestions && (!curChar || curChar.match(/\s/))) {
log('Could not determine any limits - showing all suggestions', 'suggestions');
if ((context && context.suggestInSameLine == true) || line == undefined || line.match(/^\s+$/)) {
for (var suggestion of allSuggestions) {
result.push(suggestion);
}
}
}
}
CodeMirror.registerHelper("hint", "sparql", function (editor, callback, options) {
// ************************************************************************************
//
//
// CORE LOGIC OF QLEVER UI SUGGESTIONS
//
//
// ************************************************************************************
// skip everything that is running by now
window.clearTimeout(sparqlTimeout);
if (sparqlRequest) { sparqlRequest.abort(); }
// reset the previous loader
if (activeLine) {
activeLine.html(activeLineNumber);
}
var cur = editor.getCursor(); // current cursor position
var absolutePosition = editor.indexFromPos((cur)); // absolute cursor position in text
var context = getCurrentContext(absolutePosition); // get current context
suggestions = [];
log('Position: ' + absolutePosition, 'suggestions');
if (context) {
log('Context: ' + context.w3name, 'suggestions');
} else {
log('Context: None', 'suggestions');
}
// get current token
var line = editor.getLine(cur.line).slice(0, cur.ch);
var token = getLastLineToken(line);
var start, end;
if (token.endsInWhitespace) {
start = end = cur.ch;
} else {
start = token.start;
end = token.end;
}
types = getAvailableTypes(context);
sparqlCallback = callback;
sparqlFrom = Pos(cur.line, start);
sparqlTo = Pos(cur.line, end);
var allTypeSuggestions = [];
for (var i = 0; i < types.length; i++) {
for (var suggestion of getTypeSuggestions(types[i], context)) {
if (context && context.forceLineBreak && !suggestion.endsWith('\n')) {
suggestion += "\n";
}
allTypeSuggestions.push({ word: suggestion, type: i });
}
}
addMatches(suggestions, allTypeSuggestions, context);
sparqlCallback({
list: suggestions,
from: sparqlFrom,
to: sparqlTo,
});
return false;
});
CodeMirror.hint.sparql.async = true;
});
/**
Find the complex types
@params context - the current context
**/
function getAvailableTypes(context) {
types = [];
contextName = "undefined";
if (context) {
contextName = context.w3name;
}
// check for complex types that are valid in this context
for (var i = 0; i < COMPLEXTYPES.length; i++) {
if (COMPLEXTYPES[i].availableInContext.indexOf(contextName) != -1) {
types.push(COMPLEXTYPES[i]);
}
}
return types;
}
function detectPropertyPath(predicate) {
var propertyPath = [];
var property = "";
var bracketCounter = 0;
for (var ch of predicate) {
if (ch == "/" && bracketCounter === 0) {
propertyPath.push(property);
property = "";
continue;
} else if (ch == "<") {
bracketCounter++;
} else if (ch == ">") {
bracketCounter--;
}
property += ch;
}
propertyPath.push(property);
return propertyPath;
}
function getDynamicSuggestions(context) {
var cur = editor.getCursor();
var line = editor.getLine(cur.line);
var suggestionMode = parseInt($("#dynamicSuggestions").val() + '');
word = getLastLineToken(line.slice(0, cur.ch));
if (word.endsInWhitespace) { word = ""; } else { word = word.string; }
// get current line
var words = line.slice(0, cur.ch).trimLeft().replace(' ', ' ').split(" ");
// Find words that are separated by whitespace but seem to be belong together
var whiteSpaceWord = "";
for (var i = words.length - 1; i >= 0; i--) {
var prevWord = words[i]
if (!(prevWord.startsWith("?") || prevWord.startsWith("<") || prevWord.endsWith(">") || prevWord.indexOf(":") != -1)) {
if (i == words.length - 1) {
whiteSpaceWord = prevWord;
} else {
whiteSpaceWord = (prevWord + " " + whiteSpaceWord);
}
words.splice(i, 2, whiteSpaceWord);
} else {
break;
}
}
word = words[words.length - 1];
var wordIndex = line.slice(0, cur.ch).lastIndexOf(word);
if (wordIndex != -1 && word.length > 0) {
sparqlFrom = CodeMirror.Pos(cur.line, wordIndex);
sparqlTo = CodeMirror.Pos(cur.line, cur.ch);
}
// Collect prefixes (as string and dict).
var prefixes = "";
var prefixesRelation = {};
var lines = getPrefixLines();
for (var prefLine of lines) {
if (prefLine.trim().startsWith("PREFIX")) {
var match = /PREFIX (.*): ?<(.*)>/g.exec(prefLine.trim());
if (match) {
prefixes += prefLine.trim() + '\n';
prefixesRelation[match[1]] = match[2];
}
}
}
// Get editor lines and remove current line.
var lines = context['content'].split('\n');
for (var i = 0; i < lines.length; i++) {
if (lines[i] == line) {
lines.splice(i, 1);
// watch for property paths and insert temporary lines
if (words.length == 2) {
var propertyPath = detectPropertyPath(word);
if (propertyPath.length > 1) {
// Found a property path!
for (var j = 0; j < propertyPath.length - 1; j++) {
lines.splice(i + j, 0, words[0] + " " + propertyPath[j] + " ?temp_" + j + " .");
words[0] = "?temp_" + j;
}
word = propertyPath[propertyPath.length - 1];
sparqlFrom = CodeMirror.Pos(sparqlTo.line, sparqlTo.ch - word.length);
}
}
break;
}
}
// replace the prefixes
$.each(prefixesRelation, function (key, value) {
if (word.startsWith(key + ':')) {
word = '<' + word.replace(key + ':', value);
return false;
}
});
if (words.length < 1 || words[0].toUpperCase() == "FILTER") {
var response = [];
var variables = getVariables(context, undefined, "both");
for (var i = 0; i < variables.length; i++) {
response.push(variables[i] + ' ');
}
return response;
} else {
// find connected lines in given select clause
const variableRegex = /\?\w+\b/g;
let seenVariables = line.match(variableRegex);
if (seenVariables) {
// at first we know only the variable in our current line and do not use any other lines
let linesTaken = [];
let foundNewVariables = true;
while (foundNewVariables) {
foundNewVariables = false;
for (const curLine of lines) {
if (curLine == "" || linesTaken.indexOf(curLine) != -1) {
continue;
}
// check for each already seen variable
for (const seenVariable of seenVariables) {
if (RegExp('\\' + seenVariable + "\\b").test(curLine) && curLine.indexOf('{') == -1) {
linesTaken.push(curLine);
// search for variables
for (const lineVariable of curLine.match(variableRegex)) {
if (seenVariables.indexOf(lineVariable) == -1) {
seenVariables.push(lineVariable);
// do another iteration because there are new variables
foundNewVariables = true;
}
}
break;
}
}
}
}
lines = [];
for (var line of linesTaken) {
let trimmed = line.trim();
if (!(/^FILTER/i.test(trimmed)) && !(/\.$/.test(trimmed))) {
// Add dots to lines without dots.
trimmed += " .";
}
lines.push(trimmed);
}
}
sparqlQuery = "";
var sendSparql = !(word.startsWith('?'));
var sparqlLines = "";
var mode1Query = ""; // mode 1 is context-insensitive
var mode2Query = ""; // mode 2 is context-sensitive
var suggestVariables;
var appendToSuggestions = "";
var nameList;
var response = [];
var predicateForObject = undefined;
if (suggestionMode > 0) {
if (words.length == 1) {
suggestVariables = "both";
appendToSuggestions = " ";
mode1Query = SUGGESTSUBJECTS_CONTEXT_INSENSITIVE;
mode2Query = SUGGESTSUBJECTS;
nameList = subjectNames;
} else if (words.length == 2) {
suggestVariables = word.startsWith('?') ? "normal" : false;
appendToSuggestions = " ";
nameList = predicateNames;
response = PREDICATESUGGESTIONS;
// add single prefixes to suggestions
if (SUGGEST_PREFIXNAMES_FOR_PREDICATES) {
response = response.concat(getPrefixNameSuggestions(word));
}
mode1Query = SUGGESTPREDICATES_CONTEXT_INSENSITIVE;
mode2Query = SUGGESTPREDICATES;
} else if (words.length == 3) {
predicateForObject = words[1];
suggestVariables = "normal";
appendToSuggestions = ' .';
nameList = objectNames;
mode1Query = SUGGESTOBJECTS_CONTEXT_INSENSITIVE;
mode2Query = SUGGESTOBJECTS;
// replace the prefixes
var propertyPath = detectPropertyPath(words[1]);
for (var i in propertyPath) {
var property = propertyPath[i];
$.each(prefixesRelation, function (key, value) {
if (property.startsWith(key + ':')) {
var addAsterisk = false;
if (property.endsWith('*')) {
property = property.slice(0, property.length - 1);
addAsterisk = true;
}
let noPrefixProperty = '<' + property.replace(key + ':', value) + '>';
if (REPLACE_PREDICATES[noPrefixProperty] !== undefined) {
property = REPLACE_PREDICATES[noPrefixProperty];
}
if (addAsterisk) {
property += "*";
}
propertyPath[i] = property;
return false;
}
});
}
words[1] = propertyPath.join("/");
var lastWord = words[1];
if (!lastWord.startsWith("<") && lastWord.indexOf("/") != -1) {
// property path detected. Get last predicate as lastWord
var properties = lastWord.split("/");
lastWord = properties[properties.length - 1];
}
lastWord = (predicateNames[lastWord] != "" && predicateNames[lastWord] != undefined) ? predicateNames[lastWord] : words[1];
if (typeof (lastWord) == "object") {
lastWord = String(lastWord);
}
if (lastWord == "ql:contains-entity") {
sendSparql = false;
} else if (lastWord == "ql:contains-word") {
sendSparql = false;
suggestVariables = false;
} else {
var subject = (subjectNames[words[0]] != "" && subjectNames[words[0]] != undefined) ? subjectNames[words[0]] : words[0];
var subjectVarName = subject.split(/[.\/\#:]/g).slice(-1)[0].replace(/@\w*$/, '').replace(/\s/g, '_').replace(/[^a-zA-Z0-9_]/g, '').toLowerCase();
var objectVarName = lastWord.split(/[.\/\#:]/g).slice(-1)[0]
.replace(/@\w*$/, "").replace(/\s/g, "_")
.replace(/^has([A-Z_-])/, "$1$").replace(/[^a-zA-Z0-9_]/g, "").toLowerCase();
response.push('?' + objectVarName + ' .');
response.push('?' + subjectVarName + '_' + objectVarName + ' .');
}
} else {
console.warn('Skipping every suggestions based on current position...');
return [];
}
let completionQuery = "";
let mixedModeQuery = "";
switch(suggestionMode) {
case 1:
completionQuery = mode1Query;
break;
case 2:
completionQuery = mode2Query; break;
case 3:
completionQuery = mode2Query;
mixedModeQuery = mode1Query;
break;
}
if (sendSparql && completionQuery) {
sparqlLines = replaceQueryPlaceholders(completionQuery, word, prefixes, lines, words);
if (mixedModeQuery) {
mixedModeQuery = replaceQueryPlaceholders(mixedModeQuery, word, prefixes, lines, words);
}
getQleverSuggestions(sparqlLines, prefixesRelation, appendToSuggestions, nameList, predicateForObject, word, mixedModeQuery);
}
if (suggestVariables) {
var variables = getVariables(context, undefined, suggestVariables);
for (var variable of variables) {
response.push(variable + appendToSuggestions);
}
}
return (!requestExtension) ? response : [];
}
}
console.warn('Skipping every suggestions based on current position...');
return [];
}
function replaceQueryPlaceholders(completionQuery, word, prefixes, lines, words) {
// first, build the complete AC query
sparqlLines = substituteCustomPlaceholders(completionQuery)
for (const prefixName in COLLECTEDPREFIXES) {
prefixes += `\nPREFIX ${prefixName}: <${COLLECTEDPREFIXES[prefixName]}>`
}
word = word.replaceAll('.','\\\\.')
.replaceAll('*','\\\\*')
.replaceAll('^','\\\\^')
.replaceAll('?','\\\\?')
.replaceAll('[','\\\\[')
.replaceAll(']','\\\\]');
var word_with_bracket = ((word.startsWith("<") || word.startsWith('"')) ? "" : "<") + word.replace(/'/g, "\\'");
sparqlLines = sparqlLines.replace(/%<CURRENT_WORD%/g, word_with_bracket).replace(/%CURRENT_WORD%/g, word);
sparqlLines = sparqlLines.replace(/%PREFIXES%/g, prefixes);
var linePlaceholder = sparqlLines.match(/(\s*)%CONNECTED_TRIPLES%/);
while (linePlaceholder != null) {
sparqlLines = sparqlLines.replace(/%CONNECTED_TRIPLES%/g, lines.join(linePlaceholder[1]));
linePlaceholder = sparqlLines.match(/(\s*)%CONNECTED_TRIPLES%/);
}
if (words.length > 0) {
sparqlLines = sparqlLines.replace(/%CURRENT_SUBJECT%/g, words[0]);
}
if (words.length > 1) {
// HACK (Hannah, 23.02.2021): Replace <pred1>/<pred2>* by
// <pred1>|<pred2> in object completion, but only when the subject is a
// variable.
if (words[0].startsWith("?")) {
words[1] = words[1].replace(/^([^ \/]+)\/([^ \/]+)\*$/, "$1|$2");
log("CURRENT_PREDICATE -> ", words[1], 'suggestions');
}
sparqlLines = sparqlLines.replace(/%CURRENT_PREDICATE%/g, words[1]);
}
sparqlLines = evaluateIfStatements(sparqlLines, word, lines, words)
return sparqlLines;
}
function substituteCustomPlaceholders(completionQuery) {
substitutionFinished = true;
for (const replacement in WARMUP_AC_PLACEHOLDERS) {
let sparqlLines = completionQuery.replace(new RegExp(`%${replacement}%`, "g"), WARMUP_AC_PLACEHOLDERS[replacement]);
if (sparqlLines !== completionQuery) {
substitutionFinished = false;
completionQuery = sparqlLines;
}
}
if (substitutionFinished) {
return completionQuery;
} else {
return substituteCustomPlaceholders(completionQuery)
}
}
function evaluateIfStatements(completionQuery, word, lines, words) {
// find all IF statements
let if_statements = [];
const ifRegex = /#\sIF\s+([!A-Z_\s]+)\s+#/;
let match = completionQuery.match(ifRegex);
let substrIdx = 0;
while (match != null) {
// find all IF declarations
const index = match.index;
const len = match[0].length;
substrIdx += index + len;
if_statements.push({ 'IF': { 'index': substrIdx - len, 'len': len }, 'condition': match[1] });
const substr = completionQuery.slice(substrIdx);
match = substr.match(ifRegex);
}
if_statements = if_statements.reverse();
for (let statement of if_statements) {
// find matching ELSE and ENDIFs
const start = statement['IF']['index'];
const endifMatch = completionQuery.slice(start).match(/#\sENDIF\s#/);
const elseMatch = completionQuery.slice(start).match(/#\sELSE\s#/);
if (elseMatch != null && elseMatch.index < endifMatch.index) {
const index = start + elseMatch.index;
const len = elseMatch[0].length;
statement['ELSE'] = { 'index': index, 'len': len };
}
if (endifMatch == null) {
console.error("Number of # IF # and # ENDIF # does not match!");
}
const index = start + endifMatch.index;
const len = endifMatch[0].length;
statement['ENDIF'] = { 'index': index, 'len': len }
let conditionSatisfied = parseAndEvaluateCondition(statement.condition, word, lines, words);
let result = completionQuery.slice(0, statement['IF']['index']);
if (conditionSatisfied && statement["ELSE"] == undefined) {
// Add content between IF and ENDIF
result += completionQuery.slice(statement['IF']['index'] + statement['IF']['len'], statement['ENDIF']['index']);
} else if (conditionSatisfied && statement["ELSE"] != undefined) {
// Add content between IF and ELSE
result += completionQuery.slice(statement['IF']['index'] + statement['IF']['len'], statement['ELSE']['index']);
} else if (!conditionSatisfied && statement["ELSE"] != undefined) {
// Add content between ELSE and ENDIF
result += completionQuery.slice(statement['ELSE']['index'] + statement['ELSE']['len'], statement['ENDIF']['index']);
}
result += completionQuery.slice(statement['ENDIF']['index'] + statement['ENDIF']['len']);
completionQuery = result;
}
return completionQuery
}
function parseAndEvaluateCondition(condition, word, lines, words) {
// split condition by AND and OR
const logicalOperator = condition.match(/(.*)\s+(OR)\s+(.*)/) || condition.match(/(.*)\s(AND)\s+(.*)/);
const negated = condition.startsWith("!");
let conditionSatisfied = false;
if (logicalOperator != null) {
const lhs = parseAndEvaluateCondition(logicalOperator[1], word, lines, words);
const rhs = parseAndEvaluateCondition(logicalOperator[3], word, lines, words);
if (logicalOperator[2] == "OR") {
conditionSatisfied = lhs || rhs;
} else {
conditionSatisfied = lhs && rhs;
}
} else if (negated) {
conditionSatisfied = !parseAndEvaluateCondition(condition.slice(1), word, lines, words);
} else {
if (condition == "CURRENT_WORD_EMPTY") {
conditionSatisfied = (word.length == 0);
} else if (condition == "CURRENT_SUBJECT_VARIABLE") {
conditionSatisfied = (words.length > 0 && words[0].startsWith("?"));
} else if (condition == "CURRENT_PREDICATE_VARIABLE") {
conditionSatisfied = (words.length > 1 && words[1].startsWith("?"));
} else if (condition == "CONNECTED_TRIPLES_EMPTY") {
conditionSatisfied = (lines.length == 0);
} else {
console.error(`Invalid condition in IF statement: '${condition}'`);
}
}
log(`Evaluating condition: "${condition}", word="${word}", lines=${lines.length}, words=${words}\n result: ${conditionSatisfied}`, 'other');
return conditionSatisfied;
}
const fetchTimeout = (sparqlQuery, timeoutSeconds, { ...options } = {}) => {
const ms = timeoutSeconds * 1000;
const controller = new AbortController();
const promise = fetch(BASEURL, {
// BASEURL + "?query=" + encodeURIComponent(sparqlQuery), {
method: "POST",
body: sparqlQuery,
signal: controller.signal,
headers: {
"Content-type": "application/sparql-query",
"Accept": "application/qlever-results+json"
},
...options
});
if (ms > 0) {
const timeout = setTimeout(() => controller.abort(), ms);
return promise.finally(() => clearTimeout(timeout));
} else {
return promise;
}
};
function getSuggestionsSparqlQuery(sparqlQuery) {
if (!sparqlQuery) return false;
// Do the limits for the scrolling feature.
sparqlQuery += "\nLIMIT " + size + "\nOFFSET " + lastSize;
// Rewrite queries also when obtaining suggestions (FILTER CONTAINS or
// ql:contains).
sparqlQuery = rewriteQueryNoAsyncPart(sparqlQuery);
// Show the loading indicator and badge.
activeLineBadgeLine = $('.CodeMirror-activeline-background');
activeLine = $('.CodeMirror-activeline-gutter .CodeMirror-gutter-elt');
if(activeLine.html().length < 10){
activeLineNumber = activeLine.html();
}
activeLine.html('<img src="/static/img/ajax-loader.gif">');
$('#aBadge').remove();
$('#suggestionErrorBlock').parent().hide()
log("Getting suggestions from QLever (PREFIXes omitted):\n"
+ sparqlQuery.replace(/^PREFIX.*/mg, ""), "requests");
return sparqlQuery;
// let url = BASEURL + "?query=" + encodeURIComponent(sparqlQuery);
// return url;
}
function getQleverSuggestions(sparqlQuery, prefixesRelation, appendix, nameList, predicateForObject, word, mixedModeQuery) {
/* mixedModeQuery is the case-insensitive query that is sent additionally to the case-sensitive query when mixed mode is enabled. */
// show the loading indicator and badge
activeLineBadgeLine = $('.CodeMirror-activeline-background');
activeLine = $('.CodeMirror-activeline-gutter .CodeMirror-gutter-elt');
activeLineNumber = activeLine.html();
activeLine.html('<img src="/static/img/ajax-loader.gif">');
$('#aBadge').remove();
$('#suggestionErrorBlock').parent().hide()
const lastSparqlQuery = getSuggestionsSparqlQuery(sparqlQuery);
const mixedModeSparqlQuery = getSuggestionsSparqlQuery(mixedModeQuery);
var dynamicSuggestions = [];
sparqlTimeout = window.setTimeout(async function () {
try {
let mixedModeQuery;
if (mixedModeSparqlQuery) {
mixedModeQuery = fetchTimeout(mixedModeSparqlQuery, DEFAULT_TIMEOUT); // start the mixed mode query, but async
}
const mainQueryTimeout = mixedModeSparqlQuery ? MIXED_MODE_TIMEOUT : DEFAULT_TIMEOUT;
let response;
let mainQueryHasTimedOut = false;
let showTimeoutError = false;
try {
response = await fetchTimeout(lastSparqlQuery, mainQueryTimeout); // start the main query and wait for it to return
} catch (error) {
if (error.name === "AbortError") {
mainQueryHasTimedOut = true;
showTimeoutError = true;
} else {
throw error;
}
}
if (mainQueryHasTimedOut && mixedModeSparqlQuery) {
// the main query timed out.
// get the mixedModeQuery's response and continue with that
log("The main query timed out. Using the context-insensitive suggestions.", 'requests')
try {
response = await mixedModeQuery;
showTimeoutError = false;
} catch (error) {
if (error.name !== "AbortError") {
throw error;
}
}
}
let data;
if (showTimeoutError) {
data = {exception: "The request was cancelled due to timeout"};
} else {
data = await response.json();
}
if ($('#logRequests').is(':checked')) {
runtime_log[runtime_log.length] = data.runtimeInformation;
query_log[query_log.length] = data.query;
if (runtime_log.length - 10 >= 0) {
runtime_log[runtime_log.length - 10] = null;
query_log[query_log.length - 10] = null;
}
}
if (data.res) {
log("Got suggestions from QLever.", 'other');
log("Query took " + data.time.total + " and found " + data.resultsize + " lines\nRuntime info is saved as [" + (query_log.length) + "]", 'requests');
var entityIndex = data.selected.indexOf(SUGGESTIONENTITYVARIABLE);
var suggested = {};
var ogc_contains_added = false;
for (var result of data.res) {
var entity = result[entityIndex];
// NOTE: What was the purpose of this? The AC queries group by entity,
// so there should be no duplicates. When a predicate occurs in both
// directions, this "continue" prevents the predicate showing twice
// (with and without ^).
// if (suggested[entity]) {
// continue
// }
suggested[entity] = true;
if (predicateForObject !== undefined) {
var resultType = LITERAL;
if (/^<.*>$/.test(entity)) {
resultType = ENTITY;
} else if (/@[\w-_]+$/.test(entity)) {
resultType = LANGUAGELITERAL;
}
}
// add back the prefixes
var replacePrefix = "";
var prefixName = "";
for (var prefix in prefixesRelation) {
if (entity.indexOf(prefixesRelation[prefix]) > 0 && prefixesRelation[prefix].length > replacePrefix.length) {
replacePrefix = prefixesRelation[prefix];
prefixName = prefix;
}
}
if (FILLPREFIXES) {
for (var prefix in COLLECTEDPREFIXES) {
if (entity.indexOf(COLLECTEDPREFIXES[prefix]) > 0 && COLLECTEDPREFIXES[prefix].length > replacePrefix.length) {
replacePrefix = COLLECTEDPREFIXES[prefix];
prefixName = prefix;
}
}
}
if (replacePrefix.length > 0) {
entity = entity.replace("<" + replacePrefix, prefixName + ':').slice(0, -1);
}
if (predicateForObject !== undefined) {
if (predicateResultTypes[predicateForObject] == undefined) {
predicateResultTypes[predicateForObject] = resultType;
} else {
predicateResultTypes[predicateForObject] = Math.max(predicateResultTypes[predicateForObject], resultType);
}
}
// console.log("URL: " + window.location);
var nameIndex = data.selected.indexOf(SUGGESTIONNAMEVARIABLE);
var altNameIndex = data.selected.indexOf(SUGGESTIONALTNAMEVARIABLE);
var entityName = (nameIndex != -1) ? result[nameIndex] : "";
var altEntityName = (altNameIndex != -1) ? result[altNameIndex] : "";
nameList[entity] = entityName;
// add ^ if the reversed column exists
// and is 1 (indicating that this is a predicate suggestion, but for
// the reversed predicate.
var reversedIndex = data.selected.indexOf(SUGGESTIONREVERSEDVARIABLE);
var reversed = (reversedIndex != -1 && (result[reversedIndex] == 1
|| result[reversedIndex].startsWith("\"1\"")))
var displayText = (reversed ? "^" : "") + entity + appendix;
var completion = (reversed ? "^" : "") + entity + appendix;
dynamicSuggestions.push({
displayText: displayText,
completion: completion,
name: entityName + (reversed ? " (reversed)" : ""),
altname: altEntityName,
isMixedModeSuggestion: mainQueryHasTimedOut,
});
// HACK Hannah 23.02.2021: Add transitive suggestions (for
// hand-picked predicates only -> TODO: generalize this).
// console.log("DISPLAY TEXT: \"" + displayText + "\"");
if (displayText == "wdt:P31 ") {
dynamicSuggestions.push({
displayText: displayText.trim() + "/wdt:P279* ",
completion: completion.trim() + "/wdt:P279* ",
name: entityName + " (transitive)",
altname: altEntityName,
isMixedModeSuggestion: mainQueryHasTimedOut
});
}
else if (displayText == "wdt:P131 ") {
dynamicSuggestions.push({
displayText: "wdt:P131+ ",
completion: "wdt:P131+ ",
name: entityName + " (transitive)",
altname: altEntityName,
isMixedModeSuggestion: mainQueryHasTimedOut
});
}
else if (!ogc_contains_added && displayText.startsWith("osm2rdf:contains_")) {
dynamicSuggestions.splice(dynamicSuggestions.length - 1, 0, {
displayText: "ogc:contains ",
completion: "ogc:contains ",
// displayText: "ogc:contains_area*/ogc:contains_nonarea ",
// completion: "ogc:contains_area*/ogc:contains_nonarea ",
name: "",
altname: altEntityName,
isMixedModeSuggestion: mainQueryHasTimedOut
});
ogc_contains_added = true;
}
else if (displayText == "rdf:type " && window.location.href.match(/yago-2/)) {
dynamicSuggestions.push({
displayText: displayText.trim() + "/rdfs:subClassOf* ",
completion: completion.trim() + "/rdfs:subClassOf* ",
name: entityName + " (transitive)",
altname: altEntityName,
isMixedModeSuggestion: mainQueryHasTimedOut
});
}
}
activeLine.html(activeLineNumber);
} else {
activeLine.html('<i class="glyphicon glyphicon-remove" style="color:red; cursor: pointer;" onclick="$(\'#suggestionErrorBlock\').parent().show()"></i>');
$('#suggestionErrorBlock').html('<strong>Error while collecting suggestions:</strong><br><pre>' + data.exception + '</pre>')
console.error(data.exception);
}
// reset loading indicator
$('#aBadge').remove();
// add badge
if (data.resultsize != undefined && data.resultsize != null) {
resultSize = data.resultsize;
activeLineBadgeLine.prepend('<span class="badge badge-success pull-right" id="aBadge">' + data.resultsize + '</span>');
}
sparqlCallback({
list: suggestions.concat(dynamicSuggestions),
from: sparqlFrom,
to: sparqlTo,
word: word
});
return []
} catch (err) {
// things went terribly wrong...
console.error('Failed to load suggestions from QLever', err);
activeLine.html('<i class="glyphicon glyphicon-remove" style="color:red;">');
activeLine.html(activeLineNumber);
return [];
}
}, 500);
}
/**
Returns the suggestions defined for a given complex type
**/
function getTypeSuggestions(type, context) {
typeSuggestions = []
for (var i = 0; i < type.suggestions.length; i++) {
var suggestion = type.suggestions[i];
var dynString = "";
var placeholders = [];
// evaluate placeholders in definition
for (var j = 0; j < suggestion.length; j++) {
// concat dyn string
if (typeof suggestion[j] == 'object') {
if (suggestion[j] && suggestion[j].length > 0) {
dynString += '{[' + placeholders.length + ']}';
placeholders.push(suggestion[j]);
}
} else if (typeof suggestion[j] == 'function') {
if (suggestion[j] && suggestion[j].length > 0) {
dynString += '{[' + placeholders.length + ']}';
placeholders.push(suggestion[j](context));
}
} else {
dynString += suggestion[j]
}
}
// no multiplying placeholders - simply use the string with no value
if (placeholders.length == 0) {
typeSuggestions.push(dynString);