From 43be594ec3cd6f16e1ccfc31ce226f5266a7a19b Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Mon, 23 Nov 2020 13:39:55 +0000 Subject: Remove duplicate creation of RDFox datastores --- .../scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala | 27 ++++++++++++---------- .../ac/ox/cs/rsacomb/sparql/ConjunctiveQuery.scala | 16 ------------- .../uk/ac/ox/cs/rsacomb/util/RDFoxHelpers.scala | 9 ++++---- 3 files changed, 20 insertions(+), 32 deletions(-) diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala index 4693e04..24045de 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala @@ -59,6 +59,9 @@ object RSAOntology { // Counter used to implement a simple fresh variable generator private var counter = -1; + /** Name of the RDFox data store used for CQ answering */ + private val DataStore = "answer_computation" + def apply(ontology: OWLOntology): RSAOntology = new RSAOntology(ontology) def apply(ontology: File): RSAOntology = @@ -119,7 +122,10 @@ class RSAOntology(val ontology: OWLOntology) extends RSAAxiom { .flatMap(_.objectPropertyExpressionsInSignature) .distinct - // OWLAPI reasoner for same easier tasks + /** OWLAPI reasoner + * + * Used to carry out some preliminary reasoning task. + */ private val reasoner = (new StructuralReasonerFactory()).createReasoner(ontology) @@ -300,7 +306,7 @@ class RSAOntology(val ontology: OWLOntology) extends RSAAxiom { */ def ask(query: ConjunctiveQuery): ConjunctiveQueryAnswers = { import implicits.JavaCollections._ - val (server, data) = RDFoxHelpers.openConnection("AnswerComputation") + val (server, data) = RDFoxHelpers.openConnection(RSAOntology.DataStore) data.addRules(this.canonicalModel.rules) data.addRules(this.filteringProgram(query).rules) val answers = RDFoxHelpers @@ -317,9 +323,12 @@ class RSAOntology(val ontology: OWLOntology) extends RSAAxiom { answers } - /** Query the logic program used to compute answers to a given CQ. + /** Query the RDFox data store used for query answering. * - * This method has been introduced mostly for debugging purposes. + * @note This method does not add any facts or rules to the data + * store. It is most useful after the execution of a query using + * [[uk.ac.ox.cs.rsacomb.RSAOntology.ask RSAOntology.ask]]. + * @note This method has been introduced mostly for debugging purposes. * * @param cq a CQ used to compute the environment. * @param query query to be executed against the environment @@ -327,20 +336,14 @@ class RSAOntology(val ontology: OWLOntology) extends RSAAxiom { * an empty set. * @param opts additional options to RDFox. * @return a collection of answers to the input query. - * - * @todo this function currently fails because RDFox reports a - * datastore duplication. */ - def queryEnvironment( + def queryDataStore( cq: ConjunctiveQuery, query: String, prefixes: Prefixes = new Prefixes(), opts: ju.Map[String, String] = new ju.HashMap[String, String]() ): Option[Seq[Seq[Resource]]] = { - import implicits.JavaCollections._ - val (server, data) = RDFoxHelpers.openConnection("AnswerComputation") - data.addRules(this.canonicalModel.rules) - data.addRules(this.filteringProgram(cq).rules) + val (server, data) = RDFoxHelpers.openConnection(RSAOntology.DataStore) val answers = RDFoxHelpers.submitQuery(data, query, prefixes, opts) RDFoxHelpers.closeConnection(server, data) answers diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/sparql/ConjunctiveQuery.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/sparql/ConjunctiveQuery.scala index 4d397d8..e77a913 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/sparql/ConjunctiveQuery.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/sparql/ConjunctiveQuery.scala @@ -104,21 +104,5 @@ class ConjunctiveQuery( /** Returns the collection of bounded (existential) variables in the query. */ val bounded: Set[Variable] = variables &~ answer - /** Returns the answers to a query - * - * @param data data store against which the query is executed - * @param opts additional options passed to RDFox - * @return a new [[ConjunctiveQueryAnswers]] instance containing the - * collection of answers. - */ - def answers( - data: DataStoreConnection, - opts: JMap[String, String] = new JHashMap[String, String]() - ): ConjunctiveQueryAnswers = - new ConjunctiveQueryAnswers( - bcq, - RDFoxHelpers.submitSelectQuery(data, query, opts) - ) - override def toString(): String = query.toString } diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxHelpers.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxHelpers.scala index 369da7f..0f3a1cf 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxHelpers.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxHelpers.scala @@ -37,7 +37,7 @@ object RDFoxHelpers { * details on how to close an open connection. */ def openConnection( - dataStore: String, + datastore: String, opts: RDFoxOpts = RDFoxOpts() ): (ServerConnection, DataStoreConnection) = { val serverUrl = "rdfox:local" @@ -45,8 +45,9 @@ object RDFoxHelpers { val password = "" val server = ConnectionFactory.newServerConnection(serverUrl, role, password) - server.createDataStore(dataStore, "par-complex-nn", opts) - val data = server.newDataStoreConnection(dataStore) + if (!server.containsDataStore(datastore)) + server.createDataStore(datastore, "par-complex-nn", opts) + val data = server.newDataStoreConnection(datastore) (server, data) } @@ -153,8 +154,8 @@ object RDFoxHelpers { server: ServerConnection, data: DataStoreConnection ): Unit = { - server.close(); data.close(); + server.close(); } } -- cgit v1.2.3