Skip to content

Commit

Permalink
[#184] Add implementation to executeSPINQueryCustomJavaFunction test
Browse files Browse the repository at this point in the history
x
  • Loading branch information
palagdan authored and blcham committed Aug 15, 2024
1 parent a1eec60 commit 299bb95
Showing 1 changed file with 87 additions and 29 deletions.
116 changes: 87 additions & 29 deletions s-pipes-core/src/test/java/cz/cvut/spin/SpinIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,75 @@
package cz.cvut.spin;
package cz.cvut.spin;

import cz.cvut.spipes.engine.PipelineFactory;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.query.QuerySolutionMap;
import org.apache.jena.query.*;
import org.apache.jena.rdf.model.*;
import org.apache.jena.util.FileUtils;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import org.topbraid.spin.model.SPINFactory;
import org.topbraid.spin.system.SPINModuleRegistry;
import org.topbraid.spin.util.SPINExpressions;
import org.topbraid.spin.vocabulary.SP;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class SpinIntegrationTest {


@Test
public void executeSPINExpressionWithCustomSpinFunction() throws UnsupportedEncodingException {
public void executeCustomSPINRDFFunctionWithinQuery() {
// load custom function definition from RDF
Model funcDefModel = getCustomSPINRDFFunctionModel();

// load custom function definition
Model funcDefModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
// Model funcDefModel = ModelFactory.createDefaultModel(); // TODO this does not work
// register custom function
//SPINModuleRegistry.get().init();
SPINModuleRegistry.get().registerAll(funcDefModel, null);

final InputStream funcDefIs = this.getClass().getResourceAsStream("/spin/spin-function.spin.ttl");
String repositoryUrl = "http://repository.org";
String graphId = "http://graphid.org";

funcDefModel.read(funcDefIs, null, FileUtils.langTurtle);
String queryString = String.format("""
PREFIX kbss-spif: <http://onto.fel.cvut.cz/ontologies/lib/spin-function/>
SELECT ?sparqlServiceUrl
WHERE {
BIND(kbss-spif:create-sparql-service-url(
"%s",
"%s"
) AS ?sparqlServiceUrl)
}
""", repositoryUrl, graphId);

Model model = ModelFactory.createDefaultModel();

Query query = QueryFactory.create(queryString);

QueryExecution qexec = QueryExecutionFactory.create(query, model);
ResultSet results = qexec.execSelect();

assertTrue(results.hasNext(), "No results found");

QuerySolution soln = results.nextSolution();
assertEquals(
soln.getResource("sparqlServiceUrl").getURI(),
constructServiceUrl(repositoryUrl, graphId)
);

}

@Test
public void executeSPINExpressionWithCustomSPINRDFFunction() {
// load custom function definition from RDF
Model funcDefModel = getCustomSPINRDFFunctionModel();

// register custom function
//SPINModuleRegistry.get().init();
SPINModuleRegistry.get().registerAll(funcDefModel, null);


// load custom function call
Model funcCallModel = ModelFactory.createDefaultModel();

Expand All @@ -50,32 +84,56 @@ public void executeSPINExpressionWithCustomSpinFunction() throws UnsupportedEnco
// evaluate SPIN expression
QuerySolutionMap bindings = new QuerySolutionMap();
String repositoryUrl = "http://repository.org";
String reportGraphId = "http://graphid.org";
bindings.add("repositoryUrl", ResourceFactory.createPlainLiteral(repositoryUrl));
bindings.add("reportGraphId", ResourceFactory.createPlainLiteral(reportGraphId));

String graphId = "http://graphid.org";
bindings.add("sparqlEndpoint", ResourceFactory.createPlainLiteral(repositoryUrl));
bindings.add("defaultGraphUri", ResourceFactory.createPlainLiteral(graphId));

RDFNode node = SPINExpressions.evaluate(callExpr, callExpr.getModel(), bindings); //TODO resource.getModel() should be part o context


assertEquals(node.toString(), repositoryUrl + "?default-graph-uri=" + URLEncoder.encode(reportGraphId, StandardCharsets.UTF_8) );
assertEquals(node.toString(), constructServiceUrl(repositoryUrl, graphId));
}

@Test
public void executeQueryWithCustomJavaFunction() {
SPINModuleRegistry.get().init(); //TODO is it needed ?
public void executeSPINQueryWithCustomJavaFunction() {

// make a query with AddDays function
String query = """
@prefix kbss-timef: <http://onto.fel.cvut.cz/ontologies/lib/function/time/> .
SELECT ?nextDay
WHERE {
BIND(kbss-timef:add-days("2022-01-01"^^xsd:date, 1) as ?nextDay)
}
""";
PipelineFactory.registerFunctionsOnClassPath();

String queryString = """
PREFIX kbss-timef: <http://onto.fel.cvut.cz/ontologies/lib/function/time/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?nextDay
WHERE {
BIND(kbss-timef:add-days("2022-01-01"^^xsd:date, 1) AS ?nextDay)
}
""";
Model model = ModelFactory.createDefaultModel();

Query query = QueryFactory.create(queryString);

QueryExecution qexec = QueryExecutionFactory.create(query, model);
ResultSet results = qexec.execSelect();

assertTrue(results.hasNext(), "No results found");

QuerySolution soln = results.nextSolution();
assertEquals(soln.getLiteral("nextDay").getString(), "2022-01-02");
}

@NotNull
private Model getCustomSPINRDFFunctionModel() {
// load custom function definition
Model funcDefModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
// Model funcDefModel = ModelFactory.createDefaultModel(); // TODO this does not work

// execute the query
final InputStream funcDefIs = this.getClass().getResourceAsStream("/spin/spin-function.spin.ttl");

funcDefModel.read(funcDefIs, null, FileUtils.langTurtle);

return funcDefModel;
}

// check the result
@NotNull
private String constructServiceUrl(String repositoryUrl, String graphId) {
return String.format("%s?default-graph-uri=%s", repositoryUrl, URLEncoder.encode(graphId, StandardCharsets.UTF_8));
}
}

0 comments on commit 299bb95

Please sign in to comment.