aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFederico Igne <federico.igne@cs.ox.ac.uk>2021-06-01 11:20:39 +0100
committerFederico Igne <federico.igne@cs.ox.ac.uk>2021-06-01 11:20:39 +0100
commit6b4226c41ec2a2439fb44a312ccaff01769e8212 (patch)
tree3028a3b248056baac5b50d6c41a35524eacb67fb
parent5f9815c3f67114645593840a8648bffb1207b8d0 (diff)
downloadRSAComb-6b4226c41ec2a2439fb44a312ccaff01769e8212.tar.gz
RSAComb-6b4226c41ec2a2439fb44a312ccaff01769e8212.zip
Fix minor compilation errors
-rw-r--r--src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala3
-rw-r--r--src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala130
-rw-r--r--src/main/scala/uk/ac/ox/cs/rsacomb/approximation/lowerbound.scala31
-rw-r--r--src/main/scala/uk/ac/ox/cs/rsacomb/converter/Normalizer.scala25
4 files changed, 106 insertions, 83 deletions
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala
index a807f75..f2d1a5d 100644
--- a/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala
+++ b/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala
@@ -106,12 +106,11 @@ object RSAComb extends App {
106 /* Command-line options */ 106 /* Command-line options */
107 val config = RSAConfig.parse(args.toList) 107 val config = RSAConfig.parse(args.toList)
108 108
109 val ontology = RSAOntology( 109 val rsa = RSAOntology(
110 config('ontology).get[File], 110 config('ontology).get[File],
111 config('data).get[List[File]], 111 config('data).get[List[File]],
112 None 112 None
113 ) 113 )
114 val rsa = ontology.toRSA()
115 114
116 if (config contains 'query) { 115 if (config contains 'query) {
117 val query = 116 val query =
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 4d0f13d..247b5d5 100644
--- a/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala
+++ b/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala
@@ -80,7 +80,7 @@ object RSAUtil {
80 * construction of the dependency graph is computed regardless. The 80 * construction of the dependency graph is computed regardless. The
81 * input axioms are assumed to be normalized. 81 * input axioms are assumed to be normalized.
82 */ 82 */
83 private def dependencyGraph( 83 def dependencyGraph(
84 axioms: List[OWLLogicalAxiom], 84 axioms: List[OWLLogicalAxiom],
85 datafiles: List[File] 85 datafiles: List[File]
86 ): (Graph[Resource, DiEdge], Map[String, OWLAxiom]) = { 86 ): (Graph[Resource, DiEdge], Map[String, OWLAxiom]) = {
@@ -166,18 +166,23 @@ object RSAOntology {
166 166
167 import uk.ac.ox.cs.rsacomb.implicits.JavaCollections._ 167 import uk.ac.ox.cs.rsacomb.implicits.JavaCollections._
168 168
169 /** Manager instance to interface with OWLAPI */
170 val manager = OWLManager.createOWLOntologyManager()
171 val factory = manager.getOWLDataFactory()
172
169 /** Name of the RDFox data store used for CQ answering */ 173 /** Name of the RDFox data store used for CQ answering */
170 private val DataStore = "answer_computation" 174 private val DataStore = "answer_computation"
171 175
172 /** Simple fresh variable generator */ 176 /** Simple fresh variable/class generator */
173 private var counter = -1; 177 private var counter = -1;
174 def genFreshVariable(): Variable = { 178 def genFreshVariable(): Variable = {
175 counter += 1 179 counter += 1
176 Variable.create(f"I$counter%05d") 180 Variable.create(f"I$counter%05d")
177 } 181 }
178 182 def getFreshOWLClass(): OWLClass = {
179 /** Manager instance to interface with OWLAPI */ 183 counter += 1
180 val manager = OWLManager.createOWLOntologyManager() 184 factory.getOWLClass(s"X$counter")
185 }
181 186
182 def apply( 187 def apply(
183 axioms: List[OWLLogicalAxiom], 188 axioms: List[OWLLogicalAxiom],
@@ -254,13 +259,16 @@ class RSAOntology(val axioms: List[OWLLogicalAxiom], val datafiles: File*) {
254 /** Simplify conversion between OWLAPI and RDFox concepts */ 259 /** Simplify conversion between OWLAPI and RDFox concepts */
255 import implicits.RDFox._ 260 import implicits.RDFox._
256 import uk.ac.ox.cs.rsacomb.implicits.RSAAxiom._ 261 import uk.ac.ox.cs.rsacomb.implicits.RSAAxiom._
262
263 /** Simplify conversion between Java and Scala collections */
257 import uk.ac.ox.cs.rsacomb.implicits.JavaCollections._ 264 import uk.ac.ox.cs.rsacomb.implicits.JavaCollections._
258 265
259 /** Set of axioms removed during the approximation to RSA */ 266 /** Set of axioms removed during the approximation to RSA */
260 private var removed: Seq[OWLAxiom] = Seq.empty 267 private var removed: Seq[OWLAxiom] = Seq.empty
261 268
262 /** Normalized Horn-ALCHOIQ ontology */ 269 /** Normalized Horn-ALCHOIQ ontology */
263 val ontology = RSAOntology.manager.createOntology(axioms.asJava) 270 val ontology =
271 RSAOntology.manager.createOntology((axioms: List[OWLAxiom]).asJava)
264 272
265 /** OWLAPI internal reasoner instantiated over the approximated ontology */ 273 /** OWLAPI internal reasoner instantiated over the approximated ontology */
266 private val reasoner = 274 private val reasoner =
@@ -340,61 +348,61 @@ class RSAOntology(val axioms: List[OWLLogicalAxiom], val datafiles: File*) {
340 * @param graph the graph used to compute the axioms to remove. 348 * @param graph the graph used to compute the axioms to remove.
341 * @param nodemap map from graph nodes to ontology axioms. 349 * @param nodemap map from graph nodes to ontology axioms.
342 */ 350 */
343 def toRSA(): RSAOntology = Logger.timed( 351 // def toRSA(): RSAOntology = Logger.timed(
344 { 352 // {
345 353
346 /* Compute the dependency graph for the ontology */ 354 // /* Compute the dependency graph for the ontology */
347 val (graph, nodemap) = this.dependencyGraph() 355 // val (graph, nodemap) = this.dependencyGraph()
348 356
349 /* Define node colors for the graph visit */ 357 // /* Define node colors for the graph visit */
350 sealed trait NodeColor 358 // sealed trait NodeColor
351 case object Unvisited extends NodeColor 359 // case object Unvisited extends NodeColor
352 case object Visited extends NodeColor 360 // case object Visited extends NodeColor
353 case object ToDelete extends NodeColor 361 // case object ToDelete extends NodeColor
354 362
355 /* Keep track of node colors during graph visit */ 363 // /* Keep track of node colors during graph visit */
356 var color = Map.from[Resource, NodeColor]( 364 // var color = Map.from[Resource, NodeColor](
357 graph.nodes.toOuter.map(k => (k, Unvisited)) 365 // graph.nodes.toOuter.map(k => (k, Unvisited))
358 ) 366 // )
359 367
360 for { 368 // for {
361 component <- graph.componentTraverser().map(_ to Graph) 369 // component <- graph.componentTraverser().map(_ to Graph)
362 edge <- component 370 // edge <- component
363 .outerEdgeTraverser(component.nodes.head) 371 // .outerEdgeTraverser(component.nodes.head)
364 .withKind(BreadthFirst) 372 // .withKind(BreadthFirst)
365 } yield { 373 // } yield {
366 val source = edge._1 374 // val source = edge._1
367 val target = edge._2 375 // val target = edge._2
368 color(source) match { 376 // color(source) match {
369 case Unvisited | Visited => { 377 // case Unvisited | Visited => {
370 color(target) match { 378 // color(target) match {
371 case Unvisited => 379 // case Unvisited =>
372 color(source) = Visited; 380 // color(source) = Visited;
373 color(target) = Visited 381 // color(target) = Visited
374 case Visited => 382 // case Visited =>
375 color(source) = ToDelete 383 // color(source) = ToDelete
376 case ToDelete => 384 // case ToDelete =>
377 color(source) = Visited 385 // color(source) = Visited
378 } 386 // }
379 } 387 // }
380 case ToDelete => 388 // case ToDelete =>
381 } 389 // }
382 } 390 // }
383 391
384 val toDelete = color.iterator.collect { case (resource: IRI, ToDelete) => 392 // val toDelete = color.iterator.collect { case (resource: IRI, ToDelete) =>
385 nodemap(resource.getIRI) 393 // nodemap(resource.getIRI)
386 }.toSeq 394 // }.toSeq
387 395
388 /* Remove axioms from approximated ontology */ 396 // /* Remove axioms from approximated ontology */
389 ontology.removeAxioms(toDelete: _*) 397 // ontology.removeAxioms(toDelete: _*)
390 this.removed = toDelete 398 // this.removed = toDelete
391 399
392 /* Return RSA ontology */ 400 // /* Return RSA ontology */
393 RSAOntology(ontology, datafiles: _*) 401 // RSAOntology(ontology, datafiles: _*)
394 }, 402 // },
395 "Horn-ALCHOIQ to RSA approximation:", 403 // "Horn-ALCHOIQ to RSA approximation:",
396 Logger.DEBUG 404 // Logger.DEBUG
397 ) 405 // )
398 // val edges1 = Seq('A ~> 'B, 'B ~> 'C, 'C ~> 'D, 'D ~> 'H, 'H ~> 406 // val edges1 = Seq('A ~> 'B, 'B ~> 'C, 'C ~> 'D, 'D ~> 'H, 'H ~>
399 // 'G, 'G ~> 'F, 'E ~> 'A, 'E ~> 'F, 'B ~> 'E, 'F ~> 'G, 'B ~> 'F, 407 // 'G, 'G ~> 'F, 'E ~> 'A, 'E ~> 'F, 'B ~> 'E, 'F ~> 'G, 'B ~> 'F,
400 // 'C ~> 'G, 'D ~> 'C, 'H ~> 'D) 408 // 'C ~> 'G, 'D ~> 'C, 'H ~> 'D)
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/approximation/lowerbound.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/approximation/lowerbound.scala
index 3437bcd..8a86d19 100644
--- a/src/main/scala/uk/ac/ox/cs/rsacomb/approximation/lowerbound.scala
+++ b/src/main/scala/uk/ac/ox/cs/rsacomb/approximation/lowerbound.scala
@@ -2,13 +2,18 @@ package uk.ac.ox.cs.rsacomb.approximation
2 2
3import java.io.File 3import java.io.File
4 4
5import org.semanticweb.owlapi.model._ 5import org.semanticweb.owlapi.apibinding.OWLManager
6import org.semanticweb.owlapi.model.{IRI => _, _}
7
8import tech.oxfordsemantic.jrdfox.logic.expression.{Resource, IRI}
6 9
7import scala.collection.mutable.{Set, Map} 10import scala.collection.mutable.{Set, Map}
8import scalax.collection.Graph 11import scalax.collection.Graph
9import scalax.collection.GraphPredef._, scalax.collection.GraphEdge._ 12import scalax.collection.GraphPredef._, scalax.collection.GraphEdge._
10import scalax.collection.GraphTraversal._ 13import scalax.collection.GraphTraversal._
11 14
15import uk.ac.ox.cs.rsacomb.RSAOntology
16import uk.ac.ox.cs.rsacomb.RSAUtil
12import uk.ac.ox.cs.rsacomb.converter.Normalizer 17import uk.ac.ox.cs.rsacomb.converter.Normalizer
13 18
14/** Approximation algorithm that mantains soundness for CQ answering. 19/** Approximation algorithm that mantains soundness for CQ answering.
@@ -28,6 +33,16 @@ import uk.ac.ox.cs.rsacomb.converter.Normalizer
28 */ 33 */
29class LowerBound extends Approximation { 34class LowerBound extends Approximation {
30 35
36 /** Simplify conversion between Java and Scala collections */
37 import uk.ac.ox.cs.rsacomb.implicits.JavaCollections._
38
39 /** Simplify conversion between OWLAPI and RDFox concepts */
40 import uk.ac.ox.cs.rsacomb.implicits.RDFox._
41
42 /** Manager instance to interface with OWLAPI */
43 val manager = OWLManager.createOWLOntologyManager()
44 val factory = manager.getOWLDataFactory()
45
31 val normalizer = new Normalizer() 46 val normalizer = new Normalizer()
32 47
33 /** Main entry point for the approximation algorithm */ 48 /** Main entry point for the approximation algorithm */
@@ -36,14 +51,14 @@ class LowerBound extends Approximation {
36 datafiles: List[File] 51 datafiles: List[File]
37 ): List[OWLLogicalAxiom] = { 52 ): List[OWLLogicalAxiom] = {
38 /* Normalize axioms */ 53 /* Normalize axioms */
39 val axioms1 = axioms flatMap normalizer.normalize(_) 54 val axioms1 = ontology flatMap normalizer.normalize
40 /* Delete any axiom outside of ALCHOIQ */ 55 /* Delete any axiom outside of ALCHOIQ */
41 val axioms2 = axioms1 filterNot inHornLACHOIQ 56 val axioms2 = axioms1 filterNot inALCHOIQ
42 /* Shift any axiom with disjunction on the rhs */ 57 /* Shift any axiom with disjunction on the rhs */
43 val axioms3 = for { 58 val axioms3 = for {
44 a1 <- axioms1 59 a1 <- axioms1
45 a2 <- shift(a1) 60 a2 <- shift(a1)
46 a3 <- normalize(a2) 61 a3 <- normalizer.normalize(a2)
47 } yield a3 62 } yield a3
48 /* Approximate to RSA */ 63 /* Approximate to RSA */
49 toRSA(axioms3, datafiles) 64 toRSA(axioms3, datafiles)
@@ -113,8 +128,12 @@ class LowerBound extends Approximation {
113 val sup = a.getSuperClass.getNNF 128 val sup = a.getSuperClass.getNNF
114 sup match { 129 sup match {
115 case sup: OWLObjectUnionOf => { 130 case sup: OWLObjectUnionOf => {
116 val body = sub.asConjunctSet.map((atom) => (atom, freshOWLClass())) 131 val body = sub.asConjunctSet.map((atom) =>
117 val head = sup.asDisjunctSet.map((atom) => (atom, freshOWLClass())) 132 (atom, RSAOntology.getFreshOWLClass())
133 )
134 val head = sup.asDisjunctSet.map((atom) =>
135 (atom, RSAOntology.getFreshOWLClass())
136 )
118 137
119 val r1 = 138 val r1 =
120 factory.getOWLSubClassOfAxiom( 139 factory.getOWLSubClassOfAxiom(
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/converter/Normalizer.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/converter/Normalizer.scala
index 5329f26..4b298f4 100644
--- a/src/main/scala/uk/ac/ox/cs/rsacomb/converter/Normalizer.scala
+++ b/src/main/scala/uk/ac/ox/cs/rsacomb/converter/Normalizer.scala
@@ -4,6 +4,7 @@ import org.semanticweb.owlapi.apibinding.OWLManager
4import org.semanticweb.owlapi.model._ 4import org.semanticweb.owlapi.model._
5 5
6import uk.ac.ox.cs.rsacomb.util.Logger 6import uk.ac.ox.cs.rsacomb.util.Logger
7import uk.ac.ox.cs.rsacomb.RSAOntology
7 8
8object Normalizer { 9object Normalizer {
9 10
@@ -25,12 +26,6 @@ class Normalizer() {
25 /** Simplify conversion between Java and Scala collections */ 26 /** Simplify conversion between Java and Scala collections */
26 import uk.ac.ox.cs.rsacomb.implicits.JavaCollections._ 27 import uk.ac.ox.cs.rsacomb.implicits.JavaCollections._
27 28
28 private var counter = -1
29 def freshOWLClass(): OWLClass = {
30 counter += 1
31 factory.getOWLClass(s"X$counter")
32 }
33
34 /** Statistics */ 29 /** Statistics */
35 var discarded = 0 30 var discarded = 0
36 var shifted = 0 31 var shifted = 0
@@ -58,7 +53,7 @@ class Normalizer() {
58 * C c D -> { C c X, X c D } 53 * C c D -> { C c X, X c D }
59 */ 54 */
60 case _ if !sub.isOWLClass && !sup.isOWLClass => { 55 case _ if !sub.isOWLClass && !sup.isOWLClass => {
61 val cls = freshOWLClass() 56 val cls = RSAOntology.getFreshOWLClass()
62 Seq( 57 Seq(
63 factory.getOWLSubClassOfAxiom(sub, cls), 58 factory.getOWLSubClassOfAxiom(sub, cls),
64 factory.getOWLSubClassOfAxiom(cls, sup) 59 factory.getOWLSubClassOfAxiom(cls, sup)
@@ -79,7 +74,7 @@ class Normalizer() {
79 if (conj.isOWLClass) 74 if (conj.isOWLClass)
80 (acc1 :+ conj, acc2) 75 (acc1 :+ conj, acc2)
81 else { 76 else {
82 val cls = freshOWLClass() 77 val cls = RSAOntology.getFreshOWLClass()
83 ( 78 (
84 acc1 :+ cls, 79 acc1 :+ cls,
85 acc2 :+ factory.getOWLSubClassOfAxiom(conj, cls) 80 acc2 :+ factory.getOWLSubClassOfAxiom(conj, cls)
@@ -138,7 +133,7 @@ class Normalizer() {
138 */ 133 */
139 case (sub: OWLObjectSomeValuesFrom, _) 134 case (sub: OWLObjectSomeValuesFrom, _)
140 if !sub.getFiller.isOWLClass => { 135 if !sub.getFiller.isOWLClass => {
141 val cls = freshOWLClass() 136 val cls = RSAOntology.getFreshOWLClass()
142 Seq( 137 Seq(
143 factory.getOWLSubClassOfAxiom(sub.getFiller, cls), 138 factory.getOWLSubClassOfAxiom(sub.getFiller, cls),
144 factory.getOWLSubClassOfAxiom( 139 factory.getOWLSubClassOfAxiom(
@@ -153,7 +148,7 @@ class Normalizer() {
153 */ 148 */
154 case (_, sup: OWLObjectSomeValuesFrom) 149 case (_, sup: OWLObjectSomeValuesFrom)
155 if !sup.getFiller.isOWLClass => { 150 if !sup.getFiller.isOWLClass => {
156 val cls = freshOWLClass() 151 val cls = RSAOntology.getFreshOWLClass()
157 Seq( 152 Seq(
158 factory.getOWLSubClassOfAxiom(cls, sup.getFiller), 153 factory.getOWLSubClassOfAxiom(cls, sup.getFiller),
159 factory.getOWLSubClassOfAxiom( 154 factory.getOWLSubClassOfAxiom(
@@ -298,7 +293,7 @@ class Normalizer() {
298 ) 293 )
299 case (_, sup: OWLObjectMaxCardinality) 294 case (_, sup: OWLObjectMaxCardinality)
300 if sup.getCardinality == 1 && !sup.getFiller.isOWLClass => { 295 if sup.getCardinality == 1 && !sup.getFiller.isOWLClass => {
301 val cls = freshOWLClass() 296 val cls = RSAOntology.getFreshOWLClass()
302 Seq( 297 Seq(
303 factory.getOWLSubClassOfAxiom(cls, sup.getFiller), 298 factory.getOWLSubClassOfAxiom(cls, sup.getFiller),
304 factory.getOWLSubClassOfAxiom( 299 factory.getOWLSubClassOfAxiom(
@@ -488,7 +483,7 @@ class Normalizer() {
488 * C(a) -> { X(a), X c C } 483 * C(a) -> { X(a), X c C }
489 */ 484 */
490 case a: OWLClassAssertionAxiom if !a.getClassExpression.isOWLClass => { 485 case a: OWLClassAssertionAxiom if !a.getClassExpression.isOWLClass => {
491 val cls = freshOWLClass() 486 val cls = RSAOntology.getFreshOWLClass()
492 Seq( 487 Seq(
493 factory.getOWLClassAssertionAxiom(cls, a.getIndividual), 488 factory.getOWLClassAssertionAxiom(cls, a.getIndividual),
494 factory.getOWLSubClassOfAxiom(cls, a.getClassExpression) 489 factory.getOWLSubClassOfAxiom(cls, a.getClassExpression)
@@ -532,8 +527,10 @@ class Normalizer() {
532 sub: OWLClassExpression, 527 sub: OWLClassExpression,
533 sup: OWLObjectUnionOf 528 sup: OWLObjectUnionOf
534 ): Seq[OWLLogicalAxiom] = { 529 ): Seq[OWLLogicalAxiom] = {
535 val body = sub.asConjunctSet.map((atom) => (atom, freshOWLClass())) 530 val body =
536 val head = sup.asDisjunctSet.map((atom) => (atom, freshOWLClass())) 531 sub.asConjunctSet.map((atom) => (atom, RSAOntology.getFreshOWLClass()))
532 val head =
533 sup.asDisjunctSet.map((atom) => (atom, RSAOntology.getFreshOWLClass()))
537 534
538 /* Update statistics */ 535 /* Update statistics */
539 shifted += 1 536 shifted += 1