diff options
Diffstat (limited to 'src/main/scala/rsacomb/RDFoxClassExprConverter.scala')
| -rw-r--r-- | src/main/scala/rsacomb/RDFoxClassExprConverter.scala | 159 |
1 files changed, 0 insertions, 159 deletions
diff --git a/src/main/scala/rsacomb/RDFoxClassExprConverter.scala b/src/main/scala/rsacomb/RDFoxClassExprConverter.scala deleted file mode 100644 index f4187ed..0000000 --- a/src/main/scala/rsacomb/RDFoxClassExprConverter.scala +++ /dev/null | |||
| @@ -1,159 +0,0 @@ | |||
| 1 | package rsacomb | ||
| 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 | OWLObjectIntersectionOf, | ||
| 11 | OWLObjectOneOf, | ||
| 12 | OWLObjectMaxCardinality | ||
| 13 | } | ||
| 14 | import org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx | ||
| 15 | import tech.oxfordsemantic.jrdfox.logic.Datatype | ||
| 16 | import tech.oxfordsemantic.jrdfox.logic.datalog.{ | ||
| 17 | BindAtom, | ||
| 18 | TupleTableName, | ||
| 19 | TupleTableAtom | ||
| 20 | } | ||
| 21 | import tech.oxfordsemantic.jrdfox.logic.expression.{ | ||
| 22 | Term, | ||
| 23 | Literal, | ||
| 24 | Variable, | ||
| 25 | FunctionCall, | ||
| 26 | IRI | ||
| 27 | } | ||
| 28 | |||
| 29 | import org.semanticweb.owlapi.model.OWLObjectPropertyExpression | ||
| 30 | import org.semanticweb.owlapi.model.OWLObjectProperty | ||
| 31 | |||
| 32 | import suffix.{RSASuffix, Empty} | ||
| 33 | import util.RSA | ||
| 34 | |||
| 35 | object RDFoxClassExprConverter { | ||
| 36 | |||
| 37 | def apply( | ||
| 38 | term: Term, | ||
| 39 | unsafe: List[OWLObjectPropertyExpression] = List(), | ||
| 40 | skolem: SkolemStrategy = SkolemStrategy.None, | ||
| 41 | suffix: RSASuffix = Empty | ||
| 42 | ): RDFoxClassExprConverter = | ||
| 43 | new RDFoxClassExprConverter(term, unsafe, skolem, suffix) | ||
| 44 | |||
| 45 | def merge(rules: List[RDFoxRuleShards]): RDFoxRuleShards = { | ||
| 46 | rules.foldLeft(RDFoxRuleShards(List(), List())) { (r1, r2) => | ||
| 47 | RDFoxRuleShards( | ||
| 48 | r1.res ++ r2.res, | ||
| 49 | r1.ext ++ r2.ext | ||
| 50 | ) | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | } // object RDFoxClassExprConverter | ||
| 55 | |||
| 56 | class RDFoxClassExprConverter( | ||
| 57 | term: Term, | ||
| 58 | unsafe: List[OWLObjectPropertyExpression], | ||
| 59 | skolem: SkolemStrategy, | ||
| 60 | suffix: RSASuffix | ||
| 61 | ) extends OWLClassExpressionVisitorEx[RDFoxRuleShards] { | ||
| 62 | |||
| 63 | import implicits.RDFox._ | ||
| 64 | |||
| 65 | // OWLClass | ||
| 66 | override def visit(expr: OWLClass): RDFoxRuleShards = { | ||
| 67 | val iri: IRI = if (expr.isTopEntity()) IRI.THING else expr.getIRI() | ||
| 68 | val atom = List(TupleTableAtom.rdf(term, IRI.RDF_TYPE, iri)) | ||
| 69 | RDFoxRuleShards(atom, List()) | ||
| 70 | } | ||
| 71 | |||
| 72 | // OWLObjectIntersectionOf | ||
| 73 | override def visit(expr: OWLObjectIntersectionOf): RDFoxRuleShards = { | ||
| 74 | val visitor = new RDFoxClassExprConverter(term, unsafe, skolem, suffix) | ||
| 75 | // TODO: maybe using `flatMap` instead of `merge` + `map` works as well | ||
| 76 | RDFoxClassExprConverter.merge( | ||
| 77 | expr.asConjunctSet.asScala.toList | ||
| 78 | .map((e: OWLClassExpression) => e.accept(visitor)) | ||
| 79 | ) | ||
| 80 | } | ||
| 81 | |||
| 82 | // OWLObjectOneOf | ||
| 83 | override def visit(expr: OWLObjectOneOf): RDFoxRuleShards = { | ||
| 84 | val visitor = RDFoxClassExprConverter(term, unsafe, skolem, suffix) | ||
| 85 | // TODO: review nominal handling. Here we are taking "just" one | ||
| 86 | val ind = expr.individuals | ||
| 87 | .collect(Collectors.toList()) | ||
| 88 | .asScala | ||
| 89 | .filter(_.isOWLNamedIndividual) | ||
| 90 | .head // restricts to proper "nominals" | ||
| 91 | .asOWLNamedIndividual | ||
| 92 | .getIRI | ||
| 93 | val atom = List( | ||
| 94 | TupleTableAtom.rdf(term, IRI.SAME_AS, ind) | ||
| 95 | ) | ||
| 96 | RDFoxRuleShards(atom, List()) | ||
| 97 | } | ||
| 98 | |||
| 99 | // OWLObjectSomeValuesFrom | ||
| 100 | override def visit(expr: OWLObjectSomeValuesFrom): RDFoxRuleShards = { | ||
| 101 | val y = RSAOntology.genFreshVariable() | ||
| 102 | // Here we are assuming a role name | ||
| 103 | val prop = expr.getProperty() | ||
| 104 | // Computes the result of rule skolemization. Depending on the used | ||
| 105 | // technique it might involve the introduction of additional atoms, | ||
| 106 | // and/or fresh constants and variables. | ||
| 107 | val (head, body, term1) = skolem match { | ||
| 108 | case SkolemStrategy.None => (List(), List(), y) | ||
| 109 | case SkolemStrategy.Constant(c) => (List(), List(), c) | ||
| 110 | case SkolemStrategy.ConstantRSA(c) => { | ||
| 111 | if (unsafe.contains(prop)) | ||
| 112 | (List(RSA.PE(term, c), RSA.U(c)), List(), c) | ||
| 113 | else | ||
| 114 | (List(), List(), c) | ||
| 115 | } | ||
| 116 | case SkolemStrategy.Standard(f) => { | ||
| 117 | ( | ||
| 118 | List(), | ||
| 119 | List(BindAtom.create(FunctionCall.create("SKOLEM", f, term), y)), | ||
| 120 | y | ||
| 121 | ) | ||
| 122 | } | ||
| 123 | } | ||
| 124 | val classVisitor = | ||
| 125 | new RDFoxClassExprConverter(term1, unsafe, skolem, suffix) | ||
| 126 | val classResult = expr.getFiller.accept(classVisitor) | ||
| 127 | val propertyVisitor = new RDFoxPropertyExprConverter(term, term1, suffix) | ||
| 128 | val propertyResult = expr.getProperty.accept(propertyVisitor) | ||
| 129 | RDFoxRuleShards( | ||
| 130 | classResult.res ++ propertyResult ++ head, | ||
| 131 | classResult.ext ++ body | ||
| 132 | ) | ||
| 133 | } | ||
| 134 | |||
| 135 | // OWLObjectMaxCardinality | ||
| 136 | override def visit(expr: OWLObjectMaxCardinality): RDFoxRuleShards = { | ||
| 137 | // TODO: again, no hardcoded variables | ||
| 138 | val vars = | ||
| 139 | List(RSAOntology.genFreshVariable(), RSAOntology.genFreshVariable()) | ||
| 140 | val classResult = RDFoxClassExprConverter.merge( | ||
| 141 | vars | ||
| 142 | .map(new RDFoxClassExprConverter(_, unsafe, skolem, suffix)) | ||
| 143 | .map(expr.getFiller.accept(_)) | ||
| 144 | ) | ||
| 145 | val propertyResult = | ||
| 146 | vars | ||
| 147 | .map(new RDFoxPropertyExprConverter(term, _, suffix)) | ||
| 148 | .map(expr.getProperty.accept(_)) | ||
| 149 | .flatten | ||
| 150 | RDFoxRuleShards( | ||
| 151 | List(TupleTableAtom.rdf(vars(0), IRI.SAME_AS, vars(1))), | ||
| 152 | classResult.res ++ propertyResult | ||
| 153 | ) | ||
| 154 | } | ||
| 155 | |||
| 156 | def doDefault(expr: OWLClassExpression): RDFoxRuleShards = | ||
| 157 | RDFoxRuleShards(List(), List()) | ||
| 158 | |||
| 159 | } // class RDFoxClassExprConverter | ||
