diff options
author | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-11-20 13:10:44 +0000 |
---|---|---|
committer | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-11-20 13:11:14 +0000 |
commit | 0ac35ab057257eadc297d430666d0c1da41756f6 (patch) | |
tree | fbbd64a74675989039c9c9c36e85b9aadd21fb97 /src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala | |
parent | c804109db7bb08cce1246acbca2030c3b979f242 (diff) | |
download | RSAComb-0ac35ab057257eadc297d430666d0c1da41756f6.tar.gz RSAComb-0ac35ab057257eadc297d430666d0c1da41756f6.zip |
Simplify workflow for query execution
Input query is now read from file.
Diffstat (limited to 'src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala')
-rw-r--r-- | src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala | 55 |
1 files changed, 51 insertions, 4 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 9640ccc..56fbac3 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala | |||
@@ -1,6 +1,7 @@ | |||
1 | package uk.ac.ox.cs.rsacomb | 1 | package uk.ac.ox.cs.rsacomb |
2 | 2 | ||
3 | /* Java imports */ | 3 | /* Java imports */ |
4 | import java.{util => ju} | ||
4 | import java.util.HashMap | 5 | import java.util.HashMap |
5 | import java.util.stream.{Collectors, Stream} | 6 | import java.util.stream.{Collectors, Stream} |
6 | import java.io.File | 7 | import java.io.File |
@@ -48,8 +49,8 @@ import org.semanticweb.owlapi.model.OWLObjectInverseOf | |||
48 | 49 | ||
49 | import uk.ac.ox.cs.rsacomb.converter.{RDFoxAxiomConverter, SkolemStrategy} | 50 | import uk.ac.ox.cs.rsacomb.converter.{RDFoxAxiomConverter, SkolemStrategy} |
50 | import uk.ac.ox.cs.rsacomb.implicits.RSAAxiom | 51 | import uk.ac.ox.cs.rsacomb.implicits.RSAAxiom |
51 | import uk.ac.ox.cs.rsacomb.suffix.{Empty, Forward, Backward, Inverse} | 52 | import uk.ac.ox.cs.rsacomb.suffix._ |
52 | import uk.ac.ox.cs.rsacomb.sparql.ConjunctiveQuery | 53 | import uk.ac.ox.cs.rsacomb.sparql._ |
53 | import uk.ac.ox.cs.rsacomb.util.{RDFoxHelpers, RSA} | 54 | import uk.ac.ox.cs.rsacomb.util.{RDFoxHelpers, RSA} |
54 | 55 | ||
55 | object RSAOntology { | 56 | object RSAOntology { |
@@ -256,8 +257,8 @@ class RSAOntology(val ontology: OWLOntology) extends RSAAxiom { | |||
256 | ): Graph[Resource, UnDiEdge] = { | 257 | ): Graph[Resource, UnDiEdge] = { |
257 | val query = "SELECT ?X ?Y WHERE { ?X rsa:E ?Y }" | 258 | val query = "SELECT ?X ?Y WHERE { ?X rsa:E ?Y }" |
258 | val answers = RDFoxHelpers.submitQuery(data, query, RSA.Prefixes).get | 259 | val answers = RDFoxHelpers.submitQuery(data, query, RSA.Prefixes).get |
259 | var edges: List[UnDiEdge[Resource]] = answers.map { | 260 | var edges: Seq[UnDiEdge[Resource]] = answers.map { |
260 | case n1 :: n2 :: _ => UnDiEdge(n1, n2) | 261 | case Seq(n1, n2) => UnDiEdge(n1, n2) |
261 | } | 262 | } |
262 | Graph(edges: _*) | 263 | Graph(edges: _*) |
263 | } | 264 | } |
@@ -291,6 +292,52 @@ class RSAOntology(val ontology: OWLOntology) extends RSAAxiom { | |||
291 | .filterNot(_.getInverseProperty.isOWLTopObjectProperty()) | 292 | .filterNot(_.getInverseProperty.isOWLTopObjectProperty()) |
292 | } | 293 | } |
293 | 294 | ||
295 | /** Returns the answers to a query | ||
296 | * | ||
297 | * @param query query to execute | ||
298 | * @return a collection of answers | ||
299 | */ | ||
300 | def ask(query: ConjunctiveQuery): ConjunctiveQueryAnswers = { | ||
301 | import implicits.JavaCollections._ | ||
302 | val (server, data) = RDFoxHelpers.openConnection("AnswerComputation") | ||
303 | data.addRules(this.canonicalModel.rules) | ||
304 | data.addRules(this.filteringProgram(query).rules) | ||
305 | new ConjunctiveQueryAnswers( | ||
306 | query.boolean, | ||
307 | queryInternalPredicate(data, "Ans", query.answer.size) | ||
308 | ) | ||
309 | } | ||
310 | |||
311 | /** Returns instances of a reified predicate | ||
312 | * | ||
313 | * Predicated with arity higher than 2 are internally reified to be | ||
314 | * compatible with RDFox engine. This helper queries for predicate | ||
315 | * instances and returns a set of un-reified answers. | ||
316 | * | ||
317 | * @param data open datastore connection to RDFox | ||
318 | * @param pred name of the predicate | ||
319 | * @param arity arity of the predicate | ||
320 | * @param opts additional options to RDFox | ||
321 | * @return a collection of instances of the given predicate | ||
322 | */ | ||
323 | private def queryInternalPredicate( | ||
324 | data: DataStoreConnection, | ||
325 | pred: String, | ||
326 | arity: Int, | ||
327 | opts: ju.Map[String, String] = new ju.HashMap[String, String]() | ||
328 | ): Seq[Seq[Resource]] = { | ||
329 | val query = | ||
330 | if (arity > 0) { | ||
331 | (0 until arity).mkString("SELECT ?X", " ?X", "\n") + | ||
332 | (0 until arity) | ||
333 | .map(i => s"?S rsa:${pred :: Nth(i)} ?X$i .") | ||
334 | .mkString("WHERE {\n", "\n", "\n}") | ||
335 | } else { | ||
336 | s"ASK { ?X a rsa:$pred }" | ||
337 | } | ||
338 | RDFoxHelpers.submitQuery(data, query, RSA.Prefixes).get | ||
339 | } | ||
340 | |||
294 | def self(axiom: OWLSubClassOfAxiom): Set[Term] = { | 341 | def self(axiom: OWLSubClassOfAxiom): Set[Term] = { |
295 | // Assuming just one role in the signature of a T5 axiom | 342 | // Assuming just one role in the signature of a T5 axiom |
296 | val role = axiom.objectPropertyExpressionsInSignature(0) | 343 | val role = axiom.objectPropertyExpressionsInSignature(0) |