From b7d9a8ffc8cc4a8949229b5bd3ada301878633d9 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Tue, 24 Nov 2020 11:41:08 +0000 Subject: Rework filtering program generation --- .../uk/ac/ox/cs/rsacomb/FilteringProgram.scala | 64 ++++------------------ .../uk/ac/ox/cs/rsacomb/util/RDFoxHelpers.scala | 62 ++++++++++++++++++++- 2 files changed, 70 insertions(+), 56 deletions(-) (limited to 'src/main/scala') 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 @@ package uk.ac.ox.cs.rsacomb +//import scala.collection.JavaConverters._ import tech.oxfordsemantic.jrdfox.logic.Datatype -import tech.oxfordsemantic.jrdfox.logic.expression.{ - Term, - IRI, - Variable, - Literal, - FunctionCall -} import tech.oxfordsemantic.jrdfox.logic.datalog.{ Rule, TupleTableAtom, - BindAtom, - TupleTableName, - Atom, BodyFormula, Negation } -import tech.oxfordsemantic.jrdfox.logic.sparql.statement.{SelectQuery} -import tech.oxfordsemantic.jrdfox.logic.sparql.pattern.{ - GroupGraphPattern, - ConjunctionPattern, - TriplePattern, - QueryPattern -} - -import scala.collection.JavaConverters._ - +import tech.oxfordsemantic.jrdfox.logic.expression.{Term, Variable} import uk.ac.ox.cs.rsacomb.implicits.RSAAtom -import uk.ac.ox.cs.rsacomb.suffix.{RSASuffix, Forward, Backward} -import uk.ac.ox.cs.rsacomb.util.RSA import uk.ac.ox.cs.rsacomb.sparql.ConjunctiveQuery +import uk.ac.ox.cs.rsacomb.suffix.{Forward, Backward} +import uk.ac.ox.cs.rsacomb.util.{RSA, RDFoxHelpers} /** Factory for [[uk.ac.ox.cs.rsacomb.FilteringProgram FilteringProgram]] */ object FilteringProgram { @@ -102,7 +84,7 @@ class FilteringProgram(query: ConjunctiveQuery, constants: List[Term]) * * @note corresponds to rule 1 in Table 3 in the paper. */ - val r1 = reifyRule(Rule.create(RSA.QM, query.atoms: _*)) + val r1 = Rule.create(RSA.QM, query.atoms: _*) /** Initializes instances of `rsa:Named`. * @@ -325,39 +307,13 @@ class FilteringProgram(query: ConjunctiveQuery, constants: List[Term]) (r1 :: r2 ::: r3a ::: r3b :: r3c :: r4a ::: r4b ::: r4c ::: - // r5c ::: r5b ::: r5a ::: + r5a ::: r5b ::: r5c ::: r6 ::: r7b ::: r7a ::: r8a ::: r8b :: r8c ::: - r9 :: List()) map reifyRule + r9 :: List()) map RDFoxHelpers.reify } - private def reifyAtom(atom: Atom): (Option[BindAtom], List[Atom]) = { - atom match { - case atom: TupleTableAtom => atom.reified - case other => (None, List(other)) - } - } + /** Pretty-print filtering rule */ + override def toString(): String = rules mkString "\n" - private def reifyBodyFormula(formula: BodyFormula): List[BodyFormula] = { - formula match { - case atom: TupleTableAtom => atom.reified._2 - case neg: Negation => { - val (bs, as) = neg.getNegatedAtoms.asScala.toList.map(reifyAtom).unzip - val bind = bs.flatten.map(_.getBoundVariable).asJava - val atoms = as.flatten.asJava - List(Negation.create(bind, atoms)) - } - case other => List(other) - } - } - - private def reifyRule(rule: Rule): Rule = { - val (bs, hs) = rule.getHead.asScala.toList.map(_.reified).unzip - val head: List[TupleTableAtom] = hs.flatten - val bind: List[BodyFormula] = bs.flatten - val body: List[BodyFormula] = - rule.getBody.asScala.toList.map(reifyBodyFormula).flatten - Rule.create(head.asJava, (body ++ bind).asJava) - } - -} // 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.{ DataStoreConnection } import tech.oxfordsemantic.jrdfox.formats.SPARQLParser -import tech.oxfordsemantic.jrdfox.logic.expression.Resource +import tech.oxfordsemantic.jrdfox.logic.datalog.{ + Rule, + BodyFormula, + Negation, + TupleTableAtom, + TupleTableName +} +import tech.oxfordsemantic.jrdfox.logic.expression.{Resource} import tech.oxfordsemantic.jrdfox.logic.sparql.statement.SelectQuery import uk.ac.ox.cs.rsacomb.suffix.Nth +import uk.ac.ox.cs.rsacomb.implicits.RSAAtom /** A collection of helper methods for RDFox */ -object RDFoxHelpers { +object RDFoxHelpers extends RSAAtom { + + /** Simplify conversion between Java and Scala `List`s */ + import uk.ac.ox.cs.rsacomb.implicits.JavaCollections._ + + /** Extends capabilities of + * [[tech.oxfordsemantic.jrdfox.logic.datalog.TupleTableAtom TupleTableAtom]]. + */ + //import uk.ac.ox.cs.rsacomb.implicits.RSAAtom._ /** Type alias for a collection of answers to a * [[tech.oxfordsemantic.jrdfox.logic.sparql.statement.Query]]. @@ -142,6 +158,48 @@ object RDFoxHelpers { } } + /** Reify a [[tech.oxfordsemantic.jrdfox.logic.datalog.Rule Rule]]. + * + * This is needed because RDFox supports only predicates of arity 1 + * or 2, but the filtering program uses predicates with higher arity. + * + * @note we can perform a reification of the atoms thanks to the + * built-in `SKOLEM` funtion of RDFox. + */ + def reify(rule: Rule): Rule = { + val (bs, as) = rule.getHead.map(_.reified).unzip + val head: List[TupleTableAtom] = as.flatten + val bind: List[BodyFormula] = bs.flatten + val body: List[BodyFormula] = rule.getBody.map(reify).flatten + Rule.create(head, bind ::: body) + } + + /** Reify a [[tech.oxfordsemantic.jrdfox.logic.datalog.BodyFormula BodyFormula]]. + * + * This is needed because RDFox supports only predicates of arity 1 + * or 2, but the filtering program uses predicates with higher arity. + * + * @note we can perform a reification of the atoms thanks to the + * built-in `SKOLEM` funtion of RDFox. + */ + private def reify(formula: BodyFormula): List[BodyFormula] = { + formula match { + case atom: TupleTableAtom => atom.reified._2 + case neg: Negation => { + val (bs, as) = neg.getNegatedAtoms + .map({ + case a: TupleTableAtom => a.reified + case a => (None, List(a)) + }) + .unzip + val bind = bs.flatten.map(_.getBoundVariable) + val atoms = as.flatten + List(Negation.create(bind, atoms)) + } + case other => List(other) + } + } + /** Close an open connection to RDFox. * * @param server server connection -- cgit v1.2.3