-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
191 lines (159 loc) · 6.09 KB
/
helpers.js
File metadata and controls
191 lines (159 loc) · 6.09 KB
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
// helpers.js
import * as N3 from 'n3';
import { n3reasoner } from 'eyereasoner';
import fs from 'fs';
import { QueryEngine } from '@comunica/query-sparql';
import Table from 'cli-table3';
// Function to convert JSON-LD to N-Triples
export async function jsonLdToNTriples(jsonLdData) {
// Create a simple store for holding the triples
const store = new N3.Store();
// Process each entity in the JSON-LD array
jsonLdData.forEach(entity => {
// Get the subject from @id or generate a blank node
const subject = entity['@id'] || `_:b${Math.random().toString(36).substring(2, 15)}`;
// Process each predicate-object pair
Object.entries(entity).forEach(([predicate, objects]) => {
if (predicate === '@id' || predicate === '@context') return;
// Handle type separately
if (predicate === '@type') {
const types = Array.isArray(objects) ? objects : [objects];
types.forEach(type => {
store.addQuad(
N3.DataFactory.namedNode(subject),
N3.DataFactory.namedNode('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'),
N3.DataFactory.namedNode(type)
);
});
return;
}
// Handle regular predicates
const values = Array.isArray(objects) ? objects : [objects];
values.forEach(value => {
// Determine the object type (literal or URI)
let object;
if (typeof value === 'object' && value !== null) {
if (value['@id']) {
// URI reference
object = N3.DataFactory.namedNode(value['@id']);
} else if (value['@value']) {
// Typed or language literal
const datatype = value['@type'] ? N3.DataFactory.namedNode(value['@type']) : null;
const language = value['@language'] || '';
object = N3.DataFactory.literal(value['@value'], language || datatype);
} else {
// Skip complex objects for simplicity
return;
}
} else if (typeof value === 'string') {
object = N3.DataFactory.literal(value);
} else if (value !== null) {
object = N3.DataFactory.literal(String(value));
} else {
return; // Skip null values
}
store.addQuad(
N3.DataFactory.namedNode(subject),
N3.DataFactory.namedNode(predicate),
object
);
});
});
});
// Convert to N-Triples string
const writer = new N3.Writer({ format: 'N-Triples' });
store.forEach(quad => writer.addQuad(quad));
return new Promise((resolve, reject) => {
writer.end((error, result) => {
if (error) reject(error);
else resolve(result);
});
});
}
// Helper function to serialize an array of quads to Turtle string
export async function quadsToTurtle(quads) {
return new Promise((resolve, reject) => {
const writer = new N3.Writer({ format: 'Turtle' });
writer.addQuads(quads);
writer.end((error, result) => {
if (error) reject(error);
else resolve(result);
});
});
}
// Function to run reasoning with EYE reasoner
export async function runEyeReasoner(triples, rules) {
console.log("\n=== Reasoning with EYE Reasoner ===");
try {
// Save the input data for debugging
fs.writeFileSync('reasoner-input.n3', triples + '\n' + rules, 'utf8');
// Run the reasoner with separate rules and query
const reasoningResult = await n3reasoner(triples + '\n' + rules, "");
// Save the results
fs.writeFileSync('reasoning-result.n3', reasoningResult, 'utf8');
console.log("Reasoning results saved to reasoning-result.n3");
// Parse the results into a store
const store = new N3.Store();
const parser = new N3.Parser();
const quads = parser.parse(reasoningResult);
store.addQuads(quads);
return store;
} catch (error) {
console.error("Error executing EYE reasoning:", error);
console.log("\nDiagnostic information:");
throw error;
}
}
// Function to run SPARQL query with Comunica
export async function runSparqlQuery(triples,sparqlQuery, reasoning_results, useReasoningResults) {
console.log("\n=== Execute SPARQL Query ===");
try {
// Parse the triples into an N3 Store for Comunica
const store = new N3.Store();
const parser = new N3.Parser();
// Parse the triples
const quads = parser.parse(triples);
store.addQuads(quads);
// when we want to use the reasoning results
if(useReasoningResults){
reasoning_results.getQuads(null, null, null, null).forEach(quad => {
store.addQuad(quad);
});
}
// Create a new query engine
const engine = new QueryEngine();
// Execute the query
const queryStream = await engine.queryBindings(sparqlQuery, { sources: [store] });
// Display the results
const bindings = await queryStream.toArray();
// console.log(bindings.toString());
// console.log("\nSPARQL Query Results:");
// console.log("\call | source | priority ");
// console.log("---------------------------------|--------------------------------------|----------");
// for (const binding of bindings) {
// const call = binding.get('call').value;
// const source = binding.get('source').value;
// const priority = binding.get('priority').value;
// console.log(`${call} | ${source} | ${priority}`);
// }
if (bindings.length === 0) {
console.log("No results.");
} else {
// Extract variable names dynamically
const variables = [...new Set(bindings.flatMap(binding => [...binding.keys()].map(v => v.value)))];
// Initialize table with headers
const table = new Table({ head: variables });
// Add rows to the table
bindings.forEach(binding => {
const row = variables.map(varName => binding.get(varName)?.value || '');
table.push(row);
});
// Print the table
console.log(table.toString());
}
return { bindings };
} catch (error) {
console.error("Error executing SPARQL query:", error);
throw error;
}
}