aboutsummaryrefslogtreecommitdiff
path: root/src/test/scala/rsacomb/OWLAxiomSpec.scala
blob: 65333f51e0831e3fc047e951becde3af967adc5c (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
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
package rsacomb

import java.util.{ArrayList => JList}
import org.scalatest.LoneElement
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

import org.semanticweb.owlapi.model.OWLClassExpression
import uk.ac.manchester.cs.owl.owlapi.{OWLSubClassOfAxiomImpl}
import uk.ac.manchester.cs.owl.owlapi.{
  OWLClassImpl,
  OWLObjectSomeValuesFromImpl,
  OWLObjectIntersectionOfImpl,
  OWLObjectOneOfImpl,
  OWLObjectAllValuesFromImpl,
  OWLObjectMaxCardinalityImpl,
  OWLNamedIndividualImpl
}
import uk.ac.manchester.cs.owl.owlapi.{OWLObjectPropertyImpl}
import org.semanticweb.owlapi.model.{OWLAxiom}

import tech.oxfordsemantic.jrdfox.logic.Datatype
import tech.oxfordsemantic.jrdfox.logic.datalog.{
  Rule,
  BindAtom,
  TupleTableAtom,
  TupleTableName
}
import tech.oxfordsemantic.jrdfox.logic.expression.{
  FunctionCall,
  Term,
  Variable,
  Literal
}

import org.semanticweb.owlapi.model.{IRI => OWLIRI}
import tech.oxfordsemantic.jrdfox.logic.expression.{IRI => RDFIRI}

import rsacomb.util.RSA

object OWLAxiomSpec {

  // IRI
  val iri_Professor = OWLIRI.create("univ:Professor")
  val iri_Female = OWLIRI.create("std:Female")
  val iri_Student = OWLIRI.create("univ:Student")
  val iri_PartTimeStudent = OWLIRI.create("univ:PartTimeStudent")
  val iri_Worker = OWLIRI.create("univ:Worker")
  val iri_alice = OWLIRI.create("univ:alice")
  val iri_supervises = OWLIRI.create("univ:supervises")
  val iri_hasSupervisor = OWLIRI.create("univ:hasSupervisor")
  val iri_sameAs = OWLIRI.create("owl:sameAs")

  // RDFox Terms
  val term_x = Variable.create("x")
  val term_y = Variable.create("y")
  val term_z = Variable.create("z")
  val term_c1 = RSA("c_1")
  val term_c2 = RSA("c_2")
  val term_alice = RDFIRI.create("univ:alice")

  // RDFox Predicates
  val pred_sameAs = TupleTableName.create("owl:sameAs")
  val pred_Professor = TupleTableName.create(iri_Professor.getIRIString)
  val pred_hasSupervisor = TupleTableName.create(iri_hasSupervisor.getIRIString)

  // OWL Classes
  // Name Class corresponding to
  //
  //    Professor
  //
  val class_Professor = new OWLClassImpl(iri_Professor)
  val class_Female = new OWLClassImpl(iri_Female)
  val class_Student = new OWLClassImpl(iri_Student)
  val class_PartTimeStudent = new OWLClassImpl(iri_PartTimeStudent)
  val class_Worker = new OWLClassImpl(iri_Worker)
  val class_OWLClass = class_Professor
  // Class Conjunction corresponding to
  //
  //    Student ∧ Worker
  //
  val class_OWLObjectIntersectionOf = {
    val conjuncts = new JList[OWLClassExpression]()
    conjuncts.add(class_Student)
    conjuncts.add(class_Worker)
    new OWLObjectIntersectionOfImpl(conjuncts)
  }
  // Singleton Class corresponding to
  //
  //    { alice }
  //
  val class_OWLObjectOneOf =
    new OWLObjectOneOfImpl(
      new OWLNamedIndividualImpl(iri_alice)
    )
  // Object Existential Restiction corresponding to
  //
  //    ∃ hasSupervisor.Professor
  //
  val class_OWLObjectSomeValuesFrom =
    new OWLObjectSomeValuesFromImpl(
      new OWLObjectPropertyImpl(iri_hasSupervisor),
      class_Professor
    )
  // Object Max Cardinality Restriction corresponding to
  //
  //    ≤ 1 hasSupervisor . Professor
  val class_OWLObjectMaxCardinality =
    new OWLObjectMaxCardinalityImpl(
      new OWLObjectPropertyImpl(iri_hasSupervisor),
      1,
      class_Professor
    )

  // OWL Axioms
  // Axiom SubClassOf corresponding to
  //
  //    Student ∧ Worker -> PartTimeStudent
  //
  val axiom_OWLSubClassOf1 =
    new OWLSubClassOfAxiomImpl(
      class_OWLObjectIntersectionOf,
      class_PartTimeStudent,
      new JList()
    )

  // Axiom SubClassOf corresponding to
  //
  //    Student -> ∃ hasSupervisor.Professor
  //
  val axiom_OWLSubClassOf2 =
    new OWLSubClassOfAxiomImpl(
      class_Student,
      class_OWLObjectSomeValuesFrom,
      new JList()
    )

  // Axiom SubClassOf corresponding to
  //
  //    ∃ hasSupervisor.Professor -> Student
  //
  val axiom_OWLSubClassOf3 =
    new OWLSubClassOfAxiomImpl(
      class_OWLObjectSomeValuesFrom,
      class_Student,
      new JList()
    )

  // Axiom SubClassOf corresponding to
  //
  //    Student -> { alice }
  //
  val axiom_OWLSubClassOf4 =
    new OWLSubClassOfAxiomImpl(
      class_Student,
      class_OWLObjectOneOf,
      new JList()
    )

  // Axiom SubClassOf corresponding to
  //
  //    Student -> ≤1 hasSupervisor.Professor
  //
  val axiom_OWLSubClassOf5 =
    new OWLSubClassOfAxiomImpl(
      class_Student,
      class_OWLObjectMaxCardinality,
      new JList()
    )

  def convertAxiom(
      axiom: OWLAxiom,
      term: Term,
      skolem: SkolemStrategy = SkolemStrategy.None
  ): List[Rule] = {
    axiom.accept(RDFoxAxiomConverter(term, List()))
  }

} // object OWLAxiomSpec

class OWLAxiomSpec extends AnyFlatSpec with Matchers with LoneElement {

  // Import required data
  import OWLAxiomSpec._
  // Implicit convertion from IRI in OWLAPI to IRI in JRDFox
  import rsacomb.implicits.RDFox._

  // OWLSubClassOfAxiom #1
  axiom_OWLSubClassOf1.toString should "be converted into a singleton List[Rule]" in {
    val result = convertAxiom(axiom_OWLSubClassOf1, term_x)
    result.loneElement shouldBe a[Rule]
  }

  it should "contain a conjuction of atoms (Student[?x],Worker[?x]) in the body of the rule" in {
    val result = convertAxiom(axiom_OWLSubClassOf1, term_x)
    val body = List(
      TupleTableAtom.rdf(term_x, RDFIRI.RDF_TYPE, iri_Student),
      TupleTableAtom.rdf(term_x, RDFIRI.RDF_TYPE, iri_Worker)
    )
    result.loneElement.getBody should contain theSameElementsAs body
  }

  it should "contain a single atom (PartTimeStudent[?x]) in the head of the rule" in {
    val result = convertAxiom(axiom_OWLSubClassOf1, term_x)
    val head = TupleTableAtom.rdf(term_x, RDFIRI.RDF_TYPE, iri_PartTimeStudent)
    result.loneElement.getHead.loneElement should be(head)
  }

  // OWLSubClassOfAxiom #2 (w/ constant skolemization)
  (axiom_OWLSubClassOf2.toString + "\n(w/ constant skolemization)") should
    "be converted into a singleton List[Rule]" in {
    val skolem = SkolemStrategy.Constant(axiom_OWLSubClassOf2.toString)
    val result = convertAxiom(axiom_OWLSubClassOf2, term_x, skolem)
    result.loneElement shouldBe a[Rule]
  }

  it should "contain a single atom (Student[?x]) in the body of the rule" in {
    val skolem = SkolemStrategy.Constant(axiom_OWLSubClassOf2.toString)
    val result = convertAxiom(axiom_OWLSubClassOf2, term_x, skolem)
    val body =
      TupleTableAtom.rdf(term_x, RDFIRI.RDF_TYPE, iri_Student.getIRIString)
    result.loneElement.getBody.loneElement should equal(body)
  }

  // it should "contain a conjuction of atoms (hasSupervisor[?x,?c],Professor[?c]) in the head of the rule" in {
  //   val skolem = SkolemStrategy.Constant(axiom_OWLSubClassOf2.toString)
  //   val result = convertAxiom(axiom_OWLSubClassOf2, term_x, skolem)
  //   val term_c = RSA.rsa(skolem.const.getIRI)
  //   val head = List(
  //     TupleTableAtom.rdf(term_x, iri_hasSupervisor, term_c),
  //     TupleTableAtom.rdf(term_c, RDFIRI.RDF_TYPE, iri_Professor)
  //   )
  //   result.loneElement.getHead should contain theSameElementsAs (head)
  // }

  // OWLSubClassOfAxiom #2 (w/ skolemization)
  (axiom_OWLSubClassOf2.toString + "\n(w/ skolemization)") should
    "be converted into a singleton List[Rule]" in {
    val skolem = SkolemStrategy.Standard(axiom_OWLSubClassOf2.toString)
    val result = convertAxiom(axiom_OWLSubClassOf2, term_x, skolem)
    result.loneElement shouldBe a[Rule]
  }

  it should "contain an atom (Student[?x]) in the body of the rule" in {
    val skolem = SkolemStrategy.Standard(axiom_OWLSubClassOf2.toString)
    val result = convertAxiom(axiom_OWLSubClassOf2, term_x, skolem)
    val body =
      TupleTableAtom.rdf(term_x, RDFIRI.RDF_TYPE, iri_Student)
    result.loneElement.getBody should contain(body)
  }

  // it should "contain a built-in function call (BIND(?y,SKOLEM(?f,?x))) in the body of the rule" in {
  //   val skolem = SkolemStrategy.Standard(axiom_OWLSubClassOf2.toString)
  //   val result = convertAxiom(axiom_OWLSubClassOf2, term_x, skolem)
  //   val call =
  //     BindAtom.create(BuiltinFunctionCall.create("SKOLEM", term_x), term_y)
  //   result.loneElement.getBody should contain(call)
  // }

  // it should "contain a conjuction of atom (hasSupervisor[?x,?y],Professor[?y]) in the head of the rule" in {
  //   val skolem = SkolemStrategy.Standard(axiom_OWLSubClassOf2.toString)
  //   val result = convertAxiom(axiom_OWLSubClassOf2, term_x, skolem)
  //   val head = List(
  //     TupleTableAtom.rdf(term_x, iri_hasSupervisor, term_y),
  //     TupleTableAtom.rdf(term_y, RDFIRI.RDF_TYPE, iri_Professor)
  //   )
  //   result.loneElement.getHead should contain theSameElementsAs head
  // }

  // OWLSubClassOfAxiom #3
  axiom_OWLSubClassOf3.toString should "be converted into a singleton List[Rule]" in {
    val result = convertAxiom(axiom_OWLSubClassOf3, term_x)
    result.loneElement shouldBe a[Rule]
  }

  // it should "contain a conjunction of atoms (hasSupervisor[?x,?y],Professor[?y]) in the body of the rule" in {
  //   val result = convertAxiom(axiom_OWLSubClassOf3, term_x)
  //   val body = List(
  //     TupleTableAtom.rdf(term_x, iri_hasSupervisor, term_y),
  //     TupleTableAtom.rdf(term_y, RDFIRI.RDF_TYPE, iri_Professor)
  //   )
  //   result.loneElement.getBody should contain theSameElementsAs body
  // }

  it should "contain a single atom (Student[?x]) in the head of the rule" in {
    val result = convertAxiom(axiom_OWLSubClassOf3, term_x)
    val head =
      TupleTableAtom.rdf(term_x, RDFIRI.RDF_TYPE, iri_Student)
    result.loneElement.getHead.loneElement should be(head)
  }

  // OWLSubClassOfAxiom #4
  axiom_OWLSubClassOf4.toString should "be converted into a singleton List[Rule]" in {
    val result = convertAxiom(axiom_OWLSubClassOf4, term_x)
    result.loneElement shouldBe a[Rule]
  }

  it should "contain a single atoms (Student[?x]) in the body of the rule" in {
    val result = convertAxiom(axiom_OWLSubClassOf4, term_x)
    val body =
      TupleTableAtom.rdf(term_x, RDFIRI.RDF_TYPE, iri_Student)
    result.loneElement.getBody.loneElement should be(body)
  }

  it should "contain a single atom (sameAs[?x,alice])) in the head of the rule" in {
    val result = convertAxiom(axiom_OWLSubClassOf4, term_x)
    val head = TupleTableAtom.rdf(term_x, RDFIRI.SAME_AS, term_alice)
    result.loneElement.getHead.loneElement should be(head)
  }

  // OWLSubClassOfAxiom #5
  axiom_OWLSubClassOf5.toString should "be converted into a singleton List[Rule]" in {
    val result = convertAxiom(axiom_OWLSubClassOf5, term_x)
    result.loneElement shouldBe a[Rule]
  }

  // it should "contain a conjunction of atoms (...) in the body of the rule" in {
  //   val result = convertAxiom(axiom_OWLSubClassOf5, term_x)
  //   val body = List(
  //     TupleTableAtom.rdf(term_x, RDFIRI.RDF_TYPE, iri_Student),
  //     TupleTableAtom.rdf(term_x, iri_hasSupervisor, term_y),
  //     TupleTableAtom.rdf(term_y, RDFIRI.RDF_TYPE, iri_Professor),
  //     TupleTableAtom.rdf(term_x, iri_hasSupervisor, term_z),
  //     TupleTableAtom.rdf(term_z, RDFIRI.RDF_TYPE, iri_Professor)
  //   )
  //   result.loneElement.getBody should contain theSameElementsAs body
  // }

  // it should "contain a single atom (sameAs[?x,?z])) in the head of the rule" in {
  //   val result = convertAxiom(axiom_OWLSubClassOf5, term_x)
  //   val head = TupleTableAtom.rdf(term_y, RDFIRI.SAME_AS, term_z)
  //   result.loneElement.getHead.loneElement should be(head)
  // }

} // class OWLAxiomSpec