UNPKG

sparnatural

Version:

Visual client-side SPARQL query builder and knowledge graph exploration tool

202 lines (201 loc) 9.6 kB
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter"); if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it"); return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver); }; var _JsonSparqlTranslator_instances, _JsonSparqlTranslator_addGroupBy, _JsonSparqlTranslator_needsGrouping, _JsonSparqlTranslator_createWhereClause, _JsonSparqlTranslator_varsToRDFJS1, _JsonSparqlTranslator_varsToRDFJS, _JsonSparqlTranslator_orderToRDFJS, _JsonSparqlTranslator_insertDefaultLabelVar; import { Order, } from "../../../SparnaturalQueryIfc"; import SparqlFactory from "../SparqlFactory"; import { DataFactory } from "rdf-data-factory"; import QueryWhereTranslator from "./QueryWhereTranslator"; const factory = new DataFactory(); /** * Generates a Sparql query in the SparqlJs structure from the JSON query format of Sparnatural */ export class JsonSparqlTranslator { constructor( // the Sparnatural configuration specProvider, // settings settings) { _JsonSparqlTranslator_instances.add(this); this.prefixes = {}; this.defaultLabelVars = []; this.specProvider = specProvider; this.settings = settings; } /** * @param jsonQuery the Sparnatural JSON query * @returns a SPARQL query translated from the Sparnatural JSON query structure */ generateQuery(jsonQuery) { this.jsonQuery = jsonQuery; const sparqlJsQuery = { queryType: "SELECT", distinct: jsonQuery.distinct, variables: __classPrivateFieldGet(this, _JsonSparqlTranslator_instances, "m", _JsonSparqlTranslator_varsToRDFJS).call(this, jsonQuery.variables), type: "query", where: __classPrivateFieldGet(this, _JsonSparqlTranslator_instances, "m", _JsonSparqlTranslator_createWhereClause).call(this), prefixes: this.prefixes, order: __classPrivateFieldGet(this, _JsonSparqlTranslator_instances, "m", _JsonSparqlTranslator_orderToRDFJS).call(this, this.jsonQuery.order, jsonQuery.variables[0]), // sets a limit if provided, otherwise leave to undefined limit: jsonQuery.limit && jsonQuery.limit > 0 ? jsonQuery.limit : undefined, }; // if the RdfJsQuery contains empty 'where' array, then the generator crashes. // create query with no triples if (sparqlJsQuery.where?.length === 0) { sparqlJsQuery.where = [ { type: "bgp", triples: [], }, ]; } // post processing for defaultlabel property if (this.defaultLabelVars.length > 0) { this.defaultLabelVars.forEach((defaultLabelVar) => __classPrivateFieldGet(this, _JsonSparqlTranslator_instances, "m", _JsonSparqlTranslator_insertDefaultLabelVar).call(this, sparqlJsQuery, defaultLabelVar)); } // don't set an order if there is no expression for it if (!sparqlJsQuery?.order || !sparqlJsQuery?.order[0]?.expression) delete sparqlJsQuery.order; // set a GROUP BY based on aggregation expression in the variables // add this after defaultLabel var have been inserted, and re-read them from the query sparqlJsQuery.group = __classPrivateFieldGet(this, _JsonSparqlTranslator_instances, "m", _JsonSparqlTranslator_addGroupBy).call(this, sparqlJsQuery.variables); return sparqlJsQuery; } } _JsonSparqlTranslator_instances = new WeakSet(), _JsonSparqlTranslator_addGroupBy = function _JsonSparqlTranslator_addGroupBy(variables) { if (__classPrivateFieldGet(this, _JsonSparqlTranslator_instances, "m", _JsonSparqlTranslator_needsGrouping).call(this, variables)) { let g = []; variables.forEach((v) => { if (!v.expression) { g.push({ expression: v, }); } }); return g; } else { // no aggregation, or only one column, grouping is undefined return undefined; } }, _JsonSparqlTranslator_needsGrouping = function _JsonSparqlTranslator_needsGrouping(variables) { return variables.find((v) => v.expression) && variables.length > 1 ? true : false; }, _JsonSparqlTranslator_createWhereClause = function _JsonSparqlTranslator_createWhereClause() { let whereBuilder = new QueryWhereTranslator(this.jsonQuery, this.specProvider, this.settings); whereBuilder.build(); this.defaultLabelVars = whereBuilder.getDefaultVars(); return whereBuilder.getResultPtrns(); /* const builder = new WhereBuilder( this.sparnatural.BgWrapper.componentsList.rootGroupWrapper, this.specProvider, this.typePredicate, false, false ) builder.build() this.defaultLabelVars = builder.getDefaultVars() if(builder.getExecutedAfterPtrns().length > 0) { // put all normal patterns in a subquery to garantee they are logically executed before // and their variables are bound in the rest of the query outside the subquery let subquery = SparqlFactory.buildSubQuery(builder.getResultPtrns()); // and the patterns to be executed after in the normal where clause return [subquery, ...builder.getExecutedAfterPtrns()]; } else { return builder.getResultPtrns() } */ }, _JsonSparqlTranslator_varsToRDFJS1 = function _JsonSparqlTranslator_varsToRDFJS1(variables) { return variables.map((v) => { // if it is an aggregated variable... if (v.expression) { let vExpression = v; return SparqlFactory.buildAggregateFunctionExpression(vExpression.expression.aggregation, factory.variable(vExpression.expression.expression.value), factory.variable(vExpression.variable.value)); } else { return factory.variable(v.value); } }); }, _JsonSparqlTranslator_varsToRDFJS = function _JsonSparqlTranslator_varsToRDFJS(variables) { let variablesArray = variables.map((v) => { if (v.expression) { let vExpression = v; return [ SparqlFactory.buildAggregateFunctionExpression(vExpression.expression.aggregation, factory.variable(vExpression.expression.expression.value), factory.variable(vExpression.variable.value)), ]; } else { let specProperty = undefined; // Parcourir les branches pour trouver la correspondance uniquement avec l'objet (o) this.jsonQuery.branches.forEach((branch) => { if (branch.line.o === v.value) { //console.log("Matching branch found for object:", branch); specProperty = this.specProvider.getProperty(branch.line.p); } }); //console.log("specProperty", specProperty); if (!specProperty) { // Si aucune propriété n'est trouvée, retourne une variable simple return [factory.variable(v.value)]; } if (specProperty.getBeginDateProperty() && specProperty.getEndDateProperty()) { let result = []; if (specProperty.getBeginDateProperty()) { result.push(factory.variable(v.value + "_begin")); } if (specProperty.getEndDateProperty()) { result.push(factory.variable(v.value + "_end")); } if (specProperty.getExactDateProperty()) { result.push(factory.variable(v.value + "_exact")); } return result; } else { return [factory.variable(v.value)]; } } }); // Aplatissement du tableau de tableaux en un tableau simple let finalResult = []; variablesArray.forEach((varArray) => { finalResult.push(...varArray); }); return finalResult; }, _JsonSparqlTranslator_orderToRDFJS = function _JsonSparqlTranslator_orderToRDFJS(order, variable) { if (order == Order.DESC || order == Order.ASC) { let varName = variable.expression ? variable.variable.value : variable.value; return [ { expression: factory.variable(varName), descending: order == Order.DESC ? true : false, }, ]; } else { // no order return null; } }, _JsonSparqlTranslator_insertDefaultLabelVar = function _JsonSparqlTranslator_insertDefaultLabelVar(sparqlQuery, defaultLabelVar) { // reconstruct the original var name by removing "_label" suffix var originalVar = defaultLabelVar.value.substring(0, defaultLabelVar.value.length - "_label".length); //console.log("SpraqlQuery variables", sparqlQuery.variables); for (var i = 0; i < sparqlQuery.variables.length; i++) { // find variable with the original name if (sparqlQuery.variables[i].value == originalVar) { // insert the default label var after this one sparqlQuery.variables.splice(i + 1, 0, defaultLabelVar); // don't forget, otherwise infinite loop break; } } }; //# sourceMappingURL=JsonSparqlTranslator.js.map