-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuseki.js
378 lines (345 loc) · 12.9 KB
/
fuseki.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
import { parseResults } from './helpers';
const FUSEKI_URL = "https://sparql.nextnet.top/ontotoolv2/query"
const PREFIXES = `
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX onto: <http://www.semanticweb.org/nicolas/ontologies/2021/8/patterns#>
`
const getOptions = (query) => {
return {
method: 'POST',
body: `query=${query}`,
headers: {
"Content-Type": "application/x-www-form-urlencoded"
}
}
};
export const healthCheck = async () => {
// test dummy query to check if Fuseki is live
const query = `
SELECT ?l ?p
WHERE {
onto:Pattern rdfs:label ?l
}
`
try {
let response = await fetch( FUSEKI_URL, getOptions(PREFIXES + query) );
if (response.status === 200) return true;
return false;
} catch (e) {
console.error('Failed to fetch: ' + e);
return false;
}
}
const createClassesTree = (classes) => {
let orderedClasses = {};
classes.forEach(classpair => {
if (!orderedClasses[classpair.parent.value]) {
orderedClasses[classpair.parent.value] = {
childrens: [classpair.child.value],
parent: "",
label: classpair.label.value
}
} else {
orderedClasses[classpair.parent.value].childrens.push(classpair.child.value);
}
if (!orderedClasses[classpair.child.value]) orderedClasses[classpair.child.value] = {
childrens: [],
parent: classpair.parent.value,
label: classpair.label.value
};
if (classpair.description) {
orderedClasses[classpair.child.value] = {...orderedClasses[classpair.child.value], description: classpair.description.value};
}
});
return orderedClasses;
};
export const getClassTree = async (classname) => {
// ${isProblem ? "?subject onto:problemDescription ?description" : ""}
let query = `
SELECT ?parent ?child ?description ?label
WHERE {
?parent rdfs:subClassOf* ${classname}.
?child rdfs:subClassOf ?parent.
?child rdfs:label ?label.
OPTIONAL {
?child onto:problemDescription ?description.
}
}
`
try {
let response = await fetch( FUSEKI_URL, getOptions(PREFIXES + query) );
if (response.status === 200) {
return createClassesTree(parseResults(await response.json()));
};
return [];
} catch (e) {
console.error('Failed to fetch: ' + e);
return [];
}
};
export const getVariantRelations = async (variantURI) => {
let query = `
SELECT ?relation ?variant ?variant_label
WHERE {
${variantURI} ?relation ?variant
FILTER (?relation IN (
onto:requires,
onto:createdFrom,
onto:relatedTo,
onto:variantOf,
onto:benefitsFrom
) ).
?variant rdfs:label ?variant_label
}
`;
try {
let response = await fetch( FUSEKI_URL, getOptions(PREFIXES + query) );
if (response.status === 200) {
return parseResults(await response.json());
};
return [];
} catch (e) {
console.error('Failed to fetch: ' + e);
return [];
}
};
const parseProposal = (result) => {
return {
proposal: result.proposal.value,
pattern: result.pattern.value,
context: result.context.value,
solution: result.solution.value,
label: result.proposal_label.value,
paper: {
paper: result.paper.value,
title: result.title.value,
identifier: result.identifier.value,
identifiertype: result.identifiertype.value,
authors: result.authors.value
}
}
}
// used to merge proposal duplicated in the request as we also want to retrieve each of their classes
const structurePatterns = (patterns, pCitations, vCitations, prCitations) => {
const newPatterns = {};
const pCitationsDict = {}
const vCitationsDict = {}
const prCitationsDict = {}
pCitations.forEach(citation => pCitationsDict[citation.pattern.value] = parseInt(citation.citations.value));
vCitations.forEach(citation => vCitationsDict[citation.variant.value] = parseInt(citation.citations.value));
prCitations.forEach(citation => prCitationsDict[citation.proposal.value] = parseInt(citation.citations.value));
patterns.forEach(r => {
if (newPatterns[r.pattern.value]) {
if (newPatterns[r.pattern.value].variants[r.variant.value]) {
if (newPatterns[r.pattern.value].variants[r.variant.value].proposals[r.proposal.value]) {
newPatterns[r.pattern.value].variants[r.variant.value].proposals[r.proposal.value].classes.push(r.parent.value);
} else {
newPatterns[r.pattern.value].variants[r.variant.value].proposals[r.proposal.value] = {
...parseProposal(r),
classes: [r.parent.value, r.pattern.value],
citations: prCitationsDict[r.proposal.value] ?? 0
}
}
} else {
newPatterns[r.pattern.value].variants[r.variant.value] = {
proposals: {
[r.proposal.value]: {
...parseProposal(r),
classes: [r.parent.value, r.pattern.value],
citations: prCitationsDict[r.proposal.value] ?? 0
}
},
label: r.variant_label.value,
citations: vCitationsDict[r.variant.value] ?? 0,
}
}
} else {
newPatterns[r.pattern.value] = {
pattern: r.pattern.value,
label: r.pattern_label.value,
citations: pCitationsDict[r.pattern.value] ?? 0,
variants: {
[r.variant.value]: {
proposals: {
[r.proposal.value]: {
...parseProposal(r),
classes: [r.parent.value, r.pattern.value],
citations: prCitationsDict[r.proposal.value] ?? 0
}
},
label: r.variant_label.value,
citations: vCitationsDict[r.variant.value] ?? 0,
}
}
}
}
});
return newPatterns;
};
const addScoreToPatterns = (patterns, filterProblems) => {
const newPatterns = patterns.map(pattern => ({
...pattern,
score: filterProblems[pattern.problem.value].score
}));
return newPatterns;
};
const getPatterns = async () => {
const query = `
SELECT ?parent ?pattern ?variant ?proposal ?pattern_label ?variant_label ?proposal_label ?paper ?context ?solution ?title ?identifier ?identifiertype ?authors
WHERE {
?proposal a ?parent .
?proposal a onto:Proposal .
?proposal onto:hasVariant ?variant .
?variant a ?pattern .
?pattern rdfs:subClassOf* onto:Pattern.
?pattern rdfs:label ?pattern_label .
?variant rdfs:label ?variant_label .
?proposal rdfs:label ?proposal_label .
?proposal onto:hasPaper ?paper .
?proposal onto:ContextAndProblem ?context .
?proposal onto:Solution ?solution.
?paper onto:Title ?title.
?paper onto:Identifier ?identifier.
?paper onto:IdentifierType ?identifiertype.
?paper onto:Authors ?authors
}
`
try {
let response = await fetch( FUSEKI_URL, getOptions(PREFIXES + query) );
if (response.status === 200) {
return parseResults(await response.json());
};
return [];
} catch (e) {
console.error('Failed to fetch: ' + e);
return [];
}
}
export const getPatternKnowledge = async () => {
const patterns = await getPatterns();
const pCitations = await getPatternsCitations();
const vCitations = await getVariantsCitations();
const prCitations = await getProposalCitations();
if (patterns.length) {
const grouped = structurePatterns(patterns, pCitations, vCitations, prCitations);
return grouped;
} else return [];
}
const getPatternsCitations = async () => {
let query = `
SELECT ?pattern (COUNT(*) as ?citations)
WHERE {
?pattern rdfs:subClassOf* onto:Pattern .
?variant a ?pattern .
?variant onto:hasProposal ?proposal .
?proposal onto:hasPaper ?paper .
?paper onto:hasIdentifier ?identifier .
?target onto:references ?identifier
} GROUP BY ?pattern
`;
try {
let response = await fetch( FUSEKI_URL, getOptions(PREFIXES + query) );
if (response.status === 200) {
return parseResults(await response.json());
};
return [];
} catch (e) {
console.error('Failed to fetch: ' + e);
return [];
}
}
const getVariantsCitations = async () => {
let query = `
SELECT ?variant (COUNT(*) as ?citations)
WHERE {
?variant a onto:Variant .
?variant onto:hasProposal ?proposal .
?proposal onto:hasPaper ?paper .
?paper onto:hasIdentifier ?identifier .
?target onto:references ?identifier
} GROUP BY ?variant
`;
try {
let response = await fetch( FUSEKI_URL, getOptions(PREFIXES + query) );
if (response.status === 200) {
return parseResults(await response.json());
};
return [];
} catch (e) {
console.error('Failed to fetch: ' + e);
return [];
}
}
const getProposalCitations = async () => {
let query = `
SELECT ?proposal (COUNT(*) as ?citations)
WHERE {
?proposal onto:hasPaper ?paper .
?paper onto:hasIdentifier ?identifier .
?target onto:references ?identifier
} GROUP BY ?proposal
`;
try {
let response = await fetch( FUSEKI_URL, getOptions(PREFIXES + query) );
if (response.status === 200) {
return parseResults(await response.json());
};
return [];
} catch (e) {
console.error('Failed to fetch: ' + e);
return [];
}
}
export const getPatternsByProblem = async (filterProblems = {}) => {
const queryTemplate = () => {
return `
SELECT distinct ?problem ?variant ?proposal ?pattern ?variant_label ?pattern_label ?proposal_label ?paper ?context ?solution ?title ?identifier ?identifiertype ?authors
WHERE {
?pattern rdfs:subClassOf*
[
rdf:type owl:Restriction ;
owl:onProperty onto:addressProblem ;
owl:someValuesFrom ?problem
].
FILTER (?problem IN (${Object.keys(filterProblems).join(',')}) ).
?pattern rdfs:subClassOf* onto:Pattern.
?proposal onto:hasVariant ?variant .
?variant a ?pattern .
?pattern rdfs:label ?pattern_label.
?proposal rdfs:label ?proposal_label .
?variant rdfs:label ?variant_label .
?proposal onto:hasPaper ?paper .
?proposal onto:ContextAndProblem ?context .
?proposal onto:Solution ?solution.
?paper onto:Title ?title.
?paper onto:Identifier ?identifier.
?paper onto:IdentifierType ?identifiertype.
?paper onto:Authors ?authors
}
`
}
if (Object.keys(filterProblems).length > 0) {
try {
let query = queryTemplate()
let response = await fetch( FUSEKI_URL, getOptions(PREFIXES + query) );
if (response.status === 200) {
const results = parseResults(await response.json());
const scored = addScoreToPatterns(results, filterProblems);
const citations = await getProposalCitations();
const citationsDict = {};
citations.forEach(citation => citationsDict[citation.proposal.value] = parseInt(citation.citations.value));
const mapped = scored.map(r => ({ ...parseProposal(r), score: r.score, citations: citationsDict[r.proposal.value] ? parseInt(citationsDict[r.proposal.value]) : 0 }));
return mapped;
};
return [];
} catch (e) {
console.error('Failed to fetch: ' + e);
return [];
}
} else {
return {};
}
}