From 40eb61acc9182dbb33cbdb1bb98b8638049d0918 Mon Sep 17 00:00:00 2001 From: Miroslav Blasko Date: Wed, 14 Aug 2024 23:18:48 +0200 Subject: [PATCH] [#184] Add SPINRDF custom function call from query test --- .../cz/cvut/spin/SpinIntegrationTest.java | 118 ++++++++++++------ 1 file changed, 82 insertions(+), 36 deletions(-) diff --git a/s-pipes-core/src/test/java/cz/cvut/spin/SpinIntegrationTest.java b/s-pipes-core/src/test/java/cz/cvut/spin/SpinIntegrationTest.java index bed44fc8..46ec0ebb 100644 --- a/s-pipes-core/src/test/java/cz/cvut/spin/SpinIntegrationTest.java +++ b/s-pipes-core/src/test/java/cz/cvut/spin/SpinIntegrationTest.java @@ -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: + 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(); @@ -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: @@ -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)); + } }