From 548ad06610dabeafec983449da597f1279bf6def Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Fri, 11 Dec 2020 17:54:21 +0000 Subject: Port code to RDFox v4.1.0 --- .../ox/cs/rsacomb/converter/RDFoxConverter.scala | 12 ++--- .../ox/cs/rsacomb/converter/SkolemStrategy.scala | 3 +- .../uk/ac/ox/cs/rsacomb/implicits/RSAAtom.scala | 29 ++++++------ .../scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala | 51 ++++++++++++++++++---- 4 files changed, 61 insertions(+), 34 deletions(-) (limited to 'src/main/scala/uk/ac/ox/cs') diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxConverter.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxConverter.scala index b4f5adb..845ec1f 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxConverter.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/converter/RDFoxConverter.scala @@ -40,7 +40,7 @@ import tech.oxfordsemantic.jrdfox.logic.datalog.{ import tech.oxfordsemantic.jrdfox.logic.expression.{Term, IRI, FunctionCall} import uk.ac.ox.cs.rsacomb.RSAOntology import uk.ac.ox.cs.rsacomb.suffix.{Empty, Inverse, RSASuffix} -import uk.ac.ox.cs.rsacomb.util.RSA +import uk.ac.ox.cs.rsacomb.util.{RSA, RDFoxUtil} /** Horn-ALCHOIQ to RDFox axiom converter. * @@ -328,10 +328,7 @@ trait RDFoxConverter { val (bind, term1) = skolem match { case NoSkolem => (None, varX) case c: Constant => (None, c.iri) - case s: Standard => { - val func = FunctionCall.create("SKOLEM", s.literal, term) - (Some(BindAtom.create(func, varX)), varX) - } + case s: Standard => (Some(RDFoxUtil.skolem(s.name, term, varX)), varX) } val (res, ext) = convert(cls, term1, unsafe, skolem, suffix) val prop = convert(role, term, term1, suffix) @@ -359,10 +356,7 @@ trait RDFoxConverter { val (bind, term1) = skolem match { case NoSkolem => (None, varX) case c: Constant => (None, c.iri) - case s: Standard => { - val func = FunctionCall.create("SKOLEM", s.literal, term) - (Some(BindAtom.create(func, varX)), varX) - } + case s: Standard => (Some(RDFoxUtil.skolem(s.name, term, varX)), varX) } val prop = convert(role, term, term1, suffix) (List(prop), bind.toList) diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/converter/SkolemStrategy.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/converter/SkolemStrategy.scala index 2142ff3..426a327 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/converter/SkolemStrategy.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/converter/SkolemStrategy.scala @@ -53,8 +53,7 @@ case object NoSkolem extends SkolemStrategy { case class Standard(axiom: OWLAxiom)(implicit toString: (OWLAxiom) => String) extends SkolemStrategy { def dup(_axiom: OWLAxiom): Standard = copy(axiom = _axiom)(toString) - lazy val literal = - Literal.create(s"f_${toString(axiom)}", Datatype.XSD_STRING) + lazy val name = s"f_${toString(axiom)}" } /** Constant skolemization diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/implicits/RSAAtom.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/implicits/RSAAtom.scala index 615722b..9b04f0e 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/implicits/RSAAtom.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/implicits/RSAAtom.scala @@ -1,17 +1,17 @@ package uk.ac.ox.cs.rsacomb.implicits import tech.oxfordsemantic.jrdfox.logic.Datatype -import tech.oxfordsemantic.jrdfox.logic.expression.{Literal, FunctionCall} +import tech.oxfordsemantic.jrdfox.logic.expression.{Literal, FunctionCall, Term} import tech.oxfordsemantic.jrdfox.logic.datalog.{ BindAtom, TupleTableAtom, TupleTableName } import tech.oxfordsemantic.jrdfox.logic.expression.{IRI} -import scala.collection.JavaConverters._ -import uk.ac.ox.cs.rsacomb.suffix.{RSASuffix, Nth} import uk.ac.ox.cs.rsacomb.RSAOntology +import uk.ac.ox.cs.rsacomb.suffix.{RSASuffix, Nth} +import uk.ac.ox.cs.rsacomb.util.RDFoxUtil /* Is this the best way to determine if an atom is an RDF triple? * Note that we can't use `getNumberOfArguments()` because is not @@ -24,7 +24,7 @@ import uk.ac.ox.cs.rsacomb.RSAOntology * This is probably because `Atom.rdf(...) is implemented as: * ```scala * def rdf(term1: Term, term2: Term, term3: Term): Atom = - * Atom.create(TupleTableName.create("internal:triple"), term1, term2, term3) + * Atom.create(TupleTableName.create("rdfox:DefaultTriples"), term1, term2, term3) * ``` */ @@ -33,10 +33,14 @@ object RSAAtom { implicit class RSAAtom(val atom: TupleTableAtom) { import RDFox._ + import JavaCollections._ val name: String = atom.getTupleTableName.getName - val isRDF: Boolean = name == "internal:triple" + val args: List[Term] = atom.getArguments + + val isRDF: Boolean = + name == "http://oxfordsemantic.tech/RDFox#DefaultTriples" val isClassAssertion: Boolean = { isRDF && { @@ -70,19 +74,16 @@ object RSAAtom { TupleTableAtom.create(ttname, atom.getArguments()) } - lazy val reified: (Option[BindAtom], List[TupleTableAtom]) = + lazy val reified: (Option[TupleTableAtom], List[TupleTableAtom]) = if (isRDF) { (None, List(atom)) } else { - val bvar = RSAOntology.genFreshVariable() - val str = Literal.create(name, Datatype.XSD_STRING) - val args = atom.getArguments.asScala.toList - val skolem = FunctionCall.create("SKOLEM", str :: args: _*) - val bind = BindAtom.create(skolem, bvar) + val varS = RSAOntology.genFreshVariable() + val skolem = RDFoxUtil.skolem(name, (args :+ varS): _*) + val atom = TupleTableAtom.rdf(varS, IRI.RDF_TYPE, name) val atoms = args.zipWithIndex - .map { case (t, i) => TupleTableAtom.rdf(bvar, name :: Nth(i), t) } - (Some(bind), atoms) + .map { case (a, i) => TupleTableAtom.rdf(varS, name :: Nth(i), a) } + (Some(skolem), atom :: atoms) } } - } diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala index 68a23a3..d072e48 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/util/RDFoxUtil.scala @@ -1,7 +1,6 @@ package uk.ac.ox.cs.rsacomb.util -import java.io.File -import java.io.StringReader +import java.io.{OutputStream, File, StringReader} import tech.oxfordsemantic.jrdfox.Prefixes import tech.oxfordsemantic.jrdfox.client.{ ComponentInfo, @@ -11,6 +10,7 @@ import tech.oxfordsemantic.jrdfox.client.{ UpdateType } import tech.oxfordsemantic.jrdfox.formats.SPARQLParser +import tech.oxfordsemantic.jrdfox.logic.Datatype import tech.oxfordsemantic.jrdfox.logic.datalog.{ Rule, BodyFormula, @@ -18,7 +18,12 @@ import tech.oxfordsemantic.jrdfox.logic.datalog.{ TupleTableAtom, TupleTableName } -import tech.oxfordsemantic.jrdfox.logic.expression.{Resource} +import tech.oxfordsemantic.jrdfox.logic.expression.{ + Literal, + Resource, + Variable, + Term +} import tech.oxfordsemantic.jrdfox.logic.sparql.statement.SelectQuery import uk.ac.ox.cs.rsacomb.suffix.Nth import uk.ac.ox.cs.rsacomb.util.Logger @@ -69,6 +74,13 @@ object RDFoxUtil { (server, data) } + /** Create a built-in `rdfox:SKOLEM` TupleTableAtom. */ + def skolem(name: String, terms: Term*): TupleTableAtom = + TupleTableAtom.create( + TupleTableName.SKOLEM, + (Literal.create(name, Datatype.XSD_STRING) +: terms): _* + ) + /** Prints statistics from RDFox datastore. * * Prints something only when Logger level is set to DEBUG or more. @@ -127,6 +139,26 @@ object RDFoxUtil { query } + /** Export data in `text/turtle`. + * + * @param data datastore connection from which to export data. + * @param rules output stream for rules + * @param facts output stream for facts + */ + def export( + data: DataStoreConnection, + rules: OutputStream, + facts: OutputStream + ): Unit = { + data.exportData(Prefixes.s_emptyPrefixes, facts, "text/turtle", RDFoxOpts()) + data.exportData( + Prefixes.s_emptyPrefixes, + rules, + "application/x.datalog", + RDFoxOpts() + ) + } + /** Parse a SELECT query from a string in SPARQL format. * * @param query the string containing the SPARQL query @@ -231,11 +263,11 @@ object RDFoxUtil { * built-in `SKOLEM` funtion of RDFox. */ def reify(rule: Rule): Rule = { - val (bs, as) = rule.getHead.map(_.reified).unzip + val (sk, as) = rule.getHead.map(_.reified).unzip val head: List[TupleTableAtom] = as.flatten - val bind: List[BodyFormula] = bs.flatten + val skolem: List[BodyFormula] = sk.flatten val body: List[BodyFormula] = rule.getBody.map(reify).flatten - Rule.create(head, bind ::: body) + Rule.create(head, skolem ::: body) } /** Reify a [[tech.oxfordsemantic.jrdfox.logic.datalog.BodyFormula BodyFormula]]. @@ -250,15 +282,16 @@ object RDFoxUtil { formula match { case atom: TupleTableAtom => atom.reified._2 case neg: Negation => { - val (bs, as) = neg.getNegatedAtoms + val (sk, as) = neg.getNegatedAtoms .map({ case a: TupleTableAtom => a.reified case a => (None, List(a)) }) .unzip - val bind = bs.flatten.map(_.getBoundVariable) + val skolem = + sk.flatten.map(_.getArguments.last).collect { case v: Variable => v } val atoms = as.flatten - List(Negation.create(bind, atoms)) + List(Negation.create(skolem, atoms)) } case other => List(other) } -- cgit v1.2.3