From e1e0bf5c2d634c4b2e5350614625996e843e2e9a Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Tue, 11 Aug 2020 12:40:35 +0100 Subject: Add code to communicate with a local RDFox instance --- src/main/scala/rsacomb/RDFox.scala | 24 +++++++++++++++++++ src/main/scala/rsacomb/RSAOntology.scala | 40 +++++++++++++++++++++++++++++--- 2 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 src/main/scala/rsacomb/RDFox.scala (limited to 'src') diff --git a/src/main/scala/rsacomb/RDFox.scala b/src/main/scala/rsacomb/RDFox.scala new file mode 100644 index 0000000..a263091 --- /dev/null +++ b/src/main/scala/rsacomb/RDFox.scala @@ -0,0 +1,24 @@ +package rsacomb + +/* Java imports */ +import java.util.HashMap +import tech.oxfordsemantic.jrdfox.client.{ConnectionFactory,ServerConnection,DataStoreConnection} + +object RDFox { + + def openConnection(dataStore: String): (ServerConnection,DataStoreConnection) = { + val serverUrl = "rdfox:local" + val role = "" + val password = "" + val server = ConnectionFactory.newServerConnection(serverUrl, role, password) + server.createDataStore(dataStore,"seq",new HashMap()) + val data = server.newDataStoreConnection(dataStore) + (server,data) + } + + def closeConnection(server: ServerConnection, data: DataStoreConnection): Unit = { + server.close(); + data.close(); + } + +} // object RDFox \ No newline at end of file diff --git a/src/main/scala/rsacomb/RSAOntology.scala b/src/main/scala/rsacomb/RSAOntology.scala index ebe1591..92be118 100644 --- a/src/main/scala/rsacomb/RSAOntology.scala +++ b/src/main/scala/rsacomb/RSAOntology.scala @@ -1,6 +1,7 @@ package rsacomb /* Java imports */ +import java.util.HashMap import java.util.stream.{Collectors,Stream} import org.semanticweb.owlapi.model.OWLOntology @@ -8,13 +9,17 @@ import org.semanticweb.owlapi.model.OWLObjectPropertyExpression import org.semanticweb.owlapi.model.parameters.Imports import org.semanticweb.owlapi.reasoner.structural.StructuralReasonerFactory +import tech.oxfordsemantic.jrdfox.Prefixes import tech.oxfordsemantic.jrdfox.logic.Variable +import tech.oxfordsemantic.jrdfox.client.UpdateType + /* Scala imports */ import scala.collection.JavaConverters._ /* Debug only */ import org.semanticweb.owlapi.dlsyntax.renderer.DLSyntaxObjectRenderer +import java.io.OutputStream /* Wrapper trait for the implicit class `RSAOntology`. */ @@ -48,12 +53,41 @@ trait RSAOntology { tbox.foreach(x => println(renderer.render(x))) /* Ontology convertion into LP rules */ - println("\nLP rules:") - for { + val datalog = for { axiom <- tbox visitor = new RDFoxAxiomConverter(Variable.create("x"), SkolemStrategy.ConstantRSA(axiom.toString), unsafe) rule <- axiom.accept(visitor) - } yield println(rule) + } yield rule + + val prefixes = new Prefixes() + prefixes.declarePrefix(":", "http://example.com/rsa_example.owl#") + + // Open connection with RDFox + val (server,data) = RDFox.openConnection("RSACheck") + // Add Data (hardcoded for now) + data.importData(UpdateType.ADDITION, prefixes,":a a :A .") + /* Add Datalog rules + * + * NOTE: + * - using the `addRules(...)` method in `DataStoreConnection` is not working as expected, complaining + * about missing TupleTable entries; + * - weirdly enough, the same error is returned when trying to pass the rules to the `importData` method, + * simply turning them into strings. It seems like the `toString` implementation of `Rule` uses parenthesis + * for predicate arguments (e.g., `(?X,?Y)`) while the specification for the proprietary RDFox + * syntax uses squared brackets (e.g., `[?X,?Y]`). + */ + data.importData( + UpdateType.ADDITION, + prefixes, + datalog.foldLeft("")((str,rule) => str ++ "\n" ++ rule.toString().replace("(", "[").replace(")","]")) + ) + + // Retrieve all instances of PE + println("\nQuery results:") + data.evaluateQuery(prefixes,"SELECT ?X ?Y WHERE { ?X ?Y }", new HashMap[String,String](), System.out, "text/csv"); + + // Close connection to RDFox + RDFox.closeConnection(server,data) /* DEBUG */ true -- cgit v1.2.3