sparnatural
Version:
Visual client-side SPARQL query builder and knowledge graph exploration tool
74 lines • 3.76 kB
JavaScript
import { getSettings } from "../../../sparnatural/settings/defaultSettings";
import { SparnaturalJsonGenerator } from "../../generators/json/SparnaturalJsonGenerator";
import SparqlGenerator from "../../generators/sparql/SparqlGenerator";
import { Generator } from "sparqljs";
import { SparnaturalElement } from "../../../SparnaturalElement";
import { JsonSparqlTranslator } from "../../generators/sparql/fromjson/JsonSparqlTranslator";
export class QueryGenerator {
constructor(actionStore) {
this.actionStore = actionStore;
}
generateQuery() {
// if we are quiet, don't do anything
if (this.actionStore.quiet) {
return;
}
// if the query editor is empty, don't do anything
if (this.actionStore.sparnatural.isEmpty()) {
return;
}
// triggered when Sparnatural is submitted : generates output SPARQL query
let settings = getSettings();
let qryGen = new SparnaturalJsonGenerator(this.actionStore.sparnatural);
var jsonQuery = qryGen.generateQuery(this.actionStore.sparnatural.variableSection.listVariables(), this.actionStore.sparnatural.variableSection.getOrder(), settings.addDistinct, settings.limit);
if (jsonQuery != null) {
if (settings.debug) {
console.log("*** Sparnatural JSON Query ***");
console.dir(jsonQuery);
}
var writer = new SparqlGenerator(this.actionStore.sparnatural, this.actionStore.specProvider, settings.sparqlPrefixes, settings);
let selectQuery = writer.generateQuery(this.actionStore.sparnatural.variableSection.listVariables(), this.actionStore.sparnatural.variableSection.getOrder(), settings.addDistinct, settings.limit);
// debug rdfJsQuery
if (settings.debug) {
// prints the SPARQL generated from the writing of the JSON data structure
console.log("*** Sparnatural SPARQL Query OLD ***");
console.log(selectQuery);
}
var generator = new Generator();
var queryString = generator.stringify(selectQuery);
var sparqlFromJsonGenerator = new JsonSparqlTranslator(this.actionStore.specProvider, settings);
let selectQueryFromJson = sparqlFromJsonGenerator.generateQuery(jsonQuery);
if (settings.debug) {
// prints the SPARQL generated from the writing of the JSON data structure
console.log("*** Sparnatural SPARQL Query NEW ***");
console.log(selectQueryFromJson);
}
var queryStringFromJson = generator.stringify(selectQueryFromJson);
if (settings.debug) {
console.log("*** Sparnatural SPARQL Query from JSON ***");
console.dir(queryStringFromJson);
}
// fire the event
let payload = {
queryString: queryString,
queryJson: jsonQuery,
querySparqlJs: selectQuery,
queryStringFromJson: queryStringFromJson,
};
this.fireQueryUpdatedEvent(payload);
// re-enable submit button if it was disabled
// note that the submitSection may not be present in case submitButton = false in the attributes
this.actionStore.sparnatural.submitSection?.enableSubmit();
}
}
fireQueryUpdatedEvent(payload) {
// fire the event
this.actionStore.sparnatural.html[0].dispatchEvent(new CustomEvent(SparnaturalElement.EVENT_QUERY_UPDATED, {
bubbles: true,
detail: payload,
}));
}
}
export class QueryUpdatedPayload {
}
//# sourceMappingURL=GenerateQuery.js.map