diff options
Diffstat (limited to 'src/test/scala/uk/ac/ox/cs/rsacomb/OWLAxiomSpec.scala')
-rw-r--r-- | src/test/scala/uk/ac/ox/cs/rsacomb/OWLAxiomSpec.scala | 336 |
1 files changed, 336 insertions, 0 deletions
diff --git a/src/test/scala/uk/ac/ox/cs/rsacomb/OWLAxiomSpec.scala b/src/test/scala/uk/ac/ox/cs/rsacomb/OWLAxiomSpec.scala new file mode 100644 index 0000000..8aee03d --- /dev/null +++ b/src/test/scala/uk/ac/ox/cs/rsacomb/OWLAxiomSpec.scala | |||
@@ -0,0 +1,336 @@ | |||
1 | package rsacomb | ||
2 | |||
3 | import java.util.{ArrayList => JList} | ||
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.OWLClassExpression | ||
9 | import uk.ac.manchester.cs.owl.owlapi.{OWLSubClassOfAxiomImpl} | ||
10 | import uk.ac.manchester.cs.owl.owlapi.{ | ||
11 | OWLClassImpl, | ||
12 | OWLObjectSomeValuesFromImpl, | ||
13 | OWLObjectIntersectionOfImpl, | ||
14 | OWLObjectOneOfImpl, | ||
15 | OWLObjectAllValuesFromImpl, | ||
16 | OWLObjectMaxCardinalityImpl, | ||
17 | OWLNamedIndividualImpl | ||
18 | } | ||
19 | import uk.ac.manchester.cs.owl.owlapi.{OWLObjectPropertyImpl} | ||
20 | import org.semanticweb.owlapi.model.{OWLAxiom} | ||
21 | |||
22 | import tech.oxfordsemantic.jrdfox.logic.Datatype | ||
23 | import tech.oxfordsemantic.jrdfox.logic.datalog.{ | ||
24 | Rule, | ||
25 | BindAtom, | ||
26 | TupleTableAtom, | ||
27 | TupleTableName | ||
28 | } | ||
29 | import tech.oxfordsemantic.jrdfox.logic.expression.{ | ||
30 | FunctionCall, | ||
31 | Term, | ||
32 | Variable, | ||
33 | Literal | ||
34 | } | ||
35 | |||
36 | import org.semanticweb.owlapi.model.{IRI => OWLIRI} | ||
37 | import tech.oxfordsemantic.jrdfox.logic.expression.{IRI => RDFIRI} | ||
38 | |||
39 | import uk.ac.ox.cs.rsacomb.converter.{RDFoxAxiomConverter, SkolemStrategy} | ||
40 | import uk.ac.ox.cs.rsacomb.util.RSA | ||
41 | |||
42 | object OWLAxiomSpec { | ||
43 | |||
44 | // IRI | ||
45 | val iri_Professor = OWLIRI.create("univ:Professor") | ||
46 | val iri_Female = OWLIRI.create("std:Female") | ||
47 | val iri_Student = OWLIRI.create("univ:Student") | ||
48 | val iri_PartTimeStudent = OWLIRI.create("univ:PartTimeStudent") | ||
49 | val iri_Worker = OWLIRI.create("univ:Worker") | ||
50 | val iri_alice = OWLIRI.create("univ:alice") | ||
51 | val iri_supervises = OWLIRI.create("univ:supervises") | ||
52 | val iri_hasSupervisor = OWLIRI.create("univ:hasSupervisor") | ||
53 | val iri_sameAs = OWLIRI.create("owl:sameAs") | ||
54 | |||
55 | // RDFox Terms | ||
56 | val term_x = Variable.create("x") | ||
57 | val term_y = Variable.create("y") | ||
58 | val term_z = Variable.create("z") | ||
59 | val term_c1 = RSA("c_1") | ||
60 | val term_c2 = RSA("c_2") | ||
61 | val term_alice = RDFIRI.create("univ:alice") | ||
62 | |||
63 | // RDFox Predicates | ||
64 | val pred_sameAs = TupleTableName.create("owl:sameAs") | ||
65 | val pred_Professor = TupleTableName.create(iri_Professor.getIRIString) | ||
66 | val pred_hasSupervisor = TupleTableName.create(iri_hasSupervisor.getIRIString) | ||
67 | |||
68 | // OWL Classes | ||
69 | // Name Class corresponding to | ||
70 | // | ||
71 | // Professor | ||
72 | // | ||
73 | val class_Professor = new OWLClassImpl(iri_Professor) | ||
74 | val class_Female = new OWLClassImpl(iri_Female) | ||
75 | val class_Student = new OWLClassImpl(iri_Student) | ||
76 | val class_PartTimeStudent = new OWLClassImpl(iri_PartTimeStudent) | ||
77 | val class_Worker = new OWLClassImpl(iri_Worker) | ||
78 | val class_OWLClass = class_Professor | ||
79 | // Class Conjunction corresponding to | ||
80 | // | ||
81 | // Student ∧ Worker | ||
82 | // | ||
83 | val class_OWLObjectIntersectionOf = { | ||
84 | val conjuncts = new JList[OWLClassExpression]() | ||
85 | conjuncts.add(class_Student) | ||
86 | conjuncts.add(class_Worker) | ||
87 | new OWLObjectIntersectionOfImpl(conjuncts) | ||
88 | } | ||
89 | // Singleton Class corresponding to | ||
90 | // | ||
91 | // { alice } | ||
92 | // | ||
93 | val class_OWLObjectOneOf = | ||
94 | new OWLObjectOneOfImpl( | ||
95 | new OWLNamedIndividualImpl(iri_alice) | ||
96 | ) | ||
97 | // Object Existential Restiction corresponding to | ||
98 | // | ||
99 | // ∃ hasSupervisor.Professor | ||
100 | // | ||
101 | val class_OWLObjectSomeValuesFrom = | ||
102 | new OWLObjectSomeValuesFromImpl( | ||
103 | new OWLObjectPropertyImpl(iri_hasSupervisor), | ||
104 | class_Professor | ||
105 | ) | ||
106 | // Object Max Cardinality Restriction corresponding to | ||
107 | // | ||
108 | // ≤ 1 hasSupervisor . Professor | ||
109 | val class_OWLObjectMaxCardinality = | ||
110 | new OWLObjectMaxCardinalityImpl( | ||
111 | new OWLObjectPropertyImpl(iri_hasSupervisor), | ||
112 | 1, | ||
113 | class_Professor | ||
114 | ) | ||
115 | |||
116 | // OWL Axioms | ||
117 | // Axiom SubClassOf corresponding to | ||
118 | // | ||
119 | // Student ∧ Worker -> PartTimeStudent | ||
120 | // | ||
121 | val axiom_OWLSubClassOf1 = | ||
122 | new OWLSubClassOfAxiomImpl( | ||
123 | class_OWLObjectIntersectionOf, | ||
124 | class_PartTimeStudent, | ||
125 | new JList() | ||
126 | ) | ||
127 | |||
128 | // Axiom SubClassOf corresponding to | ||
129 | // | ||
130 | // Student -> ∃ hasSupervisor.Professor | ||
131 | // | ||
132 | val axiom_OWLSubClassOf2 = | ||
133 | new OWLSubClassOfAxiomImpl( | ||
134 | class_Student, | ||
135 | class_OWLObjectSomeValuesFrom, | ||
136 | new JList() | ||
137 | ) | ||
138 | |||
139 | // Axiom SubClassOf corresponding to | ||
140 | // | ||
141 | // ∃ hasSupervisor.Professor -> Student | ||
142 | // | ||
143 | val axiom_OWLSubClassOf3 = | ||
144 | new OWLSubClassOfAxiomImpl( | ||
145 | class_OWLObjectSomeValuesFrom, | ||
146 | class_Student, | ||
147 | new JList() | ||
148 | ) | ||
149 | |||
150 | // Axiom SubClassOf corresponding to | ||
151 | // | ||
152 | // Student -> { alice } | ||
153 | // | ||
154 | val axiom_OWLSubClassOf4 = | ||
155 | new OWLSubClassOfAxiomImpl( | ||
156 | class_Student, | ||
157 | class_OWLObjectOneOf, | ||
158 | new JList() | ||
159 | ) | ||
160 | |||
161 | // Axiom SubClassOf corresponding to | ||
162 | // | ||
163 | // Student -> ≤1 hasSupervisor.Professor | ||
164 | // | ||
165 | val axiom_OWLSubClassOf5 = | ||
166 | new OWLSubClassOfAxiomImpl( | ||
167 | class_Student, | ||
168 | class_OWLObjectMaxCardinality, | ||
169 | new JList() | ||
170 | ) | ||
171 | |||
172 | def convertAxiom( | ||
173 | axiom: OWLAxiom, | ||
174 | term: Term, | ||
175 | skolem: SkolemStrategy = SkolemStrategy.None | ||
176 | ): List[Rule] = { | ||
177 | axiom.accept(RDFoxAxiomConverter(term, List())) | ||
178 | } | ||
179 | |||
180 | } // object OWLAxiomSpec | ||
181 | |||
182 | class OWLAxiomSpec extends AnyFlatSpec with Matchers with LoneElement { | ||
183 | |||
184 | // Import required data | ||
185 | import OWLAxiomSpec._ | ||
186 | // Implicit convertion from IRI in OWLAPI to IRI in JRDFox | ||
187 | import uk.ac.ox.cs.rsacomb.implicits.RDFox._ | ||
188 | |||
189 | // OWLSubClassOfAxiom #1 | ||
190 | axiom_OWLSubClassOf1.toString should "be converted into a singleton List[Rule]" in { | ||
191 | val result = convertAxiom(axiom_OWLSubClassOf1, term_x) | ||
192 | result.loneElement shouldBe a[Rule] | ||
193 | } | ||
194 | |||
195 | it should "contain a conjuction of atoms (Student[?x],Worker[?x]) in the body of the rule" in { | ||
196 | val result = convertAxiom(axiom_OWLSubClassOf1, term_x) | ||
197 | val body = List( | ||
198 | TupleTableAtom.rdf(term_x, RDFIRI.RDF_TYPE, iri_Student), | ||
199 | TupleTableAtom.rdf(term_x, RDFIRI.RDF_TYPE, iri_Worker) | ||
200 | ) | ||
201 | result.loneElement.getBody should contain theSameElementsAs body | ||
202 | } | ||
203 | |||
204 | it should "contain a single atom (PartTimeStudent[?x]) in the head of the rule" in { | ||
205 | val result = convertAxiom(axiom_OWLSubClassOf1, term_x) | ||
206 | val head = TupleTableAtom.rdf(term_x, RDFIRI.RDF_TYPE, iri_PartTimeStudent) | ||
207 | result.loneElement.getHead.loneElement should be(head) | ||
208 | } | ||
209 | |||
210 | // OWLSubClassOfAxiom #2 (w/ constant skolemization) | ||
211 | (axiom_OWLSubClassOf2.toString + "\n(w/ constant skolemization)") should | ||
212 | "be converted into a singleton List[Rule]" in { | ||
213 | val skolem = SkolemStrategy.Constant(axiom_OWLSubClassOf2.toString) | ||
214 | val result = convertAxiom(axiom_OWLSubClassOf2, term_x, skolem) | ||
215 | result.loneElement shouldBe a[Rule] | ||
216 | } | ||
217 | |||
218 | it should "contain a single atom (Student[?x]) in the body of the rule" in { | ||
219 | val skolem = SkolemStrategy.Constant(axiom_OWLSubClassOf2.toString) | ||
220 | val result = convertAxiom(axiom_OWLSubClassOf2, term_x, skolem) | ||
221 | val body = | ||
222 | TupleTableAtom.rdf(term_x, RDFIRI.RDF_TYPE, iri_Student.getIRIString) | ||
223 | result.loneElement.getBody.loneElement should equal(body) | ||
224 | } | ||
225 | |||
226 | // it should "contain a conjuction of atoms (hasSupervisor[?x,?c],Professor[?c]) in the head of the rule" in { | ||
227 | // val skolem = SkolemStrategy.Constant(axiom_OWLSubClassOf2.toString) | ||
228 | // val result = convertAxiom(axiom_OWLSubClassOf2, term_x, skolem) | ||
229 | // val term_c = RSA.rsa(skolem.const.getIRI) | ||
230 | // val head = List( | ||
231 | // TupleTableAtom.rdf(term_x, iri_hasSupervisor, term_c), | ||
232 | // TupleTableAtom.rdf(term_c, RDFIRI.RDF_TYPE, iri_Professor) | ||
233 | // ) | ||
234 | // result.loneElement.getHead should contain theSameElementsAs (head) | ||
235 | // } | ||
236 | |||
237 | // OWLSubClassOfAxiom #2 (w/ skolemization) | ||
238 | (axiom_OWLSubClassOf2.toString + "\n(w/ skolemization)") should | ||
239 | "be converted into a singleton List[Rule]" in { | ||
240 | val skolem = SkolemStrategy.Standard(axiom_OWLSubClassOf2.toString) | ||
241 | val result = convertAxiom(axiom_OWLSubClassOf2, term_x, skolem) | ||
242 | result.loneElement shouldBe a[Rule] | ||
243 | } | ||
244 | |||
245 | it should "contain an atom (Student[?x]) in the body of the rule" in { | ||
246 | val skolem = SkolemStrategy.Standard(axiom_OWLSubClassOf2.toString) | ||
247 | val result = convertAxiom(axiom_OWLSubClassOf2, term_x, skolem) | ||
248 | val body = | ||
249 | TupleTableAtom.rdf(term_x, RDFIRI.RDF_TYPE, iri_Student) | ||
250 | result.loneElement.getBody should contain(body) | ||
251 | } | ||
252 | |||
253 | // it should "contain a built-in function call (BIND(?y,SKOLEM(?f,?x))) in the body of the rule" in { | ||
254 | // val skolem = SkolemStrategy.Standard(axiom_OWLSubClassOf2.toString) | ||
255 | // val result = convertAxiom(axiom_OWLSubClassOf2, term_x, skolem) | ||
256 | // val call = | ||
257 | // BindAtom.create(BuiltinFunctionCall.create("SKOLEM", term_x), term_y) | ||
258 | // result.loneElement.getBody should contain(call) | ||
259 | // } | ||
260 | |||
261 | // it should "contain a conjuction of atom (hasSupervisor[?x,?y],Professor[?y]) in the head of the rule" in { | ||
262 | // val skolem = SkolemStrategy.Standard(axiom_OWLSubClassOf2.toString) | ||
263 | // val result = convertAxiom(axiom_OWLSubClassOf2, term_x, skolem) | ||
264 | // val head = List( | ||
265 | // TupleTableAtom.rdf(term_x, iri_hasSupervisor, term_y), | ||
266 | // TupleTableAtom.rdf(term_y, RDFIRI.RDF_TYPE, iri_Professor) | ||
267 | // ) | ||
268 | // result.loneElement.getHead should contain theSameElementsAs head | ||
269 | // } | ||
270 | |||
271 | // OWLSubClassOfAxiom #3 | ||
272 | axiom_OWLSubClassOf3.toString should "be converted into a singleton List[Rule]" in { | ||
273 | val result = convertAxiom(axiom_OWLSubClassOf3, term_x) | ||
274 | result.loneElement shouldBe a[Rule] | ||
275 | } | ||
276 | |||
277 | // it should "contain a conjunction of atoms (hasSupervisor[?x,?y],Professor[?y]) in the body of the rule" in { | ||
278 | // val result = convertAxiom(axiom_OWLSubClassOf3, term_x) | ||
279 | // val body = List( | ||
280 | // TupleTableAtom.rdf(term_x, iri_hasSupervisor, term_y), | ||
281 | // TupleTableAtom.rdf(term_y, RDFIRI.RDF_TYPE, iri_Professor) | ||
282 | // ) | ||
283 | // result.loneElement.getBody should contain theSameElementsAs body | ||
284 | // } | ||
285 | |||
286 | it should "contain a single atom (Student[?x]) in the head of the rule" in { | ||
287 | val result = convertAxiom(axiom_OWLSubClassOf3, term_x) | ||
288 | val head = | ||
289 | TupleTableAtom.rdf(term_x, RDFIRI.RDF_TYPE, iri_Student) | ||
290 | result.loneElement.getHead.loneElement should be(head) | ||
291 | } | ||
292 | |||
293 | // OWLSubClassOfAxiom #4 | ||
294 | axiom_OWLSubClassOf4.toString should "be converted into a singleton List[Rule]" in { | ||
295 | val result = convertAxiom(axiom_OWLSubClassOf4, term_x) | ||
296 | result.loneElement shouldBe a[Rule] | ||
297 | } | ||
298 | |||
299 | it should "contain a single atoms (Student[?x]) in the body of the rule" in { | ||
300 | val result = convertAxiom(axiom_OWLSubClassOf4, term_x) | ||
301 | val body = | ||
302 | TupleTableAtom.rdf(term_x, RDFIRI.RDF_TYPE, iri_Student) | ||
303 | result.loneElement.getBody.loneElement should be(body) | ||
304 | } | ||
305 | |||
306 | it should "contain a single atom (sameAs[?x,alice])) in the head of the rule" in { | ||
307 | val result = convertAxiom(axiom_OWLSubClassOf4, term_x) | ||
308 | val head = TupleTableAtom.rdf(term_x, RDFIRI.SAME_AS, term_alice) | ||
309 | result.loneElement.getHead.loneElement should be(head) | ||
310 | } | ||
311 | |||
312 | // OWLSubClassOfAxiom #5 | ||
313 | axiom_OWLSubClassOf5.toString should "be converted into a singleton List[Rule]" in { | ||
314 | val result = convertAxiom(axiom_OWLSubClassOf5, term_x) | ||
315 | result.loneElement shouldBe a[Rule] | ||
316 | } | ||
317 | |||
318 | // it should "contain a conjunction of atoms (...) in the body of the rule" in { | ||
319 | // val result = convertAxiom(axiom_OWLSubClassOf5, term_x) | ||
320 | // val body = List( | ||
321 | // TupleTableAtom.rdf(term_x, RDFIRI.RDF_TYPE, iri_Student), | ||
322 | // TupleTableAtom.rdf(term_x, iri_hasSupervisor, term_y), | ||
323 | // TupleTableAtom.rdf(term_y, RDFIRI.RDF_TYPE, iri_Professor), | ||
324 | // TupleTableAtom.rdf(term_x, iri_hasSupervisor, term_z), | ||
325 | // TupleTableAtom.rdf(term_z, RDFIRI.RDF_TYPE, iri_Professor) | ||
326 | // ) | ||
327 | // result.loneElement.getBody should contain theSameElementsAs body | ||
328 | // } | ||
329 | |||
330 | // it should "contain a single atom (sameAs[?x,?z])) in the head of the rule" in { | ||
331 | // val result = convertAxiom(axiom_OWLSubClassOf5, term_x) | ||
332 | // val head = TupleTableAtom.rdf(term_y, RDFIRI.SAME_AS, term_z) | ||
333 | // result.loneElement.getHead.loneElement should be(head) | ||
334 | // } | ||
335 | |||
336 | } // class OWLAxiomSpec | ||