Skip to content

Commit

Permalink
Improved SPARQL query generation from JSON: fixed two issues
Browse files Browse the repository at this point in the history
  • Loading branch information
BouzelatTarek committed Nov 28, 2024
1 parent 35ad5f9 commit f05befa
Show file tree
Hide file tree
Showing 7 changed files with 530 additions and 275 deletions.
12 changes: 6 additions & 6 deletions dev-page/form-page.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<a href="#" id="switch-language">switch to en</a>
<div class="col-md-4">
<!-- Sparnatural Form -->
<!-- <sparnatural-form
<sparnatural-form
src="form-configs/dbpedia/dbpediafr-config.ttl"
lang="en"
defaultLang="fr"
Expand All @@ -79,9 +79,9 @@
form="form-configs/dbpedia/form.json"
prefix="skos:http://www.w3.org/2004/02/skos/core# rico:https://www.ica.org/standards/RiC/ontology#"
query="form-configs/dbpedia/query.json"
></sparnatural-form>-->
></sparnatural-form>

<sparnatural-form
<!-- <sparnatural-form
src="https://xls2rdf.sparna.fr/rest/convert?url=https%3A%2F%2Fdocs.google.com%2Fspreadsheets%2Fd%2F1WrC2r7yDQCP5AKhu6SAK8Ba_QshzFKmm%2Fexport%3Fformat%3Dxlsx&noPostProcessings=true https://xls2rdf.sparna.fr/rest/convert?url=https%3A%2F%2Fdocs.google.com%2Fspreadsheets%2Fd%2F1UWfCJmgvgwJ0yOs6Ox_qzn47heaiQ0cx%2Fexport%3Fformat%3Dxlsx&noPostProcessings=true form-configs/sapa/collections-shacl.ttl form-configs/sapa/instantiation_statistics_sparnatural.ttl"
lang="en"
defaultLang="en"
Expand All @@ -90,7 +90,7 @@
form="form-configs/sapa/form.json"
prefix="skos:http://www.w3.org/2004/02/skos/core# rico:https://www.ica.org/standards/RiC/ontology#"
query="form-configs/sapa/query.json"
></sparnatural-form>
></sparnatural-form>-->
</div>

<!-- Éditeur SPARQL à droite -->
Expand All @@ -101,8 +101,8 @@
Les requêtes sont envoyées à
<a
id="endpoint"
href="https://proxy.sparnatural.eu/sparql-proxy/sparql?endpoint=https%3A%2F%2Fwww.performing-arts.ch%2Fsparql"
>https://proxy.sparnatural.eu/sparql-proxy/sparql?endpoint=https%3A%2F%2Fwww.performing-arts.ch%2Fsparql</a
href="https://proxy.sparnatural.eu/sparql-proxy/sparql?endpoint=http%3A%2F%2Ffr.dbpedia.org%2Fsparql"
>https://proxy.sparnatural.eu/sparql-proxy/sparql?endpoint=http%3A%2F%2Ffr.dbpedia.org%2Fsparql</a
>
</span>
</div>
Expand Down
55 changes: 31 additions & 24 deletions src/sparnatural/components/SparnaturalComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ class SparnaturalComponent extends HTMLComponent {
}

render(): this {
this.#initLang()
this.#initLang();

let afterSpecificationLoaded = (sp: ISparnaturalSpecification) => {
this.specProvider = sp;
this.actionStore = new ActionStore(this, this.specProvider);
this.BgWrapper = new BgWrapper(this, this.specProvider).render();
// display the submit button only if a callback was provided
if(getSettings().submitButton) {
if (getSettings().submitButton) {
this.submitSection = new SubmitSection(this).render();
}
console.log("submit section", this.submitSection);
Expand All @@ -47,29 +47,33 @@ class SparnaturalComponent extends HTMLComponent {
this.html[0].dispatchEvent(
new CustomEvent("redrawBackgroundAndLinks", { bubbles: true })
);
this.html.append(this.filter);
console.log("Found languages in configuration : "+this.specProvider.getLanguages());

this.html[0].dispatchEvent(new CustomEvent(SparnaturalElement.EVENT_INIT, {
bubbles: true,
detail: {
sparnatural: this
}
}));
this.html.append(this.filter);
console.log(
"Found languages in configuration : " + this.specProvider.getLanguages()
);

this.html[0].dispatchEvent(
new CustomEvent(SparnaturalElement.EVENT_INIT, {
bubbles: true,
detail: {
sparnatural: this,
},
})
);
};

// chain catalog loading and spec loading
let afterCatalogLoaded = (catalog: Catalog|undefined) => {
let afterCatalogLoaded = (catalog: Catalog | undefined) => {
this.catalog = catalog;
this.initSpecificationProvider(afterSpecificationLoaded);
}
};

this.#initCatalog(afterCatalogLoaded);

return this;
}

#initCatalog(callback:((catalog:Catalog|undefined)=>void)) {
#initCatalog(callback: (catalog: Catalog | undefined) => void) {
let settings = getSettings();
let me = this;
if (settings.catalog) {
Expand All @@ -80,36 +84,39 @@ class SparnaturalComponent extends HTMLComponent {
"Sparnatural - unable to load catalog file : " + settings.catalog
);
callback(undefined);
})
});
} else {
// no catalog provided
callback(undefined);
}
}

initSpecificationProvider(callback:((sp:ISparnaturalSpecification)=>void)) {
initSpecificationProvider(callback: (sp: ISparnaturalSpecification) => void) {
let settings = getSettings();
let specProviderFactory = new SparnaturalSpecificationFactory();
// load only the statistics of the selected endpoints
specProviderFactory.build(
settings.src,
settings.language,
(this.catalog)?this.catalog.extractSubCatalog(settings.endpoints):undefined,
(sp: any) => {
this.catalog
? this.catalog.extractSubCatalog(settings.endpoints)
: undefined,
(sp: any) => {
// call the call back when done
callback(sp);
});
}
);
}

// method is exposed from the HTMLElement
enablePlayBtn = () =>{
enablePlayBtn = () => {
this.submitSection.playBtn.removeLoading();
}
};

// method is exposed from the HTMLElement
disablePlayBtn = () => {
this.submitSection.playBtn.disable();
}
};

setQuiet(quiet: boolean) {
this.actionStore.quiet = quiet;
Expand Down
66 changes: 41 additions & 25 deletions src/sparnatural/generators/sparql/SparqlGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,54 +150,70 @@ export default class SparqlGenerator {
}

#varsToRDFJS(variables: Array<DraggableComponentState>): Variable[] {
let variablesArray:Variable[][] = variables.map((v) => {
if(v.aggregateFunction) {
return [SparqlFactory.buildAggregateFunctionExpression(
v.aggregateFunction,
factory.variable(v.originalVariable.variable),
factory.variable(v.selectedVariable.variable)
)];
let variablesArray: Variable[][] = 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:ISpecificationProperty = undefined;
let specProperty: ISpecificationProperty = undefined;
this.sparnatural.BgWrapper.componentsList.rootGroupWrapper.traversePreOrder(
(grpWrapper: GroupWrapper) => {
if(grpWrapper.CriteriaGroup.EndClassGroup.endClassVal.variable == v.selectedVariable.variable) {
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();
let propertyType =
grpWrapper.CriteriaGroup.ObjectPropertyGroup.getTypeSelected();
console.log("propertyType: ", propertyType);
specProperty = this.specProvider.getProperty(propertyType);
console.log("specProperty: ", specProperty);
}
}
); // 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) {
return [factory.variable(v.selectedVariable.variable)];
}

if(specProperty.getBeginDateProperty() && specProperty.getEndDateProperty()) {
let result:Variable[] = []
if(specProperty.getBeginDateProperty()) {
result.push(factory.variable(v.selectedVariable.variable+"_begin"));
if (
specProperty.getBeginDateProperty() &&
specProperty.getEndDateProperty()
) {
let result: Variable[] = [];
if (specProperty.getBeginDateProperty()) {
result.push(
factory.variable(v.selectedVariable.variable + "_begin")
);
}
if(specProperty.getEndDateProperty()) {
result.push(factory.variable(v.selectedVariable.variable+"_end"));
if (specProperty.getEndDateProperty()) {
result.push(factory.variable(v.selectedVariable.variable + "_end"));
}
if(specProperty.getExactDateProperty()) {
result.push(factory.variable(v.selectedVariable.variable+"_exact"));
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) ];
return [factory.variable(v.selectedVariable.variable)];
}
}
}
});

let finalResult:Variable[] = [];
variablesArray.forEach((varArray:Variable[]) => {finalResult.push(...varArray)});
let finalResult: Variable[] = [];
variablesArray.forEach((varArray: Variable[]) => {
finalResult.push(...varArray);
});
return finalResult;

}

// It will be ordered by the Provided variable
Expand Down
Loading

0 comments on commit f05befa

Please sign in to comment.