aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/rsacomb/CanonicalModel.scala
blob: d9e1641b1a24eda536a9bce0a3f13a006b862002 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package rsacomb

import org.semanticweb.owlapi.model.{OWLObjectInverseOf, OWLObjectProperty}
import org.semanticweb.owlapi.model.{
  OWLClass,
  // OWLObjectProperty,
  OWLSubObjectPropertyOfAxiom,
  // OWLObjectPropertyExpression,
  OWLObjectSomeValuesFrom,
  OWLSubClassOfAxiom
}

import tech.oxfordsemantic.jrdfox.logic.datalog.{
  Rule,
  BodyFormula,
  TupleTableAtom,
  Negation
}
import tech.oxfordsemantic.jrdfox.logic.expression.{
  Term,
  Variable,
  // Resource,
  IRI
}

import suffix.{Empty, Forward, Backward, Inverse}
import util.RSA

class CanonicalModel(val ontology: RSAOntology) extends RSAAxiom {

  import implicits.RDFox._
  import implicits.JavaCollections._

  val named: List[Rule] =
    ontology.individuals.map(a => Rule.create(RSA.Named(a)))

  val rolesAdditionalRules: List[Rule] = {
    // Given a role (predicate) compute additional logic rules
    def additional(pred: String): Seq[Rule] = {
      val varX = Variable.create("X")
      val varY = Variable.create("Y")
      for (
        (hSuffix, bSuffix) <- List(
          (Empty, Forward),
          (Empty, Backward),
          (Inverse, Forward + Inverse),
          (Inverse, Backward + Inverse),
          (Backward + Inverse, Forward),
          (Forward + Inverse, Backward),
          (Backward, Forward + Inverse),
          (Forward, Backward + Inverse)
        )
      )
        yield Rule.create(
          TupleTableAtom.rdf(varX, pred :: hSuffix, varY),
          TupleTableAtom.rdf(varX, pred :: bSuffix, varY)
        )
    }
    // Compute additional rules per role
    ontology.roles
      .collect { case prop: OWLObjectProperty => prop }
      .map(_.getIRI.getIRIString)
      .flatMap(additional)
  }

  private lazy val topAxioms: List[Rule] = {
    val varX = Variable.create("X")
    val varY = Variable.create("Y")
    val concepts = ontology.concepts.map(c => {
      Rule.create(
        RSA.Thing(varX),
        TupleTableAtom.rdf(varX, IRI.RDF_TYPE, c.getIRI)
      )
    })
    val roles = ontology.roles.map(r => {
      val name = r match {
        case x: OWLObjectProperty => x.getIRI.getIRIString
        case x: OWLObjectInverseOf =>
          x.getInverse.getNamedProperty.getIRI.getIRIString :: Inverse
      }
      Rule.create(
        List(RSA.Thing(varX), RSA.Thing(varY)),
        List(TupleTableAtom.rdf(varX, name, varY))
      )
    })
    concepts ::: roles
  }

  private val equalityAxioms: List[Rule] = {
    val varX = Variable.create("X")
    val varY = Variable.create("Y")
    val varZ = Variable.create("Z")
    List(
      // Reflexivity
      Rule.create(RSA.congruent(varX, varX), RSA.Thing(varX)),
      // Simmetry
      Rule.create(RSA.congruent(varY, varX), RSA.congruent(varX, varY)),
      // Transitivity
      Rule.create(
        RSA.congruent(varX, varZ),
        RSA.congruent(varX, varY),
        RSA.congruent(varY, varZ)
      )
    )
  }

  val rules: List[Rule] = {
    // Compute rules from ontology axioms
    val rules = ontology.axioms.flatMap(_.accept(RuleGenerator))
    // Return full set of rules
    rules ::: rolesAdditionalRules ::: topAxioms ::: equalityAxioms ::: named
  }

  object RuleGenerator
      extends RDFoxAxiomConverter(
        Variable.create("X"),
        ontology.unsafeRoles,
        SkolemStrategy.None,
        Empty
      ) {

    private def rules1(axiom: OWLSubClassOfAxiom): List[Rule] = {
      val unfold = ontology.unfold(axiom).toList
      // Fresh Variables
      val v0 = RSA("v0_" ++ axiom.hashed)
      val varX = Variable.create("X")
      implicit val unfoldTerm = RSA(unfold.hashCode.toString)
      // TODO: use axiom.toTriple instead
      val atomA: TupleTableAtom = {
        val cls = axiom.getSubClass.asInstanceOf[OWLClass].getIRI
        TupleTableAtom.rdf(varX, IRI.RDF_TYPE, cls)
      }
      val roleRf: TupleTableAtom = {
        val visitor =
          new RDFoxPropertyExprConverter(varX, v0, Forward)
        axiom.getSuperClass
          .asInstanceOf[OWLObjectSomeValuesFrom]
          .getProperty
          .accept(visitor)
          .head
      }
      val atomB: TupleTableAtom = {
        val cls = axiom.getSuperClass
          .asInstanceOf[OWLObjectSomeValuesFrom]
          .getFiller
          .asInstanceOf[OWLClass]
          .getIRI
        TupleTableAtom.rdf(v0, IRI.RDF_TYPE, cls)
      }
      // TODO: To be consistent with the specifics of the visitor we are
      // returning facts as `Rule`s with true body. While this is correct
      // there is an easier way to import facts into RDFox. Are we able to
      // do that?
      val facts = unfold.map(x => Rule.create(RSA.In(x)))
      val rules = List(
        Rule.create(roleRf, atomA, RSA.notIn(varX)),
        Rule.create(atomB, atomA, RSA.notIn(varX))
      )
      facts ++ rules
    }

    private def rules2(axiom: OWLSubClassOfAxiom): List[Rule] = {
      val roleR =
        axiom.getSuperClass
          .asInstanceOf[OWLObjectSomeValuesFrom]
          .getProperty
      if (ontology.confl(roleR) contains roleR) {
        // Fresh Variables
        val v0 = RSA("v0_" ++ axiom.hashed)
        val v1 = RSA("v1_" ++ axiom.hashed)
        val v2 = RSA("v2_" ++ axiom.hashed)
        // Predicates
        def atomA(t: Term): TupleTableAtom = {
          val cls = axiom.getSubClass.asInstanceOf[OWLClass].getIRI
          TupleTableAtom.rdf(t, IRI.RDF_TYPE, cls)
        }
        def roleRf(t1: Term, t2: Term): TupleTableAtom = {
          val visitor =
            new RDFoxPropertyExprConverter(t1, t2, Forward)
          roleR.accept(visitor).head
        }
        def atomB(t: Term): TupleTableAtom = {
          val cls = axiom.getSuperClass
            .asInstanceOf[OWLObjectSomeValuesFrom]
            .getFiller
            .asInstanceOf[OWLClass]
            .getIRI
          TupleTableAtom.rdf(t, IRI.RDF_TYPE, cls)
        }
        //Rules
        List(
          Rule.create(roleRf(v0, v1), atomA(v0)),
          Rule.create(atomB(v1), atomA(v0)),
          Rule.create(roleRf(v1, v2), atomA(v1)),
          Rule.create(atomB(v2), atomA(v1))
        )
      } else {
        List()
      }
    }

    private def rules3(axiom: OWLSubClassOfAxiom): List[Rule] = {
      val cycle = ontology.cycle(axiom).toList
      val roleR =
        axiom.getSuperClass
          .asInstanceOf[OWLObjectSomeValuesFrom]
          .getProperty
      // Fresh Variables
      val v1 = RSA("v1_" ++ axiom.hashed)
      // Predicates
      def atomA(t: Term): TupleTableAtom = {
        val cls = axiom.getSubClass.asInstanceOf[OWLClass].getIRI
        TupleTableAtom.rdf(t, IRI.RDF_TYPE, cls)
      }
      def roleRf(t: Term): TupleTableAtom = {
        val visitor =
          new RDFoxPropertyExprConverter(t, v1, Forward)
        roleR.accept(visitor).head
      }
      val atomB: TupleTableAtom = {
        val cls = axiom.getSuperClass
          .asInstanceOf[OWLObjectSomeValuesFrom]
          .getFiller
          .asInstanceOf[OWLClass]
          .getIRI
        TupleTableAtom.rdf(v1, IRI.RDF_TYPE, cls)
      }
      cycle.flatMap { x =>
        List(
          Rule.create(roleRf(x), atomA(x)),
          Rule.create(atomB, atomA(x))
        )
      }
    }

    override def visit(axiom: OWLSubClassOfAxiom): List[Rule] = {
      if (axiom.isT5) {
        // TODO: get role in T5 axiom
        // Assuming one role here
        val role = axiom.objectPropertyExpressionsInSignature(0)
        if (ontology.unsafeRoles contains role) {
          val visitor =
            new RDFoxAxiomConverter(
              Variable.create("X"),
              ontology.unsafeRoles,
              SkolemStrategy.Standard(axiom.toString),
              Forward
            )
          axiom.accept(visitor)
        } else {
          rules1(axiom) ::: rules2(axiom) ::: rules3(axiom)
        }
      } else {
        // Fallback to standard OWL to LP translation
        super.visit(axiom)
      }
    }

    override def visit(axiom: OWLSubObjectPropertyOfAxiom): List[Rule] = {
      val varX = Variable.create("X")
      val visitorF = new RDFoxAxiomConverter(
        varX,
        ontology.unsafeRoles,
        SkolemStrategy.None,
        Forward
      )
      val visitorB = new RDFoxAxiomConverter(
        varX,
        ontology.unsafeRoles,
        SkolemStrategy.None,
        Backward
      )
      axiom.accept(visitorB) ::: axiom.accept(visitorF)
    }

  }

}