aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/rsacomb/RDFoxClassExprConverter.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/scala/rsacomb/RDFoxClassExprConverter.scala')
-rw-r--r--src/main/scala/rsacomb/RDFoxClassExprConverter.scala119
1 files changed, 119 insertions, 0 deletions
diff --git a/src/main/scala/rsacomb/RDFoxClassExprConverter.scala b/src/main/scala/rsacomb/RDFoxClassExprConverter.scala
new file mode 100644
index 0000000..3e60461
--- /dev/null
+++ b/src/main/scala/rsacomb/RDFoxClassExprConverter.scala
@@ -0,0 +1,119 @@
1package rsacomb
2
3import scala.collection.JavaConverters._
4import java.util.stream.{Stream,Collectors}
5
6import org.semanticweb.owlapi.model.{OWLClassExpression, OWLClass, OWLObjectSomeValuesFrom, OWLObjectIntersectionOf, OWLObjectOneOf, OWLObjectMaxCardinality}
7import org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx
8import tech.oxfordsemantic.jrdfox.logic.{AtomicFormula, Bind,BuiltinFunctionCall}
9import tech.oxfordsemantic.jrdfox.logic.{Atom, Predicate, Term, Variable, Literal, Individual}
10
11import rsacomb.SkolemStrategy
12import rsacomb.RDFoxRuleShards
13
14object RDFoxClassExprConverter {
15
16 def apply(term : Term, skolem : SkolemStrategy) : RDFoxClassExprConverter =
17 new RDFoxClassExprConverter(term, skolem)
18
19 def apply(term : Term) : RDFoxClassExprConverter =
20 new RDFoxClassExprConverter(term, SkolemStrategy.None)
21
22 def merge(rules : List[RDFoxRuleShards]) : RDFoxRuleShards = {
23 rules.foldLeft(RDFoxRuleShards(List(),List())) {
24 (r1,r2) =>
25 RDFoxRuleShards(
26 r1.res ++ r2.res,
27 r1.ext ++ r2.ext
28 )
29 }
30 }
31
32} // object RDFoxClassExprConverter
33
34class RDFoxClassExprConverter(term : Term, skolem : SkolemStrategy)
35 extends OWLClassExpressionVisitorEx[RDFoxRuleShards]
36{
37
38 // OWLClass
39 override
40 def visit(expr : OWLClass) : RDFoxRuleShards = {
41 val name = expr.getIRI.getIRIString
42 val atom = List(Atom.create(Predicate.create(name), term))
43 RDFoxRuleShards(atom,List())
44 }
45
46 // OWLObjectIntersectionOf
47 override
48 def visit(expr : OWLObjectIntersectionOf) : RDFoxRuleShards = {
49 val visitor = new RDFoxClassExprConverter(term,skolem)
50 // TODO: maybe using `flatMap` instead of `merge` + `map` works as well
51 RDFoxClassExprConverter.merge (
52 expr.asConjunctSet.asScala.toList
53 .map((e : OWLClassExpression) => e.accept(visitor))
54 )
55 }
56
57 // OWLObjectOneOf
58 override
59 def visit(expr : OWLObjectOneOf) : RDFoxRuleShards = {
60 val visitor = RDFoxClassExprConverter(term,skolem)
61 // TODO: review nominal handling. Here we are taking "just" one
62 val ind = expr.individuals.collect(Collectors.toList()).asScala
63 .filter(_.isOWLNamedIndividual)
64 .head // restricts to proper "nominals"
65 .asOWLNamedIndividual.getIRI.getIRIString
66 val atom = List(Atom.create(
67 Predicate.create("owl:sameAs"), term, Individual.create(ind)
68 ))
69 RDFoxRuleShards(atom,List())
70 }
71
72 // OWLObjectSomeValuesFrom
73 override
74 def visit(expr : OWLObjectSomeValuesFrom) : RDFoxRuleShards = {
75 // TODO: variables needs to be handled at visitor level. Hardcoding
76 // the name of the varibles might lead to errors for complex cases.
77 val y = Variable.create("y")
78 val (fun,term1) = skolem match {
79 case SkolemStrategy.None => (List(),y)
80 case SkolemStrategy.Constant(c) => (List(), Individual.create(c))
81 case SkolemStrategy.Standard(f) =>
82 // At the time of writing the RDFox library does not have a
83 // particular class for the "SKOLEM" operator and it is instead
84 // a simple builtin function with a special name.
85 (List(Bind.create(BuiltinFunctionCall.create("SKOLEM",term),y)),y)
86 }
87 val classVisitor = new RDFoxClassExprConverter(term1,skolem)
88 val classResult = expr.getFiller.accept(classVisitor)
89 val propertyVisitor = new RDFoxPropertyExprConverter(term, term1, skolem)
90 val propertyResult = expr.getProperty.accept(propertyVisitor)
91 RDFoxRuleShards(
92 classResult.res ++ propertyResult,
93 fun ++ classResult.ext
94 )
95 }
96
97 // OWLObjectMaxCardinality
98 override
99 def visit(expr : OWLObjectMaxCardinality) : RDFoxRuleShards = {
100 // TODO: again, no hardcoded variables
101 val vars = List(Variable.create("y"),Variable.create("z"))
102 val classResult = RDFoxClassExprConverter.merge(
103 vars.map(new RDFoxClassExprConverter(_,skolem))
104 .map(expr.getFiller.accept(_))
105 )
106 val propertyResult =
107 vars.map(new RDFoxPropertyExprConverter(term,_,skolem))
108 .map(expr.getProperty.accept(_))
109 .flatten
110 RDFoxRuleShards(
111 List(Atom.create(Predicate.create("owl:sameAs"),vars(0),vars(1))),
112 classResult.res ++ propertyResult
113 )
114 }
115
116 def doDefault(expr : OWLClassExpression) : RDFoxRuleShards =
117 RDFoxRuleShards(List(),List())
118
119} // class RDFoxClassExprConverter