sparnatural
Version:
Visual client-side SPARQL query builder and knowledge graph exploration tool
104 lines • 4.92 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 _SparnaturalJsonGenerator_instances, _SparnaturalJsonGenerator_toVariables, _SparnaturalJsonGenerator_getBranch;
import { Order, } from "../../SparnaturalQueryIfc";
import { OptionTypes } from "../../components/builder-section/groupwrapper/criteriagroup/optionsgroup/OptionsGroup";
import { SelectAllValue } from "../../components/builder-section/groupwrapper/criteriagroup/edit-components/EditComponents";
import { DataFactory } from "rdf-data-factory";
const factory = new DataFactory();
/*
Reads out the UI and creates the internal JSON structure described here:
https://docs.sparnatural.eu/Query-JSON-format
*/
export class SparnaturalJsonGenerator {
constructor(sparnatural) {
_SparnaturalJsonGenerator_instances.add(this);
this.json = {
distinct: null,
variables: null,
order: null,
branches: null,
};
this.sparnatural = sparnatural;
}
generateQuery(variables, order, distinct, limit) {
this.json.distinct = distinct;
this.json.variables = __classPrivateFieldGet(this, _SparnaturalJsonGenerator_instances, "m", _SparnaturalJsonGenerator_toVariables).call(this, variables);
// don't output "noord", just set it to null
if (order != Order.NOORDER) {
this.json.order = order;
}
else {
this.json.order = null;
}
this.json.branches = __classPrivateFieldGet(this, _SparnaturalJsonGenerator_instances, "m", _SparnaturalJsonGenerator_getBranch).call(this, this.sparnatural.BgWrapper.componentsList.rootGroupWrapper, false);
this.json.limit = limit;
return this.json;
}
}
_SparnaturalJsonGenerator_instances = new WeakSet(), _SparnaturalJsonGenerator_toVariables = function _SparnaturalJsonGenerator_toVariables(variables) {
return variables.map((v) => {
if (v.aggregateFunction) {
return {
expression: {
type: "aggregate",
aggregation: v.aggregateFunction,
distinct: false,
expression: {
termType: "Variable",
value: v.originalVariable.variable,
},
},
variable: {
termType: "Variable",
value: v.selectedVariable.variable,
},
};
}
else {
return {
termType: "Variable",
value: v.selectedVariable.variable,
};
}
});
}, _SparnaturalJsonGenerator_getBranch = function _SparnaturalJsonGenerator_getBranch(grpWrapper, isInOption) {
let branches = [];
let CrtGrp = grpWrapper.CriteriaGroup;
let branch = {
line: {
s: CrtGrp.StartClassGroup.getVarName(),
p: CrtGrp.ObjectPropertyGroup.getTypeSelected(),
o: CrtGrp.EndClassGroup.getVarName(),
sType: CrtGrp.StartClassGroup.getTypeSelected(),
oType: CrtGrp.EndClassGroup.getTypeSelected(),
// extract only the value part, not the key
values: CrtGrp.endClassWidgetGroup
.getWidgetValues()
.filter((v) => !(v instanceof SelectAllValue))
.map((v) => {
return v.value;
}),
},
children: grpWrapper.whereChild
// either we are already in an option, or one was set at this level
? __classPrivateFieldGet(this, _SparnaturalJsonGenerator_instances, "m", _SparnaturalJsonGenerator_getBranch).call(this, grpWrapper.whereChild, isInOption || (grpWrapper.optionState != OptionTypes.NONE))
: []
};
// don't set the flags if they are not true
if (!isInOption && (grpWrapper.optionState == OptionTypes.OPTIONAL)) {
branch.optional = true;
}
if (!isInOption && (grpWrapper.optionState == OptionTypes.NOTEXISTS)) {
branch.notExists = true;
}
branches.push(branch);
if (grpWrapper.andSibling)
// pass the "isInOption" flag down
branches.push(...__classPrivateFieldGet(this, _SparnaturalJsonGenerator_instances, "m", _SparnaturalJsonGenerator_getBranch).call(this, grpWrapper.andSibling, isInOption)); // spread operatore since getBranch() returns an array
return branches;
};
//# sourceMappingURL=SparnaturalJsonGenerator.js.map