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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
|
/*
* Copyright 2020, 2021 KRR Oxford
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package uk.ac.ox.cs.rsacomb
import java.io.File
import org.scalatest.LoneElement
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import org.semanticweb.owlapi.model._
import uk.ac.manchester.cs.owl.owlapi._
import org.semanticweb.owlapi.dlsyntax.renderer.DLSyntaxObjectRenderer
import tech.oxfordsemantic.jrdfox.logic.datalog.Rule
import tech.oxfordsemantic.jrdfox.logic.expression.Variable
import scala.collection.JavaConverters._
import uk.ac.ox.cs.rsacomb.RSAOntology
import uk.ac.ox.cs.rsacomb.approximation.Lowerbound
import uk.ac.ox.cs.rsacomb.ontology.Ontology
import uk.ac.ox.cs.rsacomb.converter.{SkolemStrategy, NoSkolem}
import uk.ac.ox.cs.rsacomb.suffix.Empty
import uk.ac.ox.cs.rsacomb.util.{Logger, RDFoxUtil, RSA}
object Ontology1_CanonicalModelSpec {
Logger.level = Logger.QUIET
/* Renderer to display OWL Axioms with DL syntax*/
val renderer = new DLSyntaxObjectRenderer()
def base(str: String): IRI =
IRI.create("http://example.com/rsa_example.owl#" + str)
val ontology_path = os.pwd / "examples" / "example1.ttl"
val ontology = Ontology(ontology_path, List()).approximate(new Lowerbound)
val program = ontology.canonicalModel
val converter = program.CanonicalModelConverter
val roleR = new OWLObjectPropertyImpl(base("R"))
val roleS = new OWLObjectPropertyImpl(base("S"))
val roleT = new OWLObjectPropertyImpl(base("T"))
val roleR_inv = roleR.getInverseProperty()
val roleS_inv = roleS.getInverseProperty()
val roleT_inv = roleT.getInverseProperty()
val AsubClassOfD = new OWLSubClassOfAxiomImpl(
new OWLClassImpl(base("A")),
new OWLClassImpl(base("D")),
Seq().asJava
)
val DsomeValuesFromRB = new OWLSubClassOfAxiomImpl(
new OWLClassImpl(base("D")),
new OWLObjectSomeValuesFromImpl(
roleR,
new OWLClassImpl(base("B"))
),
Seq().asJava
)
val BsomeValuesFromSD = new OWLSubClassOfAxiomImpl(
new OWLClassImpl(base("B")),
new OWLObjectSomeValuesFromImpl(
roleS,
new OWLClassImpl(base("D"))
),
Seq().asJava
)
val AsomeValuesFromSiC = new OWLSubClassOfAxiomImpl(
new OWLClassImpl(base("A")),
new OWLObjectSomeValuesFromImpl(
roleS_inv,
new OWLClassImpl(base("C"))
),
Seq().asJava
)
val SsubPropertyOfT = new OWLSubObjectPropertyOfAxiomImpl(
new OWLObjectPropertyImpl(base("S")),
new OWLObjectPropertyImpl(base("T")),
Seq().asJava
)
}
class Ontology1_CanonicalModelSpec
extends AnyFlatSpec
with Matchers
with LoneElement {
import Ontology1_CanonicalModelSpec._
"The program generated from Example #1" should "not be empty" in {
program.rules should not be empty
}
renderer.render(AsubClassOfD) should "be converted into a single Rule" in {
val term = Variable.create("X")
val unsafe = ontology.unsafe
val (facts, rules) =
converter.convert(AsubClassOfD, term, unsafe, NoSkolem, Empty)
facts shouldBe empty
rules.loneElement shouldBe a[Rule]
}
// Role R //
renderer.render(roleR) should "be safe" in {
ontology.unsafe should not contain roleR
}
it should "have 3 elements in its conflict set" in {
ontology.confl(roleR) should have size 3
}
it should "contain S in its conflict set" in {
ontology.confl(roleR) should contain(roleS)
}
it should "contain T in its conflict set" in {
ontology.confl(roleR) should contain(roleT)
}
it should ("contain " + renderer.render(
roleR_inv
) + " in its conflict set") in {
ontology.confl(roleR) should contain(roleR_inv)
}
// Role S //
renderer.render(roleS) should "be safe" in {
ontology.unsafe should not contain roleS
}
it should "have 3 elements in its conflict set" in {
ontology.confl(roleS) should have size 3
}
it should "contain R in its conflict set" in {
ontology.confl(roleS) should contain(roleR)
}
it should ("contain " + renderer.render(
roleS_inv
) + " in its conflict set") in {
ontology.confl(roleS) should contain(roleS_inv)
}
it should ("contain " + renderer.render(
roleT_inv
) + " in its conflict set") in {
ontology.confl(roleS) should contain(roleT_inv)
}
// S⁻
renderer.render(roleS_inv) should "be unsafe" in {
ontology.unsafe should contain(roleS_inv)
}
renderer.render(
AsomeValuesFromSiC
) should "produce 1 rule" ignore {
val term = Variable.create("X")
val unsafe = ontology.unsafe
val (facts, rules) =
converter.convert(AsomeValuesFromSiC, term, unsafe, NoSkolem, Empty)
facts shouldBe empty
rules.loneElement shouldBe a[Rule]
}
renderer.render(
DsomeValuesFromRB
) should "have a 'cycle' set of 48 elements" ignore {
// Cycle introduces a new constant for each possible triple (the
// order among triples is total). In this example there are 4
// concept names and R has 3 safe roles in its conflict set (S, T,
// Inv(R)). Triples are
// (concept, role, concept)
// and hence we have 4*3*4=48 new constants introduced.
ontology.cycle(DsomeValuesFromRB) should have size 48
}
it should "produce 48 facts and 98 rules" ignore {
// Rule 1 provides 1 rule (split in 2) + 48 fact
// Rule 2 provides 0 rules
// Rule 3 provides 48 rule (split in 2)
val term = Variable.create("X")
val unsafe = ontology.unsafe
val (facts, rules) =
converter.convert(DsomeValuesFromRB, term, unsafe, NoSkolem, Empty)
facts should have length 48
rules should have length 98
}
renderer.render(
BsomeValuesFromSD
) should "have a 'cycle' set of 32 elements" ignore {
// Cycle introduces a new constant for each possible triple (the
// order among triples is total). In this example there are 4
// concept names and S has 2 safe roles in its conflict set (R,
// Inv(T)). Triples are
// (concept, role, concept)
// and hence we have 4*2*4=32 new constants introduced.
ontology.cycle(BsomeValuesFromSD) should have size 32
}
it should "produce 5 rules" ignore {
// Rule 1 provides 1 rule (split in 2) + 32 fact
// Rule 2 provides 0 rules
// Rule 3 provides 32 rule (split in 2)
// Then (1*2 + 32) + (0) + (32*2) = 98
val term = Variable.create("X")
val unsafe = ontology.unsafe
val (facts, rules) =
converter.convert(BsomeValuesFromSD, term, unsafe, NoSkolem, Empty)
facts should have length 32
rules should have length 66
}
renderer.render(
SsubPropertyOfT
) should "produce 3 rules" in {
val term = Variable.create("X")
val unsafe = ontology.unsafe
val (facts, rules) =
converter.convert(SsubPropertyOfT, term, unsafe, NoSkolem, Empty)
facts shouldBe empty
rules should have length 3
}
}
// object Ontology2_CanonicalModelSpec {
// /* Renderer to display OWL Axioms with DL syntax*/
// val renderer = new DLSyntaxObjectRenderer()
// def base(str: String): IRI =
// IRI.create("http://example.com/rsa_example.owl#" + str)
// val ontology_path: File = new File("examples/example2.owl")
// val ontology = Ontology(ontology_path, List()).approximate(new Lowerbound)
// val program = ontology.canonicalModel
// val converter = program.CanonicalModelConverter
// val roleR = new OWLObjectPropertyImpl(base("R"))
// val roleS = new OWLObjectPropertyImpl(base("S"))
// val roleT = new OWLObjectPropertyImpl(base("T"))
// val roleP = new OWLObjectPropertyImpl(base("P"))
// val roleR_inv = roleR.getInverseProperty()
// val roleS_inv = roleS.getInverseProperty()
// val roleT_inv = roleT.getInverseProperty()
// val roleP_inv = roleP.getInverseProperty()
// val AsomeValuesFromRB = new OWLSubClassOfAxiomImpl(
// new OWLClassImpl(base("A")),
// new OWLObjectSomeValuesFromImpl(
// roleR,
// new OWLClassImpl(base("B"))
// ),
// Seq().asJava
// )
// val BsomeValuesFromSC = new OWLSubClassOfAxiomImpl(
// new OWLClassImpl(base("B")),
// new OWLObjectSomeValuesFromImpl(
// roleS,
// new OWLClassImpl(base("C"))
// ),
// Seq().asJava
// )
// val CsomeValuesFromTD = new OWLSubClassOfAxiomImpl(
// new OWLClassImpl(base("C")),
// new OWLObjectSomeValuesFromImpl(
// roleT,
// new OWLClassImpl(base("D"))
// ),
// Seq().asJava
// )
// val DsomeValuesFromPA = new OWLSubClassOfAxiomImpl(
// new OWLClassImpl(base("D")),
// new OWLObjectSomeValuesFromImpl(
// roleP,
// new OWLClassImpl(base("A"))
// ),
// Seq().asJava
// )
// }
// class Ontology2_CanonicalModelSpec
// extends AnyFlatSpec
// with Matchers
// with LoneElement {
// import Ontology2_CanonicalModelSpec._
// "The program generated from Example #1" should "not be empty" in {
// program.rules should not be empty
// }
// // Role R //
// renderer.render(roleR) should "be unsafe" in {
// ontology.unsafe should contain(roleR)
// }
// it should "have only its inverse in its conflict set" in {
// ontology.confl(roleR).loneElement shouldBe roleR_inv
// }
// // Role S //
// renderer.render(roleS) should "be unsafe" in {
// ontology.unsafe should contain(roleS)
// }
// it should "have only its inverse in its conflict set" in {
// ontology.confl(roleS).loneElement shouldBe roleS_inv
// }
// // Role T //
// renderer.render(roleT) should "be unsafe" in {
// ontology.unsafe should contain(roleT)
// }
// it should "have only its inverse in its conflict set" in {
// ontology.confl(roleT).loneElement shouldBe roleT_inv
// }
// // Role P //
// renderer.render(roleP) should "be unsafe" in {
// ontology.unsafe should contain(roleP)
// }
// it should "have only its inverse in its conflict set" in {
// ontology.confl(roleP).loneElement shouldBe roleP_inv
// }
// // A ⊑ ∃ R.B
// renderer.render(
// AsomeValuesFromRB
// ) should "produce 1 rule" in {
// val term = Variable.create("X")
// val unsafe = ontology.unsafe
// val (facts, rules) =
// converter.convert(AsomeValuesFromRB, term, unsafe, NoSkolem, Empty)
// facts shouldBe empty
// rules should have length 1
// }
// // B ⊑ ∃ S.C
// renderer.render(
// BsomeValuesFromSC
// ) should "produce 1 rule" in {
// val term = Variable.create("X")
// val unsafe = ontology.unsafe
// val (facts, rules) =
// converter.convert(BsomeValuesFromSC, term, unsafe, NoSkolem, Empty)
// facts shouldBe empty
// rules should have length 1
// }
// // C ⊑ ∃ T.D
// renderer.render(
// CsomeValuesFromTD
// ) should "produce 1 rule" in {
// val term = Variable.create("X")
// val unsafe = ontology.unsafe
// val (facts, rules) =
// converter.convert(CsomeValuesFromTD, term, unsafe, NoSkolem, Empty)
// facts shouldBe empty
// rules should have length 1
// }
// // D ⊑ ∃ P.A
// renderer.render(
// DsomeValuesFromPA
// ) should "produce 1 rule" in {
// val term = Variable.create("X")
// val unsafe = ontology.unsafe
// val (facts, rules) =
// converter.convert(DsomeValuesFromPA, term, unsafe, NoSkolem, Empty)
// facts shouldBe empty
// rules should have length 1
// }
// }
|