terminusdb 12.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Contributing.md +36 -0
- package/LICENSE +201 -0
- package/README.md +175 -0
- package/RELEASE_NOTES.md +462 -0
- package/dist/index.html +22 -0
- package/dist/terminusdb-client.min.js +3 -0
- package/dist/terminusdb-client.min.js.LICENSE.txt +188 -0
- package/dist/terminusdb-client.min.js.map +1 -0
- package/dist/typescript/index.d.ts +14 -0
- package/dist/typescript/lib/accessControl.d.ts +554 -0
- package/dist/typescript/lib/axiosInstance.d.ts +2 -0
- package/dist/typescript/lib/connectionConfig.d.ts +381 -0
- package/dist/typescript/lib/const.d.ts +54 -0
- package/dist/typescript/lib/dispatchRequest.d.ts +17 -0
- package/dist/typescript/lib/errorMessage.d.ts +25 -0
- package/dist/typescript/lib/query/woqlBuilder.d.ts +75 -0
- package/dist/typescript/lib/query/woqlCore.d.ts +341 -0
- package/dist/typescript/lib/query/woqlDoc.d.ts +63 -0
- package/dist/typescript/lib/query/woqlLibrary.d.ts +718 -0
- package/dist/typescript/lib/query/woqlPrinter.d.ts +71 -0
- package/dist/typescript/lib/query/woqlQuery.d.ts +833 -0
- package/dist/typescript/lib/typedef.d.ts +624 -0
- package/dist/typescript/lib/utils.d.ts +199 -0
- package/dist/typescript/lib/valueHash.d.ts +146 -0
- package/dist/typescript/lib/viewer/chartConfig.d.ts +62 -0
- package/dist/typescript/lib/viewer/chooserConfig.d.ts +38 -0
- package/dist/typescript/lib/viewer/documentFrame.d.ts +44 -0
- package/dist/typescript/lib/viewer/frameConfig.d.ts +74 -0
- package/dist/typescript/lib/viewer/frameRule.d.ts +145 -0
- package/dist/typescript/lib/viewer/graphConfig.d.ts +73 -0
- package/dist/typescript/lib/viewer/objectFrame.d.ts +212 -0
- package/dist/typescript/lib/viewer/streamConfig.d.ts +23 -0
- package/dist/typescript/lib/viewer/tableConfig.d.ts +66 -0
- package/dist/typescript/lib/viewer/terminusRule.d.ts +75 -0
- package/dist/typescript/lib/viewer/viewConfig.d.ts +47 -0
- package/dist/typescript/lib/viewer/woqlChart.d.ts +1 -0
- package/dist/typescript/lib/viewer/woqlChooser.d.ts +56 -0
- package/dist/typescript/lib/viewer/woqlGraph.d.ts +26 -0
- package/dist/typescript/lib/viewer/woqlPaging.d.ts +1 -0
- package/dist/typescript/lib/viewer/woqlResult.d.ts +128 -0
- package/dist/typescript/lib/viewer/woqlRule.d.ts +96 -0
- package/dist/typescript/lib/viewer/woqlStream.d.ts +31 -0
- package/dist/typescript/lib/viewer/woqlTable.d.ts +102 -0
- package/dist/typescript/lib/viewer/woqlView.d.ts +49 -0
- package/dist/typescript/lib/woql.d.ts +1267 -0
- package/dist/typescript/lib/woqlClient.d.ts +1216 -0
- package/index.js +28 -0
- package/lib/.eslintrc +1 -0
- package/lib/accessControl.js +988 -0
- package/lib/axiosInstance.js +5 -0
- package/lib/connectionConfig.js +765 -0
- package/lib/const.js +59 -0
- package/lib/dispatchRequest.js +236 -0
- package/lib/errorMessage.js +110 -0
- package/lib/query/woqlBuilder.js +234 -0
- package/lib/query/woqlCore.js +934 -0
- package/lib/query/woqlDoc.js +177 -0
- package/lib/query/woqlLibrary.js +1015 -0
- package/lib/query/woqlPrinter.js +476 -0
- package/lib/query/woqlQuery.js +1865 -0
- package/lib/typedef.js +248 -0
- package/lib/utils.js +817 -0
- package/lib/valueHash.js_old +581 -0
- package/lib/viewer/chartConfig.js +411 -0
- package/lib/viewer/chooserConfig.js +234 -0
- package/lib/viewer/documentFrame.js +206 -0
- package/lib/viewer/frameConfig.js +469 -0
- package/lib/viewer/frameRule.js +519 -0
- package/lib/viewer/graphConfig.js +345 -0
- package/lib/viewer/objectFrame.js +1550 -0
- package/lib/viewer/streamConfig.js +82 -0
- package/lib/viewer/tableConfig.js +310 -0
- package/lib/viewer/terminusRule.js +196 -0
- package/lib/viewer/viewConfig.js +219 -0
- package/lib/viewer/woqlChart.js +17 -0
- package/lib/viewer/woqlChooser.js +171 -0
- package/lib/viewer/woqlGraph.js +295 -0
- package/lib/viewer/woqlPaging.js +148 -0
- package/lib/viewer/woqlResult.js +258 -0
- package/lib/viewer/woqlRule.js +312 -0
- package/lib/viewer/woqlStream.js +27 -0
- package/lib/viewer/woqlTable.js +332 -0
- package/lib/viewer/woqlView.js +107 -0
- package/lib/woql.js +1693 -0
- package/lib/woqlClient.js +2091 -0
- package/package.json +110 -0
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
/* eslint-disable no-fallthrough */
|
|
2
|
+
/* eslint-disable no-const-assign */
|
|
3
|
+
/* eslint-disable block-scoped-var */
|
|
4
|
+
/* eslint-disable no-case-declarations */
|
|
5
|
+
/* eslint-disable no-var */
|
|
6
|
+
/* eslint-disable no-redeclare */
|
|
7
|
+
/* eslint-disable vars-on-top */
|
|
8
|
+
/* eslint-disable no-restricted-syntax */
|
|
9
|
+
/* eslint-disable no-unused-expressions */
|
|
10
|
+
/* eslint-disable no-unused-vars */
|
|
11
|
+
/* eslint-disable no-plusplus */
|
|
12
|
+
/* eslint-disable camelcase */
|
|
13
|
+
/* eslint-disable no-use-before-define */
|
|
14
|
+
/* eslint-disable prefer-destructuring */
|
|
15
|
+
/* eslint-disable no-console */
|
|
16
|
+
/* eslint-disable no-param-reassign */
|
|
17
|
+
/**
|
|
18
|
+
* Class responsible for converting a JSON WOQL into a WOQL.js / WOQL.py string
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
function WOQLPrinter(vocab, language) {
|
|
22
|
+
this.vocab = vocab;
|
|
23
|
+
this.language = language;
|
|
24
|
+
this.indent_spaces = 4;
|
|
25
|
+
this.boxed_predicates = [
|
|
26
|
+
'variable',
|
|
27
|
+
'array_element',
|
|
28
|
+
'node',
|
|
29
|
+
// 'woql:arithmetic_value',
|
|
30
|
+
// 'woql:variable_name',
|
|
31
|
+
];
|
|
32
|
+
this.subject_cleaned_predicates = ['subject', 'element'];
|
|
33
|
+
this.schema_cleaned_predicates = [
|
|
34
|
+
'predicate',
|
|
35
|
+
'uri',
|
|
36
|
+
'of_type',
|
|
37
|
+
];
|
|
38
|
+
this.list_operators = ['ValueList', 'Array', 'NamedAsVar', 'IndexedAsVar', 'AsVar'];
|
|
39
|
+
this.query_list_operators = ['And', 'Or'];
|
|
40
|
+
this.operator_maps = {
|
|
41
|
+
IDGenerator: 'idgen',
|
|
42
|
+
IsA: 'isa',
|
|
43
|
+
PostResource: 'post',
|
|
44
|
+
QueryResource: 'remote',
|
|
45
|
+
AsVars: 'as',
|
|
46
|
+
NamedAsVars: 'as',
|
|
47
|
+
IndexedAsVars: 'as',
|
|
48
|
+
DeletedTriple: 'removed_triple',
|
|
49
|
+
};
|
|
50
|
+
this.shortcuts = {
|
|
51
|
+
optional: 'opt',
|
|
52
|
+
substring: 'substr',
|
|
53
|
+
regexp: 're',
|
|
54
|
+
subsumption: 'sub',
|
|
55
|
+
equals: 'eq',
|
|
56
|
+
concatenate: 'concat',
|
|
57
|
+
};
|
|
58
|
+
this.pythonic = {
|
|
59
|
+
and: 'woql_and',
|
|
60
|
+
or: 'woql_or',
|
|
61
|
+
as: 'woql_as',
|
|
62
|
+
with: 'woql_with',
|
|
63
|
+
from: 'woql_from',
|
|
64
|
+
not: 'woql_not',
|
|
65
|
+
};
|
|
66
|
+
this.show_context = false;
|
|
67
|
+
}
|
|
68
|
+
// WOQL.triple(WOQL.node_value("Subject"), WOQL.node_value("Predicate"), WOQL.value("Object"))
|
|
69
|
+
WOQLPrinter.prototype.printJSON = function (json, level, fluent, newline) {
|
|
70
|
+
level = level || 0;
|
|
71
|
+
fluent = fluent || false;
|
|
72
|
+
let str = '';
|
|
73
|
+
if (!json['@type']) {
|
|
74
|
+
console.log('Bad structure passed to print json, no type: ', json);
|
|
75
|
+
return '';
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// check for language
|
|
79
|
+
if (['Value', 'NodeValue', 'DataValue', 'ArithmeticValue', 'OrderTemplate'].indexOf(json['@type']) > -1) {
|
|
80
|
+
return this.pvar(json);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
let operator = json['@type'];
|
|
84
|
+
if (typeof json['@type'] === 'string' && operator.indexOf(':') > -1) {
|
|
85
|
+
operator = json['@type'].split(':')[1];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (operator === 'QueryResource') {
|
|
89
|
+
return this.getQueryResourceStr(json, level, fluent, newline);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (operator) {
|
|
93
|
+
const ujson = this.unboxJSON(operator, json);
|
|
94
|
+
if (ujson) {
|
|
95
|
+
const meat = this.printArgument(
|
|
96
|
+
operator,
|
|
97
|
+
this.getBoxedPredicate(operator, json),
|
|
98
|
+
ujson,
|
|
99
|
+
level,
|
|
100
|
+
fluent,
|
|
101
|
+
);
|
|
102
|
+
if (this.isListOperator(operator)) return `[${meat}]`;
|
|
103
|
+
return meat;
|
|
104
|
+
}
|
|
105
|
+
if (this.isListOperator(operator)) {
|
|
106
|
+
str += '[';
|
|
107
|
+
} else {
|
|
108
|
+
// get the function name from the @type field
|
|
109
|
+
const call = this.getFunctionForOperator(operator, json);
|
|
110
|
+
const indent = newline ? level * this.indent_spaces : 0;
|
|
111
|
+
str += `${this.getWOQLPrelude(call, fluent, indent)}(`;
|
|
112
|
+
}
|
|
113
|
+
// below needs to be changed to have a specific ordering
|
|
114
|
+
// get the list of the keys without the @type
|
|
115
|
+
const args = this.getArgumentOrder(operator, json);
|
|
116
|
+
// I have to review this
|
|
117
|
+
const divlimit = args.indexOf('query') === -1 ? args.length - 1 : args.length - 2;
|
|
118
|
+
// query argument when I have a subquery
|
|
119
|
+
args.forEach((item, i) => { // && operator !== 'When'
|
|
120
|
+
let nfluent = !!((item === 'query' && operator !== 'Put') || item === 'consequent'
|
|
121
|
+
|| item === 'resource');
|
|
122
|
+
// to be review
|
|
123
|
+
if (item === 'resource' && typeof json[item] === 'string') nfluent = false;
|
|
124
|
+
str += this.printArgument(operator, item, json[item], level, nfluent);
|
|
125
|
+
// something to review ??
|
|
126
|
+
if (i < divlimit && operator !== 'Get') str += ', ';
|
|
127
|
+
});
|
|
128
|
+
if (this.isListOperator(operator)) str += ']';
|
|
129
|
+
else {
|
|
130
|
+
if (this.argumentTakesNewline(operator)) str += `\n${nspaces(level * this.indent_spaces)}`;
|
|
131
|
+
if (!fluent) str += ')';
|
|
132
|
+
}
|
|
133
|
+
} else {
|
|
134
|
+
console.log('wrong structure passed to print json ', json);
|
|
135
|
+
}
|
|
136
|
+
return str;
|
|
137
|
+
};
|
|
138
|
+
// file or remote method
|
|
139
|
+
WOQLPrinter.prototype.getQueryResourceStr = function (json, level, fluent, newline) {
|
|
140
|
+
if (!json.source) {
|
|
141
|
+
console.log('wrong structure passed to print json ', json);
|
|
142
|
+
return '';
|
|
143
|
+
}
|
|
144
|
+
const functName = json.source.url ? 'remote' : 'file';
|
|
145
|
+
const indent = newline ? level * this.indent_spaces : 0;
|
|
146
|
+
let str = `${this.getWOQLPrelude(functName, fluent, indent)}(`;
|
|
147
|
+
const source = json.source.file ? `"${json.source.file}"` : `"${json.source.url}"`;
|
|
148
|
+
const format = json.format === 'csv' ? '' : json.format;
|
|
149
|
+
str += source;
|
|
150
|
+
if (format)str += `, ${format}`;
|
|
151
|
+
return str;
|
|
152
|
+
};
|
|
153
|
+
// remove the @type from the keys
|
|
154
|
+
WOQLPrinter.prototype.getArgumentOrder = function (operator, json) {
|
|
155
|
+
const args = Object.keys(json);
|
|
156
|
+
args.splice(args.indexOf('@type'), 1);
|
|
157
|
+
return args;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
WOQLPrinter.prototype.argumentTakesNewline = function (operator) {
|
|
161
|
+
return this.isQueryListOperator(operator);
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
WOQLPrinter.prototype.argumentRequiresArray = function (predicate, entries) {
|
|
165
|
+
if ((predicate === 'group_by' || predicate === 'list') && entries.length > 1) return true;
|
|
166
|
+
return false;
|
|
167
|
+
};
|
|
168
|
+
// this is the list of the argument not @type
|
|
169
|
+
WOQLPrinter.prototype.printArgument = function (operator, predicate, arg, level, fluent) {
|
|
170
|
+
let str = '';
|
|
171
|
+
if (fluent) str += ')';
|
|
172
|
+
const newline = this.argumentTakesNewline(operator);
|
|
173
|
+
if (newline) str += `\n${nspaces((level + 1) * this.indent_spaces)}`;
|
|
174
|
+
if (arg['@type'] === 'True') return 'true';
|
|
175
|
+
|
|
176
|
+
if (predicate === 'document') return this.decompileDocument(arg);
|
|
177
|
+
if (predicate === 'variables') return this.decompileVariables(arg);
|
|
178
|
+
if (predicate === 'group_by' || predicate === 'template') return this.decompileVariables(arg, true);
|
|
179
|
+
|
|
180
|
+
// list of .as in get or put function
|
|
181
|
+
if (predicate === 'columns') return this.decompileAsVars(arg, level + 1);
|
|
182
|
+
|
|
183
|
+
if (predicate === 'pattern') return this.decompileRegexPattern(arg, level + 1);
|
|
184
|
+
if (Array.isArray(arg)) {
|
|
185
|
+
const arr_entries = [];
|
|
186
|
+
for (let j = 0; j < arg.length; j++) {
|
|
187
|
+
const nlevel = newline ? level + 1 : level;
|
|
188
|
+
arr_entries.push(this.printJSON(arg[j], nlevel, fluent, newline));
|
|
189
|
+
}
|
|
190
|
+
const jstr = newline ? `,\n${nspaces(++level * this.indent_spaces)}` : ',';
|
|
191
|
+
if (this.argumentRequiresArray(predicate, arr_entries)) {
|
|
192
|
+
str += `[${arr_entries.join(jstr)}]`;
|
|
193
|
+
} else str += arr_entries.join(jstr);
|
|
194
|
+
} else if (typeof arg === 'object') {
|
|
195
|
+
const reet = this.printJSON(arg, level, fluent);
|
|
196
|
+
// if(newline) str += "\n" + nspaces(level*this.indent_spaces)
|
|
197
|
+
|
|
198
|
+
str += reet;
|
|
199
|
+
} else if (typeof arg === 'string') {
|
|
200
|
+
str += this.uncleanArgument(arg, operator, predicate);
|
|
201
|
+
} else if (typeof arg === 'number') return arg;
|
|
202
|
+
return str;
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
// to be review if array
|
|
206
|
+
WOQLPrinter.prototype.decompileDocument = function (args) {
|
|
207
|
+
const jsonDoc = {};
|
|
208
|
+
this.decompileDictionary(jsonDoc, args);
|
|
209
|
+
return `WOQL.doc(${JSON.stringify(jsonDoc)})`;
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
WOQLPrinter.prototype.decompileDictionary = function (jsonDoc, args) {
|
|
213
|
+
if (args.dictionary && args.dictionary.data && Array.isArray(args.dictionary.data)) {
|
|
214
|
+
args.dictionary.data.forEach((item) => {
|
|
215
|
+
this.decompileFieldValuePair(jsonDoc, item);
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
WOQLPrinter.prototype.decompileFieldValuePair = function (jsonDoc, fieldValue) {
|
|
221
|
+
const type = fieldValue.field || '';
|
|
222
|
+
// eslint-disable-next-line max-len
|
|
223
|
+
let value = false;
|
|
224
|
+
if (fieldValue.value && fieldValue.value) {
|
|
225
|
+
if (fieldValue.value.data) {
|
|
226
|
+
value = fieldValue.value.data['@value'];
|
|
227
|
+
} else if (fieldValue.value.dictionary && Array.isArray(fieldValue.value.dictionary.data)) {
|
|
228
|
+
const valueDoc = {};
|
|
229
|
+
this.decompileDictionary(valueDoc, fieldValue.value);
|
|
230
|
+
value = valueDoc;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
jsonDoc[type] = value;
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
WOQLPrinter.prototype.decompileVariables = function (args, checkIsArray = false) {
|
|
237
|
+
if (Array.isArray(args)) {
|
|
238
|
+
let str = '';
|
|
239
|
+
args.forEach((varName, index) => {
|
|
240
|
+
str += `"v:${varName}"`;
|
|
241
|
+
if (index < args.length - 1)str += ', ';
|
|
242
|
+
});
|
|
243
|
+
if (checkIsArray && args.length > 1)str = `[${str}]`;
|
|
244
|
+
return str;
|
|
245
|
+
}
|
|
246
|
+
return '';
|
|
247
|
+
};
|
|
248
|
+
// woql:datatype in path
|
|
249
|
+
WOQLPrinter.prototype.decompileRegexPattern = function (json) {
|
|
250
|
+
if (typeof json === 'object' && json['@type'] === 'DataValue') {
|
|
251
|
+
return this.pvar(json);
|
|
252
|
+
} if ((json['@type']).startsWith('Path')) {
|
|
253
|
+
return `"${this.decompilePathPattern(json)}"`;
|
|
254
|
+
}
|
|
255
|
+
const str = json;
|
|
256
|
+
return `"${str.replace('\\', '\\\\')}"`;
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
WOQLPrinter.prototype.pvar = function (json) {
|
|
260
|
+
// if (json['woql:variable_name'] && typeof json['woql:variable_name']['@value'] != 'undefined') {
|
|
261
|
+
if (json.variable) {
|
|
262
|
+
let varname = json.variable;
|
|
263
|
+
const order = json.order ? json.order : '';
|
|
264
|
+
if (varname.indexOf(':') === -1) {
|
|
265
|
+
varname = `v:${varname}`;
|
|
266
|
+
}
|
|
267
|
+
return (order !== '' && order !== 'asc') ? `["${varname}","${order}"]` : `"${varname}"`;
|
|
268
|
+
} if (json.node) {
|
|
269
|
+
return `"${json.node}"`;
|
|
270
|
+
} if (json.data) {
|
|
271
|
+
return JSON.stringify(json.data);
|
|
272
|
+
} if (json.list) {
|
|
273
|
+
const listArr = json.list;
|
|
274
|
+
if (Array.isArray(listArr)) {
|
|
275
|
+
const listTmp = [];
|
|
276
|
+
listArr.forEach((listItem, index) => {
|
|
277
|
+
listTmp.push(this.pvar(listItem));
|
|
278
|
+
});
|
|
279
|
+
return `[${listTmp.join(', ')}]`;
|
|
280
|
+
}
|
|
281
|
+
return this.pvar(json.list);
|
|
282
|
+
}
|
|
283
|
+
// we have list to
|
|
284
|
+
return false;
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Gets the starting characters for a WOQL query - varies depending on how the query
|
|
289
|
+
* is invoked and how indented it is
|
|
290
|
+
*/
|
|
291
|
+
WOQLPrinter.prototype.getWOQLPrelude = function (operator, fluent, inline) {
|
|
292
|
+
if (operator === 'true' || operator === 'false') {
|
|
293
|
+
if (this.language === 'python') {
|
|
294
|
+
return operator.charAt(0).toUpperCase() + operator.slice(1);
|
|
295
|
+
}
|
|
296
|
+
return operator;
|
|
297
|
+
}
|
|
298
|
+
let prelude = 'WOQL.';
|
|
299
|
+
if (this.language === 'python') {
|
|
300
|
+
this.pythonic[operator] && (operator = this.pythonic[operator]);
|
|
301
|
+
prelude = 'WOQLQuery().';
|
|
302
|
+
}
|
|
303
|
+
if (fluent) {
|
|
304
|
+
return `.${operator}`;
|
|
305
|
+
}
|
|
306
|
+
return (inline ? `\n${nspaces(inline)}` : '') + prelude + operator;
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
WOQLPrinter.prototype.uncleanArgument = function (arg, operator, predicate) {
|
|
310
|
+
if (arg.indexOf(':') !== -1) {
|
|
311
|
+
// is it a short cut?
|
|
312
|
+
for (const s in this.vocab) {
|
|
313
|
+
if (this.vocab[s] === arg) return `"${s}"`;
|
|
314
|
+
}
|
|
315
|
+
// is there a default reverse mapping
|
|
316
|
+
/* if (this.subject_cleaned_predicates.indexOf(predicate) != -1) {
|
|
317
|
+
if (arg.substring(0, 4) === 'doc:') arg = arg.substring(4)
|
|
318
|
+
}
|
|
319
|
+
if (this.schema_cleaned_predicates.indexOf(predicate) != -1) {
|
|
320
|
+
if (arg.substring(0, 4) === 'scm:') arg = arg.substring(4)
|
|
321
|
+
} */
|
|
322
|
+
}
|
|
323
|
+
return `"${arg}"`;
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
WOQLPrinter.prototype.isListOperator = function (operator) {
|
|
327
|
+
return this.list_operators.indexOf(operator) !== -1;
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
WOQLPrinter.prototype.isQueryListOperator = function (operator) {
|
|
331
|
+
return this.query_list_operators.indexOf(operator) !== -1;
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
/*
|
|
335
|
+
* transform the operator in the function name
|
|
336
|
+
* like "@type": "Triple" => WOQL.triple
|
|
337
|
+
*/
|
|
338
|
+
WOQLPrinter.prototype.getFunctionForOperator = function (operator, json) {
|
|
339
|
+
if (this.operator_maps[operator]) return this.operator_maps[operator];
|
|
340
|
+
|
|
341
|
+
// triple with the graph parameter is the old quad
|
|
342
|
+
if (operator === 'Triple' && json.graph) return 'quad';
|
|
343
|
+
const f = camelToSnake(operator);
|
|
344
|
+
if (this.shortcuts[f]) return this.shortcuts[f];
|
|
345
|
+
return f;
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
WOQLPrinter.prototype.getBoxedPredicate = function (operator, json) {
|
|
349
|
+
for (let i = 0; i < this.boxed_predicates.length; i++) {
|
|
350
|
+
if (json[this.boxed_predicates[i]]) {
|
|
351
|
+
return this.boxed_predicates[i];
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
if (operator === 'QueryListElement') {
|
|
355
|
+
return 'woql:query';
|
|
356
|
+
}
|
|
357
|
+
return false;
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
WOQLPrinter.prototype.unboxJSON = function (operator, json) {
|
|
361
|
+
const bp = this.getBoxedPredicate(operator, json);
|
|
362
|
+
if (bp) {
|
|
363
|
+
return json[bp];
|
|
364
|
+
}
|
|
365
|
+
return false;
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
// WOQL.as
|
|
369
|
+
WOQLPrinter.prototype.decompileAsVars = function (asvs, level) {
|
|
370
|
+
let str = '';
|
|
371
|
+
if (!Array.isArray(asvs)) return '';
|
|
372
|
+
asvs.forEach((wasv, i) => {
|
|
373
|
+
str += `\n${nspaces(level * this.indent_spaces)}${i === 0 ? 'WOQL' : ''}`;
|
|
374
|
+
// old 'woql:NamedAsVar'
|
|
375
|
+
if (wasv['@type'] === 'Column' && wasv.indicator) {
|
|
376
|
+
const source = wasv.indicator.name || wasv.indicator.index;
|
|
377
|
+
const target = `v:${wasv.variable}`;
|
|
378
|
+
const { type } = wasv.indicator;
|
|
379
|
+
str += `.as("${source}", "${target}"`;
|
|
380
|
+
if (type) str += `, "${type}")`;
|
|
381
|
+
else str += ')';
|
|
382
|
+
}
|
|
383
|
+
});
|
|
384
|
+
return str;
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
WOQLPrinter.prototype.decompilePathPattern = function (pstruct) {
|
|
388
|
+
const t = pstruct['@type'];
|
|
389
|
+
// eslint-disable-next-line default-case
|
|
390
|
+
switch (t) {
|
|
391
|
+
case 'InversePathPredicate':
|
|
392
|
+
return pstruct.predicate ? `<${pstruct.predicate}` : '<.';
|
|
393
|
+
case 'PathPredicate':
|
|
394
|
+
return pstruct.predicate ? `${pstruct.predicate}` : '.';
|
|
395
|
+
case 'PathPlus':
|
|
396
|
+
var next = pstruct.plus;
|
|
397
|
+
if (Array.isArray(next)) next = next[0];
|
|
398
|
+
if (needsParentheses(next)) return `(${this.decompilePathPattern(next)})+`;
|
|
399
|
+
return `${this.decompilePathPattern(next)}+`;
|
|
400
|
+
case 'PathStar':
|
|
401
|
+
var next = pstruct.star;
|
|
402
|
+
if (Array.isArray(next)) next = next[0];
|
|
403
|
+
if (needsParentheses(next)) return `(${this.decompilePathPattern(next)})*`;
|
|
404
|
+
return `${this.decompilePathPattern(next)}*`;
|
|
405
|
+
case 'PathTimes':
|
|
406
|
+
var next = pstruct.times;
|
|
407
|
+
var astr = ` {${
|
|
408
|
+
pstruct.from
|
|
409
|
+
},${
|
|
410
|
+
pstruct.to
|
|
411
|
+
}}`;
|
|
412
|
+
if (Array.isArray(next)) next = next[0];
|
|
413
|
+
if (needsParentheses(next)) return `(${this.decompilePathPattern(next)})${astr}`;
|
|
414
|
+
return this.decompilePathPattern(next) + astr;
|
|
415
|
+
case 'PathSequence':
|
|
416
|
+
const sequenceArr = pstruct.sequence;
|
|
417
|
+
if (Array.isArray(sequenceArr) && sequenceArr.length === 2) {
|
|
418
|
+
let next1 = sequenceArr[0];// pstruct['woql:path_first']
|
|
419
|
+
const next2 = sequenceArr[1];// pstruct['woql:path_second']
|
|
420
|
+
|
|
421
|
+
if (Array.isArray(next1)) next1 = next1[0];
|
|
422
|
+
var seqstr = '';
|
|
423
|
+
if (needsParentheses(next1)) seqstr += '(';
|
|
424
|
+
seqstr += this.decompilePathPattern(next1);
|
|
425
|
+
if (needsParentheses(next1)) seqstr += ')';
|
|
426
|
+
seqstr += ',';
|
|
427
|
+
if (needsParentheses(next2)) seqstr += '(';
|
|
428
|
+
seqstr += this.decompilePathPattern(next2);
|
|
429
|
+
if (next1['@type'] === 'InversePathPredicate') {
|
|
430
|
+
seqstr += '>';
|
|
431
|
+
}
|
|
432
|
+
if (needsParentheses(next2)) seqstr += ')';
|
|
433
|
+
return seqstr;
|
|
434
|
+
}
|
|
435
|
+
// there is a problem in remap
|
|
436
|
+
case 'PathOr':
|
|
437
|
+
const orArr = pstruct.or;
|
|
438
|
+
if (Array.isArray(orArr) && orArr.length === 2) {
|
|
439
|
+
let next1 = orArr[0]; // pstruct['woql:path_left']
|
|
440
|
+
const next2 = orArr[1]; // pstruct['woql:path_right']
|
|
441
|
+
if (Array.isArray(next1)) next1 = next1[0];
|
|
442
|
+
var seqstr = '';
|
|
443
|
+
if (needsParentheses(next1)) seqstr += '(';
|
|
444
|
+
seqstr += this.decompilePathPattern(next1);
|
|
445
|
+
if (needsParentheses(next1)) seqstr += ')';
|
|
446
|
+
seqstr += '|';
|
|
447
|
+
if (needsParentheses(next2)) seqstr += '(';
|
|
448
|
+
seqstr += this.decompilePathPattern(next2);
|
|
449
|
+
if (needsParentheses(next2)) seqstr += ')';
|
|
450
|
+
return seqstr;
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
return 'error';
|
|
454
|
+
};
|
|
455
|
+
|
|
456
|
+
function needsParentheses(obj) {
|
|
457
|
+
const noparens = ['PathPredicate', 'PathPlus', 'PathTimes', 'InversePathPredicate'];
|
|
458
|
+
if (noparens.indexOf(obj['@type']) !== -1) return false;
|
|
459
|
+
return true;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
function camelToSnake(string) {
|
|
463
|
+
return string
|
|
464
|
+
.replace(/[\w]([A-Z])/g, (m) => `${m[0]}_${m[1]}`)
|
|
465
|
+
.toLowerCase();
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
function nspaces(n) {
|
|
469
|
+
let spaces = '';
|
|
470
|
+
for (let i = 0; i < n; i++) {
|
|
471
|
+
spaces += ' ';
|
|
472
|
+
}
|
|
473
|
+
return spaces;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
module.exports = WOQLPrinter;
|