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 40eb61a
Showing 1 changed file with 82 additions and 36 deletions.
118 changes: 82 additions & 36 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();

import static org.junit.jupiter.api.Assertions.assertEquals;
// register custom function
//SPINModuleRegistry.get().init();
SPINModuleRegistry.get().registerAll(funcDefModel, null);

public class SpinIntegrationTest {
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);

Model model = ModelFactory.createDefaultModel();

@Test
public void executeSPINExpressionWithCustomSpinFunction() throws UnsupportedEncodingException {
Query query = QueryFactory.create(queryString);

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

// load custom function definition
Model funcDefModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
// Model funcDefModel = ModelFactory.createDefaultModel(); // TODO this does not work
assertTrue(results.hasNext(), "No results found");

final InputStream funcDefIs = this.getClass().getResourceAsStream("/spin/spin-function.spin.ttl");
QuerySolution soln = results.nextSolution();
assertEquals(
soln.getResource("sparqlServiceUrl").getURI(),
constructServiceUrl(repositoryUrl, graphId)
);

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 @@ -78,13 +107,30 @@ public void executeSPINQueryWithCustomJavaFunction() {

Query query = QueryFactory.create(queryString);


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

@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 40eb61a

Please sign in to comment.