diff options
author | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-11-19 11:45:01 +0000 |
---|---|---|
committer | Federico Igne <federico.igne@cs.ox.ac.uk> | 2020-11-19 11:46:13 +0000 |
commit | e84d4a15401b0a83dbb7d502723c33abf656b9ff (patch) | |
tree | 6d7380862399a87f1841532f8e97f2471c54e27c /src | |
parent | 3c6307166d18890212c364a82fc15fb1ba7725e1 (diff) | |
download | RSAComb-e84d4a15401b0a83dbb7d502723c33abf656b9ff.tar.gz RSAComb-e84d4a15401b0a83dbb7d502723c33abf656b9ff.zip |
Move NI instantiation in FilteringProgram
Diffstat (limited to 'src')
5 files changed, 39 insertions, 45 deletions
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/CanonicalModel.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/CanonicalModel.scala index f0f1bf8..d5956ae 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/CanonicalModel.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/CanonicalModel.scala | |||
@@ -37,9 +37,6 @@ class CanonicalModel(val ontology: RSAOntology) extends RSAAxiom { | |||
37 | import implicits.RDFox._ | 37 | import implicits.RDFox._ |
38 | import implicits.JavaCollections._ | 38 | import implicits.JavaCollections._ |
39 | 39 | ||
40 | val named: List[Rule] = | ||
41 | ontology.individuals.map(a => Rule.create(RSA.Named(a))) | ||
42 | |||
43 | val rolesAdditionalRules: List[Rule] = { | 40 | val rolesAdditionalRules: List[Rule] = { |
44 | // Given a role (predicate) compute additional logic rules | 41 | // Given a role (predicate) compute additional logic rules |
45 | def additional(pred: String): Seq[Rule] = { | 42 | def additional(pred: String): Seq[Rule] = { |
@@ -98,14 +95,14 @@ class CanonicalModel(val ontology: RSAOntology) extends RSAAxiom { | |||
98 | val varZ = Variable.create("Z") | 95 | val varZ = Variable.create("Z") |
99 | List( | 96 | List( |
100 | // Reflexivity | 97 | // Reflexivity |
101 | Rule.create(RSA.congruent(varX, varX), RSA.Thing(varX)), | 98 | Rule.create(RSA.Congruent(varX, varX), RSA.Thing(varX)), |
102 | // Simmetry | 99 | // Simmetry |
103 | Rule.create(RSA.congruent(varY, varX), RSA.congruent(varX, varY)), | 100 | Rule.create(RSA.Congruent(varY, varX), RSA.Congruent(varX, varY)), |
104 | // Transitivity | 101 | // Transitivity |
105 | Rule.create( | 102 | Rule.create( |
106 | RSA.congruent(varX, varZ), | 103 | RSA.Congruent(varX, varZ), |
107 | RSA.congruent(varX, varY), | 104 | RSA.Congruent(varX, varY), |
108 | RSA.congruent(varY, varZ) | 105 | RSA.Congruent(varY, varZ) |
109 | ) | 106 | ) |
110 | ) | 107 | ) |
111 | } | 108 | } |
@@ -114,7 +111,7 @@ class CanonicalModel(val ontology: RSAOntology) extends RSAAxiom { | |||
114 | // Compute rules from ontology axioms | 111 | // Compute rules from ontology axioms |
115 | val rules = ontology.axioms.flatMap(_.accept(RuleGenerator)) | 112 | val rules = ontology.axioms.flatMap(_.accept(RuleGenerator)) |
116 | // Return full set of rules | 113 | // Return full set of rules |
117 | rules ::: rolesAdditionalRules ::: topAxioms ::: equalityAxioms ::: named | 114 | rules ::: rolesAdditionalRules ::: topAxioms ::: equalityAxioms |
118 | } | 115 | } |
119 | 116 | ||
120 | object RuleGenerator | 117 | object RuleGenerator |
@@ -159,8 +156,8 @@ class CanonicalModel(val ontology: RSAOntology) extends RSAAxiom { | |||
159 | // do that? | 156 | // do that? |
160 | val facts = unfold.map(x => Rule.create(RSA.In(x))) | 157 | val facts = unfold.map(x => Rule.create(RSA.In(x))) |
161 | val rules = List( | 158 | val rules = List( |
162 | Rule.create(roleRf, atomA, RSA.notIn(varX)), | 159 | Rule.create(roleRf, atomA, RSA.NotIn(varX)), |
163 | Rule.create(atomB, atomA, RSA.notIn(varX)) | 160 | Rule.create(atomB, atomA, RSA.NotIn(varX)) |
164 | ) | 161 | ) |
165 | facts ++ rules | 162 | facts ++ rules |
166 | } | 163 | } |
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/FilteringProgram.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/FilteringProgram.scala index b154575..adc6f56 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/FilteringProgram.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/FilteringProgram.scala | |||
@@ -31,6 +31,13 @@ import uk.ac.ox.cs.rsacomb.implicits.RSAAtom | |||
31 | import uk.ac.ox.cs.rsacomb.suffix.{RSASuffix, Forward, Backward} | 31 | import uk.ac.ox.cs.rsacomb.suffix.{RSASuffix, Forward, Backward} |
32 | import uk.ac.ox.cs.rsacomb.util.RSA | 32 | import uk.ac.ox.cs.rsacomb.util.RSA |
33 | 33 | ||
34 | object FilteringProgram { | ||
35 | |||
36 | def apply(query: SelectQuery, constants: List[Term]): FilteringProgram = | ||
37 | new FilteringProgram(query, constants) | ||
38 | |||
39 | } // object FilteringProgram | ||
40 | |||
34 | class FilteringProgram(query: SelectQuery, constants: List[Term]) | 41 | class FilteringProgram(query: SelectQuery, constants: List[Term]) |
35 | extends RSAAtom { | 42 | extends RSAAtom { |
36 | 43 | ||
@@ -62,9 +69,17 @@ class FilteringProgram(query: SelectQuery, constants: List[Term]) | |||
62 | 69 | ||
63 | val (answer, bounded) = variables | 70 | val (answer, bounded) = variables |
64 | 71 | ||
65 | val facts: List[Rule] = constants.map(c => Rule.create(RSA.NI(c))) | 72 | val named: List[Rule] = |
73 | constants.map(a => Rule.create(RSA.Named(a))) | ||
74 | |||
75 | val nis: Rule = { | ||
76 | val varX = Variable.create("X") | ||
77 | val varY = Variable.create("Y") | ||
78 | Rule.create(RSA.NI(varX), RSA.Congruent(varX, varY), RSA.Named(varY)) | ||
79 | } | ||
80 | |||
66 | val rules: List[Rule] = | 81 | val rules: List[Rule] = |
67 | this.generateFilteringProgram().map(reifyRule) ++ facts | 82 | nis :: named ::: this.generateFilteringProgram().map(reifyRule) |
68 | 83 | ||
69 | /* NOTE: we are restricting to queries that contain conjunctions of | 84 | /* NOTE: we are restricting to queries that contain conjunctions of |
70 | * atoms for the time being. This might need to be reviewed in the | 85 | * atoms for the time being. This might need to be reviewed in the |
@@ -159,7 +174,7 @@ class FilteringProgram(query: SelectQuery, constants: List[Term]) | |||
159 | RSA(bounded.indexOf(role1.getArguments.get(2))), | 174 | RSA(bounded.indexOf(role1.getArguments.get(2))), |
160 | RSA(bounded.indexOf(role2.getArguments.get(2))) | 175 | RSA(bounded.indexOf(role2.getArguments.get(2))) |
161 | ), | 176 | ), |
162 | not(RSA.congruent(role1.getArguments.get(0), role2.getArguments.get(0))) | 177 | not(RSA.Congruent(role1.getArguments.get(0), role2.getArguments.get(0))) |
163 | ) | 178 | ) |
164 | val r4b = for { | 179 | val r4b = for { |
165 | role1 <- body.filter(_.isRoleAssertion) | 180 | role1 <- body.filter(_.isRoleAssertion) |
@@ -174,7 +189,7 @@ class FilteringProgram(query: SelectQuery, constants: List[Term]) | |||
174 | RSA(bounded.indexOf(role1.getArguments.get(2))), | 189 | RSA(bounded.indexOf(role1.getArguments.get(2))), |
175 | RSA(bounded.indexOf(role2.getArguments.get(0))) | 190 | RSA(bounded.indexOf(role2.getArguments.get(0))) |
176 | ), | 191 | ), |
177 | not(RSA.congruent(role1.getArguments.get(0), role2.getArguments.get(2))) | 192 | not(RSA.Congruent(role1.getArguments.get(0), role2.getArguments.get(2))) |
178 | ) | 193 | ) |
179 | val r4c = for { | 194 | val r4c = for { |
180 | role1 <- body.filter(_.isRoleAssertion) | 195 | role1 <- body.filter(_.isRoleAssertion) |
@@ -189,7 +204,7 @@ class FilteringProgram(query: SelectQuery, constants: List[Term]) | |||
189 | RSA(bounded.indexOf(role1.getArguments.get(0))), | 204 | RSA(bounded.indexOf(role1.getArguments.get(0))), |
190 | RSA(bounded.indexOf(role2.getArguments.get(0))) | 205 | RSA(bounded.indexOf(role2.getArguments.get(0))) |
191 | ), | 206 | ), |
192 | not(RSA.congruent(role1.getArguments.get(2), role2.getArguments.get(2))) | 207 | not(RSA.Congruent(role1.getArguments.get(2), role2.getArguments.get(2))) |
193 | ) | 208 | ) |
194 | 209 | ||
195 | /* Rules 5x */ | 210 | /* Rules 5x */ |
@@ -215,7 +230,7 @@ class FilteringProgram(query: SelectQuery, constants: List[Term]) | |||
215 | RSA(bounded indexOf role1arg2), | 230 | RSA(bounded indexOf role1arg2), |
216 | RSA(bounded indexOf role2arg2) | 231 | RSA(bounded indexOf role2arg2) |
217 | ), | 232 | ), |
218 | RSA.congruent(role1arg0, role2arg0), | 233 | RSA.Congruent(role1arg0, role2arg0), |
219 | not(RSA.NI(role1arg0)) | 234 | not(RSA.NI(role1arg0)) |
220 | ) | 235 | ) |
221 | val r5b = for { | 236 | val r5b = for { |
@@ -240,7 +255,7 @@ class FilteringProgram(query: SelectQuery, constants: List[Term]) | |||
240 | RSA(bounded indexOf role1arg2), | 255 | RSA(bounded indexOf role1arg2), |
241 | RSA(bounded indexOf role2arg0) | 256 | RSA(bounded indexOf role2arg0) |
242 | ), | 257 | ), |
243 | RSA.congruent(role1arg0, role2arg2), | 258 | RSA.Congruent(role1arg0, role2arg2), |
244 | not(RSA.NI(role1arg0)) | 259 | not(RSA.NI(role1arg0)) |
245 | ) | 260 | ) |
246 | val r5c = for { | 261 | val r5c = for { |
@@ -265,7 +280,7 @@ class FilteringProgram(query: SelectQuery, constants: List[Term]) | |||
265 | RSA(bounded indexOf role1arg0), | 280 | RSA(bounded indexOf role1arg0), |
266 | RSA(bounded indexOf role2arg0) | 281 | RSA(bounded indexOf role2arg0) |
267 | ), | 282 | ), |
268 | RSA.congruent(role1arg2, role2arg2), | 283 | RSA.Congruent(role1arg2, role2arg2), |
269 | not(RSA.NI(role1arg2)) | 284 | not(RSA.NI(role1arg2)) |
270 | ) | 285 | ) |
271 | 286 | ||
@@ -359,8 +374,3 @@ class FilteringProgram(query: SelectQuery, constants: List[Term]) | |||
359 | } | 374 | } |
360 | 375 | ||
361 | } // class FilteringProgram | 376 | } // class FilteringProgram |
362 | |||
363 | object FilteringProgram { | ||
364 | def apply(query: SelectQuery, constants: List[Term]): FilteringProgram = | ||
365 | new FilteringProgram(query, constants) | ||
366 | } // object FilteringProgram | ||
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala index c3db99d..a8107b5 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/Main.scala | |||
@@ -84,24 +84,14 @@ object RSAComb extends App { | |||
84 | println(query) | 84 | println(query) |
85 | } | 85 | } |
86 | 86 | ||
87 | // Step 1. Computing the canonical model | ||
88 | val canon = ontology.canonicalModel | 87 | val canon = ontology.canonicalModel |
89 | data.addRules(canon.rules) | 88 | data.addRules(canon.rules) |
89 | val filter = ontology.filteringProgram(query) | ||
90 | data.addRules(filter.rules) | ||
90 | 91 | ||
91 | { | 92 | { |
92 | println("\nCanonical Model rules:") | 93 | println("\nCanonical Model rules:") |
93 | canon.rules.foreach(println) | 94 | canon.rules.foreach(println) |
94 | } | ||
95 | |||
96 | // Step 2. Computing the canonical model | ||
97 | val nis = { | ||
98 | val query = "SELECT ?Y WHERE { ?X rsa:EquivTo ?Y ; a rsa:Named . }" | ||
99 | RDFoxHelpers.submitSelectQuery(data, query, RSA.Prefixes).flatten | ||
100 | } | ||
101 | val filter = ontology.filteringProgram(query, nis) | ||
102 | data.addRules(filter.rules) | ||
103 | |||
104 | { | ||
105 | println("\nFiltering rules") | 95 | println("\nFiltering rules") |
106 | filter.rules.foreach(println) | 96 | filter.rules.foreach(println) |
107 | } | 97 | } |
@@ -168,12 +158,12 @@ object RSAComb extends App { | |||
168 | ) | 158 | ) |
169 | println(ids) | 159 | println(ids) |
170 | 160 | ||
171 | println("\nEquivTo:") | 161 | println("\nCongruent:") |
172 | val equivs = RDFoxHelpers.submitSelectQuery( | 162 | val equivs = RDFoxHelpers.submitSelectQuery( |
173 | data, | 163 | data, |
174 | """ | 164 | """ |
175 | SELECT ?X ?Y { | 165 | SELECT ?X ?Y { |
176 | ?X rsa:EquivTo ?Y | 166 | ?X rsa:congruent ?Y |
177 | } | 167 | } |
178 | """, | 168 | """, |
179 | RSA.Prefixes | 169 | RSA.Prefixes |
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 ac86e3d..93fd5cd 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/RSAOntology.scala | |||
@@ -261,11 +261,8 @@ class RSAOntology(val ontology: OWLOntology) extends RSAAxiom { | |||
261 | Graph(edges: _*) | 261 | Graph(edges: _*) |
262 | } | 262 | } |
263 | 263 | ||
264 | def filteringProgram( | 264 | def filteringProgram(query: SelectQuery): FilteringProgram = |
265 | query: SelectQuery, | 265 | new FilteringProgram(query, individuals) |
266 | nis: List[Term] | ||
267 | ): FilteringProgram = | ||
268 | new FilteringProgram(query, nis) | ||
269 | 266 | ||
270 | lazy val canonicalModel = new CanonicalModel(this) | 267 | lazy val canonicalModel = new CanonicalModel(this) |
271 | 268 | ||
diff --git a/src/main/scala/uk/ac/ox/cs/rsacomb/util/RSA.scala b/src/main/scala/uk/ac/ox/cs/rsacomb/util/RSA.scala index f9ff59b..6c8d42d 100644 --- a/src/main/scala/uk/ac/ox/cs/rsacomb/util/RSA.scala +++ b/src/main/scala/uk/ac/ox/cs/rsacomb/util/RSA.scala | |||
@@ -40,9 +40,9 @@ object RSA { | |||
40 | def In(t: Term)(implicit set: Term) = | 40 | def In(t: Term)(implicit set: Term) = |
41 | TupleTableAtom.rdf(t, RSA("In"), set) | 41 | TupleTableAtom.rdf(t, RSA("In"), set) |
42 | 42 | ||
43 | def notIn(t: Term)(implicit set: Term) = Negation.create(In(t)(set)) | 43 | def NotIn(t: Term)(implicit set: Term) = Negation.create(In(t)(set)) |
44 | 44 | ||
45 | def congruent(t1: Term, t2: Term) = | 45 | def Congruent(t1: Term, t2: Term) = |
46 | TupleTableAtom.rdf(t1, RSA("congruent"), t2) | 46 | TupleTableAtom.rdf(t1, RSA("congruent"), t2) |
47 | 47 | ||
48 | def QM(implicit variables: (List[Term], List[Term])) = { | 48 | def QM(implicit variables: (List[Term], List[Term])) = { |