diff options
author | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-11-17 11:45:55 +0000 |
---|---|---|
committer | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-11-17 11:45:55 +0000 |
commit | e237c660994978588dea7c8d26043440986ba6df (patch) | |
tree | 5e3dea7dce3020c289024e7cf19e0de6cfea3842 | |
parent | c6ae617490ad7e2429207e1393415ec9bff7a501 (diff) | |
download | RSAComb-e237c660994978588dea7c8d26043440986ba6df.tar.gz RSAComb-e237c660994978588dea7c8d26043440986ba6df.zip |
Unify use of RSASuffix
This commit comes with minor fixes and code simplifications.
-rw-r--r-- | src/main/scala/rsacomb/FilteringProgram.scala | 199 | ||||
-rw-r--r-- | src/main/scala/rsacomb/RDFTriple.scala | 131 | ||||
-rw-r--r-- | src/main/scala/rsacomb/RDFoxPropertyExprConverter.scala | 9 | ||||
-rw-r--r-- | src/main/scala/rsacomb/RDFoxUtil.scala | 2 | ||||
-rw-r--r-- | src/main/scala/rsacomb/RSASuffix.scala | 21 |
5 files changed, 203 insertions, 159 deletions
diff --git a/src/main/scala/rsacomb/FilteringProgram.scala b/src/main/scala/rsacomb/FilteringProgram.scala index 44960e5..af07ac3 100644 --- a/src/main/scala/rsacomb/FilteringProgram.scala +++ b/src/main/scala/rsacomb/FilteringProgram.scala | |||
@@ -27,8 +27,11 @@ import tech.oxfordsemantic.jrdfox.logic.sparql.pattern.{ | |||
27 | 27 | ||
28 | import scala.collection.JavaConverters._ | 28 | import scala.collection.JavaConverters._ |
29 | 29 | ||
30 | import implicits.RSAAtom | ||
31 | import suffix.{RSASuffix, Forward, Backward} | ||
32 | |||
30 | class FilteringProgram(query: SelectQuery, constants: List[Term]) | 33 | class FilteringProgram(query: SelectQuery, constants: List[Term]) |
31 | extends RDFTriple { | 34 | extends RSAAtom { |
32 | 35 | ||
33 | /* Makes mplicit conversion OWLAPI IRI <-> RDFox IRI available */ | 36 | /* Makes mplicit conversion OWLAPI IRI <-> RDFox IRI available */ |
34 | import RDFoxUtil._ | 37 | import RDFoxUtil._ |
@@ -86,6 +89,10 @@ class FilteringProgram(query: SelectQuery, constants: List[Term]) | |||
86 | } | 89 | } |
87 | 90 | ||
88 | private def generateFilteringProgram(): List[Rule] = { | 91 | private def generateFilteringProgram(): List[Rule] = { |
92 | // General purpose variables | ||
93 | val varU = Variable.create("U") | ||
94 | val varV = Variable.create("V") | ||
95 | val varW = Variable.create("W") | ||
89 | // Query formula as a rule body | 96 | // Query formula as a rule body |
90 | val body = queryToBody(query.getQueryBody.getWherePattern) | 97 | val body = queryToBody(query.getQueryBody.getWherePattern) |
91 | // Auxiliar predicates/helpers | 98 | // Auxiliar predicates/helpers |
@@ -102,14 +109,14 @@ class FilteringProgram(query: SelectQuery, constants: List[Term]) | |||
102 | ) | 109 | ) |
103 | def predNAMED(t1: Term): TupleTableAtom = | 110 | def predNAMED(t1: Term): TupleTableAtom = |
104 | TupleTableAtom.rdf(t1, IRI.RDF_TYPE, RSA.rsa("NAMED")) | 111 | TupleTableAtom.rdf(t1, IRI.RDF_TYPE, RSA.rsa("NAMED")) |
105 | def predTQ(sx: String, t1: Term, t2: Term) = | 112 | def predTQ(t1: Term, t2: Term, sx: RSASuffix) = |
106 | TupleTableAtom.create( | 113 | TupleTableAtom.create( |
107 | TupleTableName.create(RSA.rsa(s"TQ_$sx").getIRI), | 114 | TupleTableName.create(RSA.rsa("TQ" :: sx).getIRI), |
108 | (answer ++ bounded).appended(t1).appended(t2): _* | 115 | (answer ++ bounded).appended(t1).appended(t2): _* |
109 | ) | 116 | ) |
110 | def predAQ(sx: String, t1: Term, t2: Term) = | 117 | def predAQ(t1: Term, t2: Term, sx: RSASuffix) = |
111 | TupleTableAtom.create( | 118 | TupleTableAtom.create( |
112 | TupleTableName.create(RSA.rsa(s"AQ_$sx").getIRI), | 119 | TupleTableName.create(RSA.rsa("AQ" :: sx).getIRI), |
113 | (answer ++ bounded).appended(t1).appended(t2): _* | 120 | (answer ++ bounded).appended(t1).appended(t2): _* |
114 | ) | 121 | ) |
115 | val predFK = | 122 | val predFK = |
@@ -140,13 +147,13 @@ class FilteringProgram(query: SelectQuery, constants: List[Term]) | |||
140 | not(predNI(v)) | 147 | not(predNI(v)) |
141 | ) | 148 | ) |
142 | val r3b = Rule.create( | 149 | val r3b = Rule.create( |
143 | predID(Variable.create("V"), Variable.create("U")), | 150 | predID(varV, varU), |
144 | predID(Variable.create("U"), Variable.create("V")) | 151 | predID(varU, varV) |
145 | ) | 152 | ) |
146 | val r3c = Rule.create( | 153 | val r3c = Rule.create( |
147 | predID(Variable.create("U"), Variable.create("W")), | 154 | predID(varU, varW), |
148 | predID(Variable.create("U"), Variable.create("V")), | 155 | predID(varU, varV), |
149 | predID(Variable.create("V"), Variable.create("W")) | 156 | predID(varV, varW) |
150 | ) | 157 | ) |
151 | 158 | ||
152 | /* Rules 4x */ | 159 | /* Rules 4x */ |
@@ -157,8 +164,8 @@ class FilteringProgram(query: SelectQuery, constants: List[Term]) | |||
157 | if bounded contains (role2.getArguments.get(2)) | 164 | if bounded contains (role2.getArguments.get(2)) |
158 | } yield Rule.create( | 165 | } yield Rule.create( |
159 | predFK, | 166 | predFK, |
160 | role1 suffix "f", | 167 | role1 << Forward, |
161 | role2 suffix "f", | 168 | role2 << Forward, |
162 | predID( | 169 | predID( |
163 | RSA.rsa(bounded.indexOf(role1.getArguments.get(2))), | 170 | RSA.rsa(bounded.indexOf(role1.getArguments.get(2))), |
164 | RSA.rsa(bounded.indexOf(role2.getArguments.get(2))) | 171 | RSA.rsa(bounded.indexOf(role2.getArguments.get(2))) |
@@ -178,8 +185,8 @@ class FilteringProgram(query: SelectQuery, constants: List[Term]) | |||
178 | if bounded contains (role2.getArguments.get(0)) | 185 | if bounded contains (role2.getArguments.get(0)) |
179 | } yield Rule.create( | 186 | } yield Rule.create( |
180 | predFK, | 187 | predFK, |
181 | role1 suffix "f", | 188 | role1 << Forward, |
182 | role2 suffix "b", | 189 | role2 << Backward, |
183 | predID( | 190 | predID( |
184 | RSA.rsa(bounded.indexOf(role1.getArguments.get(2))), | 191 | RSA.rsa(bounded.indexOf(role1.getArguments.get(2))), |
185 | RSA.rsa(bounded.indexOf(role2.getArguments.get(0))) | 192 | RSA.rsa(bounded.indexOf(role2.getArguments.get(0))) |
@@ -199,8 +206,8 @@ class FilteringProgram(query: SelectQuery, constants: List[Term]) | |||
199 | if bounded contains (role2.getArguments.get(0)) | 206 | if bounded contains (role2.getArguments.get(0)) |
200 | } yield Rule.create( | 207 | } yield Rule.create( |
201 | predFK, | 208 | predFK, |
202 | role1 suffix "b", | 209 | role1 << Backward, |
203 | role2 suffix "b", | 210 | role2 << Backward, |
204 | predID( | 211 | predID( |
205 | RSA.rsa(bounded.indexOf(role1.getArguments.get(0))), | 212 | RSA.rsa(bounded.indexOf(role1.getArguments.get(0))), |
206 | RSA.rsa(bounded.indexOf(role2.getArguments.get(0))) | 213 | RSA.rsa(bounded.indexOf(role2.getArguments.get(0))) |
@@ -231,8 +238,8 @@ class FilteringProgram(query: SelectQuery, constants: List[Term]) | |||
231 | RSA.rsa(bounded indexOf role1arg0), | 238 | RSA.rsa(bounded indexOf role1arg0), |
232 | RSA.rsa(bounded indexOf role2arg0) | 239 | RSA.rsa(bounded indexOf role2arg0) |
233 | ), | 240 | ), |
234 | role1 suffix "f", | 241 | role1 << Forward, |
235 | role2 suffix "f", | 242 | role2 << Forward, |
236 | predID( | 243 | predID( |
237 | RSA.rsa(bounded indexOf role1arg2), | 244 | RSA.rsa(bounded indexOf role1arg2), |
238 | RSA.rsa(bounded indexOf role2arg2) | 245 | RSA.rsa(bounded indexOf role2arg2) |
@@ -256,8 +263,8 @@ class FilteringProgram(query: SelectQuery, constants: List[Term]) | |||
256 | RSA.rsa(bounded indexOf role1arg0), | 263 | RSA.rsa(bounded indexOf role1arg0), |
257 | RSA.rsa(bounded indexOf role2arg2) | 264 | RSA.rsa(bounded indexOf role2arg2) |
258 | ), | 265 | ), |
259 | role1 suffix "f", | 266 | role1 << Forward, |
260 | role2 suffix "b", | 267 | role2 << Backward, |
261 | predID( | 268 | predID( |
262 | RSA.rsa(bounded indexOf role1arg2), | 269 | RSA.rsa(bounded indexOf role1arg2), |
263 | RSA.rsa(bounded indexOf role2arg0) | 270 | RSA.rsa(bounded indexOf role2arg0) |
@@ -281,8 +288,8 @@ class FilteringProgram(query: SelectQuery, constants: List[Term]) | |||
281 | RSA.rsa(bounded indexOf role1arg2), | 288 | RSA.rsa(bounded indexOf role1arg2), |
282 | RSA.rsa(bounded indexOf role2arg2) | 289 | RSA.rsa(bounded indexOf role2arg2) |
283 | ), | 290 | ), |
284 | role1 suffix "b", | 291 | role1 << Backward, |
285 | role2 suffix "b", | 292 | role2 << Backward, |
286 | predID( | 293 | predID( |
287 | RSA.rsa(bounded indexOf role1arg0), | 294 | RSA.rsa(bounded indexOf role1arg0), |
288 | RSA.rsa(bounded indexOf role2arg0) | 295 | RSA.rsa(bounded indexOf role2arg0) |
@@ -292,40 +299,38 @@ class FilteringProgram(query: SelectQuery, constants: List[Term]) | |||
292 | ) | 299 | ) |
293 | 300 | ||
294 | /* Rules 6 */ | 301 | /* Rules 6 */ |
295 | val r6 = for { | 302 | val r6 = { |
296 | role <- body.filter(_.isRoleAssertion) | 303 | for { |
297 | arg0 = role.getArguments.get(0) | 304 | role <- body.filter(_.isRoleAssertion) |
298 | arg2 = role.getArguments.get(2) | 305 | arg0 = role.getArguments.get(0) |
299 | if bounded contains arg0 | 306 | arg2 = role.getArguments.get(2) |
300 | if bounded contains arg2 | 307 | if bounded contains arg0 |
301 | sx <- List("f", "b") | 308 | if bounded contains arg2 |
302 | } yield Rule.create( | 309 | suffix <- Seq(Forward, Backward) |
303 | predAQ(sx, Variable.create("V"), Variable.create("W")), | 310 | } yield Rule.create( |
304 | role suffix sx, | 311 | predAQ(varV, varW, suffix), |
305 | predID( | 312 | role << suffix, |
306 | RSA.rsa(bounded indexOf arg0), | 313 | predID(RSA.rsa(bounded indexOf arg0), varV), |
307 | Variable.create("V") | 314 | predID(RSA.rsa(bounded indexOf arg2), varW) |
308 | ), | ||
309 | predID( | ||
310 | RSA.rsa(bounded indexOf arg2), | ||
311 | Variable.create("W") | ||
312 | ) | 315 | ) |
313 | ) | 316 | } |
314 | 317 | ||
315 | /* Rules 7x */ | 318 | /* Rules 7x */ |
316 | val r7a = | 319 | val r7a = { |
317 | for (sx <- List("f", "b")) | 320 | for (suffix <- List(Forward, Backward)) |
318 | yield Rule.create( | 321 | yield Rule.create( |
319 | predTQ(sx, Variable.create("U"), Variable.create("V")), | 322 | predTQ(varU, varV, suffix), |
320 | predAQ(sx, Variable.create("U"), Variable.create("V")) | 323 | predAQ(varU, varV, suffix) |
321 | ) | 324 | ) |
322 | val r7b = | 325 | } |
323 | for (r <- List("f", "b")) | 326 | val r7b = { |
327 | for (suffix <- List(Forward, Backward)) | ||
324 | yield Rule.create( | 328 | yield Rule.create( |
325 | predTQ(r, Variable.create("U"), Variable.create("W")), | 329 | predTQ(varU, varW, suffix), |
326 | predAQ(r, Variable.create("U"), Variable.create("V")), | 330 | predAQ(varU, varV, suffix), |
327 | predTQ(r, Variable.create("V"), Variable.create("W")) | 331 | predTQ(varV, varW, suffix) |
328 | ) | 332 | ) |
333 | } | ||
329 | 334 | ||
330 | /* Rules 8x */ | 335 | /* Rules 8x */ |
331 | val r8a = | 336 | val r8a = |
@@ -333,65 +338,75 @@ class FilteringProgram(query: SelectQuery, constants: List[Term]) | |||
333 | val r8b = | 338 | val r8b = |
334 | Rule.create(predSP, predFK) | 339 | Rule.create(predSP, predFK) |
335 | val r8c = | 340 | val r8c = |
336 | for (sx <- List("f", "b")) | 341 | for (suffix <- List(Forward, Backward)) |
337 | yield Rule.create( | 342 | yield Rule.create( |
338 | predSP, | 343 | predSP, |
339 | predTQ(sx, Variable.create("V"), Variable.create("V")) | 344 | predTQ(varV, varV, suffix) |
340 | ) | 345 | ) |
341 | 346 | ||
342 | /* Rule 9 */ | 347 | /* Rule 9 */ |
343 | val r9 = Rule.create(predANS, predQM, not(predSP)) | 348 | val r9 = Rule.create(predANS, predQM, not(predSP)) |
344 | 349 | ||
345 | List.empty | 350 | // List.empty |
346 | .prepended(r9) | 351 | // .prepended(r9) |
347 | .prependedAll(r8c) | 352 | // .prependedAll(r8c) |
348 | .prepended(r8b) | 353 | // .prepended(r8b) |
349 | .prependedAll(r8a) | 354 | // .prependedAll(r8a) |
350 | .prependedAll(r7b) | 355 | // .prependedAll(r7b) |
351 | .prependedAll(r7a) | 356 | // .prependedAll(r7a) |
352 | .prependedAll(r6) | 357 | // .prependedAll(r6) |
353 | .prependedAll(r5c) | 358 | // .prependedAll(r5c) |
354 | .prependedAll(r5b) | 359 | // .prependedAll(r5b) |
355 | .prependedAll(r5a) | 360 | // .prependedAll(r5a) |
356 | .prependedAll(r4c) | 361 | // .prependedAll(r4c) |
357 | .prependedAll(r4b) | 362 | // .prependedAll(r4b) |
358 | .prependedAll(r4a) | 363 | // .prependedAll(r4a) |
359 | .prepended(r3c) | 364 | // .prepended(r3c) |
360 | .prepended(r3b) | 365 | // .prepended(r3b) |
361 | .prependedAll(r3a) | 366 | // .prependedAll(r3a) |
362 | .prepended(r1) | 367 | // .prepended(r1) |
363 | } | ||
364 | 368 | ||
365 | private def reifyTupleTableAtom( | 369 | r1 :: |
366 | atom: TupleTableAtom | 370 | r3a ::: r3b :: r3c :: |
367 | ): (Option[BindAtom], List[TupleTableAtom]) = { | 371 | r4c ::: r4b ::: r4a ::: |
368 | if (!atom.isRdfTriple) { | 372 | r5c ::: r5b ::: r5a ::: |
369 | // Compute binding atom | 373 | r6 ::: |
370 | val bvar = RSA.getFreshVariable() | 374 | r7b ::: r7a ::: |
371 | val name = | 375 | r8a ::: r8b :: r8c ::: |
372 | Literal.create(atom.getTupleTableName.toString(), Datatype.XSD_STRING) | 376 | r9 :: |
373 | val args = atom.getArguments.asScala.toList.prepended(name) | 377 | List() |
374 | val bind = BindAtom.create(FunctionCall.create("SKOLEM", args: _*), bvar) | ||
375 | // Compute reified atom | ||
376 | def reifiedIRI(i: Int) = atom.getTupleTableName.getName ++ s"_$i" | ||
377 | val atoms = atom.getArguments.asScala.toList.zipWithIndex | ||
378 | .map { case (t, i) => TupleTableAtom.rdf(bvar, reifiedIRI(i), t) } | ||
379 | (Some(bind), atoms) | ||
380 | } else { | ||
381 | (None, List(atom)) | ||
382 | } | ||
383 | } | 378 | } |
384 | 379 | ||
380 | // private def reifyTupleTableAtom( | ||
381 | // atom: TupleTableAtom | ||
382 | // ): (Option[BindAtom], List[TupleTableAtom]) = { | ||
383 | // if (!atom.isRDF) { | ||
384 | // // Compute binding atom | ||
385 | // val bvar = RSA.getFreshVariable() | ||
386 | // val name = | ||
387 | // Literal.create(atom.getTupleTableName.toString(), Datatype.XSD_STRING) | ||
388 | // val args = atom.getArguments.asScala.toList.prepended(name) | ||
389 | // val bind = BindAtom.create(FunctionCall.create("SKOLEM", args: _*), bvar) | ||
390 | // // Compute reified atom | ||
391 | // def reifiedIRI(i: Int) = atom.getTupleTableName.getName ++ s"_$i" | ||
392 | // val atoms = atom.getArguments.asScala.toList.zipWithIndex | ||
393 | // .map { case (t, i) => TupleTableAtom.rdf(bvar, reifiedIRI(i), t) } | ||
394 | // (Some(bind), atoms) | ||
395 | // } else { | ||
396 | // (None, List(atom)) | ||
397 | // } | ||
398 | // } | ||
399 | |||
385 | private def reifyAtom(atom: Atom): (Option[BindAtom], List[Atom]) = { | 400 | private def reifyAtom(atom: Atom): (Option[BindAtom], List[Atom]) = { |
386 | atom match { | 401 | atom match { |
387 | case tta: TupleTableAtom => reifyTupleTableAtom(tta) | 402 | case atom: TupleTableAtom => atom.reified |
388 | case other => (None, List(other)) | 403 | case other => (None, List(other)) |
389 | } | 404 | } |
390 | } | 405 | } |
391 | 406 | ||
392 | private def reifyBodyFormula(formula: BodyFormula): List[BodyFormula] = { | 407 | private def reifyBodyFormula(formula: BodyFormula): List[BodyFormula] = { |
393 | formula match { | 408 | formula match { |
394 | case atom: TupleTableAtom => reifyTupleTableAtom(atom)._2 | 409 | case atom: TupleTableAtom => atom.reified._2 |
395 | case neg: Negation => { | 410 | case neg: Negation => { |
396 | val (bs, as) = neg.getNegatedAtoms.asScala.toList.map(reifyAtom).unzip | 411 | val (bs, as) = neg.getNegatedAtoms.asScala.toList.map(reifyAtom).unzip |
397 | val bind = bs.flatten.map(_.getBoundVariable).asJava | 412 | val bind = bs.flatten.map(_.getBoundVariable).asJava |
@@ -403,7 +418,7 @@ class FilteringProgram(query: SelectQuery, constants: List[Term]) | |||
403 | } | 418 | } |
404 | 419 | ||
405 | private def reifyRule(rule: Rule): Rule = { | 420 | private def reifyRule(rule: Rule): Rule = { |
406 | val (bs, hs) = rule.getHead.asScala.toList.map(reifyTupleTableAtom).unzip | 421 | val (bs, hs) = rule.getHead.asScala.toList.map(_.reified).unzip |
407 | val head: List[TupleTableAtom] = hs.flatten | 422 | val head: List[TupleTableAtom] = hs.flatten |
408 | val bind: List[BodyFormula] = bs.flatten | 423 | val bind: List[BodyFormula] = bs.flatten |
409 | val body: List[BodyFormula] = | 424 | val body: List[BodyFormula] = |
diff --git a/src/main/scala/rsacomb/RDFTriple.scala b/src/main/scala/rsacomb/RDFTriple.scala index 4054d42..a65c168 100644 --- a/src/main/scala/rsacomb/RDFTriple.scala +++ b/src/main/scala/rsacomb/RDFTriple.scala | |||
@@ -1,60 +1,87 @@ | |||
1 | package rsacomb | 1 | package rsacomb.implicits |
2 | 2 | ||
3 | import tech.oxfordsemantic.jrdfox.logic.datalog.{TupleTableAtom, TupleTableName} | 3 | import tech.oxfordsemantic.jrdfox.logic.Datatype |
4 | import tech.oxfordsemantic.jrdfox.logic.expression.{Literal, FunctionCall} | ||
5 | import tech.oxfordsemantic.jrdfox.logic.datalog.{ | ||
6 | BindAtom, | ||
7 | TupleTableAtom, | ||
8 | TupleTableName | ||
9 | } | ||
4 | import tech.oxfordsemantic.jrdfox.logic.expression.{IRI} | 10 | import tech.oxfordsemantic.jrdfox.logic.expression.{IRI} |
11 | import scala.collection.JavaConverters._ | ||
5 | 12 | ||
6 | trait RDFTriple { | 13 | import rsacomb.suffix.{RSASuffix, Nth} |
7 | 14 | import rsacomb.RSA | |
8 | implicit class RDFTriple(atom: TupleTableAtom) { | 15 | |
9 | 16 | /* Is this the best way to determine if an atom is an RDF triple? | |
10 | /* Is this the best way to determine if an atom is an RDF triple? | 17 | * Note that we can't use `getNumberOfArguments()` because is not |
11 | * Note that we can't use `getNumberOfArguments()` because is not | 18 | * "consistent": |
12 | * "consistent": | 19 | * - for an atom created with `rdf(<term1>, <term2>, <term3>)`, |
13 | * - for an atom created with `rdf(<term1>, <term2>, <term3>)`, | 20 | * `getNumberOfArguments` returns 3 |
14 | * `getNumberOfArguments` returns 3 | 21 | * - for an atom created with `Atom.create(<tupletablename>, <term1>, |
15 | * - for an atom created with `Atom.create(<tupletablename>, <term1>, | 22 | * <term2>, <term3>)`, `getNumberOfArguments()` returns 3 |
16 | * <term2>, <term3>)`, `getNumberOfArguments()` returns 3 | 23 | * |
17 | * | 24 | * This is probably because `Atom.rdf(...) is implemented as: |
18 | * This is probably because `Atom.rdf(...) is implemented as: | 25 | * ```scala |
19 | * ```scala | 26 | * def rdf(term1: Term, term2: Term, term3: Term): Atom = |
20 | * def rdf(term1: Term, term2: Term, term3: Term): Atom = | 27 | * Atom.create(TupleTableName.create("internal:triple"), term1, term2, term3) |
21 | * Atom.create(TupleTableName.create("internal:triple"), term1, term2, term3) | 28 | * ``` |
22 | * ``` | 29 | */ |
23 | */ | 30 | |
24 | def isRdfTriple: Boolean = | 31 | trait RSAAtom { |
25 | atom.getTupleTableName.getName.equals("internal:triple") | 32 | |
26 | 33 | implicit class RSAAtom(val atom: TupleTableAtom) { | |
27 | def isClassAssertion: Boolean = | 34 | |
28 | atom.isRdfTriple && atom.getArguments.get(1).equals(IRI.RDF_TYPE) | 35 | import rsacomb.RDFoxUtil.stringToRDFoxIRI |
29 | 36 | ||
30 | def isRoleAssertion: Boolean = | 37 | val name: String = atom.getTupleTableName.getName |
31 | atom.isRdfTriple && !atom.getArguments.get(1).equals(IRI.RDF_TYPE) | 38 | |
32 | 39 | val isRDF: Boolean = name == "internal:triple" | |
33 | def suffix(sx: String): TupleTableAtom = | 40 | |
34 | if (this.isClassAssertion) { | 41 | val isClassAssertion: Boolean = { |
35 | val newclass = atom.getArguments.get(2) match { | 42 | isRDF && { |
36 | case iri: IRI => IRI.create(s"${iri.getIRI}_$sx") | 43 | val pred = atom.getArguments.get(1) |
37 | case other => other | 44 | pred == IRI.RDF_TYPE |
38 | } | 45 | } |
39 | TupleTableAtom.rdf( | 46 | } |
40 | atom.getArguments.get(0), | 47 | |
41 | atom.getArguments.get(1), | 48 | val isRoleAssertion: Boolean = isRDF && !isClassAssertion |
42 | newclass | 49 | |
43 | ) | 50 | def <<(suffix: RSASuffix): TupleTableAtom = |
44 | } else if (this.isRoleAssertion) { | 51 | if (isRDF) { |
45 | val newrole = atom.getArguments.get(1) match { | 52 | val subj = atom.getArguments.get(0) |
46 | case iri: IRI => IRI.create(s"${iri.getIRI}_$sx") | 53 | val pred = atom.getArguments.get(1) |
47 | case other => other | 54 | val obj = atom.getArguments.get(2) |
55 | if (isClassAssertion) { | ||
56 | val obj1 = obj match { | ||
57 | case iri: IRI => IRI.create(iri.getIRI :: suffix) | ||
58 | case other => other | ||
59 | } | ||
60 | TupleTableAtom.rdf(subj, pred, obj1) | ||
61 | } else { | ||
62 | val pred1 = pred match { | ||
63 | case iri: IRI => IRI.create(iri.getIRI :: suffix) | ||
64 | case other => other | ||
65 | } | ||
66 | TupleTableAtom.rdf(subj, pred1, obj) | ||
48 | } | 67 | } |
49 | TupleTableAtom.rdf( | ||
50 | atom.getArguments.get(0), | ||
51 | newrole, | ||
52 | atom.getArguments.get(2) | ||
53 | ) | ||
54 | } else { | 68 | } else { |
55 | val newname = | 69 | val ttname = TupleTableName.create(name :: suffix) |
56 | TupleTableName.create(s"${atom.getTupleTableName.getName}_$sx") | 70 | TupleTableAtom.create(ttname, atom.getArguments()) |
57 | TupleTableAtom.create(newname, atom.getArguments()) | 71 | } |
72 | |||
73 | lazy val reified: (Option[BindAtom], List[TupleTableAtom]) = | ||
74 | if (isRDF) { | ||
75 | (None, List(atom)) | ||
76 | } else { | ||
77 | val bvar = RSA.getFreshVariable() | ||
78 | val str = Literal.create(name, Datatype.XSD_STRING) | ||
79 | val args = atom.getArguments.asScala.toList | ||
80 | val skolem = FunctionCall.create("SKOLEM", str :: args: _*) | ||
81 | val bind = BindAtom.create(skolem, bvar) | ||
82 | val atoms = args.zipWithIndex | ||
83 | .map { case (t, i) => TupleTableAtom.rdf(bvar, name :: Nth(i), t) } | ||
84 | (Some(bind), atoms) | ||
58 | } | 85 | } |
59 | } | 86 | } |
60 | 87 | ||
diff --git a/src/main/scala/rsacomb/RDFoxPropertyExprConverter.scala b/src/main/scala/rsacomb/RDFoxPropertyExprConverter.scala index ea1df2f..525ec62 100644 --- a/src/main/scala/rsacomb/RDFoxPropertyExprConverter.scala +++ b/src/main/scala/rsacomb/RDFoxPropertyExprConverter.scala | |||
@@ -20,15 +20,14 @@ class RDFoxPropertyExprConverter( | |||
20 | import RDFoxUtil.owlapi2rdfox; | 20 | import RDFoxUtil.owlapi2rdfox; |
21 | 21 | ||
22 | override def visit(expr: OWLObjectProperty): List[TupleTableAtom] = { | 22 | override def visit(expr: OWLObjectProperty): List[TupleTableAtom] = { |
23 | //val pred = IRI.create(expr.getIRI.getIRIString ++ suffix.getSuffix) | 23 | val base = expr.getIRI.getIRIString |
24 | val pred = IRI.create(expr :: suffix) | 24 | val pred = IRI.create(base :: suffix) |
25 | List(TupleTableAtom.rdf(term1, pred, term2)) | 25 | List(TupleTableAtom.rdf(term1, pred, term2)) |
26 | } | 26 | } |
27 | 27 | ||
28 | override def visit(expr: OWLObjectInverseOf): List[TupleTableAtom] = { | 28 | override def visit(expr: OWLObjectInverseOf): List[TupleTableAtom] = { |
29 | //expr.getInverse.getNamedProperty.getIRI.getIRIString ++ suffix.getSuffix ++ "_inv" | 29 | val visitor = new RDFoxPropertyExprConverter(term1, term2, suffix + Inverse) |
30 | val pred = IRI.create(expr :: suffix + Inverse) | 30 | expr.getInverse.accept(visitor) |
31 | List(TupleTableAtom.rdf(term1, pred, term2)) | ||
32 | } | 31 | } |
33 | 32 | ||
34 | def doDefault(expr: OWLPropertyExpression): List[TupleTableAtom] = List() | 33 | def doDefault(expr: OWLPropertyExpression): List[TupleTableAtom] = List() |
diff --git a/src/main/scala/rsacomb/RDFoxUtil.scala b/src/main/scala/rsacomb/RDFoxUtil.scala index 45d97de..d391d41 100644 --- a/src/main/scala/rsacomb/RDFoxUtil.scala +++ b/src/main/scala/rsacomb/RDFoxUtil.scala | |||
@@ -25,7 +25,7 @@ object RDFoxUtil { | |||
25 | RDFox_IRI.create(iri.getIRIString()) | 25 | RDFox_IRI.create(iri.getIRIString()) |
26 | } | 26 | } |
27 | 27 | ||
28 | implicit def owlapi2rdfox(iri: String): RDFox_IRI = { | 28 | implicit def stringToRDFoxIRI(iri: String): RDFox_IRI = { |
29 | RDFox_IRI.create(iri) | 29 | RDFox_IRI.create(iri) |
30 | } | 30 | } |
31 | 31 | ||
diff --git a/src/main/scala/rsacomb/RSASuffix.scala b/src/main/scala/rsacomb/RSASuffix.scala index 7650ae0..ce36b10 100644 --- a/src/main/scala/rsacomb/RSASuffix.scala +++ b/src/main/scala/rsacomb/RSASuffix.scala | |||
@@ -6,22 +6,25 @@ import org.semanticweb.owlapi.model.{ | |||
6 | OWLObjectProperty | 6 | OWLObjectProperty |
7 | } | 7 | } |
8 | 8 | ||
9 | import tech.oxfordsemantic.jrdfox.logic.expression.{IRI} | ||
10 | import tech.oxfordsemantic.jrdfox.logic.datalog.{TupleTableAtom, TupleTableName} | ||
11 | |||
12 | object RSASuffix { | ||
13 | |||
14 | def apply(suffix: String => String): RSASuffix = new RSASuffix(suffix) | ||
15 | |||
16 | } | ||
17 | |||
9 | class RSASuffix(val suffix: String => String) { | 18 | class RSASuffix(val suffix: String => String) { |
10 | 19 | ||
11 | def +(other: RSASuffix): RSASuffix = | 20 | def +(that: RSASuffix): RSASuffix = |
12 | new RSASuffix((s: String) => other suffix (this suffix s)) | 21 | new RSASuffix(this.suffix andThen that.suffix) |
13 | 22 | ||
14 | def ::(str: String): String = this suffix str | 23 | def ::(str: String): String = this suffix str |
15 | 24 | ||
16 | def ::(expr: OWLPropertyExpression): String = | ||
17 | expr match { | ||
18 | case e: OWLObjectProperty => e.getIRI.getIRIString :: this | ||
19 | case e: OWLObjectInverseOf => e.getInverse :: this | ||
20 | } | ||
21 | |||
22 | } | 25 | } |
23 | 26 | ||
24 | case object Empty extends RSASuffix((x) => x) | 27 | case object Empty extends RSASuffix(identity) |
25 | case object Forward extends RSASuffix((s) => s"${s}_f") | 28 | case object Forward extends RSASuffix((s) => s"${s}_f") |
26 | case object Backward extends RSASuffix((s) => s"${s}_b") | 29 | case object Backward extends RSASuffix((s) => s"${s}_b") |
27 | case object Inverse extends RSASuffix((s) => s"${s}_inv") | 30 | case object Inverse extends RSASuffix((s) => s"${s}_inv") |