From ddd8f913d5ac1c94b443d0473ccf748504a0ec58 Mon Sep 17 00:00:00 2001 From: Simon Van Braeckel Date: Sat, 28 Sep 2024 16:49:47 +0200 Subject: [PATCH 1/4] Prevent duplicate prefixes --- src/ldf-client-ui.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/ldf-client-ui.js b/src/ldf-client-ui.js index f9ac348e..caea3d39 100644 --- a/src/ldf-client-ui.js +++ b/src/ldf-client-ui.js @@ -711,6 +711,17 @@ if (typeof global.process === 'undefined') this._resultCount = 0; this._startTimer(); + // Replace prefixes from queries.json with prefixes from the query + let queryWithoutPrefixes = ''; + for (const line of this.$queryTextsIndexed[this.options.queryFormat].val().split('\n')) { + let prefixMatch = line.match(/^[ \t]*PREFIX[ \t]*([^:]+):[ \t]*<([^)]+)>/); + if (prefixMatch === null) + queryWithoutPrefixes = queryWithoutPrefixes.concat(`${line}\n`); + else + // Add or update current prefix in this.options.prefixes + this.options.prefixes[prefixMatch[1]] = prefixMatch[2]; + } + // Let the worker execute the query var context = { ...this._getQueryContext(), @@ -730,7 +741,7 @@ if (typeof global.process === 'undefined') for (var prefix in this.options.prefixes) prefixesString += 'PREFIX ' + prefix + ': <' + this.options.prefixes[prefix] + '>\n'; } - var query = prefixesString + this.$queryTextsIndexed[this.options.queryFormat].val(); + let query = prefixesString + queryWithoutPrefixes; this._queryWorker.postMessage({ type: 'query', query: query, From 6027513c850fce5250dce3b035295a432bd82709 Mon Sep 17 00:00:00 2001 From: Simon Van Braeckel Date: Fri, 18 Oct 2024 02:14:35 +0200 Subject: [PATCH 2/4] Implement deduplicating via sparql.js --- package.json | 1 + src/ldf-client-ui.js | 23 ++++++++++------------- yarn.lock | 11 +++++++++++ 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/package.json b/package.json index fd80b969..cbcefe46 100644 --- a/package.json +++ b/package.json @@ -73,6 +73,7 @@ "n3": "^1.16.2", "rdf-string": "^1.6.1", "relative-to-absolute-iri": "^1.0.6", + "sparqljs": "^3.7.3", "string-replace-loader": "^3.1.0", "webpack": "^5.69.0", "webpack-cli": "^4.9.2", diff --git a/src/ldf-client-ui.js b/src/ldf-client-ui.js index caea3d39..b9f51ef2 100644 --- a/src/ldf-client-ui.js +++ b/src/ldf-client-ui.js @@ -1,6 +1,8 @@ /*! @license MIT ©2014–2016 Ruben Verborgh, Ghent University – imec */ // jQuery widget for Triple Pattern Fragments query execution +var SparqlParser = require('sparqljs').Parser; +var SparqlGenerator = require('sparqljs').Generator; // This exports the webpacked jQuery. window.jQuery = require('../deps/jquery-2.1.0.js'); var N3 = require('n3'); @@ -711,17 +713,6 @@ if (typeof global.process === 'undefined') this._resultCount = 0; this._startTimer(); - // Replace prefixes from queries.json with prefixes from the query - let queryWithoutPrefixes = ''; - for (const line of this.$queryTextsIndexed[this.options.queryFormat].val().split('\n')) { - let prefixMatch = line.match(/^[ \t]*PREFIX[ \t]*([^:]+):[ \t]*<([^)]+)>/); - if (prefixMatch === null) - queryWithoutPrefixes = queryWithoutPrefixes.concat(`${line}\n`); - else - // Add or update current prefix in this.options.prefixes - this.options.prefixes[prefixMatch[1]] = prefixMatch[2]; - } - // Let the worker execute the query var context = { ...this._getQueryContext(), @@ -736,15 +727,21 @@ if (typeof global.process === 'undefined') return { type: type, value: datasource }; }), }; + // Add pre-defined prefixes to query var prefixesString = ''; if (this.options.queryFormat === 'sparql') { for (var prefix in this.options.prefixes) prefixesString += 'PREFIX ' + prefix + ': <' + this.options.prefixes[prefix] + '>\n'; } - let query = prefixesString + queryWithoutPrefixes; + let query = prefixesString + this.$queryTextsIndexed[this.options.queryFormat].val(); + + // Remove duplicate prefixes + const parsedQuery = new SparqlParser({ sparqlStar: true }).parse(query); + const generatedQuery = new SparqlGenerator({}).stringify(parsedQuery); + this._queryWorker.postMessage({ type: 'query', - query: query, + query: generatedQuery, context: context, resultsToTree: this.options.resultsToTree, }); diff --git a/yarn.lock b/yarn.lock index c1df9ce2..b6164986 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8371,7 +8371,18 @@ sparqljs@^3.7.1: dependencies: rdf-data-factory "^1.1.2" +<<<<<<< HEAD sparqljson-parse@^2.0.0: +======= +sparqljs@^3.7.3: + version "3.7.3" + resolved "https://registry.yarnpkg.com/sparqljs/-/sparqljs-3.7.3.tgz#075821d51ef4954284e36569503fe5558cfb71b0" + integrity sha512-FQfHUhfwn5PD9WH6xPU7DhFfXMgqK/XoDrYDVxz/grhw66Il0OjRg3JBgwuEvwHnQt7oSTiKWEiCZCPNaUbqgg== + dependencies: + rdf-data-factory "^1.1.2" + +sparqljson-parse@^2.0.0, sparqljson-parse@^2.2.0: +>>>>>>> 4b2295e (Implement deduplicating via sparql.js) version "2.2.0" resolved "https://registry.yarnpkg.com/sparqljson-parse/-/sparqljson-parse-2.2.0.tgz#58c788e896f7d2c0d3079452d8812943049d4a7e" integrity sha512-2TfvNvUsaJyWfCrq3ExdDdbF9LBLzIUCricg+D1YCYbbmyTzscgCtRk4KcIyJF178DtfCt4BkKzbKl8IXMHp8w== From 14a83c95ecf0d3ec4c9ce2e1873513d782e8c2c0 Mon Sep 17 00:00:00 2001 From: Simon Van Braeckel Date: Fri, 11 Oct 2024 14:59:57 +0200 Subject: [PATCH 3/4] Only deduplicate sparql queries --- src/ldf-client-ui.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/ldf-client-ui.js b/src/ldf-client-ui.js index b9f51ef2..1ed26d50 100644 --- a/src/ldf-client-ui.js +++ b/src/ldf-client-ui.js @@ -727,21 +727,17 @@ if (typeof global.process === 'undefined') return { type: type, value: datasource }; }), }; - // Add pre-defined prefixes to query - var prefixesString = ''; + + let query = this.$queryTextsIndexed[this.options.queryFormat].val(); if (this.options.queryFormat === 'sparql') { - for (var prefix in this.options.prefixes) - prefixesString += 'PREFIX ' + prefix + ': <' + this.options.prefixes[prefix] + '>\n'; + // Add pre-defined prefixes to query and remove duplicates + const parsedQuery = new SparqlParser({ prefixes:this.options.prefixes, sparqlStar: true }).parse(query); + query = new SparqlGenerator({}).stringify(parsedQuery); } - let query = prefixesString + this.$queryTextsIndexed[this.options.queryFormat].val(); - - // Remove duplicate prefixes - const parsedQuery = new SparqlParser({ sparqlStar: true }).parse(query); - const generatedQuery = new SparqlGenerator({}).stringify(parsedQuery); this._queryWorker.postMessage({ type: 'query', - query: generatedQuery, + query: query, context: context, resultsToTree: this.options.resultsToTree, }); From 5432f572d89baa7df5e40849dae03174331861cd Mon Sep 17 00:00:00 2001 From: Simon Van Braeckel Date: Fri, 18 Oct 2024 02:15:48 +0200 Subject: [PATCH 4/4] Update yarn.lock --- yarn.lock | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/yarn.lock b/yarn.lock index b6164986..9f1fcce7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -8357,7 +8357,7 @@ sparqlalgebrajs@^4.3.7, sparqlalgebrajs@^4.3.8: rdf-terms "^1.10.0" sparqljs "^3.7.1" -sparqljs@^3.0.0: +sparqljs@^3.0.0, sparqljs@^3.7.3: version "3.7.3" resolved "https://registry.yarnpkg.com/sparqljs/-/sparqljs-3.7.3.tgz#075821d51ef4954284e36569503fe5558cfb71b0" integrity sha512-FQfHUhfwn5PD9WH6xPU7DhFfXMgqK/XoDrYDVxz/grhw66Il0OjRg3JBgwuEvwHnQt7oSTiKWEiCZCPNaUbqgg== @@ -8371,18 +8371,7 @@ sparqljs@^3.7.1: dependencies: rdf-data-factory "^1.1.2" -<<<<<<< HEAD sparqljson-parse@^2.0.0: -======= -sparqljs@^3.7.3: - version "3.7.3" - resolved "https://registry.yarnpkg.com/sparqljs/-/sparqljs-3.7.3.tgz#075821d51ef4954284e36569503fe5558cfb71b0" - integrity sha512-FQfHUhfwn5PD9WH6xPU7DhFfXMgqK/XoDrYDVxz/grhw66Il0OjRg3JBgwuEvwHnQt7oSTiKWEiCZCPNaUbqgg== - dependencies: - rdf-data-factory "^1.1.2" - -sparqljson-parse@^2.0.0, sparqljson-parse@^2.2.0: ->>>>>>> 4b2295e (Implement deduplicating via sparql.js) version "2.2.0" resolved "https://registry.yarnpkg.com/sparqljson-parse/-/sparqljson-parse-2.2.0.tgz#58c788e896f7d2c0d3079452d8812943049d4a7e" integrity sha512-2TfvNvUsaJyWfCrq3ExdDdbF9LBLzIUCricg+D1YCYbbmyTzscgCtRk4KcIyJF178DtfCt4BkKzbKl8IXMHp8w==