diff options
Diffstat (limited to 'src/main/scala/rsacomb/RSAOntology.scala')
| -rw-r--r-- | src/main/scala/rsacomb/RSAOntology.scala | 95 |
1 files changed, 74 insertions, 21 deletions
diff --git a/src/main/scala/rsacomb/RSAOntology.scala b/src/main/scala/rsacomb/RSAOntology.scala index 3b08e61..06752ef 100644 --- a/src/main/scala/rsacomb/RSAOntology.scala +++ b/src/main/scala/rsacomb/RSAOntology.scala | |||
| @@ -2,7 +2,7 @@ package rsacomb | |||
| 2 | 2 | ||
| 3 | /* Java imports */ | 3 | /* Java imports */ |
| 4 | import java.util.HashMap | 4 | import java.util.HashMap |
| 5 | import java.util.stream.{Collectors,Stream} | 5 | import java.util.stream.{Collectors, Stream} |
| 6 | 6 | ||
| 7 | import org.semanticweb.owlapi.model.OWLOntology | 7 | import org.semanticweb.owlapi.model.OWLOntology |
| 8 | import org.semanticweb.owlapi.model.OWLObjectPropertyExpression | 8 | import org.semanticweb.owlapi.model.OWLObjectPropertyExpression |
| @@ -13,7 +13,6 @@ import tech.oxfordsemantic.jrdfox.Prefixes | |||
| 13 | import tech.oxfordsemantic.jrdfox.logic.Variable | 13 | import tech.oxfordsemantic.jrdfox.logic.Variable |
| 14 | import tech.oxfordsemantic.jrdfox.client.UpdateType | 14 | import tech.oxfordsemantic.jrdfox.client.UpdateType |
| 15 | 15 | ||
| 16 | |||
| 17 | /* Scala imports */ | 16 | /* Scala imports */ |
| 18 | import scala.collection.JavaConverters._ | 17 | import scala.collection.JavaConverters._ |
| 19 | 18 | ||
| @@ -42,9 +41,14 @@ trait RSAOntology { | |||
| 42 | * step of approximation of an Horn-ALCHOIQ to RSA | 41 | * step of approximation of an Horn-ALCHOIQ to RSA |
| 43 | */ | 42 | */ |
| 44 | 43 | ||
| 45 | val tbox = | 44 | val tbox = |
| 46 | Stream.concat(ontology.tboxAxioms(Imports.INCLUDED), ontology.rboxAxioms(Imports.INCLUDED)) | 45 | Stream |
| 47 | .collect(Collectors.toList()).asScala | 46 | .concat( |
| 47 | ontology.tboxAxioms(Imports.INCLUDED), | ||
| 48 | ontology.rboxAxioms(Imports.INCLUDED) | ||
| 49 | ) | ||
| 50 | .collect(Collectors.toList()) | ||
| 51 | .asScala | ||
| 48 | val unsafe = ontology.getUnsafeRoles | 52 | val unsafe = ontology.getUnsafeRoles |
| 49 | 53 | ||
| 50 | /* DEBUG: print rules in DL syntax */ | 54 | /* DEBUG: print rules in DL syntax */ |
| @@ -55,18 +59,29 @@ trait RSAOntology { | |||
| 55 | /* Ontology convertion into LP rules */ | 59 | /* Ontology convertion into LP rules */ |
| 56 | val datalog = for { | 60 | val datalog = for { |
| 57 | axiom <- tbox | 61 | axiom <- tbox |
| 58 | visitor = new RDFoxAxiomConverter(Variable.create("x"), SkolemStrategy.ConstantRSA(axiom.toString), unsafe) | 62 | visitor = new RDFoxAxiomConverter( |
| 59 | rule <- axiom.accept(visitor) | 63 | Variable.create("x"), |
| 64 | SkolemStrategy.ConstantRSA(axiom.toString), | ||
| 65 | unsafe | ||
| 66 | ) | ||
| 67 | rule <- axiom.accept(visitor) | ||
| 60 | } yield rule | 68 | } yield rule |
| 61 | 69 | ||
| 62 | val prefixes = new Prefixes() | 70 | val prefixes = new Prefixes() |
| 63 | prefixes.declarePrefix(":", "http://example.com/rsa_example.owl#") | 71 | prefixes.declarePrefix(":", "http://example.com/rsa_example.owl#") |
| 72 | prefixes.declarePrefix( | ||
| 73 | "rdf:", | ||
| 74 | "http://www.w3.org/1999/02/22-rdf-syntax-ns#" | ||
| 75 | ) | ||
| 76 | prefixes.declarePrefix("rdfs:", "http://www.w3.org/2000/01/rdf-schema#") | ||
| 77 | prefixes.declarePrefix("owl:", "http://www.w3.org/2002/07/owl#") | ||
| 64 | 78 | ||
| 65 | // Open connection with RDFox | 79 | // Open connection with RDFox |
| 66 | val (server,data) = RDFox.openConnection("RSACheck") | 80 | val (server, data) = RDFoxUtil.openConnection("RSACheck") |
| 67 | // Add Data (hardcoded for now) | 81 | // Add Data (hardcoded for now) |
| 68 | data.importData(UpdateType.ADDITION, prefixes,":a a :A .") | 82 | data.importData(UpdateType.ADDITION, prefixes, ":a a :A .") |
| 69 | /* Add Datalog rules | 83 | |
| 84 | /* Add rules | ||
| 70 | * | 85 | * |
| 71 | * NOTE: | 86 | * NOTE: |
| 72 | * - using the `addRules(...)` method in `DataStoreConnection` is not working as expected, complaining | 87 | * - using the `addRules(...)` method in `DataStoreConnection` is not working as expected, complaining |
| @@ -76,18 +91,45 @@ trait RSAOntology { | |||
| 76 | * for predicate arguments (e.g., `<predicate>(?X,?Y)`) while the specification for the proprietary RDFox | 91 | * for predicate arguments (e.g., `<predicate>(?X,?Y)`) while the specification for the proprietary RDFox |
| 77 | * syntax uses squared brackets (e.g., `<preditate>[?X,?Y]`). | 92 | * syntax uses squared brackets (e.g., `<preditate>[?X,?Y]`). |
| 78 | */ | 93 | */ |
| 94 | |||
| 95 | /* Add built-in rules | ||
| 96 | */ | ||
| 97 | data.importData( | ||
| 98 | UpdateType.ADDITION, | ||
| 99 | prefixes, | ||
| 100 | "<internal:E>[?X,?Y] :- <internal:PE>[?X,?Y], <internal:U>[?X], <internal:U>[?Y] ." | ||
| 101 | ) | ||
| 102 | |||
| 103 | /* Add ontology rules | ||
| 104 | */ | ||
| 79 | data.importData( | 105 | data.importData( |
| 80 | UpdateType.ADDITION, | 106 | UpdateType.ADDITION, |
| 81 | prefixes, | 107 | prefixes, |
| 82 | datalog.foldLeft("")((str,rule) => str ++ "\n" ++ rule.toString().replace("(", "[").replace(")","]")) | 108 | datalog.foldLeft("")((str, rule) => |
| 109 | str ++ "\n" ++ rule.toString().replace("(", "[").replace(")", "]") | ||
| 110 | ) | ||
| 83 | ) | 111 | ) |
| 84 | 112 | ||
| 85 | // Retrieve all instances of PE | 113 | // Retrieve all instances of PE |
| 86 | println("\nQuery results:") | 114 | println("\nQuery results:") |
| 87 | data.evaluateQuery(prefixes,"SELECT ?X ?Y WHERE { ?X <internal:PE> ?Y }", new HashMap[String,String](), System.out, "text/csv"); | 115 | data.evaluateQuery( |
| 116 | prefixes, | ||
| 117 | "SELECT ?X ?Y WHERE { ?X <internal:PE> ?Y }", | ||
| 118 | new HashMap[String, String](), | ||
| 119 | System.out, | ||
| 120 | "text/csv" | ||
| 121 | ); | ||
| 122 | |||
| 123 | data.evaluateQuery( | ||
| 124 | prefixes, | ||
| 125 | "SELECT ?X ?Y WHERE { ?X <internal:E> ?Y }", | ||
| 126 | new HashMap[String, String](), | ||
| 127 | System.out, | ||
| 128 | "text/csv" | ||
| 129 | ); | ||
| 88 | 130 | ||
| 89 | // Close connection to RDFox | 131 | // Close connection to RDFox |
| 90 | RDFox.closeConnection(server,data) | 132 | RDFoxUtil.closeConnection(server, data) |
| 91 | 133 | ||
| 92 | /* DEBUG */ | 134 | /* DEBUG */ |
| 93 | true | 135 | true |
| @@ -98,13 +140,16 @@ trait RSAOntology { | |||
| 98 | val factory = new StructuralReasonerFactory() | 140 | val factory = new StructuralReasonerFactory() |
| 99 | val reasoner = factory.createReasoner(ontology) | 141 | val reasoner = factory.createReasoner(ontology) |
| 100 | 142 | ||
| 101 | val tbox = ontology.tboxAxioms(Imports.INCLUDED).collect(Collectors.toSet()).asScala | 143 | val tbox = ontology |
| 144 | .tboxAxioms(Imports.INCLUDED) | ||
| 145 | .collect(Collectors.toSet()) | ||
| 146 | .asScala | ||
| 102 | 147 | ||
| 103 | /* DEBUG: print rules in DL syntax */ | 148 | /* DEBUG: print rules in DL syntax */ |
| 104 | //val renderer = new DLSyntaxObjectRenderer() | 149 | //val renderer = new DLSyntaxObjectRenderer() |
| 105 | 150 | ||
| 106 | /* Checking for (1) unsafety condition: | 151 | /* Checking for (1) unsafety condition: |
| 107 | * | 152 | * |
| 108 | * For all roles r1 appearing in an axiom of type T5, r1 is unsafe | 153 | * For all roles r1 appearing in an axiom of type T5, r1 is unsafe |
| 109 | * if there exists a role r2 (different from top) appearing in an axiom | 154 | * if there exists a role r2 (different from top) appearing in an axiom |
| 110 | * of type T3 and r1 is a subproperty of the inverse of r2. | 155 | * of type T3 and r1 is a subproperty of the inverse of r2. |
| @@ -113,11 +158,15 @@ trait RSAOntology { | |||
| 113 | axiom <- tbox | 158 | axiom <- tbox |
| 114 | if axiom.isT5 | 159 | if axiom.isT5 |
| 115 | role1 <- axiom.objectPropertyExpressionsInSignature | 160 | role1 <- axiom.objectPropertyExpressionsInSignature |
| 116 | roleSuper = role1 +: reasoner.superObjectProperties(role1).collect(Collectors.toList()).asScala | 161 | roleSuper = |
| 162 | role1 +: reasoner | ||
| 163 | .superObjectProperties(role1) | ||
| 164 | .collect(Collectors.toList()) | ||
| 165 | .asScala | ||
| 117 | roleSuperInv = roleSuper.map(_.getInverseProperty) | 166 | roleSuperInv = roleSuper.map(_.getInverseProperty) |
| 118 | axiom <- tbox | 167 | axiom <- tbox |
| 119 | if axiom.isT3 && !axiom.isT3top | 168 | if axiom.isT3 && !axiom.isT3top |
| 120 | role2 <- axiom.objectPropertyExpressionsInSignature | 169 | role2 <- axiom.objectPropertyExpressionsInSignature |
| 121 | if roleSuperInv.contains(role2) | 170 | if roleSuperInv.contains(role2) |
| 122 | } yield role1 | 171 | } yield role1 |
| 123 | 172 | ||
| @@ -126,17 +175,21 @@ trait RSAOntology { | |||
| 126 | * For all roles p1 appearing in an axiom of type T5, p1 is unsafe if | 175 | * For all roles p1 appearing in an axiom of type T5, p1 is unsafe if |
| 127 | * there exists a role p2 appearing in an axiom of type T4 and p1 is a | 176 | * there exists a role p2 appearing in an axiom of type T4 and p1 is a |
| 128 | * subproperty of either p2 or the inverse of p2. | 177 | * subproperty of either p2 or the inverse of p2. |
| 129 | * | 178 | * |
| 130 | */ | 179 | */ |
| 131 | val unsafe2 = for { | 180 | val unsafe2 = for { |
| 132 | axiom <- tbox | 181 | axiom <- tbox |
| 133 | if axiom.isT5 | 182 | if axiom.isT5 |
| 134 | role1 <- axiom.objectPropertyExpressionsInSignature | 183 | role1 <- axiom.objectPropertyExpressionsInSignature |
| 135 | roleSuper = role1 +: reasoner.superObjectProperties(role1).collect(Collectors.toList()).asScala | 184 | roleSuper = |
| 185 | role1 +: reasoner | ||
| 186 | .superObjectProperties(role1) | ||
| 187 | .collect(Collectors.toList()) | ||
| 188 | .asScala | ||
| 136 | roleSuperInv = roleSuper.map(_.getInverseProperty) | 189 | roleSuperInv = roleSuper.map(_.getInverseProperty) |
| 137 | axiom <- tbox | 190 | axiom <- tbox |
| 138 | if axiom.isT4 | 191 | if axiom.isT4 |
| 139 | role2 <- axiom.objectPropertyExpressionsInSignature | 192 | role2 <- axiom.objectPropertyExpressionsInSignature |
| 140 | if roleSuper.contains(role2) || roleSuperInv.contains(role2) | 193 | if roleSuper.contains(role2) || roleSuperInv.contains(role2) |
| 141 | } yield role1 | 194 | } yield role1 |
| 142 | 195 | ||
| @@ -149,4 +202,4 @@ trait RSAOntology { | |||
| 149 | 202 | ||
| 150 | } // implicit class RSAOntology | 203 | } // implicit class RSAOntology |
| 151 | 204 | ||
| 152 | } // trait RSAOntology \ No newline at end of file | 205 | } // trait RSAOntology |
