From 12ff20661c1848345473a5a4a9c200f3e7c2939a Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Fri, 11 Dec 2020 17:19:33 +0000 Subject: Remove old logic program conversion code --- .../cs/rsacomb/converter/RDFoxAxiomConverter.scala | 134 --------------- .../converter/RDFoxClassExprConverter.scala | 191 --------------------- .../converter/RDFoxPropertyExprConverter.scala | 45 ----- 3 files changed, 370 deletions(-) delete mode 100644 src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxAxiomConverter.scala delete mode 100644 src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxClassExprConverter.scala delete mode 100644 src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxPropertyExprConverter.scala (limited to 'src/main/scala') diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxAxiomConverter.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxAxiomConverter.scala deleted file mode 100644 index 1fbf28a..0000000 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxAxiomConverter.scala +++ /dev/null @@ -1,134 +0,0 @@ -package uk.ac.ox.cs.rsacomb.converter - -import org.semanticweb.owlapi.model.{ - OWLAxiom, - OWLSubClassOfAxiom, - OWLEquivalentClassesAxiom, - OWLObjectPropertyExpression, - OWLObjectPropertyDomainAxiom, - OWLObjectPropertyRangeAxiom, - OWLDataPropertyDomainAxiom, - OWLDataPropertyRangeAxiom, - OWLInverseObjectPropertiesAxiom -} -import org.semanticweb.owlapi.model.OWLAxiomVisitorEx - -import tech.oxfordsemantic.jrdfox.logic.datalog.{ - Rule, - BodyFormula, - TupleTableAtom, - TupleTableName -} -import tech.oxfordsemantic.jrdfox.logic.expression.{ - Term, - IRI => RDFoxIRI, - Variable, - Literal -} - -import scala.collection.JavaConverters._ - -import org.semanticweb.owlapi.model.OWLSubObjectPropertyOfAxiom -import org.semanticweb.owlapi.model.OWLObjectProperty -import org.semanticweb.owlapi.model.OWLClassAssertionAxiom - -import uk.ac.ox.cs.rsacomb.RSAOntology -import uk.ac.ox.cs.rsacomb.suffix.{RSASuffix, Empty} -import uk.ac.manchester.cs.owl.owlapi.OWLSubClassOfAxiomImpl -import uk.ac.manchester.cs.owl.owlapi.OWLObjectSomeValuesFromImpl -import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl -import org.semanticweb.owlapi.model.IRI - -object RDFoxAxiomConverter { - - def apply( - term: Term, - unsafe: List[OWLObjectPropertyExpression], - skolem: SkolemStrategy = NoSkolem, - suffix: RSASuffix = Empty - ): RDFoxAxiomConverter = - new RDFoxAxiomConverter(term, unsafe, skolem, suffix) - -} // object RDFoxAxiomConverter - -class RDFoxAxiomConverter( - term: Term, - unsafe: List[OWLObjectPropertyExpression], - skolem: SkolemStrategy, - suffix: RSASuffix -) extends OWLAxiomVisitorEx[List[Rule]] { - - import uk.ac.ox.cs.rsacomb.implicits.JavaCollections._ - - override def visit(axiom: OWLSubClassOfAxiom): List[Rule] = { - // Skolemization is needed only for the head of an axiom - val subVisitor = - new RDFoxClassExprConverter(term, unsafe, NoSkolem, suffix) - val superVisitor = new RDFoxClassExprConverter(term, unsafe, skolem, suffix) - // Each visitor returns a `RDFoxRuleShards`, a tuple (res,ext): - // - the `res` List is a list of atoms resulting from the conversion - // of the axiom. - // - for some Class Expressions appearing in the head of an Axiom, - // the conversion might produce atoms that need to appear in the - // body (and not in the head) of the rule. This is what the `ext` - // List is for. - val sub = axiom.getSubClass.accept(subVisitor) - val sup = axiom.getSuperClass.accept(superVisitor) - val head = sup.res.asJava - val body = (sub.res ++ sup.ext).asJava - List(Rule.create(head, body)) - } - - override def visit(axiom: OWLEquivalentClassesAxiom): List[Rule] = { - for { - axiom1 <- axiom.asPairwiseAxioms.asScala.toList - axiom2 <- axiom1.asOWLSubClassOfAxioms.asScala.toList - rule <- axiom2.accept(this) - } yield rule - } - - override def visit(axiom: OWLSubObjectPropertyOfAxiom): List[Rule] = { - val term1 = RSAOntology.genFreshVariable() - val subVisitor = - new RDFoxPropertyExprConverter(term, term1, suffix) - val superVisitor = new RDFoxPropertyExprConverter(term, term1, suffix) - val body: List[BodyFormula] = axiom.getSubProperty.accept(subVisitor) - val head: List[TupleTableAtom] = axiom.getSuperProperty.accept(superVisitor) - List(Rule.create(head.asJava, body.asJava)) - } - - override def visit(axiom: OWLObjectPropertyDomainAxiom): List[Rule] = - axiom.asOWLSubClassOfAxiom.accept(this) - - override def visit(axiom: OWLObjectPropertyRangeAxiom): List[Rule] = { - val term1 = RSAOntology.genFreshVariable() - val rangeVisitor = new RDFoxClassExprConverter(term, unsafe, skolem, suffix) - val range = axiom.getRange.accept(rangeVisitor) - val propertyVisitor = new RDFoxPropertyExprConverter(term1, term, suffix) - val prop = axiom.getProperty.accept(propertyVisitor) - List(Rule.create(range.res, range.ext ::: prop)) - } - - override def visit(axiom: OWLDataPropertyDomainAxiom): List[Rule] = - axiom.asOWLSubClassOfAxiom.accept(this) - - override def visit(axiom: OWLInverseObjectPropertiesAxiom): List[Rule] = - axiom.asSubObjectPropertyOfAxioms.asScala.toList.flatMap(_.accept(this)) - - override def visit(axiom: OWLClassAssertionAxiom): List[Rule] = { - val ind = axiom.getIndividual - if (ind.isNamed) { - val term = RDFoxIRI.create(ind.asOWLNamedIndividual().getIRI.getIRIString) - val cls = axiom.getClassExpression - val visitor = - new RDFoxClassExprConverter(term, unsafe, NoSkolem, suffix) - val shard = cls.accept(visitor) - List(Rule.create(shard.res.asJava, shard.ext.asJava)) - } else { - List() - } - } - - def doDefault(axiom: OWLAxiom): List[Rule] = List() - -} // class RDFoxAxiomConverter diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxClassExprConverter.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxClassExprConverter.scala deleted file mode 100644 index 9551c61..0000000 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxClassExprConverter.scala +++ /dev/null @@ -1,191 +0,0 @@ -package uk.ac.ox.cs.rsacomb.converter - -import scala.collection.JavaConverters._ -import java.util.stream.{Stream, Collectors} - -import org.semanticweb.owlapi.model.{ - OWLClassExpression, - OWLClass, - OWLObjectSomeValuesFrom, - OWLDataSomeValuesFrom, - OWLObjectIntersectionOf, - OWLObjectOneOf, - OWLObjectMaxCardinality -} -import org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx -import tech.oxfordsemantic.jrdfox.logic.Datatype -import tech.oxfordsemantic.jrdfox.logic.datalog.{ - BindAtom, - TupleTableName, - TupleTableAtom -} -import tech.oxfordsemantic.jrdfox.logic.expression.{ - Term, - Literal, - Variable, - FunctionCall, - IRI -} - -import org.semanticweb.owlapi.model.OWLObjectPropertyExpression -import org.semanticweb.owlapi.model.OWLObjectProperty - -import uk.ac.ox.cs.rsacomb.RSAOntology -import uk.ac.ox.cs.rsacomb.suffix.{RSASuffix, Empty} -import uk.ac.ox.cs.rsacomb.util.RSA - -object RDFoxClassExprConverter { - - def apply( - term: Term, - unsafe: List[OWLObjectPropertyExpression] = List(), - skolem: SkolemStrategy = NoSkolem, - suffix: RSASuffix = Empty - ): RDFoxClassExprConverter = - new RDFoxClassExprConverter(term, unsafe, skolem, suffix) - - def merge(rules: List[RDFoxRuleShards]): RDFoxRuleShards = { - rules.foldLeft(RDFoxRuleShards(List(), List())) { (r1, r2) => - RDFoxRuleShards( - r1.res ++ r2.res, - r1.ext ++ r2.ext - ) - } - } - -} // object RDFoxClassExprConverter - -class RDFoxClassExprConverter( - term: Term, - unsafe: List[OWLObjectPropertyExpression], - skolem: SkolemStrategy = NoSkolem, - suffix: RSASuffix = Empty -) extends OWLClassExpressionVisitorEx[RDFoxRuleShards] { - - import uk.ac.ox.cs.rsacomb.implicits.RDFox._ - - // OWLClass - override def visit(expr: OWLClass): RDFoxRuleShards = { - val iri: IRI = if (expr.isTopEntity()) IRI.THING else expr.getIRI() - val atom = List(TupleTableAtom.rdf(term, IRI.RDF_TYPE, iri)) - RDFoxRuleShards(atom, List()) - } - - // OWLObjectIntersectionOf - override def visit(expr: OWLObjectIntersectionOf): RDFoxRuleShards = { - val visitor = new RDFoxClassExprConverter(term, unsafe, skolem, suffix) - // TODO: maybe using `flatMap` instead of `merge` + `map` works as well - RDFoxClassExprConverter.merge( - expr.asConjunctSet.asScala.toList - .map((e: OWLClassExpression) => e.accept(visitor)) - ) - } - - // OWLObjectOneOf - override def visit(expr: OWLObjectOneOf): RDFoxRuleShards = { - // TODO: review nominal handling. Here we are taking "just" one - val ind = expr.individuals - .collect(Collectors.toList()) - .asScala - .filter(_.isOWLNamedIndividual) - .head // restricts to proper "nominals" - .asOWLNamedIndividual - .getIRI - val atom = List( - TupleTableAtom.rdf(term, IRI.SAME_AS, ind) - ) - RDFoxRuleShards(atom, List()) - } - - // OWLObjectSomeValuesFrom - override def visit(expr: OWLObjectSomeValuesFrom): RDFoxRuleShards = { - val y = RSAOntology.genFreshVariable() - // Here we are assuming a role name - val prop = expr.getProperty() - // Computes the result of rule skolemization. Depending on the used - // technique it might involve the introduction of additional atoms, - // and/or fresh constants and variables. - val (head, body, term1) = skolem match { - case NoSkolem => (List(), List(), y) - case c: Constant => (List(), List(), c.iri) - case s: Standard => { - ( - List(), - List( - BindAtom.create(FunctionCall.create("SKOLEM", s.literal, term), y) - ), - y - ) - } - } - val classVisitor = - new RDFoxClassExprConverter(term1, unsafe, skolem, suffix) - val classResult = expr.getFiller.accept(classVisitor) - val propertyVisitor = new RDFoxPropertyExprConverter(term, term1, suffix) - val propertyResult = expr.getProperty.accept(propertyVisitor) - RDFoxRuleShards( - classResult.res ++ propertyResult ++ head, - classResult.ext ++ body - ) - } - - /** Converts a [[org.semanticweb.owlapi.model.OWLDataSomeValuesFrom OWLDataSomeValuesFrom]] - * - * @note we assume the expression is "simple", meaning that the - * property involved is a role name or the inverse of a role name. - * This assumption will be lifted when we will deal with the - * normalization of the input ontology. - * - * @todo the "filler" of this OWL expression is currently ignored. We - * need to find a way (if any) to handle - * [[org.semanticweb.owlapi.model.OWLDataRange OWLDataRange]] - * in RDFox. - */ - override def visit(expr: OWLDataSomeValuesFrom): RDFoxRuleShards = { - val y = RSAOntology.genFreshVariable() - val prop = expr.getProperty() - // Computes the result of rule skolemization. Depending on the used - // technique it might involve the introduction of additional atoms, - // and/or fresh constants and variables. - val (head, body, term1) = skolem match { - case NoSkolem => (List(), List(), y) - case c: Constant => (List(), List(), c.iri) - case s: Standard => { - ( - List(), - List( - BindAtom.create(FunctionCall.create("SKOLEM", s.literal, term), y) - ), - y - ) - } - } - val propertyVisitor = new RDFoxPropertyExprConverter(term, term1, suffix) - val propertyResult = expr.getProperty.accept(propertyVisitor) - RDFoxRuleShards(head ::: propertyResult, body) - } - - // OWLObjectMaxCardinality - override def visit(expr: OWLObjectMaxCardinality): RDFoxRuleShards = { - val vars = - List(RSAOntology.genFreshVariable(), RSAOntology.genFreshVariable()) - val classResult = RDFoxClassExprConverter.merge( - vars - .map(new RDFoxClassExprConverter(_, unsafe, skolem, suffix)) - .map(expr.getFiller.accept(_)) - ) - val propertyResult = - vars - .map(new RDFoxPropertyExprConverter(term, _, suffix)) - .map(expr.getProperty.accept(_)) - .flatten - RDFoxRuleShards( - List(TupleTableAtom.rdf(vars(0), IRI.SAME_AS, vars(1))), - classResult.res ++ propertyResult - ) - } - - def doDefault(expr: OWLClassExpression): RDFoxRuleShards = - RDFoxRuleShards(List(), List()) - -} // class RDFoxClassExprConverter diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxPropertyExprConverter.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxPropertyExprConverter.scala deleted file mode 100644 index 46a70fa..0000000 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxPropertyExprConverter.scala +++ /dev/null @@ -1,45 +0,0 @@ -package uk.ac.ox.cs.rsacomb.converter - -import org.semanticweb.owlapi.model.{ - OWLAnnotationProperty, - OWLDataProperty, - OWLObjectInverseOf, - OWLObjectProperty, - OWLPropertyExpression -} -import org.semanticweb.owlapi.model.OWLPropertyExpressionVisitorEx - -import tech.oxfordsemantic.jrdfox.logic.datalog.TupleTableAtom -import tech.oxfordsemantic.jrdfox.logic.expression.{Term, IRI, Literal} - -import uk.ac.ox.cs.rsacomb.suffix.{RSASuffix, Inverse} - -class RDFoxPropertyExprConverter( - term1: Term, - term2: Term, - suffix: RSASuffix -) extends OWLPropertyExpressionVisitorEx[List[TupleTableAtom]] { - - // Automatically converts OWLAPI types into RDFox equivalent types. - import uk.ac.ox.cs.rsacomb.implicits.RDFox._ - - override def visit(expr: OWLObjectProperty): List[TupleTableAtom] = { - val base = expr.getIRI.getIRIString - val pred = IRI.create(base :: suffix) - List(TupleTableAtom.rdf(term1, pred, term2)) - } - - override def visit(expr: OWLDataProperty): List[TupleTableAtom] = { - val base = expr.getIRI.getIRIString - val pred = IRI.create(base :: suffix) - List(TupleTableAtom.rdf(term1, pred, term2)) - } - - override def visit(expr: OWLObjectInverseOf): List[TupleTableAtom] = { - val visitor = new RDFoxPropertyExprConverter(term1, term2, suffix + Inverse) - expr.getInverse.accept(visitor) - } - - def doDefault(expr: OWLPropertyExpression): List[TupleTableAtom] = List() - -} // class RDFoxPropertyExprConverter -- cgit v1.2.3