sparnatural
Version:
Visual client-side SPARQL query builder and knowledge graph exploration tool
169 lines • 8.6 kB
JavaScript
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 _SparqlGenerator_instances, _SparqlGenerator_addGroupBy, _SparqlGenerator_needsGrouping, _SparqlGenerator_createWhereClause, _SparqlGenerator_varsToRDFJS, _SparqlGenerator_orderToRDFJS, _SparqlGenerator_insertDefaultLabelVar;
import { Order } from "../../SparnaturalQueryIfc";
import SparnaturalComponent from "../../components/SparnaturalComponent";
import WhereBuilder from "./WhereBuilder";
import SparqlFactory from "./SparqlFactory";
import { DataFactory } from "rdf-data-factory";
const factory = new DataFactory();
/*
Reads out the UI and creates the and sparqljs pattern.
sparqljs pattern builds pattern structure on top of rdfjs datamodel. see:https://rdf.js.org/data-model-spec/
It goes recursively through all the grpWrappers and reads out their values.
*/
export default class SparqlGenerator {
constructor(sparnatural, specProvider, prefixes = {}, settings) {
_SparqlGenerator_instances.add(this);
this.prefixes = {};
this.defaultLabelVars = []; // see: #checkForDefaultLabel()
this.sparnatural = sparnatural;
this.specProvider = specProvider;
this.prefixes = prefixes;
this.settings = settings;
}
generateQuery(variables, order, distinct, limit) {
const sparqlJsQuery = {
queryType: "SELECT",
distinct: distinct,
variables: __classPrivateFieldGet(this, _SparqlGenerator_instances, "m", _SparqlGenerator_varsToRDFJS).call(this, variables),
type: "query",
where: __classPrivateFieldGet(this, _SparqlGenerator_instances, "m", _SparqlGenerator_createWhereClause).call(this),
prefixes: this.prefixes,
order: __classPrivateFieldGet(this, _SparqlGenerator_instances, "m", _SparqlGenerator_orderToRDFJS).call(this, order, factory.variable(variables[0].selectedVariable.variable)),
limit: limit && limit > 0 ? 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, _SparqlGenerator_instances, "m", _SparqlGenerator_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, _SparqlGenerator_instances, "m", _SparqlGenerator_addGroupBy).call(this, sparqlJsQuery.variables);
return sparqlJsQuery;
}
}
_SparqlGenerator_instances = new WeakSet(), _SparqlGenerator_addGroupBy = function _SparqlGenerator_addGroupBy(variables) {
if (__classPrivateFieldGet(this, _SparqlGenerator_instances, "m", _SparqlGenerator_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;
}
}, _SparqlGenerator_needsGrouping = function _SparqlGenerator_needsGrouping(variables) {
return variables.find((v) => v.expression) &&
variables.length > 1
? true
: false;
}, _SparqlGenerator_createWhereClause = function _SparqlGenerator_createWhereClause() {
if (this.sparnatural instanceof SparnaturalComponent) {
const builder = new WhereBuilder(this.sparnatural.BgWrapper.componentsList.rootGroupWrapper, this.specProvider, this.typePredicate, false, false, this.settings);
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();
}
}
}, _SparqlGenerator_varsToRDFJS = function _SparqlGenerator_varsToRDFJS(variables) {
let variablesArray = variables.map((v) => {
if (v.aggregateFunction) {
return [
SparqlFactory.buildAggregateFunctionExpression(v.aggregateFunction, factory.variable(v.originalVariable.variable), factory.variable(v.selectedVariable.variable)),
];
}
else {
// find where the variable comes from
let specProperty = undefined;
this.sparnatural.BgWrapper.componentsList.rootGroupWrapper.traversePreOrder((grpWrapper) => {
if (grpWrapper.CriteriaGroup.EndClassGroup.endClassVal.variable ==
v.selectedVariable.variable) {
// check if the spec tells us that begin date / end date / exact date propeties are used
let propertyType = grpWrapper.CriteriaGroup.ObjectPropertyGroup.getTypeSelected();
specProperty = this.specProvider.getProperty(propertyType);
}
}); // end traverse
// not found as an object, we cannot read the specification property, so return as normal
if (!specProperty) {
return [factory.variable(v.selectedVariable.variable)];
}
if (specProperty.getBeginDateProperty() &&
specProperty.getEndDateProperty()) {
let result = [];
if (specProperty.getBeginDateProperty()) {
result.push(factory.variable(v.selectedVariable.variable + "_begin"));
}
if (specProperty.getEndDateProperty()) {
result.push(factory.variable(v.selectedVariable.variable + "_end"));
}
if (specProperty.getExactDateProperty()) {
result.push(factory.variable(v.selectedVariable.variable + "_exact"));
}
return result;
}
else {
// no begin or date, return as normal
return [factory.variable(v.selectedVariable.variable)];
}
}
});
let finalResult = [];
variablesArray.forEach((varArray) => {
finalResult.push(...varArray);
});
return finalResult;
}, _SparqlGenerator_orderToRDFJS = function _SparqlGenerator_orderToRDFJS(order, variable) {
if (order == Order.DESC || order == Order.ASC) {
return [
{
expression: variable,
descending: order == Order.DESC ? true : false,
},
];
}
else {
// no order
return null;
}
}, _SparqlGenerator_insertDefaultLabelVar = function _SparqlGenerator_insertDefaultLabelVar(sparqlQuery, defaultLabelVar) {
var originalVar = defaultLabelVar.value.substring(0, defaultLabelVar.value.length - "_label".length);
for (var i = 0; i < sparqlQuery.variables.length; i++) {
if (sparqlQuery.variables[i].value == originalVar) {
sparqlQuery.variables.splice(i + 1, 0, defaultLabelVar);
// don't forget, otherwise infinite loop
break;
}
}
};
//# sourceMappingURL=SparqlGenerator.js.map