From 3408515a868ca65ab907e21160f75c858ead8d46 Mon Sep 17 00:00:00 2001 From: Federico Igne Date: Thu, 6 Aug 2020 12:04:38 +0100 Subject: Refactor code into different files This has been done to better accommodate the code to detect all unsafe roles in an ontology. --- src/main/scala/rsacomb/Main.scala | 84 ++++++++++++++++ src/main/scala/rsacomb/RSA.scala | 17 ++++ src/main/scala/rsacomb/RSAAxiom.scala | 86 ++++++++++++++++ src/main/scala/rsacomb/RSAComb.scala | 167 ------------------------------- src/main/scala/rsacomb/RSAOntology.scala | 148 +++++++++++++++++++++++++++ 5 files changed, 335 insertions(+), 167 deletions(-) create mode 100644 src/main/scala/rsacomb/Main.scala create mode 100644 src/main/scala/rsacomb/RSA.scala create mode 100644 src/main/scala/rsacomb/RSAAxiom.scala delete mode 100644 src/main/scala/rsacomb/RSAComb.scala create mode 100644 src/main/scala/rsacomb/RSAOntology.scala (limited to 'src') diff --git a/src/main/scala/rsacomb/Main.scala b/src/main/scala/rsacomb/Main.scala new file mode 100644 index 0000000..9cd6680 --- /dev/null +++ b/src/main/scala/rsacomb/Main.scala @@ -0,0 +1,84 @@ +package rsacomb + +/* Java imports */ +import java.io.File + +/* Local imports */ +import rsacomb.RSA._ + +object RSAComb { + + val help: String = """ + rsacomb - combined approach for CQ answering for RSA ontologies. + + USAGE + rsacomb + + where + the ontology is expected to be an OWL file and the (single) + query a SPARQL query file. + """ + + def main(args: Array[String]): Unit = { + + /* Simple arguments handling + * + * TODO: use something better later on + */ + + if (args.length < 2) { + println(help) + return () + } + + val ontoPath = new File(args(0)) + val queryPath = new File(args(1)) + + if (!ontoPath.isFile || !queryPath.isFile) { + println("The provided arguments are not regular files.\n\n") + println(help) + return () + } + + /* Create RSA object from generic OWLOntology + * + * TODO: It might be required to check if the ontology in input is + * Horn-ALCHOIQ. At the moment we are assuming this is always the + * case. + */ + + val ontology = RSA.loadOntology(ontoPath) + ontology.isRSA + + /* Build canonical model */ + //val tboxCanon = rsa.canonicalModel() + + /* Load query */ + //val query = ... + + /* Compute the filtering program from the given query */ + //val tboxFilter = rsa.filteringProgram(query) + + /* ... */ + + /* DEBUG ONLY */ + println("Ok!") + } +} + +/* Notes: + * + * To establish a connection with a local RDFox instance, do the + * following: + * + * ``` + * val serverConnection : ServerConnection = ConnectionFactory.newServerConnection("rdfox:local", "", "") + * serverConnection.createDataStore("test","seq",new HashMap()) + * val dataStoreConnection : DataStoreConnection = serverConnection.newDataStoreConnection("test") + * dataStoreConnection.importData( + * UpdateType.ADDITION, + * Prefixes.s_emptyPrefixes, + * new File("./path/to/file") + * ) + * ``` + */ diff --git a/src/main/scala/rsacomb/RSA.scala b/src/main/scala/rsacomb/RSA.scala new file mode 100644 index 0000000..229255c --- /dev/null +++ b/src/main/scala/rsacomb/RSA.scala @@ -0,0 +1,17 @@ +package rsacomb + +/* Java imports */ +import java.io.File + +import org.semanticweb.owlapi.apibinding.OWLManager +import org.semanticweb.owlapi.model.OWLOntology + +object RSA extends RSAOntology { + + // TODO: move this somewhere else... maybe an OntoUtils class or something. + def loadOntology(onto: File ): OWLOntology = { + val manager = OWLManager.createOWLOntologyManager() + manager.loadOntologyFromOntologyDocument(onto) + } + +} // object RSA diff --git a/src/main/scala/rsacomb/RSAAxiom.scala b/src/main/scala/rsacomb/RSAAxiom.scala new file mode 100644 index 0000000..032b7f9 --- /dev/null +++ b/src/main/scala/rsacomb/RSAAxiom.scala @@ -0,0 +1,86 @@ +package rsacomb + +/* Java imports */ +// import java.io.File +// import java.util.stream.{Collectors,Stream} + +// import org.semanticweb.owlapi.apibinding.OWLManager +// import org.semanticweb.owlapi.model.{OWLOntologyManager,OWLOntology} +// import org.semanticweb.owlapi.model.{OWLAxiom,OWLObjectPropertyExpression} +import org.semanticweb.owlapi.model.{OWLAxiom,OWLSubClassOfAxiom, OWLEquivalentClassesAxiom} +import org.semanticweb.owlapi.model.OWLAxiomVisitorEx +// import org.semanticweb.owlapi.model.parameters.Imports +// import org.semanticweb.owlapi.reasoner.OWLReasoner +// import org.semanticweb.owlapi.reasoner.structural.StructuralReasonerFactory + +// import tech.oxfordsemantic.jrdfox.logic.Variable + +/* Scala imports */ +// import scala.collection.JavaConverters._ + +/* Local imports */ +// import rsacomb.RSAAxiom + +/* Debug only */ +// import org.semanticweb.owlapi.dlsyntax.renderer.DLSyntaxObjectRenderer + +// import java.util.HashMap +// import java.util.stream.{Stream,Collectors} + +// import org.semanticweb.owlapi.model.{AxiomType, ClassExpressionType, OWLObjectSomeValuesFrom} +// import org.semanticweb.owlapi.model.OWLClassExpression +// import org.semanticweb.owlapi.model.IRI +// import org.semanticweb.owlapi.reasoner.{OWLReasonerFactory, OWLReasoner} +// import uk.ac.manchester.cs.owl.owlapi.OWLObjectPropertyImpl + +// import tech.oxfordsemantic.jrdfox.Prefixes +// import tech.oxfordsemantic.jrdfox.client.{ConnectionFactory, ServerConnection, DataStoreConnection} +// import tech.oxfordsemantic.jrdfox.client.UpdateType +// import tech.oxfordsemantic.jrdfox.logic.{Rule, Atom, Literal, Term, Variable} +// import tech.oxfordsemantic.jrdfox.logic.{BuiltinFunctionCall, TupleTableName} +// import tech.oxfordsemantic.jrdfox.logic.{LogicFormat} + + +// import rsacomb.SkolemStrategy +//import org.semanticweb.owlapi.model.{OWLAxiom,OWLObjectPropertyExpression} + +trait RSAAxiom { + + sealed trait RSAAxiomType + object RSAAxiomType { + case object T3 extends RSAAxiomType + case object T4 extends RSAAxiomType + case object T5 extends RSAAxiomType + } + + implicit class RSAAxiom(axiom: OWLAxiom) { + + private class RSAAxiomTypeDetector(t: RSAAxiomType) + extends OWLAxiomVisitorEx[Boolean] + { + + override + def visit(axiom: OWLSubClassOfAxiom): Boolean = { + true + } + + override + def visit(axiom: OWLEquivalentClassesAxiom): Boolean = { + true + } + + def doDefault(axiom : OWLAxiom): Boolean = false + + } + + private def isOfType(t: RSAAxiomType): Boolean = { + val visitor = new RSAAxiomTypeDetector(t) + axiom.accept(visitor) + } + + def isT3: Boolean = isOfType(RSAAxiomType.T3) + def isT4: Boolean = isOfType(RSAAxiomType.T4) + def isT5: Boolean = isOfType(RSAAxiomType.T5) + } + +} diff --git a/src/main/scala/rsacomb/RSAComb.scala b/src/main/scala/rsacomb/RSAComb.scala deleted file mode 100644 index 62414e9..0000000 --- a/src/main/scala/rsacomb/RSAComb.scala +++ /dev/null @@ -1,167 +0,0 @@ -package rsacomb - -import java.io.File -import java.util.HashMap -import java.util.stream.{Stream,Collectors} - -import org.semanticweb.owlapi.apibinding.OWLManager -import org.semanticweb.owlapi.model.{AxiomType, ClassExpressionType, OWLObjectSomeValuesFrom} -import org.semanticweb.owlapi.model.{OWLAxiom, OWLSubClassOfAxiom, OWLEquivalentClassesAxiom} -import org.semanticweb.owlapi.model.OWLClassExpression -import org.semanticweb.owlapi.model.OWLOntology -import org.semanticweb.owlapi.model.OWLOntologyManager -import org.semanticweb.owlapi.model.IRI -import org.semanticweb.owlapi.model.parameters.Imports -import uk.ac.manchester.cs.owl.owlapi.OWLObjectPropertyImpl - -import tech.oxfordsemantic.jrdfox.Prefixes -import tech.oxfordsemantic.jrdfox.client.{ConnectionFactory, ServerConnection, DataStoreConnection} -import tech.oxfordsemantic.jrdfox.client.UpdateType -import tech.oxfordsemantic.jrdfox.logic.{Rule, Atom, Literal, Term, Variable} -import tech.oxfordsemantic.jrdfox.logic.{BuiltinFunctionCall, TupleTableName} -import tech.oxfordsemantic.jrdfox.logic.{LogicFormat} - -import scala.collection.JavaConverters._ - -import rsacomb.SkolemStrategy -import org.semanticweb.owlapi.dlsyntax.renderer.DLSyntaxObjectRenderer - -class RSA(ontology : OWLOntology) { - - /* Alternative constructor(s) */ - def this(file : File) = this(RSA.loadOntology(file)) - - def getOntology : OWLOntology = ontology - -} // class RSA - -object RSA { - - def loadOntology( onto : File ) : OWLOntology = { - /* Retrieve ontology manager */ - val manager : OWLOntologyManager = OWLManager.createOWLOntologyManager() - /* Retrieve ontology */ - manager.loadOntologyFromOntologyDocument(onto) - } - - def isRSA( onto : OWLOntology ) : Boolean = { - /* TODO: Steps for RSA check - * 1) convert ontology axioms into LP rules - * 2) call RDFox on the onto and compute materialization - * 3) build graph from E(x,y) facts - * 4) check if the graph is tree-like - * ideally this annotates the graph with info about the reasons - * why the ontology might not be RSA. This could help a second - * step of approximation of an Horn-ALCHOIQ to RSA - */ - - val renderer = new DLSyntaxObjectRenderer() - - // Here we need to compute the unsafe roles. This is hardcoded for now. - val unsafe = List( - new OWLObjectPropertyImpl(IRI.create("http://example.com/rsa_example.owl#S")).getInverseProperty() - ) - - /* Print TBox axioms */ - println("TBox/RBox:") - for { - axiom <- onto.tboxAxioms(Imports.EXCLUDED).collect(Collectors.toList()).asScala - } yield println(renderer.render(axiom)) - for { - axiom <- onto.rboxAxioms(Imports.EXCLUDED).collect(Collectors.toList()).asScala - } yield println(renderer.render(axiom)) - - /* Ontology axiom convertion into LP rules */ - println("Logic rules:") - for { - axiom <- onto.tboxAxioms(Imports.EXCLUDED).collect(Collectors.toList()).asScala - visitor = new RDFoxAxiomConverter(Variable.create("x"), SkolemStrategy.ConstantRSA(axiom.toString), unsafe) - rule <- axiom.accept(visitor) - } yield println(rule) - for { - axiom <- onto.rboxAxioms(Imports.EXCLUDED).collect(Collectors.toList()).asScala - visitor = new RDFoxAxiomConverter(Variable.create("x"), SkolemStrategy.ConstantRSA(axiom.toString), unsafe) - rule <- axiom.accept(visitor) - } yield println(rule) - - /* Return true for now... */ - true - } - -} // object RSA - -object RSAComb { - - val help : String = """ - rsacomb - combined approach for CQ answering for RSA ontologies. - - USAGE - rsacomb - - where - the ontology is expected to be an OWL file and the (single) - query a SPARQL query file. - """ - - def main( args : Array[String] ) : Unit = { - - /* Simple arguments handling - * - * TODO: use something better later on - */ - - if (args.length < 2) { - println(help) - return () - } - - val ontologyPath = new File(args(0)) - val queryPath = new File(args(1)) - - if (!ontologyPath.isFile || !queryPath.isFile) { - println("The provided arguments are not regular files.\n\n") - println(help) - return () - } - - /* Create RSA object from generic OWLOntology - * - * TODO: It might be required to check if the ontology in input is - * Horn-ALCHOIQ. At the moment we are assuming this is always the - * case. - */ - val rsa = new RSA(ontologyPath) - RSA.isRSA(rsa.getOntology) - - /* Build canonical model */ - //val tboxCanon = rsa.canonicalModel() - - /* Load query */ - //val query = ... - - /* Compute the filtering program from the given query */ - //val tboxFilter = rsa.filteringProgram(query) - - /* ... */ - - /* DEBUG ONLY */ - println("Ok!") - } -} - -/* Notes: - * - * To establish a connection with a local RDFox instance, do the - * following: - * - * ``` - * val serverConnection : ServerConnection = ConnectionFactory.newServerConnection("rdfox:local", "", "") - * serverConnection.createDataStore("test","seq",new HashMap()) - * val dataStoreConnection : DataStoreConnection = serverConnection.newDataStoreConnection("test") - * dataStoreConnection.importData( - * UpdateType.ADDITION, - * Prefixes.s_emptyPrefixes, - * new File("./path/to/file") - * ) - * ``` - */ diff --git a/src/main/scala/rsacomb/RSAOntology.scala b/src/main/scala/rsacomb/RSAOntology.scala new file mode 100644 index 0000000..7fc8781 --- /dev/null +++ b/src/main/scala/rsacomb/RSAOntology.scala @@ -0,0 +1,148 @@ +package rsacomb + +/* Java imports */ +// import java.io.File +import java.util.stream.{Collectors,Stream} + +import org.semanticweb.owlapi.model.OWLOntology +import org.semanticweb.owlapi.model.OWLObjectPropertyExpression +// import org.semanticweb.owlapi.model.OWLAxiomVisitorEx +import org.semanticweb.owlapi.model.parameters.Imports +// import org.semanticweb.owlapi.reasoner.OWLReasoner +import org.semanticweb.owlapi.reasoner.structural.StructuralReasonerFactory + +import tech.oxfordsemantic.jrdfox.logic.Variable + +/* Scala imports */ +import scala.collection.JavaConverters._ + +/* Local imports */ +// import rsacomb.RSAAxiom + +/* Debug only */ +import org.semanticweb.owlapi.dlsyntax.renderer.DLSyntaxObjectRenderer + +// import java.util.HashMap +// import java.util.stream.{Stream,Collectors} + +// import org.semanticweb.owlapi.model.{AxiomType, ClassExpressionType, OWLObjectSomeValuesFrom} +// import org.semanticweb.owlapi.model.{OWLSubClassOfAxiom, OWLEquivalentClassesAxiom} +// import org.semanticweb.owlapi.model.OWLClassExpression +// import org.semanticweb.owlapi.model.IRI +// import org.semanticweb.owlapi.reasoner.{OWLReasonerFactory, OWLReasoner} +// import uk.ac.manchester.cs.owl.owlapi.OWLObjectPropertyImpl + +// import tech.oxfordsemantic.jrdfox.Prefixes +// import tech.oxfordsemantic.jrdfox.client.{ConnectionFactory, ServerConnection, DataStoreConnection} +// import tech.oxfordsemantic.jrdfox.client.UpdateType +// import tech.oxfordsemantic.jrdfox.logic.{Rule, Atom, Literal, Term, Variable} +// import tech.oxfordsemantic.jrdfox.logic.{BuiltinFunctionCall, TupleTableName} +// import tech.oxfordsemantic.jrdfox.logic.{LogicFormat} +// import rsacomb.SkolemStrategy + +trait RSAOntology { + + implicit class RSAOntology(ontology: OWLOntology) extends RSAAxiom { + + def isRSA: Boolean = { + + /* TODO: Steps for RSA check + * 1) convert ontology axioms into LP rules + * 2) call RDFox on the onto and compute materialization + * 3) build graph from E(x,y) facts + * 4) check if the graph is tree-like + * ideally this annotates the graph with info about the reasons + * why the ontology might not be RSA. This could help a second + * step of approximation of an Horn-ALCHOIQ to RSA + */ + + val tbox = + Stream.concat(ontology.tboxAxioms(Imports.INCLUDED), ontology.rboxAxioms(Imports.INCLUDED)) + .collect(Collectors.toList()).asScala + val unsafe = ontology.getUnsafeRoles + + /* DEBUG: print rules in DL syntax */ + val renderer = new DLSyntaxObjectRenderer() + println("\nDL rules:") + tbox.foreach(x => println(renderer.render(x))) + + /* Ontology convertion into LP rules */ + println("\nLP rules:") + for { + axiom <- tbox + visitor = new RDFoxAxiomConverter(Variable.create("x"), SkolemStrategy.ConstantRSA(axiom.toString), unsafe) + rule <- axiom.accept(visitor) + } yield println(rule) + + /* DEBUG */ + true + } + + def getUnsafeRoles: List[OWLObjectPropertyExpression] = { + + val factory = new StructuralReasonerFactory() + val reasoner = factory.createReasoner(ontology) + val tbox = ontology.tboxAxioms(Imports.INCLUDED).collect(Collectors.toList()).asScala + val rbox = ontology.rboxAxioms(Imports.INCLUDED).collect(Collectors.toList()).asScala + + /* DEBUG: print rules in DL syntax */ + val renderer = new DLSyntaxObjectRenderer() + println("\nT5 axioms:") + for { + axiom <- tbox + if axiom.isT5 + } yield println(renderer.render(axiom)) + + // def isT3(axiom : OWLAxiom) : Boolean = { + // println(axiom) + // axiom.getAxiomType match { + // case AxiomType.SUBCLASS_OF => + // val axiom1 = axiom.asInstanceOf[OWLSubClassOfAxiom] + // axiom1.getSubClass().getClassExpressionType() == ClassExpressionType.OBJECT_SOME_VALUES_FROM && + // axiom1.getSuperClass().getClassExpressionType() == ClassExpressionType.OWL_CLASS + // case AxiomType.EQUIVALENT_CLASSES => false // TODO + // } + // } + + // def isT4(axiom : OWLAxiom) : Boolean = { + // axiom.getAxiomType match { + // case AxiomType.SUBCLASS_OF => + // val axiom1 = axiom.asInstanceOf[OWLSubClassOfAxiom] + // axiom1.getSubClass().getClassExpressionType() == ClassExpressionType.OWL_CLASS && + // axiom1.getSuperClass().getClassExpressionType() == ClassExpressionType.OBJECT_MAX_CARDINALITY + // case AxiomType.EQUIVALENT_CLASSES => false // TODO + // } + // } + + // def isT5(axiom : OWLAxiom) : Boolean = { + // axiom.getAxiomType match { + // case AxiomType.SUBCLASS_OF => { + // val axiom1 = axiom.asInstanceOf[OWLSubClassOfAxiom] + // axiom1.getSubClass().getClassExpressionType() == ClassExpressionType.OWL_CLASS && + // axiom1.getSuperClass().getClassExpressionType() == ClassExpressionType.OBJECT_SOME_VALUES_FROM + // } + // case AxiomType.EQUIVALENT_CLASSES => false // TODO + // } + // } + + + // println("T3") + // for { + // axiom <- ontology.tboxAxioms(Imports.INCLUDED) + // //role <- axiom.objectPropertiesInSignature() + // } yield println(axiom) + + // println("T4") + // for { + // axiom <- ontology.tboxAxioms(Imports.INCLUDED).filter(isT4) + // //role <- axiom.objectPropertiesInSignature() + // } yield println(axiom) + + + /* DEBUG */ + List() + } + + } // implicit class RSAOntology + +} // trait RSAOntology \ No newline at end of file -- cgit v1.2.3