-
Notifications
You must be signed in to change notification settings - Fork 1
/
sparql.js
243 lines (207 loc) · 10.6 KB
/
sparql.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
const QUADS_MEDIA_TYPES = ["text/turtle", "application/trig", "application/n-triples", "application/n-quads", "application/ld+json"]; // not supported by Comunica: "application/rdf+xml"
const BINDINGS_MEDIA_TYPES = ["application/sparql-results+json", "application/sparql-results+xml"]; // not supported by Comunica: "application/sparql-results+csv"
window.onload = function() {
document.getElementById("exec-query").addEventListener("click", query);
}
async function query() {
const baseURI = window.location.href.split('#')[0];
const queryString = document.getElementById("query-string").value.trim();
const inputMediaType = document.getElementById("rdf-input-media-type").value;
const sources = [{
type: 'stringSource',
value: document.getElementById("rdf-input").value,
mediaType: inputMediaType,
baseIRI: baseURI,
}];
const engine = new Comunica.QueryEngine();
const resultsDiv = document.getElementById("results");
const outputTextarea = document.getElementById("output");
try {
var result = await engine.query(queryString, {
"sources": sources
});
const outputMediaTypeSelect = document.getElementById("output-media-type");
// make sure the selected output media type is compatible with the result type (reset it if it's not)
if (result.resultType == "quads") {
if (!QUADS_MEDIA_TYPES.includes(outputMediaTypeSelect.value)) outputMediaTypeSelect.value = QUADS_MEDIA_TYPES[0];
outputMediaTypeSelect.querySelector("optgroup[value = 'quads']").disabled = false;
outputMediaTypeSelect.querySelector("optgroup[value = 'bindings']").disabled = true;
}
if (result.resultType == "bindings" || result.resultType == "boolean") {
if (!BINDINGS_MEDIA_TYPES.includes(outputMediaTypeSelect.value)) outputMediaTypeSelect.value = BINDINGS_MEDIA_TYPES[0];
outputMediaTypeSelect.querySelector("optgroup[value = 'bindings']").disabled = false;
outputMediaTypeSelect.querySelector("optgroup[value = 'quads']").disabled = true;
}
const outputMediaType = outputMediaTypeSelect.value;
const {
data
} = await engine.resultToString(result, outputMediaType);
const resultString = await streamToString(data);
outputTextarea.value = resultString;
// query again to get results in JSON which will be used to render HTML results
result = await engine.query(queryString, {
"sources": sources
});
if (result.resultType == "quads") {
const {
data
} = await engine.resultToString(result, "application/ld+json");
const jsonString = await streamToString(data);
const resultJson = JSON.parse(jsonString);
renderJSONLD(resultJson, resultsDiv);
}
if (result.resultType == "bindings") {
const {
data
} = await engine.resultToString(result, "application/sparql-results+json");
const jsonString = await streamToString(data);
const resultJson = JSON.parse(jsonString);
renderSPARQLJSON(resultJson, resultsDiv);
}
if (result.resultType == "boolean") {
const {
data
} = await engine.resultToString(result, "application/sparql-results+json");
const jsonString = await streamToString(data);
const resultJson = JSON.parse(jsonString);
const booleanPre = document.createElement("pre");
const booleanText = document.createTextNode(resultJson["boolean"]);
booleanPre.appendChild(booleanText);
resultsDiv.replaceChildren(booleanPre);
}
} catch (error) {
outputTextarea.value = "";
const errorDiv = document.createElement("div");
errorDiv.className = "alert alert-danger";
const errorText = document.createTextNode(error.toString());
errorDiv.appendChild(errorText);
resultsDiv.replaceChildren(errorDiv);
}
}
function streamToString(stream) {
return new Promise((resolve, reject) => {
let result = ''
stream.on('data', (data) => result += data)
stream.on('end', () => resolve(result))
stream.on('error', reject)
});
}
function renderJSONLD(resultJson, resultsDiv) {
const resultsTable = document.createElement("table");
resultsTable.className = "table"; // for Bootstrap
resultsDiv.replaceChildren(resultsTable);
const resultsTHead = document.createElement("thead");
resultsTable.appendChild(resultsTHead);
const resultsVarTR = document.createElement("tr");
resultsTHead.appendChild(resultsVarTR);
for (var variable of ["s", "p", "o"]) {
const resultsVarTH = document.createElement("th");
resultsVarTR.appendChild(resultsVarTH);
const resultsVarText = document.createTextNode(variable);
resultsVarTH.appendChild(resultsVarText);
}
const resultsTBody = document.createElement("tbody");
resultsTable.appendChild(resultsTBody);
resultJson.forEach(function(resource) {
const keys = Object.keys(resource);
keys.filter(key => key != "@id" && key != "@type").forEach((property, index) => {
const valuesObjs = resource[property];
valuesObjs.forEach((valueObj) => {
const tripleTR = document.createElement("tr");
resultsTBody.appendChild(tripleTR);
const subjectString = (resource["@id"].startsWith("_:")) ? resource["@id"] : "<" + resource["@id"] + ">";
const subjectTD = document.createElement("td");
tripleTR.appendChild(subjectTD);
const subjectText = document.createTextNode(subjectString);
subjectTD.appendChild(subjectText);
const propertyTD = document.createElement("td");
tripleTR.appendChild(propertyTD);
const propertyText = document.createTextNode("<" + property + ">");
propertyTD.appendChild(propertyText);
const objectTD = document.createElement("td");
tripleTR.appendChild(objectTD);
var objectString = (valueObj["@value"]) ? "\"" + valueObj["@value"] + "\"" : (valueObj["@id"].startsWith("_:")) ? valueObj["@id"] : "<" + valueObj["@id"] + ">";
if (valueObj["@type"]) objectString += "^^" + "<" + valueObj["@type"] + ">";
if (valueObj["@language"]) objectString += "@" + valueObj["@language"];
const objectText = document.createTextNode(objectString);
objectTD.appendChild(objectText);
});
});
if (resource["@type"]) {
resource["@type"].forEach((typeObj) => {
const tripleTR = document.createElement("tr");
resultsTBody.appendChild(tripleTR);
const subjectString = (resource["@id"].startsWith("_:")) ? resource["@id"] : "<" + resource["@id"] + ">";
const subjectTD = document.createElement("td");
tripleTR.appendChild(subjectTD);
const subjectText = document.createTextNode(subjectString);
subjectTD.appendChild(subjectText);
const propertyTD = document.createElement("td");
tripleTR.appendChild(propertyTD);
const propertyText = document.createTextNode("<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>");
propertyTD.appendChild(propertyText);
const objectTD = document.createElement("td");
tripleTR.appendChild(objectTD);
var objectString;
if (typeof typeObj === 'string') objectString = (typeObj.startsWith("_:")) ? typeObj : "<" + typeObj + ">";
else {
objectString = (typeObj["@value"]) ? "\"" + typeObj["@value"] + "\"" : (typeObj["@id"].startsWith("_:")) ? typeObj["@id"] : "<" + typeObj["@id"] + ">";
if (typeObj["@type"]) objectString += "^^" + "<" + typeObj["@type"] + ">";
if (typeObj["@language"]) objectString += "@" + typeObj["@language"];
}
const objectText = document.createTextNode(objectString);
objectTD.appendChild(objectText);
});
}
});
}
function renderSPARQLJSON(resultJson, resultsDiv) {
const resultsTable = document.createElement("table");
resultsTable.className = "table"; // for Bootstrap
resultsDiv.replaceChildren(resultsTable);
const resultsTHead = document.createElement("thead");
resultsTable.appendChild(resultsTHead);
const resultsVarTR = document.createElement("tr");
resultsTHead.appendChild(resultsVarTR);
for (const variable of resultJson.head.vars) {
const resultsVarTH = document.createElement("th");
resultsVarTR.appendChild(resultsVarTH);
const resultsVarText = document.createTextNode(variable);
resultsVarTH.appendChild(resultsVarText);
}
const resultsTBody = document.createElement("tbody");
resultsTable.appendChild(resultsTBody);
for (const binding of resultJson.results.bindings) {
const resultsBindingTR = document.createElement("tr");
resultsTBody.appendChild(resultsBindingTR);
for (const variable of resultJson.head.vars) {
const resultsBindingTD = document.createElement("td");
resultsBindingTR.appendChild(resultsBindingTD);
const variableBinding = binding[variable];
if (variableBinding) {
let resultsBindingString;
const value = variableBinding.value;
const datatype = variableBinding.datatype;
const lang = variableBinding["xml:lang"];
const type = variableBinding.type;
switch (type) {
case "literal":
resultsBindingString = `"${value}"`;
if (datatype) resultsBindingString += `^^<${datatype}>`;
if (lang) resultsBindingString += `@${lang}`;
break;
case "bnode":
resultsBindingString = `_:${value}`;
break;
case "uri":
resultsBindingString = `<${value}>`;
break;
default:
console.error(`Encountered unknown type: ${type}`);
}
const resultsBindingText = document.createTextNode(resultsBindingString);
resultsBindingTD.appendChild(resultsBindingText);
}
}
}
}