diff options
author | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-12-02 18:52:39 +0000 |
---|---|---|
committer | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-12-02 18:52:39 +0000 |
commit | d7fa665c289923c362c17ce16cda03588911a817 (patch) | |
tree | 6b027414dba598859c5f27740e7a30799308b38e /src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala | |
parent | 6462d8566cc10b47473803e3e9e9547cec6524be (diff) | |
parent | 91de03f187f298004d90708c5815e86134b10eae (diff) | |
download | RSAComb-d7fa665c289923c362c17ce16cda03588911a817.tar.gz RSAComb-d7fa665c289923c362c17ce16cda03588911a817.zip |
Merge branch 'rdfox-converter' into master
Diffstat (limited to 'src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala')
-rw-r--r-- | src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala | 106 |
1 files changed, 63 insertions, 43 deletions
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala index 4dd554a..a965ef9 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala | |||
@@ -7,13 +7,15 @@ import java.util.stream.{Collectors, Stream} | |||
7 | import java.io.File | 7 | import java.io.File |
8 | import org.semanticweb.owlapi.apibinding.OWLManager | 8 | import org.semanticweb.owlapi.apibinding.OWLManager |
9 | import org.semanticweb.owlapi.util.OWLOntologyMerger | 9 | import org.semanticweb.owlapi.util.OWLOntologyMerger |
10 | import org.semanticweb.owlapi.model.{OWLOntology, OWLAxiom} | 10 | import org.semanticweb.owlapi.model.{OWLOntology, OWLAxiom, OWLLogicalAxiom} |
11 | import org.semanticweb.owlapi.model.{ | 11 | import org.semanticweb.owlapi.model.{ |
12 | OWLClass, | 12 | OWLClass, |
13 | OWLClassExpression, | ||
13 | OWLObjectProperty, | 14 | OWLObjectProperty, |
14 | OWLSubObjectPropertyOfAxiom, | 15 | OWLSubObjectPropertyOfAxiom, |
15 | OWLObjectPropertyExpression, | 16 | OWLObjectPropertyExpression, |
16 | OWLObjectSomeValuesFrom, | 17 | OWLObjectSomeValuesFrom, |
18 | OWLDataSomeValuesFrom, | ||
17 | OWLSubClassOfAxiom | 19 | OWLSubClassOfAxiom |
18 | } | 20 | } |
19 | import org.semanticweb.owlapi.model.parameters.Imports | 21 | import org.semanticweb.owlapi.model.parameters.Imports |
@@ -48,7 +50,7 @@ import org.semanticweb.owlapi.dlsyntax.renderer.DLSyntaxObjectRenderer | |||
48 | import tech.oxfordsemantic.jrdfox.logic._ | 50 | import tech.oxfordsemantic.jrdfox.logic._ |
49 | import org.semanticweb.owlapi.model.OWLObjectInverseOf | 51 | import org.semanticweb.owlapi.model.OWLObjectInverseOf |
50 | 52 | ||
51 | import uk.ac.ox.cs.rsacomb.converter.{RDFoxAxiomConverter, SkolemStrategy} | 53 | import uk.ac.ox.cs.rsacomb.converter.{RDFoxConverter, SkolemStrategy} |
52 | import uk.ac.ox.cs.rsacomb.suffix._ | 54 | import uk.ac.ox.cs.rsacomb.suffix._ |
53 | import uk.ac.ox.cs.rsacomb.sparql._ | 55 | import uk.ac.ox.cs.rsacomb.sparql._ |
54 | import uk.ac.ox.cs.rsacomb.util.{RDFoxUtil, RSA} | 56 | import uk.ac.ox.cs.rsacomb.util.{RDFoxUtil, RSA} |
@@ -82,30 +84,28 @@ object RSAOntology { | |||
82 | class RSAOntology(val ontology: OWLOntology) { | 84 | class RSAOntology(val ontology: OWLOntology) { |
83 | 85 | ||
84 | import uk.ac.ox.cs.rsacomb.implicits.RSAAxiom._ | 86 | import uk.ac.ox.cs.rsacomb.implicits.RSAAxiom._ |
87 | import uk.ac.ox.cs.rsacomb.implicits.JavaCollections._ | ||
85 | 88 | ||
86 | // Gather TBox/RBox/ABox from original ontology | 89 | // Gather TBox/RBox/ABox from original ontology |
87 | val tbox: List[OWLAxiom] = | 90 | val tbox: List[OWLLogicalAxiom] = |
88 | ontology | 91 | ontology |
89 | .tboxAxioms(Imports.INCLUDED) | 92 | .tboxAxioms(Imports.INCLUDED) |
90 | .collect(Collectors.toList()) | 93 | .collect(Collectors.toList()) |
91 | .asScala | 94 | .collect { case a: OWLLogicalAxiom => a } |
92 | .toList | ||
93 | 95 | ||
94 | val rbox: List[OWLAxiom] = | 96 | val rbox: List[OWLLogicalAxiom] = |
95 | ontology | 97 | ontology |
96 | .rboxAxioms(Imports.INCLUDED) | 98 | .rboxAxioms(Imports.INCLUDED) |
97 | .collect(Collectors.toList()) | 99 | .collect(Collectors.toList()) |
98 | .asScala | 100 | .collect { case a: OWLLogicalAxiom => a } |
99 | .toList | ||
100 | 101 | ||
101 | val abox: List[OWLAxiom] = | 102 | val abox: List[OWLLogicalAxiom] = |
102 | ontology | 103 | ontology |
103 | .aboxAxioms(Imports.INCLUDED) | 104 | .aboxAxioms(Imports.INCLUDED) |
104 | .collect(Collectors.toList()) | 105 | .collect(Collectors.toList()) |
105 | .asScala | 106 | .collect { case a: OWLLogicalAxiom => a } |
106 | .toList | ||
107 | 107 | ||
108 | val axioms: List[OWLAxiom] = abox ::: tbox ::: rbox | 108 | val axioms: List[OWLLogicalAxiom] = abox ::: tbox ::: rbox |
109 | 109 | ||
110 | /* Retrieve individuals in the original ontology | 110 | /* Retrieve individuals in the original ontology |
111 | */ | 111 | */ |
@@ -152,28 +152,54 @@ class RSAOntology(val ontology: OWLOntology) { | |||
152 | //println("\nUnsafe roles:") | 152 | //println("\nUnsafe roles:") |
153 | //println(unsafe) | 153 | //println(unsafe) |
154 | 154 | ||
155 | object RSAConverter extends RDFoxConverter { | ||
156 | |||
157 | override def convert( | ||
158 | expr: OWLClassExpression, | ||
159 | term: Term, | ||
160 | unsafe: List[OWLObjectPropertyExpression], | ||
161 | skolem: SkolemStrategy, | ||
162 | suffix: RSASuffix | ||
163 | ): Shards = | ||
164 | (expr, skolem) match { | ||
165 | |||
166 | case (e: OWLObjectSomeValuesFrom, SkolemStrategy.Constant(c)) | ||
167 | if unsafe contains e.getProperty => { | ||
168 | val (res, ext) = super.convert(e, term, unsafe, skolem, suffix) | ||
169 | (RSA.PE(term, c) :: RSA.U(c) :: res, ext) | ||
170 | } | ||
171 | |||
172 | case (e: OWLDataSomeValuesFrom, SkolemStrategy.Constant(c)) | ||
173 | if unsafe contains e.getProperty => { | ||
174 | val (res, ext) = super.convert(e, term, unsafe, skolem, suffix) | ||
175 | (RSA.PE(term, c) :: RSA.U(c) :: res, ext) | ||
176 | } | ||
177 | |||
178 | case _ => super.convert(expr, term, unsafe, skolem, suffix) | ||
179 | } | ||
180 | |||
181 | } | ||
182 | |||
155 | /* Ontology convertion into LP rules */ | 183 | /* Ontology convertion into LP rules */ |
156 | val datalog = for { | 184 | val term = RSAOntology.genFreshVariable() |
157 | axiom <- axioms | 185 | val datalog = axioms |
158 | visitor = new RDFoxAxiomConverter( | 186 | .map(a => { |
159 | RSAOntology.genFreshVariable(), | 187 | val skolem = SkolemStrategy.Constant(a.toString) |
160 | unsafe, | 188 | RSAConverter.convert(a, term, unsafe, skolem, Empty) |
161 | SkolemStrategy.ConstantRSA(axiom.toString), | 189 | }) |
162 | Empty | 190 | .unzip |
163 | ) | 191 | val facts = datalog._1.flatten |
164 | rule <- axiom.accept(visitor) | 192 | val rules = datalog._2.flatten |
165 | } yield rule | ||
166 | 193 | ||
167 | /* DEBUG: print datalog rules */ | 194 | /* DEBUG: print datalog rules */ |
168 | println("\nDatalog roles:") | 195 | //println("\nDatalog rules:") |
169 | datalog.foreach(println) | 196 | //rules.foreach(println) |
170 | 197 | ||
171 | // Open connection with RDFox | 198 | // Open connection with RDFox |
172 | val (server, data) = RDFoxUtil.openConnection("RSACheck") | 199 | val (server, data) = RDFoxUtil.openConnection("RSACheck") |
173 | // Add Data (hardcoded for now) | ||
174 | //data.importData(UpdateType.ADDITION, RSA.Prefixes, ":a a :A .") | ||
175 | 200 | ||
176 | /* Add built-in rules | 201 | /* Add built-in rules |
202 | * TODO: substitute with RDFoxUtil.addRules | ||
177 | */ | 203 | */ |
178 | data.importData( | 204 | data.importData( |
179 | UpdateType.ADDITION, | 205 | UpdateType.ADDITION, |
@@ -181,20 +207,11 @@ class RSAOntology(val ontology: OWLOntology) { | |||
181 | "rsa:E[?X,?Y] :- rsa:PE[?X,?Y], rsa:U[?X], rsa:U[?Y] ." | 207 | "rsa:E[?X,?Y] :- rsa:PE[?X,?Y], rsa:U[?X], rsa:U[?Y] ." |
182 | ) | 208 | ) |
183 | 209 | ||
184 | /* Add built-in rules | 210 | /* Add ontology facts and rules */ |
185 | */ | 211 | RDFoxUtil.addFacts(data, facts) |
186 | // data.importData( | 212 | RDFoxUtil.addRules(data, rules) |
187 | // UpdateType.ADDITION, | ||
188 | // RSA.Prefixes, | ||
189 | // "[?entity, a, ?superClass] :- [?entity, a, ?class], [?class, rdfs:subClassOf, ?superClass] ." | ||
190 | // ) | ||
191 | |||
192 | /* Add ontology rules | ||
193 | */ | ||
194 | data.addRules(datalog.asJava) | ||
195 | 213 | ||
196 | /* Build graph | 214 | /* Build graph */ |
197 | */ | ||
198 | val graph = this.rsaGraph(data); | 215 | val graph = this.rsaGraph(data); |
199 | //println(graph) | 216 | //println(graph) |
200 | 217 | ||
@@ -267,8 +284,8 @@ class RSAOntology(val ontology: OWLOntology) { | |||
267 | ): Graph[Resource, UnDiEdge] = { | 284 | ): Graph[Resource, UnDiEdge] = { |
268 | val query = "SELECT ?X ?Y WHERE { ?X rsa:E ?Y }" | 285 | val query = "SELECT ?X ?Y WHERE { ?X rsa:E ?Y }" |
269 | val answers = RDFoxUtil.submitQuery(data, query, RSA.Prefixes).get | 286 | val answers = RDFoxUtil.submitQuery(data, query, RSA.Prefixes).get |
270 | var edges: Seq[UnDiEdge[Resource]] = answers.map { | 287 | var edges: Seq[UnDiEdge[Resource]] = answers.map { case Seq(n1, n2) => |
271 | case Seq(n1, n2) => UnDiEdge(n1, n2) | 288 | UnDiEdge(n1, n2) |
272 | } | 289 | } |
273 | Graph(edges: _*) | 290 | Graph(edges: _*) |
274 | } | 291 | } |
@@ -310,8 +327,11 @@ class RSAOntology(val ontology: OWLOntology) { | |||
310 | def ask(query: ConjunctiveQuery): ConjunctiveQueryAnswers = { | 327 | def ask(query: ConjunctiveQuery): ConjunctiveQueryAnswers = { |
311 | import implicits.JavaCollections._ | 328 | import implicits.JavaCollections._ |
312 | val (server, data) = RDFoxUtil.openConnection(RSAOntology.DataStore) | 329 | val (server, data) = RDFoxUtil.openConnection(RSAOntology.DataStore) |
313 | data.addRules(this.canonicalModel.rules) | 330 | val filter = this.filteringProgram(query) |
314 | data.addRules(this.filteringProgram(query).rules) | 331 | RDFoxUtil.addRules(data, this.canonicalModel.rules) |
332 | RDFoxUtil.addFacts(data, this.canonicalModel.facts) | ||
333 | RDFoxUtil.addRules(data, filter.rules) | ||
334 | RDFoxUtil.addFacts(data, filter.facts) | ||
315 | val answers = RDFoxUtil | 335 | val answers = RDFoxUtil |
316 | .submitQuery( | 336 | .submitQuery( |
317 | data, | 337 | data, |