aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/query/rollup/QueryGraph.java
diff options
context:
space:
mode:
authorFederico Igne <federico.igne@cs.ox.ac.uk>2022-05-10 18:17:06 +0100
committerFederico Igne <federico.igne@cs.ox.ac.uk>2022-05-11 12:34:47 +0100
commit17bd9beaf7f358a44e5bf36a5855fe6727d506dc (patch)
tree47e9310a0cff869d9ec017dcb2c81876407782c8 /src/uk/ac/ox/cs/pagoda/query/rollup/QueryGraph.java
parent8651164cd632a5db310b457ce32d4fbc97bdc41c (diff)
downloadACQuA-17bd9beaf7f358a44e5bf36a5855fe6727d506dc.tar.gz
ACQuA-17bd9beaf7f358a44e5bf36a5855fe6727d506dc.zip
[pagoda] Move project to Scala
This commit includes a few changes: - The repository still uses Maven to manage dependency but it is now a Scala project. - The code has been ported from OWLAPI 3.4.10 to 5.1.20 - A proof of concept program using both RSAComb and PAGOdA has been added.
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/query/rollup/QueryGraph.java')
-rw-r--r--src/uk/ac/ox/cs/pagoda/query/rollup/QueryGraph.java400
1 files changed, 0 insertions, 400 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/query/rollup/QueryGraph.java b/src/uk/ac/ox/cs/pagoda/query/rollup/QueryGraph.java
deleted file mode 100644
index 9b4ce01..0000000
--- a/src/uk/ac/ox/cs/pagoda/query/rollup/QueryGraph.java
+++ /dev/null
@@ -1,400 +0,0 @@
1package uk.ac.ox.cs.pagoda.query.rollup;
2
3import org.semanticweb.HermiT.model.*;
4import org.semanticweb.owlapi.model.*;
5import uk.ac.ox.cs.pagoda.util.Namespace;
6
7import java.util.*;
8
9public class QueryGraph {
10
11 Set<Variable> freeVars = new HashSet<Variable>();
12 Set<Variable> existVars = new HashSet<Variable>();
13 Set<Individual> constants = new HashSet<Individual>();
14
15 MultiMap<Term, OWLClassExpression> concepts = new MultiMap<Term, OWLClassExpression>();
16 MultiMap<Term, ObjectEdge> rollable_edges = new MultiMap<Term, ObjectEdge>();
17 MultiMap<Term, ObjectEdge> edges = new MultiMap<Term, ObjectEdge>();
18 OWLOntology ontology;
19 OWLDataFactory factory;
20
21 public QueryGraph(Atom[] bodyAtoms, String[] distinguishedVars, OWLOntology onto) {
22 for (String vName: distinguishedVars)
23 freeVars.add(Variable.create(vName));
24
25 ontology = onto;
26 factory = onto.getOWLOntologyManager().getOWLDataFactory();
27
28 for (Atom atom: bodyAtoms) {
29 if (atom.getArity() == 1) {
30 updateExistentiallyVariables(atom.getArgumentVariable(0));
31 String id = ((AtomicConcept) atom.getDLPredicate()).getIRI();
32 if (!id.equals(Namespace.PAGODA_ORIGINAL))
33 concepts.add(atom.getArgument(0), factory.getOWLClass(IRI.create(id)));
34 }
35 else if (atom.getArity() == 2) {
36 updateExistentiallyVariables(atom.getArgumentVariable(0));
37 updateExistentiallyVariables(atom.getArgumentVariable(1));
38 if (atom.getArgument(0).equals(atom.getArgument(1)) && atom.getArgument(0) instanceof Variable) {
39 concepts.add(atom.getArgument(0), factory.getOWLObjectHasSelf(factory.getOWLObjectProperty(IRI.create(((AtomicRole) atom.getDLPredicate()).getIRI()))));
40 }
41 else createEdges(atom.getArgument(0), (AtomicRole) atom.getDLPredicate(), atom.getArgument(1));
42 }
43 }
44
45 rollup();
46 }
47
48 public void createEdges(Term u, AtomicRole r, Term v) {
49 if(ontology.containsDataPropertyInSignature(IRI.create(r.getIRI()))) {
50// edges.add(u, new DataEdge(r, v));
51 Constant c = (Constant) v;
52 OWLLiteral l = factory.getOWLLiteral(c.getLexicalForm(), c.getDatatypeURI());
53 concepts.add(u, factory.getOWLDataHasValue(factory.getOWLDataProperty(IRI.create(r.getIRI())), l));
54 }
55 else {
56 boolean rollable = existVars.contains(u) || existVars.contains(v);
57
58 ObjectEdge edge = new ObjectEdge(r, v, false);
59 if(rollable) {
60 rollable_edges.add(u, edge);
61 edge = new ObjectEdge(r, u, true);
62 rollable_edges.add(v, edge);
63 }
64 else edges.add(u, edge);
65
66 }
67 }
68
69 public Set<OWLAxiom> getPropertyAssertions(Map<Variable, Term> assignment) {
70 OWLIndividual sub, obj;
71 Set<OWLAxiom> axioms = new HashSet<OWLAxiom>();
72 for(Map.Entry<Term, Set<ObjectEdge>> entry : edges.map.entrySet()) {
73 sub = factory.getOWLNamedIndividual(IRI.create(getIndividual(entry.getKey(), assignment).getIRI()));
74 for(ObjectEdge edge : entry.getValue()) {
75 Individual individual = getIndividual(edge.v, assignment);
76 String iri = individual.getIRI();
77 obj = factory.getOWLNamedIndividual(IRI.create(iri));
78 axioms.add(factory.getOWLObjectPropertyAssertionAxiom(edge.p, sub, obj));
79 }
80 }
81 return axioms;
82 }
83
84// public Set<OWLClassExpression> getExistentialConditions(Map<Variable, Term> assignment) {
85// if(!rollable_edges.isEmpty()) return null;
86//
87// OWLIndividual sub;
88// Visitor visitor = new Visitor(factory, assignment);
89// Set<OWLClassExpression> axioms = new HashSet<>();
90// for(Map.Entry<Term, Set<OWLClassExpression>> entry : concepts.map.entrySet()) {
91// // TODO check correctness!!!
92// if(existVars.contains(entry.getKey())) {
93// OWLClassExpression conjunction =
94// factory.getOWLObjectIntersectionOf(factory.getOWLThing());
95// for(OWLClassExpression owlClassExpression : entry.getValue()) {
96// conjunction = factory.getOWLObjectIntersectionOf(conjunction, owlClassExpression.accept(visitor));
97// }
98// axioms.add(conjunction);
99//// continue; // previously the "then" contained only this
100// }
101// }
102// return axioms;
103// }
104
105 public Set<OWLAxiom> getExistentialAxioms(Map<Variable, Term> assignment) {
106 if(!rollable_edges.isEmpty()) return null;
107
108 Visitor visitor = new Visitor(factory, assignment);
109 Set<OWLAxiom> axioms = new HashSet<>();
110 for(Map.Entry<Term, Set<OWLClassExpression>> entry : concepts.map.entrySet()) {
111 if(existVars.contains(entry.getKey())) {
112 OWLClassExpression conjunction = factory.getOWLThing();
113 for(OWLClassExpression owlClassExpression : entry.getValue()) {
114 conjunction = factory.getOWLObjectIntersectionOf(conjunction, owlClassExpression.accept(visitor));
115 }
116 axioms.add(factory.getOWLSubClassOfAxiom(conjunction, factory.getOWLNothing()));
117 }
118 }
119 return axioms;
120 }
121
122 public Set<OWLAxiom> getAssertions(Map<Variable, Term> assignment) {
123 if(!rollable_edges.isEmpty()) return null;
124
125 OWLIndividual sub;
126 Visitor visitor = new Visitor(factory, assignment);
127 Set<OWLAxiom> axioms = getPropertyAssertions(assignment);
128 for(Map.Entry<Term, Set<OWLClassExpression>> entry : concepts.map.entrySet()) {
129 // TODO check correctness!!!
130 if(existVars.contains(entry.getKey())) {
131// OWLClassExpression conjunction =
132// factory.getOWLObjectIntersectionOf(factory.getOWLThing());
133// for(OWLClassExpression owlClassExpression : entry.getValue()) {
134// conjunction = factory.getOWLObjectIntersectionOf(conjunction, owlClassExpression.accept(visitor));
135// }
136// axioms.add(factory.getOWLSubClassOfAxiom(conjunction, factory.getOWLNothing()));
137 continue; // previously the "then" contained only this
138 }
139 else {
140 sub = factory.getOWLNamedIndividual(IRI.create(getIndividual(entry.getKey(), assignment).getIRI()));
141 for(OWLClassExpression clsExp : entry.getValue()) {
142 axioms.add(factory.getOWLClassAssertionAxiom(clsExp.accept(visitor), sub));
143 }
144 }
145 }
146 return axioms;
147 }
148
149 private void updateExistentiallyVariables(Variable argumentVariable) {
150 if(freeVars.contains(argumentVariable)) return;
151 existVars.add(argumentVariable);
152 }
153
154 private void rollup() {
155 for (boolean updated = true; updated; ) {
156 updated = false;
157
158 Set<ObjectEdge> set;
159 for (Variable var: existVars) {
160 if ((set = rollable_edges.map.get(var)) != null && set.size() == 1) {
161 updated = true;
162 ObjectEdge edge = set.iterator().next();
163 rollupEdge(edge.v, edge.p.getInverseProperty().getSimplified(), var, true);
164 set.clear();
165 }
166 }
167 if(updated) continue;
168
169 for (Variable var: existVars) {
170 set = rollable_edges.map.get(var);
171 if(set == null) continue;
172 for (Iterator<ObjectEdge> iter = set.iterator(); iter.hasNext(); ) {
173 ObjectEdge edge = iter.next();
174 if (constants.contains(edge.v) || freeVars.contains(edge.v)) {
175 updated = true;
176 rollupEdge(var, edge.p, edge.v, false);
177 iter.remove();
178 }
179 }
180 }
181 }
182
183 }
184
185 private void rollupEdge(Term u, OWLObjectPropertyExpression op, Term v, boolean inverse) {
186 if (existVars.contains(v)) {
187 Set<OWLClassExpression> exps = concepts.get(v);
188 if (exps == null) exps = new HashSet<OWLClassExpression>();
189 concepts.add(u, factory.getOWLObjectSomeValuesFrom(op, factory.getOWLObjectIntersectionOf(exps)));
190 }
191 else {
192 OWLIndividual obj = getOWLIndividual(v);
193 concepts.add(u, factory.getOWLObjectHasValue(op, obj));
194 }
195
196 if(inverse)
197 removeRollableEdge(u, op, v);
198 else
199 removeRollableEdge(v, op.getInverseProperty().getSimplified(), u);
200 }
201
202 private void removeRollableEdge(Term u, OWLObjectPropertyExpression op, Term v) {
203 Set<ObjectEdge> set = rollable_edges.get(u);
204 ObjectEdge edge;
205 if (set != null)
206 for (Iterator<ObjectEdge> iter = set.iterator(); iter.hasNext(); ) {
207 edge = iter.next();
208 if(edge.p.equals(op) && edge.v.equals(v)) iter.remove();
209 }
210 }
211
212 OWLNamedIndividual getOWLIndividual(Term t) {
213 if (freeVars.contains(t))
214 return new VariableIndividual((Variable) t);
215 else if (t instanceof Variable)
216 return null;
217 else
218 return factory.getOWLNamedIndividual(IRI.create(((Individual) t).getIRI()));
219 }
220
221 private Individual getIndividual(Term key, Map<Variable, Term> assignment) {
222 if(key instanceof Individual)
223 return (Individual) key;
224 else
225 return (Individual) assignment.get(key);
226 }
227
228 class ObjectEdge {
229 OWLObjectPropertyExpression p;
230 Term v;
231
232 public ObjectEdge(AtomicRole r, Term t, boolean inverse) {
233 p = factory.getOWLObjectProperty(IRI.create(r.getIRI()));
234 if(inverse) p = p.getInverseProperty();
235 v = t;
236
237 }
238 }
239
240 class MultiMap<K, V> {
241
242 HashMap<K, Set<V>> map = new HashMap<K, Set<V>>();
243
244 public Set<V> get(K v) {
245 return map.get(v);
246 }
247
248 public boolean isEmpty() {
249 for(Map.Entry<K, Set<V>> entry : map.entrySet())
250 if(!entry.getValue().isEmpty())
251 return false;
252 return true;
253 }
254
255 void add(K key, V value) {
256 Set<V> list = map.get(key);
257 if(list == null)
258 map.put(key, list = new HashSet<V>());
259 list.add(value);
260 }
261
262 }
263}
264
265class Visitor implements OWLClassExpressionVisitorEx<OWLClassExpression> {
266
267 OWLDataFactory factory;
268 Map<Variable, Term> assignment;
269
270 public Visitor(OWLDataFactory factory, Map<Variable, Term> assignment) {
271 this.factory = factory;
272 this.assignment = assignment;
273 }
274
275 @Override
276 public OWLClassExpression visit(OWLClass ce) {
277 // TODO Auto-generated method stub
278 return ce;
279 }
280
281 @Override
282 public OWLClassExpression visit(OWLObjectIntersectionOf ce) {
283 Set<OWLClassExpression> clsExps = new HashSet<OWLClassExpression>();
284 OWLClassExpression newExp;
285 boolean updated = false;
286 for (OWLClassExpression clsExp: ce.asConjunctSet()) {
287 clsExps.add(newExp = clsExp.accept(this));
288 if (newExp != clsExp) updated = true;
289 }
290
291 if (updated) return factory.getOWLObjectIntersectionOf(clsExps);
292 else return ce;
293 }
294
295 @Override
296 public OWLClassExpression visit(OWLObjectUnionOf ce) {
297 // TODO Auto-generated method stub
298 return ce;
299 }
300
301 @Override
302 public OWLClassExpression visit(OWLObjectComplementOf ce) {
303 // TODO Auto-generated method stub
304 return ce;
305 }
306
307 @Override
308 public OWLClassExpression visit(OWLObjectSomeValuesFrom ce) {
309 OWLClassExpression newFiller = ce.getFiller().accept(this);
310 if (newFiller == ce.getFiller()) return ce;
311 return factory.getOWLObjectSomeValuesFrom(ce.getProperty(), newFiller);
312 }
313
314 @Override
315 public OWLClassExpression visit(OWLObjectAllValuesFrom ce) {
316 // TODO Auto-generated method stub
317 return ce;
318 }
319
320 @Override
321 public OWLClassExpression visit(OWLObjectHasValue ce) {
322 if (ce.getValue() instanceof VariableIndividual) {
323 Individual c = (Individual) assignment.get(((VariableIndividual) ce.getValue()).var);
324 OWLIndividual l = factory.getOWLNamedIndividual(IRI.create(c.getIRI()));
325 return factory.getOWLObjectHasValue(ce.getProperty(), l);
326 }
327 return ce;
328 }
329
330 @Override
331 public OWLClassExpression visit(OWLObjectMinCardinality ce) {
332 // TODO Auto-generated method stub
333 return ce;
334 }
335
336 @Override
337 public OWLClassExpression visit(OWLObjectExactCardinality ce) {
338 // TODO Auto-generated method stub
339 return ce;
340 }
341
342 @Override
343 public OWLClassExpression visit(OWLObjectMaxCardinality ce) {
344 // TODO Auto-generated method stub
345 return ce;
346 }
347
348 @Override
349 public OWLClassExpression visit(OWLObjectHasSelf ce) {
350 // TODO Auto-generated method stub
351 return ce;
352 }
353
354 @Override
355 public OWLClassExpression visit(OWLObjectOneOf ce) {
356 // TODO Auto-generated method stub
357 return ce;
358 }
359
360 @Override
361 public OWLClassExpression visit(OWLDataSomeValuesFrom ce) {
362 // TODO Auto-generated method stub
363 return ce;
364 }
365
366 @Override
367 public OWLClassExpression visit(OWLDataAllValuesFrom ce) {
368 // TODO Auto-generated method stub
369 return ce;
370 }
371
372 @Override
373 public OWLClassExpression visit(OWLDataHasValue ce) {
374 if (ce.getValue() instanceof VariableConstant) {
375 Constant c = (Constant) assignment.get(((VariableConstant) ce.getValue()).var);
376 OWLLiteral l = factory.getOWLLiteral(c.getLexicalForm(), c.getDatatypeURI());
377 return factory.getOWLDataHasValue(ce.getProperty(), l);
378 }
379 return ce;
380 }
381
382 @Override
383 public OWLClassExpression visit(OWLDataMinCardinality ce) {
384 // TODO Auto-generated method stub
385 return ce;
386 }
387
388 @Override
389 public OWLClassExpression visit(OWLDataExactCardinality ce) {
390 // TODO Auto-generated method stub
391 return ce;
392 }
393
394 @Override
395 public OWLClassExpression visit(OWLDataMaxCardinality ce) {
396 // TODO Auto-generated method stub
397 return ce;
398 }
399
400} \ No newline at end of file