diff options
| author | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-12-11 17:19:33 +0000 |
|---|---|---|
| committer | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-12-11 17:19:33 +0000 |
| commit | 12ff20661c1848345473a5a4a9c200f3e7c2939a (patch) | |
| tree | d18733c6b5e41291f6b91b4810722eac0e2a4655 /src/main/scala | |
| parent | c36d98cea32c9ecd50ea1f5bc71b81d1c4adf07f (diff) | |
| download | RSAComb-12ff20661c1848345473a5a4a9c200f3e7c2939a.tar.gz RSAComb-12ff20661c1848345473a5a4a9c200f3e7c2939a.zip | |
Remove old logic program conversion code
Diffstat (limited to 'src/main/scala')
3 files changed, 0 insertions, 370 deletions
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 @@ | |||
| 1 | package uk.ac.ox.cs.rsacomb.converter | ||
| 2 | |||
| 3 | import org.semanticweb.owlapi.model.{ | ||
| 4 | OWLAxiom, | ||
| 5 | OWLSubClassOfAxiom, | ||
| 6 | OWLEquivalentClassesAxiom, | ||
| 7 | OWLObjectPropertyExpression, | ||
| 8 | OWLObjectPropertyDomainAxiom, | ||
| 9 | OWLObjectPropertyRangeAxiom, | ||
| 10 | OWLDataPropertyDomainAxiom, | ||
| 11 | OWLDataPropertyRangeAxiom, | ||
| 12 | OWLInverseObjectPropertiesAxiom | ||
| 13 | } | ||
| 14 | import org.semanticweb.owlapi.model.OWLAxiomVisitorEx | ||
| 15 | |||
| 16 | import tech.oxfordsemantic.jrdfox.logic.datalog.{ | ||
| 17 | Rule, | ||
| 18 | BodyFormula, | ||
| 19 | TupleTableAtom, | ||
| 20 | TupleTableName | ||
| 21 | } | ||
| 22 | import tech.oxfordsemantic.jrdfox.logic.expression.{ | ||
| 23 | Term, | ||
| 24 | IRI => RDFoxIRI, | ||
| 25 | Variable, | ||
| 26 | Literal | ||
| 27 | } | ||
| 28 | |||
| 29 | import scala.collection.JavaConverters._ | ||
| 30 | |||
| 31 | import org.semanticweb.owlapi.model.OWLSubObjectPropertyOfAxiom | ||
| 32 | import org.semanticweb.owlapi.model.OWLObjectProperty | ||
| 33 | import org.semanticweb.owlapi.model.OWLClassAssertionAxiom | ||
| 34 | |||
| 35 | import uk.ac.ox.cs.rsacomb.RSAOntology | ||
| 36 | import uk.ac.ox.cs.rsacomb.suffix.{RSASuffix, Empty} | ||
| 37 | import uk.ac.manchester.cs.owl.owlapi.OWLSubClassOfAxiomImpl | ||
| 38 | import uk.ac.manchester.cs.owl.owlapi.OWLObjectSomeValuesFromImpl | ||
| 39 | import uk.ac.manchester.cs.owl.owlapi.OWLClassImpl | ||
| 40 | import org.semanticweb.owlapi.model.IRI | ||
| 41 | |||
| 42 | object RDFoxAxiomConverter { | ||
| 43 | |||
| 44 | def apply( | ||
| 45 | term: Term, | ||
| 46 | unsafe: List[OWLObjectPropertyExpression], | ||
| 47 | skolem: SkolemStrategy = NoSkolem, | ||
| 48 | suffix: RSASuffix = Empty | ||
| 49 | ): RDFoxAxiomConverter = | ||
| 50 | new RDFoxAxiomConverter(term, unsafe, skolem, suffix) | ||
| 51 | |||
| 52 | } // object RDFoxAxiomConverter | ||
| 53 | |||
| 54 | class RDFoxAxiomConverter( | ||
| 55 | term: Term, | ||
| 56 | unsafe: List[OWLObjectPropertyExpression], | ||
| 57 | skolem: SkolemStrategy, | ||
| 58 | suffix: RSASuffix | ||
| 59 | ) extends OWLAxiomVisitorEx[List[Rule]] { | ||
| 60 | |||
| 61 | import uk.ac.ox.cs.rsacomb.implicits.JavaCollections._ | ||
| 62 | |||
| 63 | override def visit(axiom: OWLSubClassOfAxiom): List[Rule] = { | ||
| 64 | // Skolemization is needed only for the head of an axiom | ||
| 65 | val subVisitor = | ||
| 66 | new RDFoxClassExprConverter(term, unsafe, NoSkolem, suffix) | ||
| 67 | val superVisitor = new RDFoxClassExprConverter(term, unsafe, skolem, suffix) | ||
| 68 | // Each visitor returns a `RDFoxRuleShards`, a tuple (res,ext): | ||
| 69 | // - the `res` List is a list of atoms resulting from the conversion | ||
| 70 | // of the axiom. | ||
| 71 | // - for some Class Expressions appearing in the head of an Axiom, | ||
| 72 | // the conversion might produce atoms that need to appear in the | ||
| 73 | // body (and not in the head) of the rule. This is what the `ext` | ||
| 74 | // List is for. | ||
| 75 | val sub = axiom.getSubClass.accept(subVisitor) | ||
| 76 | val sup = axiom.getSuperClass.accept(superVisitor) | ||
| 77 | val head = sup.res.asJava | ||
| 78 | val body = (sub.res ++ sup.ext).asJava | ||
| 79 | List(Rule.create(head, body)) | ||
| 80 | } | ||
| 81 | |||
| 82 | override def visit(axiom: OWLEquivalentClassesAxiom): List[Rule] = { | ||
| 83 | for { | ||
| 84 | axiom1 <- axiom.asPairwiseAxioms.asScala.toList | ||
| 85 | axiom2 <- axiom1.asOWLSubClassOfAxioms.asScala.toList | ||
| 86 | rule <- axiom2.accept(this) | ||
| 87 | } yield rule | ||
| 88 | } | ||
| 89 | |||
| 90 | override def visit(axiom: OWLSubObjectPropertyOfAxiom): List[Rule] = { | ||
| 91 | val term1 = RSAOntology.genFreshVariable() | ||
| 92 | val subVisitor = | ||
| 93 | new RDFoxPropertyExprConverter(term, term1, suffix) | ||
| 94 | val superVisitor = new RDFoxPropertyExprConverter(term, term1, suffix) | ||
| 95 | val body: List[BodyFormula] = axiom.getSubProperty.accept(subVisitor) | ||
| 96 | val head: List[TupleTableAtom] = axiom.getSuperProperty.accept(superVisitor) | ||
| 97 | List(Rule.create(head.asJava, body.asJava)) | ||
| 98 | } | ||
| 99 | |||
| 100 | override def visit(axiom: OWLObjectPropertyDomainAxiom): List[Rule] = | ||
| 101 | axiom.asOWLSubClassOfAxiom.accept(this) | ||
| 102 | |||
| 103 | override def visit(axiom: OWLObjectPropertyRangeAxiom): List[Rule] = { | ||
| 104 | val term1 = RSAOntology.genFreshVariable() | ||
| 105 | val rangeVisitor = new RDFoxClassExprConverter(term, unsafe, skolem, suffix) | ||
| 106 | val range = axiom.getRange.accept(rangeVisitor) | ||
| 107 | val propertyVisitor = new RDFoxPropertyExprConverter(term1, term, suffix) | ||
| 108 | val prop = axiom.getProperty.accept(propertyVisitor) | ||
| 109 | List(Rule.create(range.res, range.ext ::: prop)) | ||
| 110 | } | ||
| 111 | |||
| 112 | override def visit(axiom: OWLDataPropertyDomainAxiom): List[Rule] = | ||
| 113 | axiom.asOWLSubClassOfAxiom.accept(this) | ||
| 114 | |||
| 115 | override def visit(axiom: OWLInverseObjectPropertiesAxiom): List[Rule] = | ||
| 116 | axiom.asSubObjectPropertyOfAxioms.asScala.toList.flatMap(_.accept(this)) | ||
| 117 | |||
| 118 | override def visit(axiom: OWLClassAssertionAxiom): List[Rule] = { | ||
| 119 | val ind = axiom.getIndividual | ||
| 120 | if (ind.isNamed) { | ||
| 121 | val term = RDFoxIRI.create(ind.asOWLNamedIndividual().getIRI.getIRIString) | ||
| 122 | val cls = axiom.getClassExpression | ||
| 123 | val visitor = | ||
| 124 | new RDFoxClassExprConverter(term, unsafe, NoSkolem, suffix) | ||
| 125 | val shard = cls.accept(visitor) | ||
| 126 | List(Rule.create(shard.res.asJava, shard.ext.asJava)) | ||
| 127 | } else { | ||
| 128 | List() | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | def doDefault(axiom: OWLAxiom): List[Rule] = List() | ||
| 133 | |||
| 134 | } // 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 @@ | |||
| 1 | package uk.ac.ox.cs.rsacomb.converter | ||
| 2 | |||
| 3 | import scala.collection.JavaConverters._ | ||
| 4 | import java.util.stream.{Stream, Collectors} | ||
| 5 | |||
| 6 | import org.semanticweb.owlapi.model.{ | ||
| 7 | OWLClassExpression, | ||
| 8 | OWLClass, | ||
| 9 | OWLObjectSomeValuesFrom, | ||
| 10 | OWLDataSomeValuesFrom, | ||
| 11 | OWLObjectIntersectionOf, | ||
| 12 | OWLObjectOneOf, | ||
| 13 | OWLObjectMaxCardinality | ||
| 14 | } | ||
| 15 | import org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx | ||
| 16 | import tech.oxfordsemantic.jrdfox.logic.Datatype | ||
| 17 | import tech.oxfordsemantic.jrdfox.logic.datalog.{ | ||
| 18 | BindAtom, | ||
| 19 | TupleTableName, | ||
| 20 | TupleTableAtom | ||
| 21 | } | ||
| 22 | import tech.oxfordsemantic.jrdfox.logic.expression.{ | ||
| 23 | Term, | ||
| 24 | Literal, | ||
| 25 | Variable, | ||
| 26 | FunctionCall, | ||
| 27 | IRI | ||
| 28 | } | ||
| 29 | |||
| 30 | import org.semanticweb.owlapi.model.OWLObjectPropertyExpression | ||
| 31 | import org.semanticweb.owlapi.model.OWLObjectProperty | ||
| 32 | |||
| 33 | import uk.ac.ox.cs.rsacomb.RSAOntology | ||
| 34 | import uk.ac.ox.cs.rsacomb.suffix.{RSASuffix, Empty} | ||
| 35 | import uk.ac.ox.cs.rsacomb.util.RSA | ||
| 36 | |||
| 37 | object RDFoxClassExprConverter { | ||
| 38 | |||
| 39 | def apply( | ||
| 40 | term: Term, | ||
| 41 | unsafe: List[OWLObjectPropertyExpression] = List(), | ||
| 42 | skolem: SkolemStrategy = NoSkolem, | ||
| 43 | suffix: RSASuffix = Empty | ||
| 44 | ): RDFoxClassExprConverter = | ||
| 45 | new RDFoxClassExprConverter(term, unsafe, skolem, suffix) | ||
| 46 | |||
| 47 | def merge(rules: List[RDFoxRuleShards]): RDFoxRuleShards = { | ||
| 48 | rules.foldLeft(RDFoxRuleShards(List(), List())) { (r1, r2) => | ||
| 49 | RDFoxRuleShards( | ||
| 50 | r1.res ++ r2.res, | ||
| 51 | r1.ext ++ r2.ext | ||
| 52 | ) | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | } // object RDFoxClassExprConverter | ||
| 57 | |||
| 58 | class RDFoxClassExprConverter( | ||
| 59 | term: Term, | ||
| 60 | unsafe: List[OWLObjectPropertyExpression], | ||
| 61 | skolem: SkolemStrategy = NoSkolem, | ||
| 62 | suffix: RSASuffix = Empty | ||
| 63 | ) extends OWLClassExpressionVisitorEx[RDFoxRuleShards] { | ||
| 64 | |||
| 65 | import uk.ac.ox.cs.rsacomb.implicits.RDFox._ | ||
| 66 | |||
| 67 | // OWLClass | ||
| 68 | override def visit(expr: OWLClass): RDFoxRuleShards = { | ||
| 69 | val iri: IRI = if (expr.isTopEntity()) IRI.THING else expr.getIRI() | ||
| 70 | val atom = List(TupleTableAtom.rdf(term, IRI.RDF_TYPE, iri)) | ||
| 71 | RDFoxRuleShards(atom, List()) | ||
| 72 | } | ||
| 73 | |||
| 74 | // OWLObjectIntersectionOf | ||
| 75 | override def visit(expr: OWLObjectIntersectionOf): RDFoxRuleShards = { | ||
| 76 | val visitor = new RDFoxClassExprConverter(term, unsafe, skolem, suffix) | ||
| 77 | // TODO: maybe using `flatMap` instead of `merge` + `map` works as well | ||
| 78 | RDFoxClassExprConverter.merge( | ||
| 79 | expr.asConjunctSet.asScala.toList | ||
| 80 | .map((e: OWLClassExpression) => e.accept(visitor)) | ||
| 81 | ) | ||
| 82 | } | ||
| 83 | |||
| 84 | // OWLObjectOneOf | ||
| 85 | override def visit(expr: OWLObjectOneOf): RDFoxRuleShards = { | ||
| 86 | // TODO: review nominal handling. Here we are taking "just" one | ||
| 87 | val ind = expr.individuals | ||
| 88 | .collect(Collectors.toList()) | ||
| 89 | .asScala | ||
| 90 | .filter(_.isOWLNamedIndividual) | ||
| 91 | .head // restricts to proper "nominals" | ||
| 92 | .asOWLNamedIndividual | ||
| 93 | .getIRI | ||
| 94 | val atom = List( | ||
| 95 | TupleTableAtom.rdf(term, IRI.SAME_AS, ind) | ||
| 96 | ) | ||
| 97 | RDFoxRuleShards(atom, List()) | ||
| 98 | } | ||
| 99 | |||
| 100 | // OWLObjectSomeValuesFrom | ||
| 101 | override def visit(expr: OWLObjectSomeValuesFrom): RDFoxRuleShards = { | ||
| 102 | val y = RSAOntology.genFreshVariable() | ||
| 103 | // Here we are assuming a role name | ||
| 104 | val prop = expr.getProperty() | ||
| 105 | // Computes the result of rule skolemization. Depending on the used | ||
| 106 | // technique it might involve the introduction of additional atoms, | ||
| 107 | // and/or fresh constants and variables. | ||
| 108 | val (head, body, term1) = skolem match { | ||
| 109 | case NoSkolem => (List(), List(), y) | ||
| 110 | case c: Constant => (List(), List(), c.iri) | ||
| 111 | case s: Standard => { | ||
| 112 | ( | ||
| 113 | List(), | ||
| 114 | List( | ||
| 115 | BindAtom.create(FunctionCall.create("SKOLEM", s.literal, term), y) | ||
| 116 | ), | ||
| 117 | y | ||
| 118 | ) | ||
| 119 | } | ||
| 120 | } | ||
| 121 | val classVisitor = | ||
| 122 | new RDFoxClassExprConverter(term1, unsafe, skolem, suffix) | ||
| 123 | val classResult = expr.getFiller.accept(classVisitor) | ||
| 124 | val propertyVisitor = new RDFoxPropertyExprConverter(term, term1, suffix) | ||
| 125 | val propertyResult = expr.getProperty.accept(propertyVisitor) | ||
| 126 | RDFoxRuleShards( | ||
| 127 | classResult.res ++ propertyResult ++ head, | ||
| 128 | classResult.ext ++ body | ||
| 129 | ) | ||
| 130 | } | ||
| 131 | |||
| 132 | /** Converts a [[org.semanticweb.owlapi.model.OWLDataSomeValuesFrom OWLDataSomeValuesFrom]] | ||
| 133 | * | ||
| 134 | * @note we assume the expression is "simple", meaning that the | ||
| 135 | * property involved is a role name or the inverse of a role name. | ||
| 136 | * This assumption will be lifted when we will deal with the | ||
| 137 | * normalization of the input ontology. | ||
| 138 | * | ||
| 139 | * @todo the "filler" of this OWL expression is currently ignored. We | ||
| 140 | * need to find a way (if any) to handle | ||
| 141 | * [[org.semanticweb.owlapi.model.OWLDataRange OWLDataRange]] | ||
| 142 | * in RDFox. | ||
| 143 | */ | ||
| 144 | override def visit(expr: OWLDataSomeValuesFrom): RDFoxRuleShards = { | ||
| 145 | val y = RSAOntology.genFreshVariable() | ||
| 146 | val prop = expr.getProperty() | ||
| 147 | // Computes the result of rule skolemization. Depending on the used | ||
| 148 | // technique it might involve the introduction of additional atoms, | ||
| 149 | // and/or fresh constants and variables. | ||
| 150 | val (head, body, term1) = skolem match { | ||
| 151 | case NoSkolem => (List(), List(), y) | ||
| 152 | case c: Constant => (List(), List(), c.iri) | ||
| 153 | case s: Standard => { | ||
| 154 | ( | ||
| 155 | List(), | ||
| 156 | List( | ||
| 157 | BindAtom.create(FunctionCall.create("SKOLEM", s.literal, term), y) | ||
| 158 | ), | ||
| 159 | y | ||
| 160 | ) | ||
| 161 | } | ||
| 162 | } | ||
| 163 | val propertyVisitor = new RDFoxPropertyExprConverter(term, term1, suffix) | ||
| 164 | val propertyResult = expr.getProperty.accept(propertyVisitor) | ||
| 165 | RDFoxRuleShards(head ::: propertyResult, body) | ||
| 166 | } | ||
| 167 | |||
| 168 | // OWLObjectMaxCardinality | ||
| 169 | override def visit(expr: OWLObjectMaxCardinality): RDFoxRuleShards = { | ||
| 170 | val vars = | ||
| 171 | List(RSAOntology.genFreshVariable(), RSAOntology.genFreshVariable()) | ||
| 172 | val classResult = RDFoxClassExprConverter.merge( | ||
| 173 | vars | ||
| 174 | .map(new RDFoxClassExprConverter(_, unsafe, skolem, suffix)) | ||
| 175 | .map(expr.getFiller.accept(_)) | ||
| 176 | ) | ||
| 177 | val propertyResult = | ||
| 178 | vars | ||
| 179 | .map(new RDFoxPropertyExprConverter(term, _, suffix)) | ||
| 180 | .map(expr.getProperty.accept(_)) | ||
| 181 | .flatten | ||
| 182 | RDFoxRuleShards( | ||
| 183 | List(TupleTableAtom.rdf(vars(0), IRI.SAME_AS, vars(1))), | ||
| 184 | classResult.res ++ propertyResult | ||
| 185 | ) | ||
| 186 | } | ||
| 187 | |||
| 188 | def doDefault(expr: OWLClassExpression): RDFoxRuleShards = | ||
| 189 | RDFoxRuleShards(List(), List()) | ||
| 190 | |||
| 191 | } // 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 @@ | |||
| 1 | package uk.ac.ox.cs.rsacomb.converter | ||
| 2 | |||
| 3 | import org.semanticweb.owlapi.model.{ | ||
| 4 | OWLAnnotationProperty, | ||
| 5 | OWLDataProperty, | ||
| 6 | OWLObjectInverseOf, | ||
| 7 | OWLObjectProperty, | ||
| 8 | OWLPropertyExpression | ||
| 9 | } | ||
| 10 | import org.semanticweb.owlapi.model.OWLPropertyExpressionVisitorEx | ||
| 11 | |||
| 12 | import tech.oxfordsemantic.jrdfox.logic.datalog.TupleTableAtom | ||
| 13 | import tech.oxfordsemantic.jrdfox.logic.expression.{Term, IRI, Literal} | ||
| 14 | |||
| 15 | import uk.ac.ox.cs.rsacomb.suffix.{RSASuffix, Inverse} | ||
| 16 | |||
| 17 | class RDFoxPropertyExprConverter( | ||
| 18 | term1: Term, | ||
| 19 | term2: Term, | ||
| 20 | suffix: RSASuffix | ||
| 21 | ) extends OWLPropertyExpressionVisitorEx[List[TupleTableAtom]] { | ||
| 22 | |||
| 23 | // Automatically converts OWLAPI types into RDFox equivalent types. | ||
| 24 | import uk.ac.ox.cs.rsacomb.implicits.RDFox._ | ||
| 25 | |||
| 26 | override def visit(expr: OWLObjectProperty): List[TupleTableAtom] = { | ||
| 27 | val base = expr.getIRI.getIRIString | ||
| 28 | val pred = IRI.create(base :: suffix) | ||
| 29 | List(TupleTableAtom.rdf(term1, pred, term2)) | ||
| 30 | } | ||
| 31 | |||
| 32 | override def visit(expr: OWLDataProperty): List[TupleTableAtom] = { | ||
| 33 | val base = expr.getIRI.getIRIString | ||
| 34 | val pred = IRI.create(base :: suffix) | ||
| 35 | List(TupleTableAtom.rdf(term1, pred, term2)) | ||
| 36 | } | ||
| 37 | |||
| 38 | override def visit(expr: OWLObjectInverseOf): List[TupleTableAtom] = { | ||
| 39 | val visitor = new RDFoxPropertyExprConverter(term1, term2, suffix + Inverse) | ||
| 40 | expr.getInverse.accept(visitor) | ||
| 41 | } | ||
| 42 | |||
| 43 | def doDefault(expr: OWLPropertyExpression): List[TupleTableAtom] = List() | ||
| 44 | |||
| 45 | } // class RDFoxPropertyExprConverter | ||
