1 | // Von-Sparql Collection Comparison Script
|
---|
2 | //
|
---|
3 | // Contains script to build queries to compare contents of
|
---|
4 | // one collection with another.
|
---|
5 |
|
---|
6 |
|
---|
7 | //Builds a single-collection query from inputs
|
---|
8 | function generateQuery(side,dataset){
|
---|
9 | var predicate = $('#sel_' + side + 'Predicate').val();
|
---|
10 | var sparqlQuery = "";
|
---|
11 |
|
---|
12 | var isRefinedQuery = 0;
|
---|
13 | if($('#cb_' + side + 'RefineSearch').is(':checked'))
|
---|
14 | isRefinedQuery = 1;
|
---|
15 |
|
---|
16 | //makes sure {<,>} are not mucking up the query
|
---|
17 | var speechMarks = "\"";
|
---|
18 | if($('#sel_' + side + 'PredicateValue').val().substring(0,1)=="<") speechMarks="";
|
---|
19 |
|
---|
20 | var refineDataset = "GRAPH gsdl:" + dataset + " {\n";
|
---|
21 | if(isRefinedQuery == 0)
|
---|
22 | sparqlQuery += "SELECT (?o as ?Objects) (COUNT (?o) as ?Count) WHERE {\n" +
|
---|
23 | " " + refineDataset +
|
---|
24 | " ?s <" + predicate + "> ?o\n } \n}\n";
|
---|
25 | else
|
---|
26 | sparqlQuery += "SELECT (?o as ?Objects) (COUNT (?o) as ?Count) WHERE {\n" +
|
---|
27 | " " + refineDataset +
|
---|
28 | " ?s <"+ predicate + "> "+ speechMarks + $('#sel_' + side + 'PredicateValue').val() + speechMarks +";\n" +
|
---|
29 | " <"+$('#sel_' + side + 'VisualizePredicate').val() +"> ?o\n}\n}\n" ;
|
---|
30 | sparqlQuery += "GROUP BY ?o ORDER BY DESC(?Count)\n";
|
---|
31 |
|
---|
32 | if($('#sel_' + side + 'Limit').val()!="None")
|
---|
33 | sparqlQuery += " LIMIT " + $('#sel_' + side + 'Limit').val();
|
---|
34 | else
|
---|
35 | sparqlQuery += " LIMIT " + hardLimit;
|
---|
36 |
|
---|
37 | return(sparqlQuery);
|
---|
38 | }
|
---|
39 |
|
---|
40 | //Builds a query comparing data selected from two collections
|
---|
41 | function generateComparisonQuery(){
|
---|
42 |
|
---|
43 | var query = comparisonQuery;
|
---|
44 | var predOut= $('#sel_comparisonPredicate').val();
|
---|
45 | var comparisonMethod = $('#sel_comparisonMethod').val();
|
---|
46 |
|
---|
47 | //Start generating query
|
---|
48 | var sparqlQuery = "";
|
---|
49 | var sparqlQueryHeader = "";
|
---|
50 |
|
---|
51 | //If query type is an intersection of the collection data
|
---|
52 | if(query.type=="intersection"){
|
---|
53 | sparqlQueryHeader += "SELECT DISTINCT ?common ";
|
---|
54 |
|
---|
55 | sparqlQuery += "GRAPH gsdl:" + leftCollection + "{" ;
|
---|
56 | if(query.refine_L!="")
|
---|
57 | sparqlQuery += " ?s <" + query.pred_L + "> \"" + query.predval_L + "\";" +
|
---|
58 | query.refine_L;
|
---|
59 | else
|
---|
60 | sparqlQuery += " ?s <" + query.pred_L + "> "// ?common;";
|
---|
61 |
|
---|
62 | sparqlQuery += setComparisonMethod("left", comparisonMethod);
|
---|
63 |
|
---|
64 | if(!$('#cb_showOnlyCommonObjects').is(':checked')){
|
---|
65 | if($('#sel_comparisonPredicate').val()!=""){
|
---|
66 | var objName = "?Left_" + $('#sel_comparisonPredicate option:selected').text().replace(/\./g, '_');
|
---|
67 | sparqlQuery += "OPTIONAL{ ?s <" + $('#sel_comparisonPredicate').val() + "> " + objName + "}";
|
---|
68 | sparqlQueryHeader+= objName + " ";
|
---|
69 | }
|
---|
70 | if($('#sel_comparisonPredicate1').val()!=""){
|
---|
71 | var objName = "?Left_" + $('#sel_comparisonPredicate1 option:selected').text().replace(/\./g, '_');
|
---|
72 | sparqlQuery += "OPTIONAL{ ?s <" + $('#sel_comparisonPredicate1').val() + "> " + objName + "}";
|
---|
73 | sparqlQueryHeader+= objName + " ";
|
---|
74 | }
|
---|
75 | if($('#sel_comparisonPredicate2').val()!=""){
|
---|
76 | var objName = "?Left_" + $('#sel_comparisonPredicate2 option:selected').text().replace(/\./g, '_');
|
---|
77 | sparqlQuery += "OPTIONAL{ ?s <" + $('#sel_comparisonPredicate2').val() + "> " + objName + "}";
|
---|
78 | sparqlQueryHeader+= objName + " ";
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | sparqlQuery += "}" +
|
---|
83 | "GRAPH gsdl:" + rightCollection +"{";
|
---|
84 | if(query.refine_R!="")
|
---|
85 | sparqlQuery += " ?s1 <" + query.pred_R + "> \"" + query.predval_R + "\";" +
|
---|
86 | query.refine_R;
|
---|
87 | else
|
---|
88 | sparqlQuery += " ?s1 <" + query.pred_R + "> "// ?common;" ;
|
---|
89 |
|
---|
90 | sparqlQuery += setComparisonMethod("right", comparisonMethod);
|
---|
91 |
|
---|
92 | if(!$('#cb_showOnlyCommonObjects').is(':checked')){
|
---|
93 | if($('#sel_comparisonPredicate').val()!=""){
|
---|
94 | var objName = "?Right_" + $('#sel_comparisonPredicate option:selected').text().replace(/\./g, '_');
|
---|
95 | sparqlQuery += "OPTIONAL{ ?s1 <" + $('#sel_comparisonPredicate').val() + "> " + objName + "}";
|
---|
96 | sparqlQueryHeader+= objName + " ";
|
---|
97 | }
|
---|
98 | if($('#sel_comparisonPredicate1').val()!=""){
|
---|
99 | var objName = "?Right_" + $('#sel_comparisonPredicate1 option:selected').text().replace(/\./g, '_');
|
---|
100 | sparqlQuery += "OPTIONAL{ ?s1 <" + $('#sel_comparisonPredicate1').val() + "> " + objName + "}";
|
---|
101 | sparqlQueryHeader+= objName + " ";
|
---|
102 | }
|
---|
103 | if($('#sel_comparisonPredicate2').val()!=""){
|
---|
104 | var objName = "?Right_" + $('#sel_comparisonPredicate2 option:selected').text().replace(/\./g, '_');
|
---|
105 | sparqlQuery += "OPTIONAL{ ?s1 <" + $('#sel_comparisonPredicate2').val() + "> " + objName + "}";
|
---|
106 | sparqlQueryHeader+= objName + " ";
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | sparqlQuery += "}" +
|
---|
111 | "FILTER (?common!=\"\")" +
|
---|
112 | "}";
|
---|
113 | sparqlQuery += "ORDER BY DESC (?common)"
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | //If query is for data that is in one collection, but not the other (MINUS)
|
---|
118 | if(query.type=="minus"){
|
---|
119 | sparqlQueryHeader += "SELECT DISTINCT ?minus " ;
|
---|
120 |
|
---|
121 | var subj;
|
---|
122 | if(query.datasetSelection==leftCollection){
|
---|
123 | sparqlQuery += "GRAPH gsdl:" + leftCollection + "{ ?s <" + query.pred_L + "> ?minus;"
|
---|
124 |
|
---|
125 | if(query.refine_L!="")
|
---|
126 | sparqlQuery += " <" + query.pred_L + "> \"" + query.predval_L + "\";"
|
---|
127 |
|
---|
128 | subj = "?s ";
|
---|
129 | }
|
---|
130 | else{
|
---|
131 | sparqlQuery += "GRAPH gsdl:" + rightCollection +"{ ?s1 <" + query.pred_R + "> ?minus;";
|
---|
132 |
|
---|
133 | if(query.refine_R!="")
|
---|
134 | sparqlQuery += " <" + query.pred_R + "> \"" + query.predval_L + "\";"
|
---|
135 |
|
---|
136 | subj = "?s1 ";
|
---|
137 | }
|
---|
138 |
|
---|
139 | if(!$('#cb_showOnlyCommonObjects').is(':checked')){
|
---|
140 | if($('#sel_comparisonPredicate').val()!=""){
|
---|
141 | var objName = "?" + $('#sel_comparisonPredicate option:selected').text().replace(/\./g, '_');
|
---|
142 | sparqlQuery += "OPTIONAL{ " + subj + " <" + $('#sel_comparisonPredicate').val() + "> " + objName + "}";
|
---|
143 | sparqlQueryHeader+= objName + " ";
|
---|
144 | }
|
---|
145 | if($('#sel_comparisonPredicate1').val()!=""){
|
---|
146 | var objName = "?" + $('#sel_comparisonPredicate1 option:selected').text().replace(/\./g, '_');
|
---|
147 | sparqlQuery += "OPTIONAL{ " + subj + " <" + $('#sel_comparisonPredicate1').val() + "> " + objName + "}";
|
---|
148 | sparqlQueryHeader+= objName + " ";
|
---|
149 | }
|
---|
150 | if($('#sel_comparisonPredicate2').val()!=""){
|
---|
151 | var objName = "?" + $('#sel_comparisonPredicate2 option:selected').text().replace(/\./g, '_');
|
---|
152 | sparqlQuery += "OPTIONAL{ " + subj + " <" + $('#sel_comparisonPredicate2').val() + "> " + objName + "}";
|
---|
153 | sparqlQueryHeader+= objName + " ";
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | sparqlQuery += "} ";
|
---|
158 | sparqlQuery += "MINUS{"
|
---|
159 | + "GRAPH gsdl:" + leftCollection + "{" ;
|
---|
160 |
|
---|
161 | if(query.refine_L!="")
|
---|
162 | sparqlQuery += " ?s <" + query.pred_L + "> \"" + query.predval_L + "\";"
|
---|
163 | + query.refine_L;
|
---|
164 | else
|
---|
165 | sparqlQuery += " ?s <" + query.pred_L + "> ";
|
---|
166 |
|
---|
167 | sparqlQuery += setComparisonMethod("left", comparisonMethod)
|
---|
168 | sparqlQuery += "}"
|
---|
169 | + "GRAPH gsdl:" + rightCollection +"{";
|
---|
170 | if(query.refine_R!="")
|
---|
171 | sparqlQuery += " ?s1 <" + query.pred_R + "> \"" + query.predval_R + "\";"
|
---|
172 | + query.refine_R;
|
---|
173 | else
|
---|
174 | sparqlQuery += " ?s1 <" + query.pred_R + "> " ;
|
---|
175 |
|
---|
176 | sparqlQuery += setComparisonMethod("right", comparisonMethod)
|
---|
177 | sparqlQuery += "}"
|
---|
178 | + "}}";
|
---|
179 | }
|
---|
180 | sparqlQuery = sparqlQueryHeader + " WHERE{" + sparqlQuery;
|
---|
181 | sparqlQuery += "LIMIT " + $('#sel_comparisonLimit').val() + " ";
|
---|
182 | sparqlQuery += "OFFSET " + ((query.page-1) * $('#sel_comparisonLimit').val());
|
---|
183 | return sparqlQuery;
|
---|
184 | }
|
---|
185 |
|
---|
186 | //Used when substrings of returned values are used for comparison
|
---|
187 | //i.e. when numbers are extracted from DATE triples for plotting in a line graph, etc"
|
---|
188 | function setComparisonMethod(side, comparisonMethod){
|
---|
189 | var query = "";
|
---|
190 | var tempVar;
|
---|
191 | if(side == "left")
|
---|
192 | tempVar = "a";
|
---|
193 | else if(side == "right")
|
---|
194 | tempVar = "b";
|
---|
195 | else if(side == "minus")
|
---|
196 | tempVar = "x";
|
---|
197 | switch(comparisonMethod){
|
---|
198 | case "exact":
|
---|
199 | query += "?common;";
|
---|
200 | break;
|
---|
201 | case "10numeric":
|
---|
202 | query += "?"+tempVar + "; "
|
---|
203 | + "BIND (REPLACE (?o, \"\\\\D\",\"\") as ?" + tempVar + "1) "
|
---|
204 | + "BIND (xsd:integer(SUBSTR (?" + tempVar + "1, 1, 10)) as ?common) ";
|
---|
205 | break;
|
---|
206 | case "numeric":
|
---|
207 | query += "?" + tempVar + "; ";
|
---|
208 | if(side == "minus")
|
---|
209 | query += "BIND (REPLACE (?" + tempVar + ", \"\\\\D\",\"\") as ?minus) ";
|
---|
210 | else
|
---|
211 | query += "BIND (REPLACE (?" + tempVar + ", \"\\\\D\",\"\") as ?common) ";
|
---|
212 | break;
|
---|
213 | case "10char":
|
---|
214 | query += "?" + tempVar + "; ";
|
---|
215 | if(side == "minus")
|
---|
216 | query += "BIND (SUBSTR(?" + tempVar + ", 0,10) as ?minus) ";
|
---|
217 | else
|
---|
218 | query += "BIND (SUBSTR(?" + tempVar + ", 0,10) as ?common) ";
|
---|
219 | break;
|
---|
220 | case "minus":
|
---|
221 | query += "?minus;";
|
---|
222 | break;
|
---|
223 | default:
|
---|
224 | break;
|
---|
225 | }
|
---|
226 | return query;
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | //Updates the "value" selector with possible values
|
---|
231 | function updateValueSelector(predicate, selector, collectionName){
|
---|
232 | var refineDataset = "GRAPH gsdl:" + collectionName + " {\n";
|
---|
233 | var sparqlQuery = "PREFIX gsdl: <http://localhost:3030/greenstone/data/> \n";
|
---|
234 | sparqlQuery += "SELECT ?obj WHERE{"+
|
---|
235 | refineDataset +
|
---|
236 | " ?s <"+predicate+"> ?obj }}"+
|
---|
237 | "GROUP BY ?obj ORDER BY (?obj)"+
|
---|
238 | "LIMIT 1000";
|
---|
239 | var ajaxSparqlQuery = sparqlEndpoint + "?query=" + encodeURIComponent(sparqlQuery);
|
---|
240 | currentQueries++;
|
---|
241 | setCursor("wait");
|
---|
242 |
|
---|
243 | $.ajax({
|
---|
244 | url: ajaxSparqlQuery,
|
---|
245 | type:"GET",
|
---|
246 | data:"output=json",
|
---|
247 | })
|
---|
248 | .done(function( json ) {
|
---|
249 | var parsedJSON = ((typeof json) == "string") ? $.parseJSON(json) : json;
|
---|
250 |
|
---|
251 | //Clear selector. Add empty option to start.
|
---|
252 | var valueSelector = $('#' + selector);
|
---|
253 | valueSelector.empty();
|
---|
254 | valueSelector
|
---|
255 | .append("<option value=\"None\"> </option>");
|
---|
256 |
|
---|
257 | $.each(parsedJSON.results.bindings,function(index,item){
|
---|
258 | var obj;
|
---|
259 | if(item.obj.type == "uri"){
|
---|
260 | obj = "<" + item.obj.value + ">";
|
---|
261 | valueSelector
|
---|
262 | .append("<option value="+ obj + ">" + obj.substring(0,75) + "</option>");
|
---|
263 | }
|
---|
264 | else{
|
---|
265 | obj = item.obj.value;
|
---|
266 | valueSelector
|
---|
267 | .append("<option value=\""+ obj +"\">" + obj.substring(0,75) + "</option>");
|
---|
268 | }
|
---|
269 | });
|
---|
270 | valueSelector.next().find('.custom-combobox-input').val("");
|
---|
271 | if(--currentQueries == 0)
|
---|
272 | setCursor("");
|
---|
273 | });
|
---|
274 | }
|
---|
275 |
|
---|
276 | //Updates the selector which contains available predicates after
|
---|
277 | //previously restricting a predicate to a certain value
|
---|
278 | function updateVisualizePredicateSelector(predicate,value,selector,collection){
|
---|
279 | var sparqlQuery;
|
---|
280 | if(value.substring(0,1) == "<")
|
---|
281 | sparqlQuery = "SELECT ?predicate WHERE{"+
|
---|
282 | " ?s <" + predicate + "> "+ value + ";"+
|
---|
283 | " ?predicate ?o}"+
|
---|
284 | "GROUP BY ?predicate ORDER BY DESC(?predicate)";
|
---|
285 | else
|
---|
286 | sparqlQuery = "SELECT ?predicate WHERE{"+
|
---|
287 | " ?s <" + predicate + "> \""+ value + "\";"+
|
---|
288 | " ?predicate ?o}"+
|
---|
289 | "GROUP BY ?predicate ORDER BY DESC(?predicate)";
|
---|
290 |
|
---|
291 | var ajaxSparqlQuery = sparqlEndpoint + "?query=" + encodeURIComponent(sparqlQuery);
|
---|
292 | currentQueries++;
|
---|
293 | setCursor("wait");
|
---|
294 |
|
---|
295 | $.ajax({
|
---|
296 | url: ajaxSparqlQuery,
|
---|
297 | type:"GET",
|
---|
298 | data:"output=json",
|
---|
299 | })
|
---|
300 | .done(function( json ) {
|
---|
301 | var parsedJSON = ((typeof json) == "string") ? $.parseJSON(json) : json;
|
---|
302 | var dropdown = $('#' + selector);
|
---|
303 | dropdown.empty();
|
---|
304 | dropdown
|
---|
305 | .append("<option value=\"None\"> </option>");
|
---|
306 | $.each(parsedJSON.results.bindings,function(index,item){
|
---|
307 | var pred = item.predicate.value;
|
---|
308 | var name = tidyPredicate(pred);
|
---|
309 | dropdown
|
---|
310 | .append("<option value=\""+pred+"\">" + name + "</option>");
|
---|
311 | });
|
---|
312 | dropdown.next().find('.custom-combobox-input').val("");
|
---|
313 | if(--currentQueries == 0)
|
---|
314 | setCursor("");
|
---|
315 | });
|
---|
316 | }
|
---|
317 |
|
---|
318 | function drawCollectionChart(side, collection){
|
---|
319 | var Q = new sgvizler.Query();
|
---|
320 | Q.query(generateQuery(side, collection))
|
---|
321 | .endpointURL(sparqlEndpoint)
|
---|
322 | .endpointOutputFormat("json")
|
---|
323 | .chartFunction($('#sel_' + side + 'ChartType').val())
|
---|
324 | .draw("div_" + side + "FilteredDataset");
|
---|
325 |
|
---|
326 | }
|
---|
327 |
|
---|
328 | function drawComparisonTable(){
|
---|
329 | sgvizler.defaultChartWidth("auto");
|
---|
330 | sgvizler.defaultChartHeight(280);
|
---|
331 | var Q = new sgvizler.Query();
|
---|
332 | Q.query(generateComparisonQuery())
|
---|
333 | .endpointURL(sparqlEndpoint)
|
---|
334 | .endpointOutputFormat("json")
|
---|
335 | .chartFunction("google.visualization.Table")
|
---|
336 | .draw("div_combinedResultsVisualization");
|
---|
337 | sgvizler.defaultChartWidth(defaultChartWidth);
|
---|
338 | sgvizler.defaultChartHeight(defaultChartHeight);
|
---|
339 | }
|
---|
340 |
|
---|
341 | function tidyPredicate(name){
|
---|
342 | name = name.replace("http://purl.org/dc/elements/1.1/","dc.");
|
---|
343 | name = name.replace("http://purl.org/ontology/bibo/","bibo.");
|
---|
344 | name = name.replace("http://greenstone.org/gsembedded#","gsembedded.");
|
---|
345 | name = name.replace("http://greenstone.org/gsextracted#","gsextracted.");
|
---|
346 | return name;
|
---|
347 | }
|
---|
348 |
|
---|
349 | //Fills the predicate selector with values
|
---|
350 | function populatePredicateSelector(collection, selector){
|
---|
351 | $('#' + selector).empty();
|
---|
352 | if(selector=="sel_comparisonPredicate"){
|
---|
353 | $('#' + selector + "1")
|
---|
354 | .empty()
|
---|
355 | .append("<option value=\"\"> </option>");
|
---|
356 | $('#' + selector + "2")
|
---|
357 | .empty()
|
---|
358 | .append("<option value=\"\"> </option>");
|
---|
359 | }
|
---|
360 |
|
---|
361 | var ajaxSparqlQuery = sparqlEndpoint + "?query=" + encodeURIComponent(createOverviewQuery(collection));
|
---|
362 | currentQueries++;
|
---|
363 | setCursor("wait");
|
---|
364 | $.ajax({
|
---|
365 | url: ajaxSparqlQuery,
|
---|
366 | type:"GET",
|
---|
367 | data:"output=json",
|
---|
368 | })
|
---|
369 | .done(function( json ) {
|
---|
370 | var parsedJSON = ((typeof json) == "string") ? $.parseJSON(json) : json;
|
---|
371 | $.each(parsedJSON.results.bindings,function(index,item){
|
---|
372 | if(typeof(item.Predicate)!="undefined"){
|
---|
373 | if(selector=="sel_leftPredicate" || selector == "sel_rightPredicate"){
|
---|
374 | var side = selector.substring(0, selector.indexOf("P"));
|
---|
375 | var pred = item.Predicate.value;
|
---|
376 | var name = tidyPredicate(pred);
|
---|
377 | $('#' + selector)
|
---|
378 | .append("<option value=\""+pred+"\">" + name + "</option>");
|
---|
379 | $('#' + side + "VisualizePredicate")
|
---|
380 | .append("<option value=\""+pred+"\">" + name + "</option>");
|
---|
381 |
|
---|
382 | }
|
---|
383 | else{
|
---|
384 | var pred = item.Predicate.value;
|
---|
385 | var name = tidyPredicate(pred);
|
---|
386 | $('#' + selector)
|
---|
387 | .append("<option value=\""+pred+"\">" + name + "</option>");
|
---|
388 | $('#' + selector + "1")
|
---|
389 | .append("<option value=\""+pred+"\">" + name + "</option>");
|
---|
390 | $('#' + selector + "2")
|
---|
391 | .append("<option value=\""+pred+"\">" + name + "</option>");
|
---|
392 | }
|
---|
393 | }
|
---|
394 | });
|
---|
395 | if(--currentQueries == 0)
|
---|
396 | setCursor("");
|
---|
397 | });
|
---|
398 | }
|
---|
399 |
|
---|
400 | function setCursor(style){
|
---|
401 | document.body.style.cursor = style;
|
---|
402 | }
|
---|