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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
package uk.ac.ox.cs.rsacomb
import org.semanticweb.owlapi.model.{OWLObjectInverseOf, OWLObjectProperty}
import org.semanticweb.owlapi.model.{
OWLClass,
OWLLogicalAxiom,
// 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 implicits.JavaCollections._
import uk.ac.ox.cs.rsacomb.converter._
import uk.ac.ox.cs.rsacomb.suffix._
import uk.ac.ox.cs.rsacomb.util.RSA
/** Canonical model generator
*
* Converts the input axioms in a given ontology into logic rules that
* can then be passed to RDFox to compute the actual canonical model
* (via materialization).
*
* @param ontology the RSA ontology the canonical model is targeting.
*/
class CanonicalModel(val ontology: RSAOntology) {
/** Simplify conversion between OWLAPI and RDFox concepts */
import implicits.RDFox._
/** Extends capabilities of
* [[tech.oxfordsemantic.jrdfox.logic.datalog.TupleTableAtom TupleTableAtom]]
*/
import uk.ac.ox.cs.rsacomb.implicits.RSAAxiom._
/** Introduce additional rules for each role.
*
* Some relations between roles and their inverse or their "suffixed"
* versions need to be explicitly stated in terms of logic rules.
*/
val rolesAdditionalRules: List[Rule] = {
ontology.roles
.collect { case prop: OWLObjectProperty => prop }
.flatMap((pred) => {
val iri = pred.getIRI.getIRIString
val (varX, varY) = (Variable.create("X"), Variable.create("Y"))
for (
(hSuffix, bSuffix) <- Seq(
(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, iri :: hSuffix, varY),
TupleTableAtom.rdf(varX, iri :: bSuffix, varY)
)
})
}
/** Top axiomatization
*
* Corresponding to the following rules:
*
* ```
* [?a, rdf:type, owl:Thing] :- [?a, rdf:type, ?b] .
* [?a, rdf:type, owl:Thing], [?b, rdf:type, owl:Thing] :- [?a, ?r, ?b], FILTER(?r != rdf:type).
* ```
*
* @note this is a naïve implementation of top axiomatization and
* might change in the future. The ideal solution would be for RDFox
* to take care of this, but at the time of writing this is not
* compatible with the way we are using the tool.
*/
private val topAxioms: List[Rule] = {
val varA = Variable.create("A")
val varR = Variable.create("R")
val varB = Variable.create("B")
List(
Rule.create(
RSA.Thing(varA),
TupleTableAtom.rdf(varA, IRI.RDF_TYPE, varB)
),
Rule.create(
List(RSA.Thing(varA), RSA.Thing(varB)),
List(
TupleTableAtom.rdf(varA, varR, varB),
FilterAtom.create(FunctionCall.notEqual(varR, IRI.RDF_TYPE))
)
)
)
}
/** Equality axiomatization
*
* Introduce reflexivity, simmetry and transitivity rules for a naïve
* equality axiomatization.
*
* @note that we are using a custom `congruent` predicate to indicate
* equality. This is to avoid interfering with the standard
* `owl:sameAs`.
*
* @note RDFox is able to handle equality in a "smart" way, but this
* behaviour is incompatible with other needed features like
* negation-as-failure and aggregates.
*
* @todo to complete the equality axiomatization we need to introduce
* substitution rules to explicate a complete "equality" semantics.
*/
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 (facts, rules): (List[TupleTableAtom], List[Rule]) = {
// Compute rules from ontology axioms
val (facts, rules) = {
val term = RSAOntology.genFreshVariable()
val unsafe = ontology.unsafeRoles
ontology.axioms
.map(CanonicalModelConverter.convert(_, term, unsafe, NoSkolem, Empty))
.unzip
}
(
facts.flatten,
rolesAdditionalRules ::: topAxioms ::: equalityAxioms ::: rules.flatten
)
}
object CanonicalModelConverter extends RDFoxConverter {
private def rules1(
axiom: OWLSubClassOfAxiom
): Result = {
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 prop =
axiom.getSuperClass.asInstanceOf[OWLObjectSomeValuesFrom].getProperty
super.convert(prop, varX, v0, Forward)
}
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 RSA.In
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 =
super.convert(roleR, t1, t2, Forward)
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 =
super.convert(roleR, t, v1, Forward)
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 convert(
axiom: OWLLogicalAxiom,
term: Term,
unsafe: List[OWLObjectPropertyExpression],
skolem: SkolemStrategy,
suffix: RSASuffix
): Result =
axiom match {
case a: OWLSubClassOfAxiom if a.isT5 => {
val role = axiom.objectPropertyExpressionsInSignature(0)
if (unsafe contains role)
super.convert(a, term, unsafe, new Standard(a), Forward)
else {
val (f1, r1) = rules1(a)
(f1, r1 ::: rules2(a) ::: rules3(a))
}
}
case a: OWLSubObjectPropertyOfAxiom => {
val (facts, rules) = List(Empty, Forward, Backward)
.map(super.convert(a, term, unsafe, NoSkolem, _))
.unzip
(facts.flatten, rules.flatten)
}
case a => super.convert(a, term, unsafe, skolem, suffix)
}
}
}
|