From 5f9815c3f67114645593840a8648bffb1207b8d0 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Mon, 31 May 2021 16:58:15 +0100 Subject: Multiple fixes --- src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala | 4 +- .../scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala | 88 ++++++++++++---------- .../cs/rsacomb/approximation/approximation.scala | 7 +- .../ox/cs/rsacomb/approximation/lowerbound.scala | 35 ++++++--- 4 files changed, 80 insertions(+), 54 deletions(-) (limited to 'src/main') 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 74084af..a807f75 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala @@ -108,10 +108,10 @@ object RSAComb extends App { val ontology = RSAOntology( config('ontology).get[File], - config('data).get[List[File]]: _* + config('data).get[List[File]], + None ) val rsa = ontology.toRSA() - ontology.statistics() if (config contains 'query) { 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 e048c28..4d0f13d 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala @@ -50,13 +50,13 @@ import scala.collection.JavaConverters._ import scala.collection.mutable.{Set, Map} import scalax.collection.Graph import scalax.collection.GraphPredef._, scalax.collection.GraphEdge._ -import scalax.collection.GraphTraversal._ /* Debug only */ import org.semanticweb.owlapi.dlsyntax.renderer.DLSyntaxObjectRenderer import tech.oxfordsemantic.jrdfox.logic._ import org.semanticweb.owlapi.model.OWLObjectInverseOf +import uk.ac.ox.cs.rsacomb.approximation.Approximation import uk.ac.ox.cs.rsacomb.converter._ import uk.ac.ox.cs.rsacomb.filtering.{FilteringProgram, FilterType} import uk.ac.ox.cs.rsacomb.suffix._ @@ -66,10 +66,10 @@ import uk.ac.ox.cs.rsacomb.util.Logger object RSAUtil { - implicit def axiomsToOntology(axioms: Seq[OWLAxiom]) = { - val manager = OWLManager.createOWLOntologyManager() - manager.createOntology(axioms.asJava) - } + // implicit def axiomsToOntology(axioms: Seq[OWLAxiom]) = { + // val manager = OWLManager.createOWLOntologyManager() + // manager.createOntology(axioms.asJava) + // } /** Compute the RSA dependency graph for a set of axioms * @@ -81,10 +81,10 @@ object RSAUtil { * input axioms are assumed to be normalized. */ private def dependencyGraph( - axioms: Seq[OWLAxiom], - datafiles: Seq[File] + axioms: List[OWLLogicalAxiom], + datafiles: List[File] ): (Graph[Resource, DiEdge], Map[String, OWLAxiom]) = { - val unsafe = this.unsafeRoles + val unsafe = RSAOntology(axioms, datafiles).unsafeRoles var nodemap = Map.empty[String, OWLAxiom] object RSAConverter extends RDFoxConverter { @@ -164,6 +164,8 @@ object RSAUtil { object RSAOntology { + import uk.ac.ox.cs.rsacomb.implicits.JavaCollections._ + /** Name of the RDFox data store used for CQ answering */ private val DataStore = "answer_computation" @@ -177,10 +179,15 @@ object RSAOntology { /** Manager instance to interface with OWLAPI */ val manager = OWLManager.createOWLOntologyManager() + def apply( + axioms: List[OWLLogicalAxiom], + datafiles: List[File] + ): RSAOntology = new RSAOntology(axioms, datafiles: _*) + def apply( ontofile: File, - datafiles: Seq[File], - approx: Option[Approximation] = None + datafiles: List[File], + approx: Option[Approximation] ): RSAOntology = { val ontology = manager.loadOntologyFromOntologyDocument(ontofile) RSAOntology(ontology, datafiles, approx) @@ -188,14 +195,14 @@ object RSAOntology { def apply( ontology: OWLOntology, - datafiles: Seq[File], - approx: Option[Approximation] = None + datafiles: List[File], + approx: Option[Approximation] ): RSAOntology = { val normalizer = new Normalizer() /** TBox axioms */ var tbox: List[OWLLogicalAxiom] = - original + ontology .tboxAxioms(Imports.INCLUDED) .collect(Collectors.toList()) .collect { case a: OWLLogicalAxiom => a } @@ -203,7 +210,7 @@ object RSAOntology { /** RBox axioms */ var rbox: List[OWLLogicalAxiom] = - original + ontology .rboxAxioms(Imports.INCLUDED) .collect(Collectors.toList()) .collect { case a: OWLLogicalAxiom => a } @@ -217,7 +224,7 @@ object RSAOntology { * large data files via OWLAPI. */ var abox: List[OWLLogicalAxiom] = - original + ontology .aboxAxioms(Imports.INCLUDED) .collect(Collectors.toList()) .collect { case a: OWLLogicalAxiom => a } @@ -231,9 +238,10 @@ object RSAOntology { case Some(a) => a.approximate(axioms, datafiles) case None => axioms }, - datafiles + datafiles: _* ) } + } /** Wrapper class for an ontology in RSA @@ -241,7 +249,7 @@ object RSAOntology { * @param ontology the input OWL2 ontology. * @param datafiles additinal data (treated as part of the ABox) */ -class RSAOntology(val axioms: Seq[OWLAxiom], val datafiles: File*) { +class RSAOntology(val axioms: List[OWLLogicalAxiom], val datafiles: File*) { /** Simplify conversion between OWLAPI and RDFox concepts */ import implicits.RDFox._ @@ -697,28 +705,28 @@ class RSAOntology(val axioms: Seq[OWLAxiom], val datafiles: File*) { this.self(axiom) | this.cycle(axiom) /** Log normalization/approximation statistics */ - def statistics(level: Logger.Level = Logger.DEBUG): Unit = { - Logger.print( - s"Logical axioms in original input ontology: ${original.getLogicalAxiomCount(true)}", - level - ) - Logger.print( - s"Logical axioms discarded in Horn-ALCHOIQ approximation: ${normalizer.discarded}", - level - ) - Logger.print( - s"Logical axioms shifted in Horn-ALCHOIQ approximation: ${normalizer.shifted}", - level - ) - Logger.print( - s"Logical axioms in Horn-ALCHOIQ ontology: ${ontology - .getLogicalAxiomCount(true)} (${axioms.length}/${axioms.length}/${axioms.length})", - level - ) - Logger.print( - s"Logical axioms discarded in RSA approximation: ${removed.length}", - level - ) - } + // def statistics(level: Logger.Level = Logger.DEBUG): Unit = { + // Logger.print( + // s"Logical axioms in original input ontology: ${original.getLogicalAxiomCount(true)}", + // level + // ) + // Logger.print( + // s"Logical axioms discarded in Horn-ALCHOIQ approximation: ${normalizer.discarded}", + // level + // ) + // Logger.print( + // s"Logical axioms shifted in Horn-ALCHOIQ approximation: ${normalizer.shifted}", + // level + // ) + // Logger.print( + // s"Logical axioms in Horn-ALCHOIQ ontology: ${ontology + // .getLogicalAxiomCount(true)} (${axioms.length}/${axioms.length}/${axioms.length})", + // level + // ) + // Logger.print( + // s"Logical axioms discarded in RSA approximation: ${removed.length}", + // level + // ) + // } } // class RSAOntology diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/approximation/approximation.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/approximation/approximation.scala index 073d0d9..1b49413 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/approximation/approximation.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/approximation/approximation.scala @@ -1,7 +1,7 @@ package uk.ac.ox.cs.rsacomb.approximation import java.io.File -import org.semanticweb.owlapi.model.OWLAxiom +import org.semanticweb.owlapi.model.OWLLogicalAxiom /** Ontology approximation technique. */ trait Approximation { @@ -11,6 +11,9 @@ trait Approximation { * @param ontology input ontology * @return a new approximated ontology */ - def approximate(ontology: Seq[OWLAxiom], datafiles: Seq[File]): Seq[OWLAxiom] + def approximate( + ontology: List[OWLLogicalAxiom], + datafiles: List[File] + ): List[OWLLogicalAxiom] } 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 8036250..3437bcd 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 @@ -1,5 +1,16 @@ package uk.ac.ox.cs.rsacomb.approximation +import java.io.File + +import org.semanticweb.owlapi.model._ + +import scala.collection.mutable.{Set, Map} +import scalax.collection.Graph +import scalax.collection.GraphPredef._, scalax.collection.GraphEdge._ +import scalax.collection.GraphTraversal._ + +import uk.ac.ox.cs.rsacomb.converter.Normalizer + /** Approximation algorithm that mantains soundness for CQ answering. * * The input OWL 2 ontology is assumed to be normalized and the output @@ -21,11 +32,11 @@ class LowerBound extends Approximation { /** Main entry point for the approximation algorithm */ def approximate( - ontology: Seq[OWLAxiom], - datafiles: Seq[File] - ): Seq[OWLAxiom] = { + ontology: List[OWLLogicalAxiom], + datafiles: List[File] + ): List[OWLLogicalAxiom] = { /* Normalize axioms */ - val axioms1 = axioms flatMap normalizer.normalize + val axioms1 = axioms flatMap normalizer.normalize(_) /* Delete any axiom outside of ALCHOIQ */ val axioms2 = axioms1 filterNot inHornLACHOIQ /* Shift any axiom with disjunction on the rhs */ @@ -95,7 +106,7 @@ class LowerBound extends Approximation { * where nA, nB1, nB2, nB3 are fresh predicates "corresponding" to * the negation of A, B1, B2, B3 respectively. */ - def shift(axiom: OWLLogicalAxiom): Seq[OWLLogicalAxiom] = + def shift(axiom: OWLLogicalAxiom): List[OWLLogicalAxiom] = axiom match { case a: OWLSubClassOfAxiom => { val sub = a.getSubClass.getNNF @@ -135,12 +146,12 @@ class LowerBound extends Approximation { na ) - Seq(r1) ++ r2s ++ r3s + List(r1) ++ r2s ++ r3s } - case _ => Seq(axiom) + case _ => List(axiom) } } - case _ => Seq(axiom) + case _ => List(axiom) } /** Approximate a Horn-ALCHOIQ ontology to RSA @@ -151,7 +162,10 @@ class LowerBound extends Approximation { * @param axioms the set of axioms to approximate. * @return the approximated set of axioms. */ - def toRSA(axioms: Seq[OWLAxiom], datafiles: Seq[File]): Seq[OWLAxiom] = { + def toRSA( + axioms: List[OWLLogicalAxiom], + datafiles: List[File] + ): List[OWLLogicalAxiom] = { /* Compute the dependency graph for the ontology */ val (graph, nodemap) = RSAUtil.dependencyGraph(axioms, datafiles) @@ -192,11 +206,12 @@ class LowerBound extends Approximation { val toDelete = color.iterator.collect { case (resource: IRI, ToDelete) => nodemap(resource.getIRI) - }.toSeq + }.toList /* Remove axioms from approximated ontology */ axioms diff toDelete } + // val edges1 = Seq('A ~> 'B, 'B ~> 'C, 'C ~> 'D, 'D ~> 'H, 'H ~> // 'G, 'G ~> 'F, 'E ~> 'A, 'E ~> 'F, 'B ~> 'E, 'F ~> 'G, 'B ~> 'F, // 'C ~> 'G, 'D ~> 'C, 'H ~> 'D) -- cgit v1.2.3