From b721703c349cebd3ffe888d9644f2b85d5a8eeb7 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Fri, 6 Nov 2020 17:18:07 +0000 Subject: Rework canonical model computation This is a first attempt to avoid a bug triggered by the nature of the class RSAOntology and CanonicalModel. An OWLOntology is converted implicitly to an RSAOntology object whenever it is needed. From within the RSAOntology class we used to create a CanonicalModel object (and pass the underling OWLOntology object as a parameter). Inside CanonicalModel we require RSAOntology functionalities from the OWLOntology, triggering a new conversion into RSAOntology (that would compute a new CanonicalModel and so on in a loop). While declaring the CanonicalModel as lazy in RSAOntology could solve the problem, it does not fix the underlying issue of having a class strictly related to RSAOntology as a top level class. As a first attempt we moved CanonicalModel as an object inside RSAOntology. --- src/main/scala/rsacomb/Main.scala | 69 +++++++++++++++++++++++++++++++-------- 1 file changed, 56 insertions(+), 13 deletions(-) (limited to 'src/main/scala/rsacomb/Main.scala') diff --git a/src/main/scala/rsacomb/Main.scala b/src/main/scala/rsacomb/Main.scala index 830f1e0..64343f5 100644 --- a/src/main/scala/rsacomb/Main.scala +++ b/src/main/scala/rsacomb/Main.scala @@ -2,6 +2,10 @@ package rsacomb /* Java imports */ import java.io.File +import java.util.HashMap +import scala.collection.JavaConverters._ + +import tech.oxfordsemantic.jrdfox.client.UpdateType /* Local imports */ import rsacomb.RSA._ @@ -46,25 +50,64 @@ object RSAComb extends App { * case. */ - val ontology = RSA.loadOntology(ontoPath) + val ontology: RSAOntology = RSA.loadOntology(ontoPath) if (ontology.isRSA) { - /* Build canonical model */ - //val tboxCanon = rsa.canonicalModel() - - // DEBUG: print program to generate canonical model - { - ontology.canonicalModel.foreach(println) - } - /* Load query */ val query = RDFoxUtil.parseQuery( - "SELECT ?X WHERE {?X ?Y ?Z}" + """ + SELECT ?uno + WHERE { + ?uno a :D ; + :R ?due . + ?due :S ?tre . + ?tre a :D . + } + """ ) - val filter = query map { q => ontology.filteringProgram(q) } - - /* ... */ + /* Compute answers to query */ + query match { + case Some(query) => { + // Open connection to RDFox + val (server, data) = RDFoxUtil.openConnection("AnswerComputation") + + // Gather canonical model and filtering rules + val canon = ontology.canonicalModel + val filter = ontology.filteringProgram(query) + + // Import relevant data + data.importData(UpdateType.ADDITION, RSA.Prefixes, ":a a :A .") + data.addRules(canon.rules.asJava) + data.addRules(filter.rules.asJava) + + // Collect answers to query + for ((v, i) <- filter.variables.view.zipWithIndex) { + println(s"Variable $i:") + val query = s"SELECT ?X ?Y WHERE { ?X internal:Ans_$i ?Y }" + val cursor = + data.createCursor( + RSA.Prefixes, + query, + new HashMap[String, String]() + ); + var mul = cursor.open() + while (mul > 0) { + printf( + "Ans_%d(%s,%s)", + i, + cursor.getResource(0), + cursor.getResource(1) + ) + mul = cursor.advance() + } + } + + // Close connection to RDFox + RDFoxUtil.closeConnection(server, data) + } + case None => {} + } } } -- cgit v1.2.3