aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Igne <federico.igne@cs.ox.ac.uk>2020-11-23 13:39:55 +0000
committerFederico Igne <federico.igne@cs.ox.ac.uk>2020-11-23 13:39:55 +0000
commit43be594ec3cd6f16e1ccfc31ce226f5266a7a19b (patch)
tree128efcea3d5ce1400dd0b304c11cf084fdb965d7
parent402e3ec13ca1a0a870dde530d1364a528079782d (diff)
downloadRSAComb-43be594ec3cd6f16e1ccfc31ce226f5266a7a19b.tar.gz
RSAComb-43be594ec3cd6f16e1ccfc31ce226f5266a7a19b.zip
Remove duplicate creation of RDFox datastores
-rw-r--r--src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala27
-rw-r--r--src/main/scala/uk/ac/ox/cs/rsacomb/sparql/ConjunctiveQuery.scala16
-rw-r--r--src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxHelpers.scala9
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 {
59 // Counter used to implement a simple fresh variable generator 59 // Counter used to implement a simple fresh variable generator
60 private var counter = -1; 60 private var counter = -1;
61 61
62 /** Name of the RDFox data store used for CQ answering */
63 private val DataStore = "answer_computation"
64
62 def apply(ontology: OWLOntology): RSAOntology = new RSAOntology(ontology) 65 def apply(ontology: OWLOntology): RSAOntology = new RSAOntology(ontology)
63 66
64 def apply(ontology: File): RSAOntology = 67 def apply(ontology: File): RSAOntology =
@@ -119,7 +122,10 @@ class RSAOntology(val ontology: OWLOntology) extends RSAAxiom {
119 .flatMap(_.objectPropertyExpressionsInSignature) 122 .flatMap(_.objectPropertyExpressionsInSignature)
120 .distinct 123 .distinct
121 124
122 // OWLAPI reasoner for same easier tasks 125 /** OWLAPI reasoner
126 *
127 * Used to carry out some preliminary reasoning task.
128 */
123 private val reasoner = 129 private val reasoner =
124 (new StructuralReasonerFactory()).createReasoner(ontology) 130 (new StructuralReasonerFactory()).createReasoner(ontology)
125 131
@@ -300,7 +306,7 @@ class RSAOntology(val ontology: OWLOntology) extends RSAAxiom {
300 */ 306 */
301 def ask(query: ConjunctiveQuery): ConjunctiveQueryAnswers = { 307 def ask(query: ConjunctiveQuery): ConjunctiveQueryAnswers = {
302 import implicits.JavaCollections._ 308 import implicits.JavaCollections._
303 val (server, data) = RDFoxHelpers.openConnection("AnswerComputation") 309 val (server, data) = RDFoxHelpers.openConnection(RSAOntology.DataStore)
304 data.addRules(this.canonicalModel.rules) 310 data.addRules(this.canonicalModel.rules)
305 data.addRules(this.filteringProgram(query).rules) 311 data.addRules(this.filteringProgram(query).rules)
306 val answers = RDFoxHelpers 312 val answers = RDFoxHelpers
@@ -317,9 +323,12 @@ class RSAOntology(val ontology: OWLOntology) extends RSAAxiom {
317 answers 323 answers
318 } 324 }
319 325
320 /** Query the logic program used to compute answers to a given CQ. 326 /** Query the RDFox data store used for query answering.
321 * 327 *
322 * This method has been introduced mostly for debugging purposes. 328 * @note This method does not add any facts or rules to the data
329 * store. It is most useful after the execution of a query using
330 * [[uk.ac.ox.cs.rsacomb.RSAOntology.ask RSAOntology.ask]].
331 * @note This method has been introduced mostly for debugging purposes.
323 * 332 *
324 * @param cq a CQ used to compute the environment. 333 * @param cq a CQ used to compute the environment.
325 * @param query query to be executed against the environment 334 * @param query query to be executed against the environment
@@ -327,20 +336,14 @@ class RSAOntology(val ontology: OWLOntology) extends RSAAxiom {
327 * an empty set. 336 * an empty set.
328 * @param opts additional options to RDFox. 337 * @param opts additional options to RDFox.
329 * @return a collection of answers to the input query. 338 * @return a collection of answers to the input query.
330 *
331 * @todo this function currently fails because RDFox reports a
332 * datastore duplication.
333 */ 339 */
334 def queryEnvironment( 340 def queryDataStore(
335 cq: ConjunctiveQuery, 341 cq: ConjunctiveQuery,
336 query: String, 342 query: String,
337 prefixes: Prefixes = new Prefixes(), 343 prefixes: Prefixes = new Prefixes(),
338 opts: ju.Map[String, String] = new ju.HashMap[String, String]() 344 opts: ju.Map[String, String] = new ju.HashMap[String, String]()
339 ): Option[Seq[Seq[Resource]]] = { 345 ): Option[Seq[Seq[Resource]]] = {
340 import implicits.JavaCollections._ 346 val (server, data) = RDFoxHelpers.openConnection(RSAOntology.DataStore)
341 val (server, data) = RDFoxHelpers.openConnection("AnswerComputation")
342 data.addRules(this.canonicalModel.rules)
343 data.addRules(this.filteringProgram(cq).rules)
344 val answers = RDFoxHelpers.submitQuery(data, query, prefixes, opts) 347 val answers = RDFoxHelpers.submitQuery(data, query, prefixes, opts)
345 RDFoxHelpers.closeConnection(server, data) 348 RDFoxHelpers.closeConnection(server, data)
346 answers 349 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(
104 /** Returns the collection of bounded (existential) variables in the query. */ 104 /** Returns the collection of bounded (existential) variables in the query. */
105 val bounded: Set[Variable] = variables &~ answer 105 val bounded: Set[Variable] = variables &~ answer
106 106
107 /** Returns the answers to a query
108 *
109 * @param data data store against which the query is executed
110 * @param opts additional options passed to RDFox
111 * @return a new [[ConjunctiveQueryAnswers]] instance containing the
112 * collection of answers.
113 */
114 def answers(
115 data: DataStoreConnection,
116 opts: JMap[String, String] = new JHashMap[String, String]()
117 ): ConjunctiveQueryAnswers =
118 new ConjunctiveQueryAnswers(
119 bcq,
120 RDFoxHelpers.submitSelectQuery(data, query, opts)
121 )
122
123 override def toString(): String = query.toString 107 override def toString(): String = query.toString
124} 108}
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 {
37 * details on how to close an open connection. 37 * details on how to close an open connection.
38 */ 38 */
39 def openConnection( 39 def openConnection(
40 dataStore: String, 40 datastore: String,
41 opts: RDFoxOpts = RDFoxOpts() 41 opts: RDFoxOpts = RDFoxOpts()
42 ): (ServerConnection, DataStoreConnection) = { 42 ): (ServerConnection, DataStoreConnection) = {
43 val serverUrl = "rdfox:local" 43 val serverUrl = "rdfox:local"
@@ -45,8 +45,9 @@ object RDFoxHelpers {
45 val password = "" 45 val password = ""
46 val server = 46 val server =
47 ConnectionFactory.newServerConnection(serverUrl, role, password) 47 ConnectionFactory.newServerConnection(serverUrl, role, password)
48 server.createDataStore(dataStore, "par-complex-nn", opts) 48 if (!server.containsDataStore(datastore))
49 val data = server.newDataStoreConnection(dataStore) 49 server.createDataStore(datastore, "par-complex-nn", opts)
50 val data = server.newDataStoreConnection(datastore)
50 (server, data) 51 (server, data)
51 } 52 }
52 53
@@ -153,8 +154,8 @@ object RDFoxHelpers {
153 server: ServerConnection, 154 server: ServerConnection,
154 data: DataStoreConnection 155 data: DataStoreConnection
155 ): Unit = { 156 ): Unit = {
156 server.close();
157 data.close(); 157 data.close();
158 server.close();
158 } 159 }
159 160
160} 161}