Skip to content

Commit

Permalink
[#184] Add SPINRDF custom function call from query test
Browse files Browse the repository at this point in the history
  • Loading branch information
blcham committed Aug 14, 2024
1 parent 651c154 commit b3dfe39
Showing 1 changed file with 81 additions and 35 deletions.
116 changes: 81 additions & 35 deletions s-pipes-core/src/test/java/cz/cvut/spin/SpinIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,74 @@
package cz.cvut.spin;

import cz.cvut.spipes.engine.PipelineFactory;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.query.*;
import org.apache.jena.rdf.model.*;
import org.apache.jena.util.FileUtils;
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 org.apache.jena.ontology.OntModelSpec;
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.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 {

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
@Test
public void executeCustomSPINRDFFunctionWithinQuery() {
// load custom function definition from RDF
Model funcDefModel = getCustomSPINRDFFunctionModel();

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

String repositoryUrl = "http://repository.org";
String graphId = "http://graphid.org";

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);

import static org.junit.jupiter.api.Assertions.assertEquals;
Model model = ModelFactory.createDefaultModel();

public class SpinIntegrationTest {
Query query = QueryFactory.create(queryString);

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

@Test
public void executeSPINExpressionWithCustomSpinFunction() throws UnsupportedEncodingException {
assertTrue(results.hasNext(), "No results found");

// load custom function definition
Model funcDefModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
// Model funcDefModel = ModelFactory.createDefaultModel(); // TODO this does not work
QuerySolution soln = results.nextSolution();
assertEquals(
soln.getResource("sparqlServiceUrl").getURI(),
constructServiceUrl(repositoryUrl, graphId)
);

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

funcDefModel.read(funcDefIs, null, FileUtils.langTurtle);
@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 @@ -51,20 +83,17 @@ public void executeSPINExpressionWithCustomSpinFunction() throws UnsupportedEnco
// evaluate SPIN expression
QuerySolutionMap bindings = new QuerySolutionMap();
String repositoryUrl = "http://repository.org";
String reportGraphId = "http://graphid.org";
String graphId = "http://graphid.org";
bindings.add("repositoryUrl", ResourceFactory.createPlainLiteral(repositoryUrl));
bindings.add("reportGraphId", ResourceFactory.createPlainLiteral(reportGraphId));

bindings.add("reportGraphId", 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 executeSPINQueryWithCustomJavaFunction() {
PipelineFactory pipelineFactory = new PipelineFactory();

String queryString = """
PREFIX kbss-timef: <http://onto.fel.cvut.cz/ontologies/lib/function/time/>
Expand All @@ -82,9 +111,26 @@ public void executeSPINQueryWithCustomJavaFunction() {
QueryExecution qexec = QueryExecutionFactory.create(query, model);
ResultSet results = qexec.execSelect();

if (results.hasNext()) {
QuerySolution soln = results.nextSolution();
assertEquals(soln.getLiteral("nextDay").getString(), "2022-01-02");
}
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

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

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

return funcDefModel;
}

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 b3dfe39

Please sign in to comment.