From 159605a3b8b55a8394600a9c73d0c8bccba0546f Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Tue, 10 Nov 2020 15:18:35 +0000 Subject: Generalize axiom hashing for constant generation --- src/main/scala/rsacomb/RSA.scala | 19 ++++++++++++++++++- src/main/scala/rsacomb/RSAAxiom.scala | 14 ++++++++++++++ src/main/scala/rsacomb/RSAOntology.scala | 23 ++++++++++++----------- 3 files changed, 44 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/main/scala/rsacomb/RSA.scala b/src/main/scala/rsacomb/RSA.scala index a6f4b88..66de974 100644 --- a/src/main/scala/rsacomb/RSA.scala +++ b/src/main/scala/rsacomb/RSA.scala @@ -9,12 +9,17 @@ import tech.oxfordsemantic.jrdfox.Prefixes import tech.oxfordsemantic.jrdfox.logic.expression.{Variable, IRI} import org.semanticweb.owlapi.apibinding.OWLManager import org.semanticweb.owlapi.model.OWLOntology +import org.semanticweb.owlapi.model.{ + OWLAxiom, + OWLClass, + OWLObjectPropertyExpression +} import rsacomb.RSAOntology // Debug only import scala.collection.JavaConverters._ -object RSA extends RSAOntology { +object RSA extends RSAOntology with RSAAxiom { val Prefixes = new Prefixes() Prefixes.declarePrefix(":", "http://example.com/rsa_example.owl#") @@ -43,6 +48,18 @@ object RSA extends RSAOntology { + name.toString ) + def hashed( + cls1: OWLClass, + prop: OWLObjectPropertyExpression, + cls2: OWLClass + ): String = + (cls1, prop, cls2).hashCode.toString + + def hashed(axiom: OWLAxiom): String = { + val (cls1, prop, cls2) = axiom.toTriple.get + this.hashed(cls1, prop, cls2) + } + // TODO: move this somewhere else... maybe an OntoUtils class or something. def loadOntology(onto: File): OWLOntology = { val manager = OWLManager.createOWLOntologyManager() diff --git a/src/main/scala/rsacomb/RSAAxiom.scala b/src/main/scala/rsacomb/RSAAxiom.scala index aca44b1..f504bbe 100644 --- a/src/main/scala/rsacomb/RSAAxiom.scala +++ b/src/main/scala/rsacomb/RSAAxiom.scala @@ -19,6 +19,7 @@ import org.semanticweb.owlapi.model.{ OWLAxiomVisitorEx, OWLClassExpressionVisitorEx } +import org.semanticweb.owlapi.model.OWLObjectProperty /* Wrapper trait for the implicit class `RSAAxiom`. */ @@ -163,6 +164,19 @@ trait RSAAxiom { val visitor = new RSAAxiomRoleExtractor() axiom.accept(visitor) } + + lazy val toTriple: Option[(OWLClass, OWLObjectProperty, OWLClass)] = + for { + subClass <- Some(axiom) collect { case a: OWLSubClassOfAxiom => a } + cls1 <- Some(subClass.getSubClass) collect { case a: OWLClass => a } + someValues <- Some(subClass.getSuperClass) collect { + case a: OWLObjectSomeValuesFrom => a + } + prop <- Some(someValues.getProperty) collect { + case a: OWLObjectProperty => a + } + cls2 <- Some(someValues.getFiller) collect { case a: OWLClass => a } + } yield (cls1, prop, cls2) } } // trait RSAAxiom diff --git a/src/main/scala/rsacomb/RSAOntology.scala b/src/main/scala/rsacomb/RSAOntology.scala index e273dc5..5681843 100644 --- a/src/main/scala/rsacomb/RSAOntology.scala +++ b/src/main/scala/rsacomb/RSAOntology.scala @@ -38,6 +38,7 @@ import scalax.collection.GraphEdge.UnDiEdge import org.semanticweb.owlapi.dlsyntax.renderer.DLSyntaxObjectRenderer import tech.oxfordsemantic.jrdfox.logic._ +object RSAOntology {} /* Wrapper trait for the implicit class `RSAOntology`. */ trait RSAOntology { @@ -267,8 +268,8 @@ trait RSAOntology { val role = axiom.objectPropertyExpressionsInSignature(0) if (this.confl(role).contains(role)) { Set( - RSA.internal("v0_" ++ axiom.hashCode.toString()), - RSA.internal("v1_" ++ axiom.hashCode.toString()) + RSA.internal("v0_" ++ RSA.hashed(axiom)), + RSA.internal("v1_" ++ RSA.hashed(axiom)) ) } else { Set() @@ -329,15 +330,15 @@ trait RSAOntology { classC <- classes // Keeping this check for now if !unsafeRoles.contains(roleS) - tripleARB = Seq(classA, roleR, classB).hashCode - tripleDSC = Seq(classD, roleS, classC).hashCode + tripleARB = RSA.hashed(classA, roleR, classB) + tripleDSC = RSA.hashed(classD, roleS, classC) individual = if (tripleARB > tripleDSC) { - RSA.internal("v1_" ++ tripleDSC.hashCode.toString()) + RSA.internal("v1_" ++ tripleDSC) } else { // Note that this is also the case for // `tripleARB == tripleDSC` - RSA.internal("v0_" ++ tripleDSC.hashCode.toString()) + RSA.internal("v0_" ++ tripleDSC) } } yield individual } @@ -433,7 +434,7 @@ trait RSAOntology { private def rules1(axiom: OWLSubClassOfAxiom): List[Rule] = { val unfold = ontology.unfold(axiom).toList // Fresh Variables - val v0 = RSA.internal("v0_" ++ axiom.hashCode.toString) + val v0 = RSA.internal("v0_" ++ RSA.hashed(axiom)) val varX = Variable.create("X") // Predicates val atomA: TupleTableAtom = { @@ -484,9 +485,9 @@ trait RSAOntology { .getProperty if (ontology.confl(roleR) contains roleR) { // Fresh Variables - val v0 = RSA.internal("v0_" ++ axiom.hashCode.toString) - val v1 = RSA.internal("v1_" ++ axiom.hashCode.toString) - val v2 = RSA.internal("v2_" ++ axiom.hashCode.toString) + val v0 = RSA.internal("v0_" ++ RSA.hashed(axiom)) + val v1 = RSA.internal("v1_" ++ RSA.hashed(axiom)) + val v2 = RSA.internal("v2_" ++ RSA.hashed(axiom)) // Predicates def atomA(t: Term): TupleTableAtom = { val cls = axiom.getSubClass.asInstanceOf[OWLClass].getIRI @@ -524,7 +525,7 @@ trait RSAOntology { .asInstanceOf[OWLObjectSomeValuesFrom] .getProperty // Fresh Variables - val v1 = RSA.internal("v1_" ++ axiom.hashCode.toString) + val v1 = RSA.internal("v1_" ++ RSA.hashed(axiom)) // Predicates def atomA(t: Term): TupleTableAtom = { val cls = axiom.getSubClass.asInstanceOf[OWLClass].getIRI -- cgit v1.2.3