From 903983b48d49e17e035bd233d94cd5cb58661a19 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Fri, 13 Nov 2020 18:48:24 +0000 Subject: Simplify role extraction from axioms The previous implementation was using the visitor pattern suggested by the OWLAPI. This was buggy for some reasons and resulting in runtime exceptions (probably due to the complex class tree of the API). In Scala using type pattern matching results in a shorter and more readable way of traversing an AST. --- src/main/scala/rsacomb/RSAAxiom.scala | 76 +++++++++-------------------------- 1 file changed, 18 insertions(+), 58 deletions(-) (limited to 'src/main/scala/rsacomb') diff --git a/src/main/scala/rsacomb/RSAAxiom.scala b/src/main/scala/rsacomb/RSAAxiom.scala index f504bbe..3cd9a9d 100644 --- a/src/main/scala/rsacomb/RSAAxiom.scala +++ b/src/main/scala/rsacomb/RSAAxiom.scala @@ -20,6 +20,7 @@ import org.semanticweb.owlapi.model.{ OWLClassExpressionVisitorEx } import org.semanticweb.owlapi.model.OWLObjectProperty +import scala.collection.JavaConverters._ /* Wrapper trait for the implicit class `RSAAxiom`. */ @@ -103,68 +104,27 @@ trait RSAAxiom { * of a role if this appears in the axiom (but we will get the role * itself instead). */ - private class RSAAxiomRoleExtractor() - extends OWLAxiomVisitorEx[List[OWLObjectPropertyExpression]] { - - private class RSAExprRoleExtractor() - extends OWLClassExpressionVisitorEx[ - List[OWLObjectPropertyExpression] - ] { - override def visit( - expr: OWLObjectSomeValuesFrom - ): List[OWLObjectPropertyExpression] = - List(expr.getProperty) - - override def visit( - expr: OWLObjectMaxCardinality - ): List[OWLObjectPropertyExpression] = - List(expr.getProperty) - - /* NOTE: this instance of `visit` for `OWLClass` shouldn't be necessary. However - * if missing, the code throws a `NullPointerException`. It seems like, for some - * reason, `OWLClass` is not really a subinterface of `OWLClassExpression`, as - * stated in the JavaDocs. - */ - override def visit(expr: OWLClass): List[OWLObjectPropertyExpression] = - List() - - def doDefault( - expr: OWLClassExpression - ): List[OWLObjectPropertyExpression] = - List() - } - - override def visit( - axiom: OWLSubClassOfAxiom - ): List[OWLObjectPropertyExpression] = { - val visitor = new RSAExprRoleExtractor() - val sub = axiom.getSubClass.accept(visitor) - val sup = axiom.getSuperClass.accept(visitor) - sub ++ sup + lazy val objectPropertyExpressionsInSignature + : List[OWLObjectPropertyExpression] = + axiom match { + case a: OWLSubClassOfAxiom => + rolesInExpr(a.getSubClass) ++ rolesInExpr(a.getSuperClass) + case a: OWLEquivalentClassesAxiom => + a.getClassExpressions.asScala.toList.flatMap(rolesInExpr(_)) + case a: OWLSubObjectPropertyOfAxiom => + List(a.getSubProperty, a.getSuperProperty) + case _ => List() } - override def visit( - axiom: OWLEquivalentClassesAxiom - ): List[OWLObjectPropertyExpression] = { - // TODO - List() + private def rolesInExpr( + expr: OWLClassExpression + ): List[OWLObjectPropertyExpression] = + expr match { + case e: OWLObjectSomeValuesFrom => List(e.getProperty) + case e: OWLObjectMaxCardinality => List(e.getProperty) + case _ => List() } - override def visit( - axiom: OWLSubObjectPropertyOfAxiom - ): List[OWLObjectPropertyExpression] = - List(axiom.getSubProperty(), axiom.getSuperProperty()) - - def doDefault(axiom: OWLAxiom): List[OWLObjectPropertyExpression] = List() - } - - /* Exposed methods */ - lazy val objectPropertyExpressionsInSignature - : List[OWLObjectPropertyExpression] = { - val visitor = new RSAAxiomRoleExtractor() - axiom.accept(visitor) - } - lazy val toTriple: Option[(OWLClass, OWLObjectProperty, OWLClass)] = for { subClass <- Some(axiom) collect { case a: OWLSubClassOfAxiom => a } -- cgit v1.2.3