diff options
| author | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-08-11 12:40:35 +0100 |
|---|---|---|
| committer | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-08-11 12:40:35 +0100 |
| commit | e1e0bf5c2d634c4b2e5350614625996e843e2e9a (patch) | |
| tree | d2fd666ee214838a505bb865c7a6fe4e95842fd5 /src/main/scala | |
| parent | f3b29090e6139b90b4bda0e46b5d3bb2daad65a8 (diff) | |
| download | RSAComb-e1e0bf5c2d634c4b2e5350614625996e843e2e9a.tar.gz RSAComb-e1e0bf5c2d634c4b2e5350614625996e843e2e9a.zip | |
Add code to communicate with a local RDFox instance
Diffstat (limited to 'src/main/scala')
| -rw-r--r-- | src/main/scala/rsacomb/RDFox.scala | 24 | ||||
| -rw-r--r-- | src/main/scala/rsacomb/RSAOntology.scala | 40 |
2 files changed, 61 insertions, 3 deletions
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 @@ | |||
| 1 | package rsacomb | ||
| 2 | |||
| 3 | /* Java imports */ | ||
| 4 | import java.util.HashMap | ||
| 5 | import tech.oxfordsemantic.jrdfox.client.{ConnectionFactory,ServerConnection,DataStoreConnection} | ||
| 6 | |||
| 7 | object RDFox { | ||
| 8 | |||
| 9 | def openConnection(dataStore: String): (ServerConnection,DataStoreConnection) = { | ||
| 10 | val serverUrl = "rdfox:local" | ||
| 11 | val role = "" | ||
| 12 | val password = "" | ||
| 13 | val server = ConnectionFactory.newServerConnection(serverUrl, role, password) | ||
| 14 | server.createDataStore(dataStore,"seq",new HashMap()) | ||
| 15 | val data = server.newDataStoreConnection(dataStore) | ||
| 16 | (server,data) | ||
| 17 | } | ||
| 18 | |||
| 19 | def closeConnection(server: ServerConnection, data: DataStoreConnection): Unit = { | ||
| 20 | server.close(); | ||
| 21 | data.close(); | ||
| 22 | } | ||
| 23 | |||
| 24 | } // 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 @@ | |||
| 1 | package rsacomb | 1 | package rsacomb |
| 2 | 2 | ||
| 3 | /* Java imports */ | 3 | /* Java imports */ |
| 4 | import java.util.HashMap | ||
| 4 | import java.util.stream.{Collectors,Stream} | 5 | import java.util.stream.{Collectors,Stream} |
| 5 | 6 | ||
| 6 | import org.semanticweb.owlapi.model.OWLOntology | 7 | import org.semanticweb.owlapi.model.OWLOntology |
| @@ -8,13 +9,17 @@ import org.semanticweb.owlapi.model.OWLObjectPropertyExpression | |||
| 8 | import org.semanticweb.owlapi.model.parameters.Imports | 9 | import org.semanticweb.owlapi.model.parameters.Imports |
| 9 | import org.semanticweb.owlapi.reasoner.structural.StructuralReasonerFactory | 10 | import org.semanticweb.owlapi.reasoner.structural.StructuralReasonerFactory |
| 10 | 11 | ||
| 12 | import tech.oxfordsemantic.jrdfox.Prefixes | ||
| 11 | import tech.oxfordsemantic.jrdfox.logic.Variable | 13 | import tech.oxfordsemantic.jrdfox.logic.Variable |
| 14 | import tech.oxfordsemantic.jrdfox.client.UpdateType | ||
| 15 | |||
| 12 | 16 | ||
| 13 | /* Scala imports */ | 17 | /* Scala imports */ |
| 14 | import scala.collection.JavaConverters._ | 18 | import scala.collection.JavaConverters._ |
| 15 | 19 | ||
| 16 | /* Debug only */ | 20 | /* Debug only */ |
| 17 | import org.semanticweb.owlapi.dlsyntax.renderer.DLSyntaxObjectRenderer | 21 | import org.semanticweb.owlapi.dlsyntax.renderer.DLSyntaxObjectRenderer |
| 22 | import java.io.OutputStream | ||
| 18 | 23 | ||
| 19 | /* Wrapper trait for the implicit class `RSAOntology`. | 24 | /* Wrapper trait for the implicit class `RSAOntology`. |
| 20 | */ | 25 | */ |
| @@ -48,12 +53,41 @@ trait RSAOntology { | |||
| 48 | tbox.foreach(x => println(renderer.render(x))) | 53 | tbox.foreach(x => println(renderer.render(x))) |
| 49 | 54 | ||
| 50 | /* Ontology convertion into LP rules */ | 55 | /* Ontology convertion into LP rules */ |
| 51 | println("\nLP rules:") | 56 | val datalog = for { |
| 52 | for { | ||
| 53 | axiom <- tbox | 57 | axiom <- tbox |
| 54 | visitor = new RDFoxAxiomConverter(Variable.create("x"), SkolemStrategy.ConstantRSA(axiom.toString), unsafe) | 58 | visitor = new RDFoxAxiomConverter(Variable.create("x"), SkolemStrategy.ConstantRSA(axiom.toString), unsafe) |
| 55 | rule <- axiom.accept(visitor) | 59 | rule <- axiom.accept(visitor) |
| 56 | } yield println(rule) | 60 | } yield rule |
| 61 | |||
| 62 | val prefixes = new Prefixes() | ||
| 63 | prefixes.declarePrefix(":", "http://example.com/rsa_example.owl#") | ||
| 64 | |||
| 65 | // Open connection with RDFox | ||
| 66 | val (server,data) = RDFox.openConnection("RSACheck") | ||
| 67 | // Add Data (hardcoded for now) | ||
| 68 | data.importData(UpdateType.ADDITION, prefixes,":a a :A .") | ||
| 69 | /* Add Datalog rules | ||
| 70 | * | ||
| 71 | * NOTE: | ||
| 72 | * - using the `addRules(...)` method in `DataStoreConnection` is not working as expected, complaining | ||
| 73 | * about missing TupleTable entries; | ||
| 74 | * - weirdly enough, the same error is returned when trying to pass the rules to the `importData` method, | ||
| 75 | * simply turning them into strings. It seems like the `toString` implementation of `Rule` uses parenthesis | ||
| 76 | * for predicate arguments (e.g., `<predicate>(?X,?Y)`) while the specification for the proprietary RDFox | ||
| 77 | * syntax uses squared brackets (e.g., `<preditate>[?X,?Y]`). | ||
| 78 | */ | ||
| 79 | data.importData( | ||
| 80 | UpdateType.ADDITION, | ||
| 81 | prefixes, | ||
| 82 | datalog.foldLeft("")((str,rule) => str ++ "\n" ++ rule.toString().replace("(", "[").replace(")","]")) | ||
| 83 | ) | ||
| 84 | |||
| 85 | // Retrieve all instances of PE | ||
| 86 | println("\nQuery results:") | ||
| 87 | data.evaluateQuery(prefixes,"SELECT ?X ?Y WHERE { ?X <internal:PE> ?Y }", new HashMap[String,String](), System.out, "text/csv"); | ||
| 88 | |||
| 89 | // Close connection to RDFox | ||
| 90 | RDFox.closeConnection(server,data) | ||
| 57 | 91 | ||
| 58 | /* DEBUG */ | 92 | /* DEBUG */ |
| 59 | true | 93 | true |
