diff options
| author | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-11-24 11:41:08 +0000 |
|---|---|---|
| committer | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-11-24 11:41:08 +0000 |
| commit | b7d9a8ffc8cc4a8949229b5bd3ada301878633d9 (patch) | |
| tree | 8bf1ea5fcada85028fe754c902906d890546cd60 /src/main | |
| parent | aeb5ad23e5f13952efdeaf4aec2e97a96b469655 (diff) | |
| download | RSAComb-b7d9a8ffc8cc4a8949229b5bd3ada301878633d9.tar.gz RSAComb-b7d9a8ffc8cc4a8949229b5bd3ada301878633d9.zip | |
Rework filtering program generation
Diffstat (limited to 'src/main')
| -rw-r--r-- | src/main/scala/uk/ac/ox/cs/rsacomb/FilteringProgram.scala | 64 | ||||
| -rw-r--r-- | src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxHelpers.scala | 62 |
2 files changed, 70 insertions, 56 deletions
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/FilteringProgram.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/FilteringProgram.scala index 055cf2a..b70de66 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/FilteringProgram.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/FilteringProgram.scala | |||
| @@ -1,36 +1,18 @@ | |||
| 1 | package uk.ac.ox.cs.rsacomb | 1 | package uk.ac.ox.cs.rsacomb |
| 2 | 2 | ||
| 3 | //import scala.collection.JavaConverters._ | ||
| 3 | import tech.oxfordsemantic.jrdfox.logic.Datatype | 4 | import tech.oxfordsemantic.jrdfox.logic.Datatype |
| 4 | import tech.oxfordsemantic.jrdfox.logic.expression.{ | ||
| 5 | Term, | ||
| 6 | IRI, | ||
| 7 | Variable, | ||
| 8 | Literal, | ||
| 9 | FunctionCall | ||
| 10 | } | ||
| 11 | import tech.oxfordsemantic.jrdfox.logic.datalog.{ | 5 | import tech.oxfordsemantic.jrdfox.logic.datalog.{ |
| 12 | Rule, | 6 | Rule, |
| 13 | TupleTableAtom, | 7 | TupleTableAtom, |
| 14 | BindAtom, | ||
| 15 | TupleTableName, | ||
| 16 | Atom, | ||
| 17 | BodyFormula, | 8 | BodyFormula, |
| 18 | Negation | 9 | Negation |
| 19 | } | 10 | } |
| 20 | import tech.oxfordsemantic.jrdfox.logic.sparql.statement.{SelectQuery} | 11 | import tech.oxfordsemantic.jrdfox.logic.expression.{Term, Variable} |
| 21 | import tech.oxfordsemantic.jrdfox.logic.sparql.pattern.{ | ||
| 22 | GroupGraphPattern, | ||
| 23 | ConjunctionPattern, | ||
| 24 | TriplePattern, | ||
| 25 | QueryPattern | ||
| 26 | } | ||
| 27 | |||
| 28 | import scala.collection.JavaConverters._ | ||
| 29 | |||
| 30 | import uk.ac.ox.cs.rsacomb.implicits.RSAAtom | 12 | import uk.ac.ox.cs.rsacomb.implicits.RSAAtom |
| 31 | import uk.ac.ox.cs.rsacomb.suffix.{RSASuffix, Forward, Backward} | ||
| 32 | import uk.ac.ox.cs.rsacomb.util.RSA | ||
| 33 | import uk.ac.ox.cs.rsacomb.sparql.ConjunctiveQuery | 13 | import uk.ac.ox.cs.rsacomb.sparql.ConjunctiveQuery |
| 14 | import uk.ac.ox.cs.rsacomb.suffix.{Forward, Backward} | ||
| 15 | import uk.ac.ox.cs.rsacomb.util.{RSA, RDFoxHelpers} | ||
| 34 | 16 | ||
| 35 | /** Factory for [[uk.ac.ox.cs.rsacomb.FilteringProgram FilteringProgram]] */ | 17 | /** Factory for [[uk.ac.ox.cs.rsacomb.FilteringProgram FilteringProgram]] */ |
| 36 | object FilteringProgram { | 18 | object FilteringProgram { |
| @@ -102,7 +84,7 @@ class FilteringProgram(query: ConjunctiveQuery, constants: List[Term]) | |||
| 102 | * | 84 | * |
| 103 | * @note corresponds to rule 1 in Table 3 in the paper. | 85 | * @note corresponds to rule 1 in Table 3 in the paper. |
| 104 | */ | 86 | */ |
| 105 | val r1 = reifyRule(Rule.create(RSA.QM, query.atoms: _*)) | 87 | val r1 = Rule.create(RSA.QM, query.atoms: _*) |
| 106 | 88 | ||
| 107 | /** Initializes instances of `rsa:Named`. | 89 | /** Initializes instances of `rsa:Named`. |
| 108 | * | 90 | * |
| @@ -325,39 +307,13 @@ class FilteringProgram(query: ConjunctiveQuery, constants: List[Term]) | |||
| 325 | (r1 :: r2 ::: | 307 | (r1 :: r2 ::: |
| 326 | r3a ::: r3b :: r3c :: | 308 | r3a ::: r3b :: r3c :: |
| 327 | r4a ::: r4b ::: r4c ::: | 309 | r4a ::: r4b ::: r4c ::: |
| 328 | // r5c ::: r5b ::: r5a ::: | 310 | r5a ::: r5b ::: r5c ::: |
| 329 | r6 ::: r7b ::: r7a ::: | 311 | r6 ::: r7b ::: r7a ::: |
| 330 | r8a ::: r8b :: r8c ::: | 312 | r8a ::: r8b :: r8c ::: |
| 331 | r9 :: List()) map reifyRule | 313 | r9 :: List()) map RDFoxHelpers.reify |
| 332 | } | 314 | } |
| 333 | 315 | ||
| 334 | private def reifyAtom(atom: Atom): (Option[BindAtom], List[Atom]) = { | 316 | /** Pretty-print filtering rule */ |
| 335 | atom match { | 317 | override def toString(): String = rules mkString "\n" |
| 336 | case atom: TupleTableAtom => atom.reified | ||
| 337 | case other => (None, List(other)) | ||
| 338 | } | ||
| 339 | } | ||
| 340 | 318 | ||
| 341 | private def reifyBodyFormula(formula: BodyFormula): List[BodyFormula] = { | 319 | } |
| 342 | formula match { | ||
| 343 | case atom: TupleTableAtom => atom.reified._2 | ||
| 344 | case neg: Negation => { | ||
| 345 | val (bs, as) = neg.getNegatedAtoms.asScala.toList.map(reifyAtom).unzip | ||
| 346 | val bind = bs.flatten.map(_.getBoundVariable).asJava | ||
| 347 | val atoms = as.flatten.asJava | ||
| 348 | List(Negation.create(bind, atoms)) | ||
| 349 | } | ||
| 350 | case other => List(other) | ||
| 351 | } | ||
| 352 | } | ||
| 353 | |||
| 354 | private def reifyRule(rule: Rule): Rule = { | ||
| 355 | val (bs, hs) = rule.getHead.asScala.toList.map(_.reified).unzip | ||
| 356 | val head: List[TupleTableAtom] = hs.flatten | ||
| 357 | val bind: List[BodyFormula] = bs.flatten | ||
| 358 | val body: List[BodyFormula] = | ||
| 359 | rule.getBody.asScala.toList.map(reifyBodyFormula).flatten | ||
| 360 | Rule.create(head.asJava, (body ++ bind).asJava) | ||
| 361 | } | ||
| 362 | |||
| 363 | } // class FilteringProgram | ||
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxHelpers.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxHelpers.scala index 0f3a1cf..a05e416 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxHelpers.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxHelpers.scala | |||
| @@ -8,12 +8,28 @@ import tech.oxfordsemantic.jrdfox.client.{ | |||
| 8 | DataStoreConnection | 8 | DataStoreConnection |
| 9 | } | 9 | } |
| 10 | import tech.oxfordsemantic.jrdfox.formats.SPARQLParser | 10 | import tech.oxfordsemantic.jrdfox.formats.SPARQLParser |
| 11 | import tech.oxfordsemantic.jrdfox.logic.expression.Resource | 11 | import tech.oxfordsemantic.jrdfox.logic.datalog.{ |
| 12 | Rule, | ||
| 13 | BodyFormula, | ||
| 14 | Negation, | ||
| 15 | TupleTableAtom, | ||
| 16 | TupleTableName | ||
| 17 | } | ||
| 18 | import tech.oxfordsemantic.jrdfox.logic.expression.{Resource} | ||
| 12 | import tech.oxfordsemantic.jrdfox.logic.sparql.statement.SelectQuery | 19 | import tech.oxfordsemantic.jrdfox.logic.sparql.statement.SelectQuery |
| 13 | import uk.ac.ox.cs.rsacomb.suffix.Nth | 20 | import uk.ac.ox.cs.rsacomb.suffix.Nth |
| 21 | import uk.ac.ox.cs.rsacomb.implicits.RSAAtom | ||
| 14 | 22 | ||
| 15 | /** A collection of helper methods for RDFox */ | 23 | /** A collection of helper methods for RDFox */ |
| 16 | object RDFoxHelpers { | 24 | object RDFoxHelpers extends RSAAtom { |
| 25 | |||
| 26 | /** Simplify conversion between Java and Scala `List`s */ | ||
| 27 | import uk.ac.ox.cs.rsacomb.implicits.JavaCollections._ | ||
| 28 | |||
| 29 | /** Extends capabilities of | ||
| 30 | * [[tech.oxfordsemantic.jrdfox.logic.datalog.TupleTableAtom TupleTableAtom]]. | ||
| 31 | */ | ||
| 32 | //import uk.ac.ox.cs.rsacomb.implicits.RSAAtom._ | ||
| 17 | 33 | ||
| 18 | /** Type alias for a collection of answers to a | 34 | /** Type alias for a collection of answers to a |
| 19 | * [[tech.oxfordsemantic.jrdfox.logic.sparql.statement.Query]]. | 35 | * [[tech.oxfordsemantic.jrdfox.logic.sparql.statement.Query]]. |
| @@ -142,6 +158,48 @@ object RDFoxHelpers { | |||
| 142 | } | 158 | } |
| 143 | } | 159 | } |
| 144 | 160 | ||
| 161 | /** Reify a [[tech.oxfordsemantic.jrdfox.logic.datalog.Rule Rule]]. | ||
| 162 | * | ||
| 163 | * This is needed because RDFox supports only predicates of arity 1 | ||
| 164 | * or 2, but the filtering program uses predicates with higher arity. | ||
| 165 | * | ||
| 166 | * @note we can perform a reification of the atoms thanks to the | ||
| 167 | * built-in `SKOLEM` funtion of RDFox. | ||
| 168 | */ | ||
| 169 | def reify(rule: Rule): Rule = { | ||
| 170 | val (bs, as) = rule.getHead.map(_.reified).unzip | ||
| 171 | val head: List[TupleTableAtom] = as.flatten | ||
| 172 | val bind: List[BodyFormula] = bs.flatten | ||
| 173 | val body: List[BodyFormula] = rule.getBody.map(reify).flatten | ||
| 174 | Rule.create(head, bind ::: body) | ||
| 175 | } | ||
| 176 | |||
| 177 | /** Reify a [[tech.oxfordsemantic.jrdfox.logic.datalog.BodyFormula BodyFormula]]. | ||
| 178 | * | ||
| 179 | * This is needed because RDFox supports only predicates of arity 1 | ||
| 180 | * or 2, but the filtering program uses predicates with higher arity. | ||
| 181 | * | ||
| 182 | * @note we can perform a reification of the atoms thanks to the | ||
| 183 | * built-in `SKOLEM` funtion of RDFox. | ||
| 184 | */ | ||
| 185 | private def reify(formula: BodyFormula): List[BodyFormula] = { | ||
| 186 | formula match { | ||
| 187 | case atom: TupleTableAtom => atom.reified._2 | ||
| 188 | case neg: Negation => { | ||
| 189 | val (bs, as) = neg.getNegatedAtoms | ||
| 190 | .map({ | ||
| 191 | case a: TupleTableAtom => a.reified | ||
| 192 | case a => (None, List(a)) | ||
| 193 | }) | ||
| 194 | .unzip | ||
| 195 | val bind = bs.flatten.map(_.getBoundVariable) | ||
| 196 | val atoms = as.flatten | ||
| 197 | List(Negation.create(bind, atoms)) | ||
| 198 | } | ||
| 199 | case other => List(other) | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 145 | /** Close an open connection to RDFox. | 203 | /** Close an open connection to RDFox. |
| 146 | * | 204 | * |
| 147 | * @param server server connection | 205 | * @param server server connection |
