diff options
author | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-11-18 19:13:25 +0000 |
---|---|---|
committer | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-11-18 19:13:25 +0000 |
commit | 1efc189e90240c162b54cbc50362b46786643dad (patch) | |
tree | 9beabe0a2af7ba1674aea0060787782aa72e8a83 /src/test/scala/rsacomb/CanonicalModelSpec.scala | |
parent | a45aeff72b82bbc9a52f10929bf15b414c868525 (diff) | |
download | RSAComb-1efc189e90240c162b54cbc50362b46786643dad.tar.gz RSAComb-1efc189e90240c162b54cbc50362b46786643dad.zip |
Reorganize project with Java-like folder structure
Diffstat (limited to 'src/test/scala/rsacomb/CanonicalModelSpec.scala')
-rw-r--r-- | src/test/scala/rsacomb/CanonicalModelSpec.scala | 369 |
1 files changed, 0 insertions, 369 deletions
diff --git a/src/test/scala/rsacomb/CanonicalModelSpec.scala b/src/test/scala/rsacomb/CanonicalModelSpec.scala deleted file mode 100644 index 06602e3..0000000 --- a/src/test/scala/rsacomb/CanonicalModelSpec.scala +++ /dev/null | |||
@@ -1,369 +0,0 @@ | |||
1 | package rsacomb | ||
2 | |||
3 | import java.io.File | ||
4 | import org.scalatest.LoneElement | ||
5 | import org.scalatest.flatspec.AnyFlatSpec | ||
6 | import org.scalatest.matchers.should.Matchers | ||
7 | |||
8 | import org.semanticweb.owlapi.model._ | ||
9 | import uk.ac.manchester.cs.owl.owlapi._ | ||
10 | import org.semanticweb.owlapi.dlsyntax.renderer.DLSyntaxObjectRenderer | ||
11 | |||
12 | import tech.oxfordsemantic.jrdfox.logic.datalog.Rule | ||
13 | import tech.oxfordsemantic.jrdfox.logic.expression.Variable | ||
14 | |||
15 | import scala.collection.JavaConverters._ | ||
16 | |||
17 | import rsacomb.util.{RDFoxHelpers, RSA} | ||
18 | |||
19 | object Ontology1_CanonicalModelSpec { | ||
20 | |||
21 | /* Renderer to display OWL Axioms with DL syntax*/ | ||
22 | val renderer = new DLSyntaxObjectRenderer() | ||
23 | |||
24 | def base(str: String): IRI = | ||
25 | IRI.create("http://example.com/rsa_example.owl#" + str) | ||
26 | |||
27 | val ontology_path: File = new File("examples/example1.ttl") | ||
28 | val ontology = RSAOntology(ontology_path) | ||
29 | val program = ontology.canonicalModel | ||
30 | |||
31 | val roleR = new OWLObjectPropertyImpl(base("R")) | ||
32 | val roleS = new OWLObjectPropertyImpl(base("S")) | ||
33 | val roleT = new OWLObjectPropertyImpl(base("T")) | ||
34 | val roleR_inv = roleR.getInverseProperty() | ||
35 | val roleS_inv = roleS.getInverseProperty() | ||
36 | val roleT_inv = roleT.getInverseProperty() | ||
37 | |||
38 | val AsubClassOfD = new OWLSubClassOfAxiomImpl( | ||
39 | new OWLClassImpl(base("A")), | ||
40 | new OWLClassImpl(base("D")), | ||
41 | Seq().asJava | ||
42 | ) | ||
43 | |||
44 | val DsomeValuesFromRB = new OWLSubClassOfAxiomImpl( | ||
45 | new OWLClassImpl(base("D")), | ||
46 | new OWLObjectSomeValuesFromImpl( | ||
47 | roleR, | ||
48 | new OWLClassImpl(base("B")) | ||
49 | ), | ||
50 | Seq().asJava | ||
51 | ) | ||
52 | |||
53 | val BsomeValuesFromSD = new OWLSubClassOfAxiomImpl( | ||
54 | new OWLClassImpl(base("B")), | ||
55 | new OWLObjectSomeValuesFromImpl( | ||
56 | roleS, | ||
57 | new OWLClassImpl(base("D")) | ||
58 | ), | ||
59 | Seq().asJava | ||
60 | ) | ||
61 | |||
62 | val AsomeValuesFromSiC = new OWLSubClassOfAxiomImpl( | ||
63 | new OWLClassImpl(base("A")), | ||
64 | new OWLObjectSomeValuesFromImpl( | ||
65 | roleS_inv, | ||
66 | new OWLClassImpl(base("C")) | ||
67 | ), | ||
68 | Seq().asJava | ||
69 | ) | ||
70 | |||
71 | val SsubPropertyOfT = new OWLSubObjectPropertyOfAxiomImpl( | ||
72 | new OWLObjectPropertyImpl(base("S")), | ||
73 | new OWLObjectPropertyImpl(base("T")), | ||
74 | Seq().asJava | ||
75 | ) | ||
76 | |||
77 | } | ||
78 | |||
79 | class Ontology1_CanonicalModelSpec | ||
80 | extends AnyFlatSpec | ||
81 | with Matchers | ||
82 | with LoneElement { | ||
83 | |||
84 | import Ontology1_CanonicalModelSpec._ | ||
85 | |||
86 | "The program generated from Example #1" should "not be empty" in { | ||
87 | program.rules should not be empty | ||
88 | } | ||
89 | |||
90 | renderer.render(AsubClassOfD) should "be converted into a single Rule" in { | ||
91 | val varX = Variable.create("X") | ||
92 | val visitor = program.RuleGenerator | ||
93 | val rules = AsubClassOfD.accept(visitor) | ||
94 | rules.loneElement shouldBe a[Rule] | ||
95 | } | ||
96 | |||
97 | // Role R // | ||
98 | |||
99 | renderer.render(roleR) should "be safe" in { | ||
100 | ontology.unsafeRoles should not contain roleR | ||
101 | } | ||
102 | |||
103 | it should "have 3 elements in its conflict set" in { | ||
104 | ontology.confl(roleR) should have size 3 | ||
105 | } | ||
106 | |||
107 | it should "contain S in its conflict set" in { | ||
108 | ontology.confl(roleR) should contain(roleS) | ||
109 | } | ||
110 | |||
111 | it should "contain T in its conflict set" in { | ||
112 | ontology.confl(roleR) should contain(roleT) | ||
113 | } | ||
114 | |||
115 | it should ("contain " + renderer.render( | ||
116 | roleR_inv | ||
117 | ) + " in its conflict set") in { | ||
118 | ontology.confl(roleR) should contain(roleR_inv) | ||
119 | } | ||
120 | |||
121 | // Role S // | ||
122 | |||
123 | renderer.render(roleS) should "be safe" in { | ||
124 | ontology.unsafeRoles should not contain roleS | ||
125 | } | ||
126 | |||
127 | it should "have 3 elements in its conflict set" in { | ||
128 | ontology.confl(roleS) should have size 3 | ||
129 | } | ||
130 | |||
131 | it should "contain R in its conflict set" in { | ||
132 | ontology.confl(roleS) should contain(roleR) | ||
133 | } | ||
134 | |||
135 | it should ("contain " + renderer.render( | ||
136 | roleS_inv | ||
137 | ) + " in its conflict set") in { | ||
138 | ontology.confl(roleS) should contain(roleS_inv) | ||
139 | } | ||
140 | |||
141 | it should ("contain " + renderer.render( | ||
142 | roleT_inv | ||
143 | ) + " in its conflict set") in { | ||
144 | ontology.confl(roleS) should contain(roleT_inv) | ||
145 | } | ||
146 | |||
147 | // S⁻ | ||
148 | |||
149 | renderer.render(roleS_inv) should "be unsafe" in { | ||
150 | ontology.unsafeRoles should contain(roleS_inv) | ||
151 | } | ||
152 | |||
153 | renderer.render( | ||
154 | AsomeValuesFromSiC | ||
155 | ) should "produce 1 rule" in { | ||
156 | val varX = Variable.create("X") | ||
157 | val visitor = program.RuleGenerator | ||
158 | val rules = AsomeValuesFromSiC.accept(visitor) | ||
159 | rules should have length 1 | ||
160 | } | ||
161 | |||
162 | renderer.render( | ||
163 | DsomeValuesFromRB | ||
164 | ) should "have a 'cycle' set of 48 elements" in { | ||
165 | // Cycle introduces a new constant for each possible triple (the | ||
166 | // order among triples is total). In this example there are 4 | ||
167 | // concept names and R has 3 safe roles in its conflict set (S, T, | ||
168 | // Inv(R)). Triples are | ||
169 | // (concept, role, concept) | ||
170 | // and hence we have 4*3*4=48 new constants introduced. | ||
171 | ontology.cycle(DsomeValuesFromRB) should have size 48 | ||
172 | } | ||
173 | |||
174 | it should "produce 5 rules" in { | ||
175 | // Rule 1 provides 1 rule (split in 2) + 48 fact | ||
176 | // Rule 2 provides 0 rules | ||
177 | // Rule 3 provides 48 rule (split in 2) | ||
178 | // Then (1*2 + 48) + (0) + (48*2) = 146 | ||
179 | val varX = Variable.create("X") | ||
180 | val visitor = program.RuleGenerator | ||
181 | val rules = DsomeValuesFromRB.accept(visitor) | ||
182 | rules should have length 146 | ||
183 | } | ||
184 | |||
185 | renderer.render( | ||
186 | BsomeValuesFromSD | ||
187 | ) should "have a 'cycle' set of 32 elements" in { | ||
188 | // Cycle introduces a new constant for each possible triple (the | ||
189 | // order among triples is total). In this example there are 4 | ||
190 | // concept names and S has 2 safe roles in its conflict set (R, | ||
191 | // Inv(T)). Triples are | ||
192 | // (concept, role, concept) | ||
193 | // and hence we have 4*2*4=32 new constants introduced. | ||
194 | ontology.cycle(BsomeValuesFromSD) should have size 32 | ||
195 | } | ||
196 | |||
197 | it should "produce 5 rules" in { | ||
198 | // Rule 1 provides 1 rule (split in 2) + 32 fact | ||
199 | // Rule 2 provides 0 rules | ||
200 | // Rule 3 provides 32 rule (split in 2) | ||
201 | // Then (1*2 + 32) + (0) + (32*2) = 98 | ||
202 | val varX = Variable.create("X") | ||
203 | val visitor = program.RuleGenerator | ||
204 | val rules = DsomeValuesFromRB.accept(visitor) | ||
205 | rules should have length 146 | ||
206 | } | ||
207 | |||
208 | renderer.render( | ||
209 | SsubPropertyOfT | ||
210 | ) should "produce 2 rules" in { | ||
211 | val varX = Variable.create("X") | ||
212 | val visitor = program.RuleGenerator | ||
213 | val rules = SsubPropertyOfT.accept(visitor) | ||
214 | rules should have length 2 | ||
215 | } | ||
216 | |||
217 | } | ||
218 | |||
219 | object Ontology2_CanonicalModelSpec { | ||
220 | |||
221 | /* Renderer to display OWL Axioms with DL syntax*/ | ||
222 | val renderer = new DLSyntaxObjectRenderer() | ||
223 | |||
224 | def base(str: String): IRI = | ||
225 | IRI.create("http://example.com/rsa_example.owl#" + str) | ||
226 | |||
227 | val ontology_path: File = new File("examples/example2.owl") | ||
228 | val ontology = RSAOntology(ontology_path) | ||
229 | val program = ontology.canonicalModel | ||
230 | |||
231 | val roleR = new OWLObjectPropertyImpl(base("R")) | ||
232 | val roleS = new OWLObjectPropertyImpl(base("S")) | ||
233 | val roleT = new OWLObjectPropertyImpl(base("T")) | ||
234 | val roleP = new OWLObjectPropertyImpl(base("P")) | ||
235 | val roleR_inv = roleR.getInverseProperty() | ||
236 | val roleS_inv = roleS.getInverseProperty() | ||
237 | val roleT_inv = roleT.getInverseProperty() | ||
238 | val roleP_inv = roleP.getInverseProperty() | ||
239 | |||
240 | val AsomeValuesFromRB = new OWLSubClassOfAxiomImpl( | ||
241 | new OWLClassImpl(base("A")), | ||
242 | new OWLObjectSomeValuesFromImpl( | ||
243 | roleR, | ||
244 | new OWLClassImpl(base("B")) | ||
245 | ), | ||
246 | Seq().asJava | ||
247 | ) | ||
248 | |||
249 | val BsomeValuesFromSC = new OWLSubClassOfAxiomImpl( | ||
250 | new OWLClassImpl(base("B")), | ||
251 | new OWLObjectSomeValuesFromImpl( | ||
252 | roleS, | ||
253 | new OWLClassImpl(base("C")) | ||
254 | ), | ||
255 | Seq().asJava | ||
256 | ) | ||
257 | |||
258 | val CsomeValuesFromTD = new OWLSubClassOfAxiomImpl( | ||
259 | new OWLClassImpl(base("C")), | ||
260 | new OWLObjectSomeValuesFromImpl( | ||
261 | roleT, | ||
262 | new OWLClassImpl(base("D")) | ||
263 | ), | ||
264 | Seq().asJava | ||
265 | ) | ||
266 | |||
267 | val DsomeValuesFromPA = new OWLSubClassOfAxiomImpl( | ||
268 | new OWLClassImpl(base("D")), | ||
269 | new OWLObjectSomeValuesFromImpl( | ||
270 | roleP, | ||
271 | new OWLClassImpl(base("A")) | ||
272 | ), | ||
273 | Seq().asJava | ||
274 | ) | ||
275 | |||
276 | } | ||
277 | |||
278 | class Ontology2_CanonicalModelSpec | ||
279 | extends AnyFlatSpec | ||
280 | with Matchers | ||
281 | with LoneElement { | ||
282 | |||
283 | import Ontology2_CanonicalModelSpec._ | ||
284 | |||
285 | "The program generated from Example #1" should "not be empty" in { | ||
286 | program.rules should not be empty | ||
287 | } | ||
288 | |||
289 | // Role R // | ||
290 | |||
291 | renderer.render(roleR) should "be unsafe" in { | ||
292 | ontology.unsafeRoles should contain(roleR) | ||
293 | } | ||
294 | |||
295 | it should "have only its inverse in its conflict set" in { | ||
296 | ontology.confl(roleR).loneElement shouldBe roleR_inv | ||
297 | } | ||
298 | |||
299 | // Role S // | ||
300 | |||
301 | renderer.render(roleS) should "be unsafe" in { | ||
302 | ontology.unsafeRoles should contain(roleS) | ||
303 | } | ||
304 | |||
305 | it should "have only its inverse in its conflict set" in { | ||
306 | ontology.confl(roleS).loneElement shouldBe roleS_inv | ||
307 | } | ||
308 | |||
309 | // Role T // | ||
310 | |||
311 | renderer.render(roleT) should "be unsafe" in { | ||
312 | ontology.unsafeRoles should contain(roleT) | ||
313 | } | ||
314 | |||
315 | it should "have only its inverse in its conflict set" in { | ||
316 | ontology.confl(roleT).loneElement shouldBe roleT_inv | ||
317 | } | ||
318 | |||
319 | // Role P // | ||
320 | |||
321 | renderer.render(roleP) should "be unsafe" in { | ||
322 | ontology.unsafeRoles should contain(roleP) | ||
323 | } | ||
324 | |||
325 | it should "have only its inverse in its conflict set" in { | ||
326 | ontology.confl(roleP).loneElement shouldBe roleP_inv | ||
327 | } | ||
328 | |||
329 | // A ⊑ ∃ R.B | ||
330 | |||
331 | renderer.render( | ||
332 | AsomeValuesFromRB | ||
333 | ) should "produce 1 rule" in { | ||
334 | val visitor = program.RuleGenerator | ||
335 | val rules = AsomeValuesFromRB.accept(visitor) | ||
336 | rules should have length 1 | ||
337 | } | ||
338 | |||
339 | // B ⊑ ∃ S.C | ||
340 | |||
341 | renderer.render( | ||
342 | BsomeValuesFromSC | ||
343 | ) should "produce 1 rule" in { | ||
344 | val visitor = program.RuleGenerator | ||
345 | val rules = BsomeValuesFromSC.accept(visitor) | ||
346 | rules should have length 1 | ||
347 | } | ||
348 | |||
349 | // C ⊑ ∃ T.D | ||
350 | |||
351 | renderer.render( | ||
352 | CsomeValuesFromTD | ||
353 | ) should "produce 1 rule" in { | ||
354 | val visitor = program.RuleGenerator | ||
355 | val rules = CsomeValuesFromTD.accept(visitor) | ||
356 | rules should have length 1 | ||
357 | } | ||
358 | |||
359 | // D ⊑ ∃ P.A | ||
360 | |||
361 | renderer.render( | ||
362 | DsomeValuesFromPA | ||
363 | ) should "produce 1 rule" in { | ||
364 | val visitor = program.RuleGenerator | ||
365 | val rules = DsomeValuesFromPA.accept(visitor) | ||
366 | rules should have length 1 | ||
367 | } | ||
368 | |||
369 | } | ||