From 360675d5b659ba8e9e5fe83dfa038d27f962b4de Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Thu, 5 Nov 2020 20:00:34 +0000 Subject: Update code to RDFox v4.0 --- src/main/scala/rsacomb/CanonicalModel.scala | 73 +++---- src/main/scala/rsacomb/FilteringProgram.scala | 214 +++++++++++++-------- src/main/scala/rsacomb/RDFTriple.scala | 33 ++-- src/main/scala/rsacomb/RDFoxAxiomConverter.scala | 12 +- .../scala/rsacomb/RDFoxClassExprConverter.scala | 25 ++- .../scala/rsacomb/RDFoxPropertyExprConverter.scala | 15 +- src/main/scala/rsacomb/RDFoxRuleShards.scala | 5 +- src/main/scala/rsacomb/RDFoxUtil.scala | 19 +- src/main/scala/rsacomb/RSA.scala | 10 +- src/main/scala/rsacomb/RSAOntology.scala | 11 +- src/main/scala/rsacomb/SkolemStrategy.scala | 2 +- 11 files changed, 247 insertions(+), 172 deletions(-) (limited to 'src/main/scala') diff --git a/src/main/scala/rsacomb/CanonicalModel.scala b/src/main/scala/rsacomb/CanonicalModel.scala index cc89a85..fcc404a 100644 --- a/src/main/scala/rsacomb/CanonicalModel.scala +++ b/src/main/scala/rsacomb/CanonicalModel.scala @@ -9,14 +9,14 @@ import org.semanticweb.owlapi.model.{ OWLObjectPropertyExpression, OWLObjectSomeValuesFrom } -import tech.oxfordsemantic.jrdfox.logic.{ - IRI, +import tech.oxfordsemantic.jrdfox.logic.Datatype +import tech.oxfordsemantic.jrdfox.logic.expression.{IRI, Term, Variable} +import tech.oxfordsemantic.jrdfox.logic.datalog.{ BodyFormula, - Atom, - Rule, - Term, - Variable + TupleTableAtom, + Rule } + import scala.collection.JavaConverters._ import rsacomb.RSA._ @@ -36,28 +36,35 @@ object ProgramGenerator { val varY = Variable.create("Y") List( Rule.create( - Atom.rdf(varX, IRI.create(pred), varY), - Atom.rdf(varX, IRI.create(pred ++ RSASuffix.Forward.getSuffix), varY) + TupleTableAtom.rdf(varX, IRI.create(pred), varY), + TupleTableAtom + .rdf(varX, IRI.create(pred ++ RSASuffix.Forward.getSuffix), varY) ), Rule.create( - Atom.rdf(varX, IRI.create(pred), varY), - Atom.rdf(varX, IRI.create(pred ++ RSASuffix.Backward.getSuffix), varY) + TupleTableAtom.rdf(varX, IRI.create(pred), varY), + TupleTableAtom + .rdf(varX, IRI.create(pred ++ RSASuffix.Backward.getSuffix), varY) ), Rule.create( - Atom.rdf( + TupleTableAtom.rdf( varY, IRI.create(pred ++ RSASuffix.Backward.getSuffix ++ "_inv"), varX ), - Atom.rdf(varX, IRI.create(pred ++ RSASuffix.Forward.getSuffix), varY) + TupleTableAtom + .rdf(varX, IRI.create(pred ++ RSASuffix.Forward.getSuffix), varY) ), Rule.create( - Atom.rdf( + TupleTableAtom.rdf( varY, IRI.create(pred ++ RSASuffix.Forward.getSuffix ++ "_inv"), varX ), - Atom.rdf(varX, IRI.create(pred ++ RSASuffix.Backward.getSuffix), varY) + TupleTableAtom.rdf( + varX, + IRI.create(pred ++ RSASuffix.Backward.getSuffix), + varY + ) ) ) } @@ -67,8 +74,8 @@ object ProgramGenerator { .toList } - def NIs(individuals: List[IRI]): List[Atom] = - individuals.map(Atom.rdf(_, IRI.RDF_TYPE, RSA.internal("NI"))) + def NIs(individuals: List[IRI]): List[TupleTableAtom] = + individuals.map(TupleTableAtom.rdf(_, IRI.RDF_TYPE, RSA.internal("NI"))) } @@ -91,18 +98,18 @@ class ProgramGenerator( val v0 = IRI.create("v0_" ++ axiom.hashCode.toString) val varX = Variable.create("X") // Predicates - val atomA: Atom = { + val atomA: TupleTableAtom = { val cls = axiom.getSubClass.asInstanceOf[OWLClass].getIRI - Atom.rdf(varX, IRI.RDF_TYPE, cls) + TupleTableAtom.rdf(varX, IRI.RDF_TYPE, cls) } - def notIn(t: Term): Atom = { - Atom.rdf( + def notIn(t: Term): TupleTableAtom = { + TupleTableAtom.rdf( t, RSA.internal("notIn"), RSA.internal(unfold.hashCode.toString) ) } - val roleRf: Atom = { + val roleRf: TupleTableAtom = { val visitor = new RDFoxPropertyExprConverter(varX, v0, RSASuffix.Forward) axiom.getSuperClass @@ -111,13 +118,13 @@ class ProgramGenerator( .accept(visitor) .head } - val atomB: Atom = { + val atomB: TupleTableAtom = { val cls = axiom.getSuperClass .asInstanceOf[OWLObjectSomeValuesFrom] .getFiller .asInstanceOf[OWLClass] .getIRI - Atom.rdf(v0, IRI.RDF_TYPE, cls) + TupleTableAtom.rdf(v0, IRI.RDF_TYPE, cls) } // TODO: To be consistent with the specifics of the visitor we are // returning facts as `Rule`s with true body. While this is correct @@ -142,21 +149,21 @@ class ProgramGenerator( val v1 = IRI.create("v1_" ++ axiom.hashCode.toString) val v2 = IRI.create("v2_" ++ axiom.hashCode.toString) // Predicates - def atomA(t: Term): Atom = { + def atomA(t: Term): TupleTableAtom = { val cls = axiom.getSubClass.asInstanceOf[OWLClass].getIRI - Atom.rdf(t, IRI.RDF_TYPE, cls) + TupleTableAtom.rdf(t, IRI.RDF_TYPE, cls) } - def roleRf(t1: Term, t2: Term): Atom = { + def roleRf(t1: Term, t2: Term): TupleTableAtom = { val visitor = new RDFoxPropertyExprConverter(t1, t2, RSASuffix.Forward) roleR.accept(visitor).head } - def atomB(t: Term): Atom = { + def atomB(t: Term): TupleTableAtom = { val cls = axiom.getSuperClass .asInstanceOf[OWLObjectSomeValuesFrom] .getFiller .asInstanceOf[OWLClass] .getIRI - Atom.rdf(t, IRI.RDF_TYPE, cls) + TupleTableAtom.rdf(t, IRI.RDF_TYPE, cls) } //Rules List( @@ -179,22 +186,22 @@ class ProgramGenerator( // Fresh Variables val v1 = IRI.create("v1_" ++ axiom.hashCode.toString) // Predicates - def atomA(t: Term): Atom = { + def atomA(t: Term): TupleTableAtom = { val cls = axiom.getSubClass.asInstanceOf[OWLClass].getIRI - Atom.rdf(t, IRI.RDF_TYPE, cls) + TupleTableAtom.rdf(t, IRI.RDF_TYPE, cls) } - def roleRf(t: Term): Atom = { + def roleRf(t: Term): TupleTableAtom = { val visitor = new RDFoxPropertyExprConverter(t, v1, RSASuffix.Forward) roleR.accept(visitor).head } - val atomB: Atom = { + val atomB: TupleTableAtom = { val cls = axiom.getSuperClass .asInstanceOf[OWLObjectSomeValuesFrom] .getFiller .asInstanceOf[OWLClass] .getIRI - Atom.rdf(v1, IRI.RDF_TYPE, cls) + TupleTableAtom.rdf(v1, IRI.RDF_TYPE, cls) } cycle.flatMap { x => List( diff --git a/src/main/scala/rsacomb/FilteringProgram.scala b/src/main/scala/rsacomb/FilteringProgram.scala index 9c19b7f..dc5e95e 100644 --- a/src/main/scala/rsacomb/FilteringProgram.scala +++ b/src/main/scala/rsacomb/FilteringProgram.scala @@ -1,93 +1,130 @@ package rsacomb -import tech.oxfordsemantic.jrdfox.logic._ +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, + 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._ -class FilteringProgram(query: Query, constants: List[Term]) extends RDFTriple { +class FilteringProgram(query: SelectQuery, constants: List[Term]) + extends RDFTriple { /* Makes mplicit conversion OWLAPI IRI <-> RDFox IRI available */ import RDFoxUtil._ - val answer: List[Term] = query.getAnswerVariables.asScala.toList + val answer: List[Term] = query.getSelection.asScala.map(_.getVariable).toList val bounded: List[Term] = this.getBoundedVariables - val facts: List[Atom] = constants.map(named) + val facts: List[TupleTableAtom] = constants.map(named) val rules: List[Rule] = this.generateFilteringProgram().map(reifyRule) - private def named(t: Term): Atom = - Atom.rdf(t, IRI.RDF_TYPE, RSA.internal("NAMED")) + private def named(t: Term): TupleTableAtom = + TupleTableAtom.rdf( + t, + IRI.RDF_TYPE, + RSA.internal("NAMED") + ) private def getBoundedVariables: List[Term] = { - def extract(f: Formula): Set[Term] = { - f match { - case b: BodyFormula => - b.getArguments() - .asScala - .filter(_.isInstanceOf[Variable]) - .filterNot(answer.contains(_)) - .toSet - case c: Conjunction => - c.getConjuncts().asScala.flatMap(extract).toSet - case d: Disjunction => - d.getDisjuncts().asScala.flatMap(extract).toSet - /* Ignoring Minus, Optional, Query, Values cases for now */ + def extract(body: GroupGraphPattern): Set[Term] = { + body match { + case b: ConjunctionPattern => { + b.getConjuncts.asScala.toSet.flatMap { conj: QueryPattern => + conj match { + case c: TriplePattern => + Set(c.getSubject, c.getPredicate, c.getObject).filter( + _.isInstanceOf[Variable] + ) + case _ => Set() + } + } + } case _ => Set() } } - extract(query.getQueryFormula()).toList + extract(query.getQueryBody.getWherePattern).toList } /* NOTE: we are restricting to queries that contain conjunctions of * atoms for the time being. This might need to be reviewed in the * future. */ - private def queryToBody(body: Formula): List[Atom] = + private def queryToBody(body: GroupGraphPattern): List[TupleTableAtom] = body match { - case a: Atom => List(a); - case a: Conjunction => - a.getConjuncts.asScala.toList.flatMap(queryToBody); + case b: ConjunctionPattern => { + val conjuncts = b.getConjuncts.asScala.toList + conjuncts flatMap { conj => + conj match { + case c: TriplePattern => + List( + TupleTableAtom.rdf(c.getSubject, c.getPredicate, c.getObject) + ) + case _ => List() + } + } + } case _ => List() } private def generateFilteringProgram(): List[Rule] = { // Query formula as a rule body - val body = queryToBody(query.getQueryFormula) + val body = queryToBody(query.getQueryBody.getWherePattern) // Auxiliar predicates/helpers - def not(atom: Atom): BodyFormula = Negation.create(atom) + def not(atom: TupleTableAtom): BodyFormula = Negation.create(atom) val predQM = - Atom.create( + TupleTableAtom.create( TupleTableName.create(RSA.internal("QM").getIRI), (answer ++ bounded): _* ) def predID(t1: Term, t2: Term) = - Atom.create( + TupleTableAtom.create( TupleTableName.create(RSA.internal("ID").getIRI), (answer ++ bounded).appended(t1).appended(t2): _* ) - def predNI(t1: Term): Atom = - Atom.rdf(t1, IRI.RDF_TYPE, RSA.internal("NI")) + def predNI(t1: Term): TupleTableAtom = + TupleTableAtom.rdf(t1, IRI.RDF_TYPE, RSA.internal("NI")) def predTQ(sx: String, t1: Term, t2: Term) = - Atom.create( + TupleTableAtom.create( TupleTableName.create(RSA.internal(s"TQ_$sx").getIRI), (answer ++ bounded).appended(t1).appended(t2): _* ) def predAQ(sx: String, t1: Term, t2: Term) = - Atom.create( + TupleTableAtom.create( TupleTableName.create(RSA.internal(s"AQ_$sx").getIRI), (answer ++ bounded).appended(t1).appended(t2): _* ) val predFK = - Atom.create( + TupleTableAtom.create( TupleTableName.create(RSA.internal("FK").getIRI), (answer ++ bounded): _* ) val predSP = - Atom.create( + TupleTableAtom.create( TupleTableName.create(RSA.internal("SP").getIRI), (answer ++ bounded): _* ) val predANS = - Atom.create( + TupleTableAtom.create( TupleTableName.create(RSA.internal("ANS").getIRI), answer: _* ) @@ -116,60 +153,78 @@ class FilteringProgram(query: Query, constants: List[Term]) extends RDFTriple { /* Rules 4x */ val r4a = for { role1 <- body.filter(_.isRoleAssertion) - if bounded contains (role1 getArgument 2) + if bounded contains (role1.getArguments.get(2)) role2 <- body.filter(_.isRoleAssertion) - if bounded contains (role2 getArgument 2) + if bounded contains (role2.getArguments.get(2)) } yield Rule.create( predFK, role1 suffix "_f", role2 suffix "_f", predID( - RSA.internal(bounded.indexOf(role1 getArgument 2)), - RSA.internal(bounded.indexOf(role2 getArgument 2)) + RSA.internal(bounded.indexOf(role1.getArguments.get(2))), + RSA.internal(bounded.indexOf(role2.getArguments.get(2))) ), - not(Atom.rdf(role1 getArgument 0, IRI.SAME_AS, role2 getArgument 0)) + not( + TupleTableAtom.rdf( + role1.getArguments.get(0), + IRI.SAME_AS, + role2.getArguments.get(0) + ) + ) ) val r4b = for { role1 <- body.filter(_.isRoleAssertion) - if bounded contains (role1 getArgument 2) + if bounded contains (role1.getArguments.get(2)) role2 <- body.filter(_.isRoleAssertion) - if bounded contains (role2 getArgument 0) + if bounded contains (role2.getArguments.get(0)) } yield Rule.create( predFK, role1 suffix "_f", role2 suffix "_b", predID( - RSA.internal(bounded.indexOf(role1 getArgument 2)), - RSA.internal(bounded.indexOf(role2 getArgument 0)) + RSA.internal(bounded.indexOf(role1.getArguments.get(2))), + RSA.internal(bounded.indexOf(role2.getArguments.get(0))) ), - not(Atom.rdf(role1 getArgument 0, IRI.SAME_AS, role2 getArgument 2)) + not( + TupleTableAtom.rdf( + role1.getArguments.get(0), + IRI.SAME_AS, + role2.getArguments.get(2) + ) + ) ) val r4c = for { role1 <- body.filter(_.isRoleAssertion) - if bounded contains (role1 getArgument 0) + if bounded contains (role1.getArguments.get(0)) role2 <- body.filter(_.isRoleAssertion) - if bounded contains (role2 getArgument 0) + if bounded contains (role2.getArguments.get(0)) } yield Rule.create( predFK, role1 suffix "_b", role2 suffix "_b", predID( - RSA.internal(bounded.indexOf(role1 getArgument 0)), - RSA.internal(bounded.indexOf(role2 getArgument 0)) + RSA.internal(bounded.indexOf(role1.getArguments.get(0))), + RSA.internal(bounded.indexOf(role2.getArguments.get(0))) ), - not(Atom.rdf(role1 getArgument 2, IRI.SAME_AS, role2 getArgument 2)) + not( + TupleTableAtom.rdf( + role1.getArguments.get(2), + IRI.SAME_AS, + role2.getArguments.get(2) + ) + ) ) /* Rules 5x */ val r5a = for { role1 <- body.filter(_.isRoleAssertion) - role1arg0 = role1 getArgument 0 - role1arg2 = role1 getArgument 2 + role1arg0 = role1.getArguments.get(0) + role1arg2 = role1.getArguments.get(2) if bounded contains role1arg0 if bounded contains role1arg2 role2 <- body.filter(_.isRoleAssertion) - role2arg0 = role2 getArgument 0 - role2arg2 = role2 getArgument 2 + role2arg0 = role2.getArguments.get(0) + role2arg2 = role2.getArguments.get(2) if bounded contains role2arg0 if bounded contains role2arg2 } yield Rule.create( @@ -183,18 +238,18 @@ class FilteringProgram(query: Query, constants: List[Term]) extends RDFTriple { RSA.internal(bounded indexOf role1arg2), RSA.internal(bounded indexOf role2arg2) ), - Atom.rdf(role1arg0, IRI.SAME_AS, role2arg0), + TupleTableAtom.rdf(role1arg0, IRI.SAME_AS, role2arg0), not(predNI(role1arg0)) ) val r5b = for { role1 <- body.filter(_.isRoleAssertion) - role1arg0 = role1 getArgument 0 - role1arg2 = role1 getArgument 2 + role1arg0 = role1.getArguments.get(0) + role1arg2 = role1.getArguments.get(2) if bounded contains role1arg0 if bounded contains role1arg2 role2 <- body.filter(_.isRoleAssertion) - role2arg0 = role2 getArgument 0 - role2arg2 = role2 getArgument 2 + role2arg0 = role2.getArguments.get(0) + role2arg2 = role2.getArguments.get(2) if bounded contains role2arg0 if bounded contains role2arg2 } yield Rule.create( @@ -208,18 +263,18 @@ class FilteringProgram(query: Query, constants: List[Term]) extends RDFTriple { RSA.internal(bounded indexOf role1arg2), RSA.internal(bounded indexOf role2arg0) ), - Atom.rdf(role1arg0, IRI.SAME_AS, role2arg2), + TupleTableAtom.rdf(role1arg0, IRI.SAME_AS, role2arg2), not(predNI(role1arg0)) ) val r5c = for { role1 <- body.filter(_.isRoleAssertion) - role1arg0 = role1 getArgument 0 - role1arg2 = role1 getArgument 2 + role1arg0 = role1.getArguments.get(0) + role1arg2 = role1.getArguments.get(2) if bounded contains role1arg0 if bounded contains role1arg2 role2 <- body.filter(_.isRoleAssertion) - role2arg0 = role2 getArgument 0 - role2arg2 = role2 getArgument 2 + role2arg0 = role2.getArguments.get(0) + role2arg2 = role2.getArguments.get(2) if bounded contains role2arg0 if bounded contains role2arg2 } yield Rule.create( @@ -233,15 +288,15 @@ class FilteringProgram(query: Query, constants: List[Term]) extends RDFTriple { RSA.internal(bounded indexOf role1arg0), RSA.internal(bounded indexOf role2arg0) ), - Atom.rdf(role1arg2, IRI.SAME_AS, role2arg2), + TupleTableAtom.rdf(role1arg2, IRI.SAME_AS, role2arg2), not(predNI(role1arg2)) ) /* Rules 6 */ val r6 = for { role <- body.filter(_.isRoleAssertion) - arg0 = role getArgument 0 - arg2 = role getArgument 2 + arg0 = role.getArguments.get(0) + arg2 = role.getArguments.get(2) if bounded contains arg0 if bounded contains arg2 sx <- List("_f", "_b") @@ -309,34 +364,37 @@ class FilteringProgram(query: Query, constants: List[Term]) extends RDFTriple { } private sealed trait Reified; - private case class ReifiedHead(bind: BindAtom, atoms: List[Atom]) + private case class ReifiedHead(bind: BindAtom, atoms: List[TupleTableAtom]) extends Reified - private case class ReifiedBody(atoms: List[Atom]) extends Reified + private case class ReifiedBody(atoms: List[TupleTableAtom]) extends Reified private case class Unaltered(formula: BodyFormula) extends Reified - private def getBindAtom(atom: Atom): BindAtom = { + private def getBindAtom(atom: TupleTableAtom): BindAtom = { val newvar = RSA.getFreshVariable() val name = - Literal.create(atom.getTupleTableName.getIRI, Datatype.XSD_STRING) + Literal.create(atom.getTupleTableName.toString(), Datatype.XSD_STRING) val args = atom .getArguments() .asScala .toSeq .prepended(name) /* Unclear requirement for SKOLEM func calls */ BindAtom.create( - BuiltinFunctionCall + FunctionCall .create("SKOLEM", args: _*), newvar ) } - private def reifyAtom(atom: Atom, variable: Variable): List[Atom] = { - def iri(i: Int) = atom.getTupleTableName().getIRI() ++ s"_$i" + private def reifyAtom( + atom: TupleTableAtom, + variable: Variable + ): List[TupleTableAtom] = { + def iri(i: Int) = atom.getTupleTableName.getName ++ s"_$i" atom .getArguments() .asScala .zipWithIndex - .map { case (t, i) => Atom.rdf(variable, iri(i), t) } + .map { case (t, i) => TupleTableAtom.rdf(variable, iri(i), t) } .toList } @@ -345,7 +403,7 @@ class FilteringProgram(query: Query, constants: List[Term]) extends RDFTriple { head: Boolean ): Reified = { formula match { - case a: Atom => { + case a: TupleTableAtom => { if (!a.isRdfTriple) { if (head) { val b = getBindAtom(a) @@ -380,7 +438,7 @@ class FilteringProgram(query: Query, constants: List[Term]) extends RDFTriple { case ReifiedHead(_, x) => x; case ReifiedBody(_) => List(); /* handle impossible case */ case Unaltered(x) => - List(x.asInstanceOf[Atom]) /* Can we do better that a cast? */ + List(x.asInstanceOf[TupleTableAtom]) /* Can we do better that a cast? */ } Rule.create(head.asJava, (skols ++ body).asJava) } @@ -388,6 +446,6 @@ class FilteringProgram(query: Query, constants: List[Term]) extends RDFTriple { } // class FilteringProgram object FilteringProgram { - def apply(query: Query, constants: List[Term]): FilteringProgram = + def apply(query: SelectQuery, constants: List[Term]): FilteringProgram = new FilteringProgram(query, constants) } // object FilteringProgram diff --git a/src/main/scala/rsacomb/RDFTriple.scala b/src/main/scala/rsacomb/RDFTriple.scala index 11ad6d4..9e593df 100644 --- a/src/main/scala/rsacomb/RDFTriple.scala +++ b/src/main/scala/rsacomb/RDFTriple.scala @@ -1,10 +1,11 @@ package rsacomb -import tech.oxfordsemantic.jrdfox.logic.{Atom, IRI, TupleTableName} +import tech.oxfordsemantic.jrdfox.logic.datalog.{TupleTableAtom, TupleTableName} +import tech.oxfordsemantic.jrdfox.logic.expression.{IRI} trait RDFTriple { - implicit class RDFTriple(atom: Atom) { + implicit class RDFTriple(atom: TupleTableAtom) { /* Is this the best way to determine if an atom is an RDF triple? * Note that we can't use `getNumberOfArguments()` because is not @@ -21,39 +22,39 @@ trait RDFTriple { * ``` */ def isRdfTriple: Boolean = - atom.getTupleTableName.getIRI.equals("internal:triple") + atom.getTupleTableName.getName.equals("internal:triple") def isClassAssertion: Boolean = - atom.isRdfTriple && atom.getArgument(1).equals(IRI.RDF_TYPE) + atom.isRdfTriple && atom.getArguments.get(1).equals(IRI.RDF_TYPE) def isRoleAssertion: Boolean = - atom.isRdfTriple && !atom.getArgument(1).equals(IRI.RDF_TYPE) + atom.isRdfTriple && !atom.getArguments.get(1).equals(IRI.RDF_TYPE) - def suffix(sx: String): Atom = + def suffix(sx: String): TupleTableAtom = if (this.isClassAssertion) { - val newclass = atom.getArgument(2) match { + val newclass = atom.getArguments.get(2) match { case iri: IRI => IRI.create(iri.getIRI.appendedAll(sx)) case other => other } - Atom.rdf( - atom getArgument 0, - atom getArgument 1, + TupleTableAtom.rdf( + atom.getArguments.get(0), + atom.getArguments.get(1), newclass ) } else if (this.isRoleAssertion) { - val newrole = atom.getArgument(1) match { + val newrole = atom.getArguments.get(1) match { case iri: IRI => IRI.create(iri.getIRI.appendedAll(sx)) case other => other } - Atom.rdf( - atom getArgument 0, + TupleTableAtom.rdf( + atom.getArguments.get(0), newrole, - atom getArgument 2 + atom.getArguments.get(2) ) } else { val newname = - TupleTableName.create(atom.getTupleTableName.getIRI.appendedAll(sx)) - Atom.create(newname, atom.getArguments()) + TupleTableName.create(atom.getTupleTableName.getName.appendedAll(sx)) + TupleTableAtom.create(newname, atom.getArguments()) } } diff --git a/src/main/scala/rsacomb/RDFoxAxiomConverter.scala b/src/main/scala/rsacomb/RDFoxAxiomConverter.scala index dab6765..2d04c56 100644 --- a/src/main/scala/rsacomb/RDFoxAxiomConverter.scala +++ b/src/main/scala/rsacomb/RDFoxAxiomConverter.scala @@ -8,9 +8,13 @@ import org.semanticweb.owlapi.model.{ } import org.semanticweb.owlapi.model.OWLAxiomVisitorEx -import tech.oxfordsemantic.jrdfox.logic.{Rule, BodyFormula} -import tech.oxfordsemantic.jrdfox.logic.{Atom, Term, Variable, Literal} -import tech.oxfordsemantic.jrdfox.logic.{TupleTableName} +import tech.oxfordsemantic.jrdfox.logic.datalog.{ + Rule, + BodyFormula, + TupleTableAtom, + TupleTableName +} +import tech.oxfordsemantic.jrdfox.logic.expression.{Term, Variable, Literal} import scala.collection.JavaConverters._ @@ -71,7 +75,7 @@ class RDFoxAxiomConverter( new RDFoxPropertyExprConverter(term, term1, suffix) val superVisitor = new RDFoxPropertyExprConverter(term, term1, suffix) val body: List[BodyFormula] = axiom.getSubProperty.accept(subVisitor) - val head: List[Atom] = axiom.getSuperProperty.accept(superVisitor) + val head: List[TupleTableAtom] = axiom.getSuperProperty.accept(superVisitor) List(Rule.create(head.asJava, body.asJava)) } diff --git a/src/main/scala/rsacomb/RDFoxClassExprConverter.scala b/src/main/scala/rsacomb/RDFoxClassExprConverter.scala index f07e3c6..de0f623 100644 --- a/src/main/scala/rsacomb/RDFoxClassExprConverter.scala +++ b/src/main/scala/rsacomb/RDFoxClassExprConverter.scala @@ -12,17 +12,16 @@ import org.semanticweb.owlapi.model.{ OWLObjectMaxCardinality } import org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx -import tech.oxfordsemantic.jrdfox.logic.{ +import tech.oxfordsemantic.jrdfox.logic.Datatype +import tech.oxfordsemantic.jrdfox.logic.datalog.{ BindAtom, - BuiltinFunctionCall, - TupleTableName + TupleTableName, + TupleTableAtom } -import tech.oxfordsemantic.jrdfox.logic.{ - Atom, +import tech.oxfordsemantic.jrdfox.logic.expression.{ Term, Variable, - Literal, - Datatype, + FunctionCall, IRI } @@ -64,7 +63,7 @@ class RDFoxClassExprConverter( // OWLClass override def visit(expr: OWLClass): RDFoxRuleShards = { val iri: IRI = if (expr.isTopEntity()) IRI.THING else expr.getIRI() - val atom = List(Atom.rdf(term, IRI.RDF_TYPE, iri)) + val atom = List(TupleTableAtom.rdf(term, IRI.RDF_TYPE, iri)) RDFoxRuleShards(atom, List()) } @@ -90,7 +89,7 @@ class RDFoxClassExprConverter( .asOWLNamedIndividual .getIRI val atom = List( - Atom.sameAs(term, ind) + TupleTableAtom.rdf(term, IRI.SAME_AS, ind) ) RDFoxRuleShards(atom, List()) } @@ -110,8 +109,8 @@ class RDFoxClassExprConverter( if (unsafe.contains(prop)) ( List( - Atom.rdf(term, RSA.internal("PE"), c), - Atom.rdf(c, IRI.RDF_TYPE, RSA.internal("U")) + TupleTableAtom.rdf(term, RSA.internal("PE"), c), + TupleTableAtom.rdf(c, IRI.RDF_TYPE, RSA.internal("U")) ), List(), c @@ -124,7 +123,7 @@ class RDFoxClassExprConverter( // a simple builtin function with a "special" name. ( List(), - List(BindAtom.create(BuiltinFunctionCall.create("SKOLEM", term), y)), + List(BindAtom.create(FunctionCall.create("SKOLEM", term), y)), y ) } @@ -154,7 +153,7 @@ class RDFoxClassExprConverter( .map(expr.getProperty.accept(_)) .flatten RDFoxRuleShards( - List(Atom.sameAs(vars(0), vars(1))), + List(TupleTableAtom.rdf(vars(0), IRI.SAME_AS, vars(1))), classResult.res ++ propertyResult ) } diff --git a/src/main/scala/rsacomb/RDFoxPropertyExprConverter.scala b/src/main/scala/rsacomb/RDFoxPropertyExprConverter.scala index ba151d0..11d6d86 100644 --- a/src/main/scala/rsacomb/RDFoxPropertyExprConverter.scala +++ b/src/main/scala/rsacomb/RDFoxPropertyExprConverter.scala @@ -3,7 +3,8 @@ package rsacomb import org.semanticweb.owlapi.model.{OWLPropertyExpression, OWLObjectProperty} import org.semanticweb.owlapi.model.OWLPropertyExpressionVisitorEx -import tech.oxfordsemantic.jrdfox.logic.{Atom, Term, IRI, Literal} +import tech.oxfordsemantic.jrdfox.logic.datalog.TupleTableAtom +import tech.oxfordsemantic.jrdfox.logic.expression.{Term, IRI, Literal} import org.semanticweb.owlapi.model.OWLObjectInverseOf @@ -11,23 +12,23 @@ class RDFoxPropertyExprConverter( term1: Term, term2: Term, suffix: RSASuffix -) extends OWLPropertyExpressionVisitorEx[List[Atom]] { +) extends OWLPropertyExpressionVisitorEx[List[TupleTableAtom]] { // Automatically converts OWLAPI types into RDFox equivalent types. import RDFoxUtil.owlapi2rdfox; - override def visit(expr: OWLObjectProperty): List[Atom] = { + override def visit(expr: OWLObjectProperty): List[TupleTableAtom] = { val pred = IRI.create(expr.getIRI.getIRIString ++ suffix.getSuffix) - List(Atom.rdf(term1, pred, term2)) + List(TupleTableAtom.rdf(term1, pred, term2)) } - override def visit(expr: OWLObjectInverseOf): List[Atom] = { + override def visit(expr: OWLObjectInverseOf): List[TupleTableAtom] = { val pred = IRI.create( expr.getInverse.getNamedProperty.getIRI.getIRIString ++ suffix.getSuffix ++ "_inv" ) - List(Atom.rdf(term1, pred, term2)) + List(TupleTableAtom.rdf(term1, pred, term2)) } - def doDefault(expr: OWLPropertyExpression): List[Atom] = List() + def doDefault(expr: OWLPropertyExpression): List[TupleTableAtom] = List() } // class RDFoxPropertyExprConverter diff --git a/src/main/scala/rsacomb/RDFoxRuleShards.scala b/src/main/scala/rsacomb/RDFoxRuleShards.scala index 8f72983..b61cc38 100644 --- a/src/main/scala/rsacomb/RDFoxRuleShards.scala +++ b/src/main/scala/rsacomb/RDFoxRuleShards.scala @@ -1,6 +1,5 @@ package rsacomb -import tech.oxfordsemantic.jrdfox.logic.{Atom, BodyFormula} - -case class RDFoxRuleShards(res : List[Atom], ext : List[BodyFormula]) +import tech.oxfordsemantic.jrdfox.logic.datalog.{TupleTableAtom, BodyFormula} +case class RDFoxRuleShards(res: List[TupleTableAtom], ext: List[BodyFormula]) diff --git a/src/main/scala/rsacomb/RDFoxUtil.scala b/src/main/scala/rsacomb/RDFoxUtil.scala index 4e0a118..748c72f 100644 --- a/src/main/scala/rsacomb/RDFoxUtil.scala +++ b/src/main/scala/rsacomb/RDFoxUtil.scala @@ -2,9 +2,9 @@ package rsacomb /* Java imports */ import java.util.HashMap -import java.io.ByteArrayInputStream +import java.io.StringReader import tech.oxfordsemantic.jrdfox.Prefixes -import tech.oxfordsemantic.jrdfox.logic.{IRI, Query} +import tech.oxfordsemantic.jrdfox.logic.sparql.statement.{Query, SelectQuery} import tech.oxfordsemantic.jrdfox.client.{ ConnectionFactory, ServerConnection, @@ -12,7 +12,7 @@ import tech.oxfordsemantic.jrdfox.client.{ } import tech.oxfordsemantic.jrdfox.formats.SPARQLParser -import tech.oxfordsemantic.jrdfox.logic.{IRI => RDFox_IRI} +import tech.oxfordsemantic.jrdfox.logic.expression.{IRI => RDFox_IRI} import org.semanticweb.owlapi.model.{IRI => OWL_IRI} object RDFoxUtil { @@ -51,12 +51,19 @@ object RDFoxUtil { (server, data) } - def parseQuery(query: String, prefixes: Prefixes = RSA.Prefixes): Query = { + def parseQuery( + query: String, + prefixes: Prefixes = RSA.Prefixes + ): Option[SelectQuery] = { val parser = new SPARQLParser( prefixes, - new ByteArrayInputStream(query.getBytes()) + new StringReader(query) ) - parser.parseSingleQuery() + // NOTE: return only conjunctive queries for now (SelectQuery) + parser.parseSingleQuery() match { + case q: SelectQuery => Some(q) + case _ => None + } } def submitQuery( diff --git a/src/main/scala/rsacomb/RSA.scala b/src/main/scala/rsacomb/RSA.scala index 01982fc..a6f4b88 100644 --- a/src/main/scala/rsacomb/RSA.scala +++ b/src/main/scala/rsacomb/RSA.scala @@ -6,20 +6,12 @@ import java.util.Map import tech.oxfordsemantic.jrdfox.formats.SPARQLParser import tech.oxfordsemantic.jrdfox.Prefixes -import tech.oxfordsemantic.jrdfox.logic.IRI +import tech.oxfordsemantic.jrdfox.logic.expression.{Variable, IRI} import org.semanticweb.owlapi.apibinding.OWLManager import org.semanticweb.owlapi.model.OWLOntology import rsacomb.RSAOntology // Debug only -import tech.oxfordsemantic.jrdfox.logic.{ - Formula, - Atom, - Variable, - Query, - QueryType, - Conjunction -} import scala.collection.JavaConverters._ object RSA extends RSAOntology { diff --git a/src/main/scala/rsacomb/RSAOntology.scala b/src/main/scala/rsacomb/RSAOntology.scala index b81a8f5..ef1885b 100644 --- a/src/main/scala/rsacomb/RSAOntology.scala +++ b/src/main/scala/rsacomb/RSAOntology.scala @@ -17,7 +17,14 @@ import org.semanticweb.owlapi.model.{IRI => OWLIRI} import uk.ac.manchester.cs.owl.owlapi.OWLObjectPropertyImpl import tech.oxfordsemantic.jrdfox.client.{UpdateType, DataStoreConnection} -import tech.oxfordsemantic.jrdfox.logic.{Resource, Rule, Atom, Variable, IRI} +import tech.oxfordsemantic.jrdfox.logic.datalog.{Rule, TupleTableAtom} +import tech.oxfordsemantic.jrdfox.logic.expression.{ + Term, + Variable, + IRI, + Resource +} +import tech.oxfordsemantic.jrdfox.logic.sparql.statement.SelectQuery /* Scala imports */ import scala.collection.JavaConverters._ @@ -249,7 +256,7 @@ trait RSAOntology { Graph(edges: _*) } - def filteringProgram(query: Query): List[Rule] = + def filteringProgram(query: SelectQuery): List[Rule] = FilteringProgram(query, individuals).rules // TODO: the following functions needs testing diff --git a/src/main/scala/rsacomb/SkolemStrategy.scala b/src/main/scala/rsacomb/SkolemStrategy.scala index 9d45b4c..b05cd17 100644 --- a/src/main/scala/rsacomb/SkolemStrategy.scala +++ b/src/main/scala/rsacomb/SkolemStrategy.scala @@ -1,6 +1,6 @@ package rsacomb -import tech.oxfordsemantic.jrdfox.logic.IRI +import tech.oxfordsemantic.jrdfox.logic.expression.IRI sealed trait SkolemStrategy -- cgit v1.2.3