From 3a166085e656be5f957423e6e371b6647b313997 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Wed, 19 Aug 2020 11:11:53 +0100 Subject: Use `rdf(..)` instead of `create(..)` to create `Atom`s --- .../scala/rsacomb/RDFoxClassExprConverter.scala | 28 +++++++++---------- .../scala/rsacomb/RDFoxPropertyExprConverter.scala | 29 ++++++++++---------- src/main/scala/rsacomb/RDFoxUtil.scala | 9 ++++-- src/main/scala/rsacomb/RSA.scala | 8 +++++- src/main/scala/rsacomb/RSAOntology.scala | 28 +++++++++++-------- src/main/scala/rsacomb/SkolemStrategy.scala | 32 ++++++++++++---------- 6 files changed, 76 insertions(+), 58 deletions(-) (limited to 'src') diff --git a/src/main/scala/rsacomb/RDFoxClassExprConverter.scala b/src/main/scala/rsacomb/RDFoxClassExprConverter.scala index 9116be0..c1fd85a 100644 --- a/src/main/scala/rsacomb/RDFoxClassExprConverter.scala +++ b/src/main/scala/rsacomb/RDFoxClassExprConverter.scala @@ -22,7 +22,8 @@ import tech.oxfordsemantic.jrdfox.logic.{ Term, Variable, Literal, - Datatype + Datatype, + IRI } import rsacomb.SkolemStrategy @@ -56,10 +57,11 @@ class RDFoxClassExprConverter( unsafe: List[OWLObjectPropertyExpression] ) extends OWLClassExpressionVisitorEx[RDFoxRuleShards] { + import RDFoxUtil.owlapi2rdfox; + // OWLClass override def visit(expr: OWLClass): RDFoxRuleShards = { - val name = expr.getIRI.getIRIString - val atom = List(Atom.create(TupleTableName.create(name), term)) + val atom = List(Atom.rdf(term, IRI.RDF_TYPE, expr.getIRI())) RDFoxRuleShards(atom, List()) } @@ -84,9 +86,8 @@ class RDFoxClassExprConverter( .head // restricts to proper "nominals" .asOWLNamedIndividual .getIRI - .getIRIString val atom = List( - Atom.sameAs(term, Literal.create(ind, Datatype.IRI_REFERENCE)) + Atom.sameAs(term, ind) ) RDFoxRuleShards(atom, List()) } @@ -101,25 +102,22 @@ class RDFoxClassExprConverter( // technique it might involve the introduction of additional atoms, // and/or fresh constants and variables. val (head, body, term1) = skolem match { - case SkolemStrategy.None => (List(), List(), y) - case SkolemStrategy.Constant(c) => - (List(), List(), Literal.create(c, Datatype.IRI_REFERENCE)) + case SkolemStrategy.None => (List(), List(), y) + case SkolemStrategy.Constant(c) => (List(), List(), c) case SkolemStrategy.ConstantRSA(c) => { - val lit = Literal.create(c, Datatype.IRI_REFERENCE) if (unsafe.contains(prop)) ( List( - Atom.create(TupleTableName.create("internal:PE"), term, lit), - Atom.create(TupleTableName.create("internal:U"), lit) + Atom.rdf(term, IRI.create(RSA.PredicatePE), c), + Atom.rdf(c, IRI.RDF_TYPE, IRI.create(RSA.PredicateU)) ), List(), - lit + c ) else - (List(), List(), lit) + (List(), List(), c) } case SkolemStrategy.Standard(f) => - // At the time of writing the RDFox library does not have a // particular class for the "SKOLEM" operator and it is instead // a simple builtin function with a "special" name. ( @@ -153,7 +151,7 @@ class RDFoxClassExprConverter( .map(expr.getProperty.accept(_)) .flatten RDFoxRuleShards( - List(Atom.create(TupleTableName.create("owl:sameAs"), vars(0), vars(1))), + List(Atom.sameAs(vars(0), vars(1))), classResult.res ++ propertyResult ) } diff --git a/src/main/scala/rsacomb/RDFoxPropertyExprConverter.scala b/src/main/scala/rsacomb/RDFoxPropertyExprConverter.scala index 78ac98c..340fa90 100644 --- a/src/main/scala/rsacomb/RDFoxPropertyExprConverter.scala +++ b/src/main/scala/rsacomb/RDFoxPropertyExprConverter.scala @@ -3,29 +3,28 @@ package rsacomb import org.semanticweb.owlapi.model.{OWLPropertyExpression, OWLObjectProperty} import org.semanticweb.owlapi.model.OWLPropertyExpressionVisitorEx -import tech.oxfordsemantic.jrdfox.logic.{TupleTableName} -import tech.oxfordsemantic.jrdfox.logic.{Atom, Term, Variable, Literal} +import tech.oxfordsemantic.jrdfox.logic.{Atom, Term, IRI, Variable, Literal} import rsacomb.SkolemStrategy import org.semanticweb.owlapi.model.OWLObjectInverseOf -class RDFoxPropertyExprConverter(term1 : Term, term2 : Term, skolem : SkolemStrategy) - extends OWLPropertyExpressionVisitorEx[List[Atom]] -{ +class RDFoxPropertyExprConverter( + term1: Term, + term2: Term, + skolem: SkolemStrategy +) extends OWLPropertyExpressionVisitorEx[List[Atom]] { - override - def visit(expr : OWLObjectProperty) : List[Atom] = { - val name = expr.getIRI.getIRIString - List(Atom.create(TupleTableName.create(name), term1, term2)) - } + // Automatically converts OWLAPI types into RDFox equivalent types. + import RDFoxUtil.owlapi2rdfox; + + override def visit(expr: OWLObjectProperty): List[Atom] = + List(Atom.rdf(term1, expr.getIRI, term2)) - override - def visit(expr : OWLObjectInverseOf) : List[Atom] = { + override def visit(expr: OWLObjectInverseOf): List[Atom] = { val name = expr.getInverse.getNamedProperty.getIRI.getIRIString; - List(Atom.create(TupleTableName.create(name ++ "_inv"), term1, term2)) + List(Atom.rdf(term1, IRI.create(name ++ "_inv"), term2)) } - def doDefault(expr : OWLPropertyExpression) : List[Atom] = List() + def doDefault(expr: OWLPropertyExpression): List[Atom] = List() } // class RDFoxPropertyExprConverter - diff --git a/src/main/scala/rsacomb/RDFoxUtil.scala b/src/main/scala/rsacomb/RDFoxUtil.scala index 9be7a2d..4cefd83 100644 --- a/src/main/scala/rsacomb/RDFoxUtil.scala +++ b/src/main/scala/rsacomb/RDFoxUtil.scala @@ -3,14 +3,18 @@ package rsacomb /* Java imports */ import java.util.HashMap import tech.oxfordsemantic.jrdfox.Prefixes +import tech.oxfordsemantic.jrdfox.logic.IRI import tech.oxfordsemantic.jrdfox.client.{ ConnectionFactory, ServerConnection, DataStoreConnection } - object RDFoxUtil { + implicit def owlapi2rdfox(iri: org.semanticweb.owlapi.model.IRI): IRI = { + IRI.create(iri.getIRIString()) + } + def openConnection( dataStore: String ): (ServerConnection, DataStoreConnection) = { @@ -25,8 +29,9 @@ object RDFoxUtil { /* Create datastore connection */ val parameters = new HashMap[String, String]() + parameters.put("owl-in-rdf-support", "relaxed") //parameters.put("equality", "noUNA") - server.createDataStore(dataStore, "seq", parameters) + server.createDataStore(dataStore, "par-complex-nn", parameters) val data = server.newDataStoreConnection(dataStore) (server, data) diff --git a/src/main/scala/rsacomb/RSA.scala b/src/main/scala/rsacomb/RSA.scala index 229255c..1fa0fc1 100644 --- a/src/main/scala/rsacomb/RSA.scala +++ b/src/main/scala/rsacomb/RSA.scala @@ -8,8 +8,14 @@ import org.semanticweb.owlapi.model.OWLOntology object RSA extends RSAOntology { + val PrefixBase = "http://example.com/rsa_example.owl#" + val PrefixInternal = "http://127.0.0.1/" + val PredicatePE = PrefixInternal + "PE" + val PredicateU = PrefixInternal + "U" + val PredicateE = PrefixInternal + "E" + // TODO: move this somewhere else... maybe an OntoUtils class or something. - def loadOntology(onto: File ): OWLOntology = { + def loadOntology(onto: File): OWLOntology = { val manager = OWLManager.createOWLOntologyManager() manager.loadOntologyFromOntologyDocument(onto) } diff --git a/src/main/scala/rsacomb/RSAOntology.scala b/src/main/scala/rsacomb/RSAOntology.scala index c658227..681dbb0 100644 --- a/src/main/scala/rsacomb/RSAOntology.scala +++ b/src/main/scala/rsacomb/RSAOntology.scala @@ -12,6 +12,7 @@ import org.semanticweb.owlapi.reasoner.structural.StructuralReasonerFactory import tech.oxfordsemantic.jrdfox.Prefixes import tech.oxfordsemantic.jrdfox.logic.Variable import tech.oxfordsemantic.jrdfox.client.UpdateType +import tech.oxfordsemantic.jrdfox.logic.{Rule, Atom, Variable, IRI} /* Scala imports */ import scala.collection.JavaConverters._ @@ -69,8 +70,14 @@ trait RSAOntology { rule <- axiom.accept(visitor) } yield rule + /* DEBUG: print datalog rules */ + println("\nDatalog roles:") + datalog.foreach(println) + + // TODO: Define Prefixes in RSA object val prefixes = new Prefixes() - prefixes.declarePrefix(":", "http://example.com/rsa_example.owl#") + prefixes.declarePrefix(":", RSA.PrefixBase) + prefixes.declarePrefix("internal:", RSA.PrefixInternal) prefixes.declarePrefix( "rdf:", "http://www.w3.org/1999/02/22-rdf-syntax-ns#" @@ -99,30 +106,29 @@ trait RSAOntology { data.importData( UpdateType.ADDITION, prefixes, - "[?X,?Y] :- [?X,?Y], [?X], [?Y] ." + "[?X,?Y] :- [?X,?Y], [?X], [?Y] ." ) /* Add ontology rules */ - data.importData( - UpdateType.ADDITION, - prefixes, - datalog.foldLeft("")((str, rule) => - str ++ "\n" ++ rule.toString().replace("(", "[").replace(")", "]") - ) - ) + data.addRules(datalog.asJava) // Retrieve all instances of PE println("\nQueries:") RDFoxUtil.query( data, prefixes, - "SELECT ?X ?Y WHERE { ?X ?Y }" + "SELECT ?X ?Y WHERE { ?X internal:PE ?Y }" + ) + RDFoxUtil.query( + data, + prefixes, + "SELECT ?X ?Y WHERE { ?X internal:E ?Y }" ) RDFoxUtil.query( data, prefixes, - "SELECT ?X ?Y WHERE { ?X ?Y }" + "SELECT ?X WHERE { ?X rdf:type owl:Thing }" ) // Close connection to RDFox diff --git a/src/main/scala/rsacomb/SkolemStrategy.scala b/src/main/scala/rsacomb/SkolemStrategy.scala index bcf6828..9d45b4c 100644 --- a/src/main/scala/rsacomb/SkolemStrategy.scala +++ b/src/main/scala/rsacomb/SkolemStrategy.scala @@ -1,5 +1,7 @@ package rsacomb +import tech.oxfordsemantic.jrdfox.logic.IRI + sealed trait SkolemStrategy object SkolemStrategy { @@ -8,7 +10,7 @@ object SkolemStrategy { /* No skolemization at all. * - * From + * From * ∃R.A ⊑ B * to * R(x,y), B(y) -> B(x) @@ -17,46 +19,48 @@ object SkolemStrategy { /* Functional skolemization * - * From + * From * A ⊑ ∃R.B * to * A(y) -> R(x,f(x)), B(f(x)) * for f, fresh function associated with the input axiom */ - case class Standard(func : String) extends SkolemStrategy + case class Standard(func: IRI) extends SkolemStrategy object Standard { - def apply(axiom : String) = new Standard(genFunctionString(axiom)) - def genFunctionString(str : String) = "f_" ++ str.hashCode.toString + def apply(axiom: String) = + new Standard(IRI.create(genFunctionString(axiom))) + def genFunctionString(str: String) = "f_" ++ str.hashCode.toString } /* Constant skolemization * - * From + * From * A ⊑ ∃R.B * to * A(y) -> R(x,c), B(c) * for c, fresh constant associated with the input axiom */ - case class Constant(const : String) extends SkolemStrategy + case class Constant(const: IRI) extends SkolemStrategy object Constant { - def apply(axiom : String) = new Constant(genConstantString(axiom)) - def genConstantString(str : String) = "internal:c_" ++ str.hashCode.toString + def apply(axiom: String) = + new Constant(IRI.create(genConstantString(axiom))) + def genConstantString(str: String) = "c_" ++ str.hashCode.toString } /* (RSA) Constant skolemization * This is a special skolemization option to introduce additional atoms for RSA * checking algorithm. * - * From + * From * A ⊑ ∃R.B * to * A(y) -> R(x,c), PE(x,c), B(c) * for c, fresh constant associated with the input axiom and PE an internal predicate. */ - case class ConstantRSA(const : String) extends SkolemStrategy + case class ConstantRSA(const: IRI) extends SkolemStrategy object ConstantRSA { - def apply(axiom : String) = new ConstantRSA(genConstantString(axiom)) - def genConstantString(str : String) = "internal:c_" ++ str.hashCode.toString + def apply(axiom: String) = + new ConstantRSA(IRI.create(genConstantString(axiom))) + def genConstantString(str: String) = "c_" ++ str.hashCode.toString } } - -- cgit v1.2.3