diff options
| author | RncLsn <rnc.lsn@gmail.com> | 2015-06-03 15:21:30 +0100 |
|---|---|---|
| committer | RncLsn <rnc.lsn@gmail.com> | 2015-06-03 15:21:30 +0100 |
| commit | b3b822d187a6402a39d30e471fe90a5dfad64312 (patch) | |
| tree | 912e1914610c8cf6103b7b7aef07087ddd7eb62f /src/uk/ac/ox/cs/pagoda/query/rollup/QueryGraph.java | |
| parent | 691964863246bbf6ef9f72cc5e82c83df34f135a (diff) | |
| download | ACQuA-b3b822d187a6402a39d30e471fe90a5dfad64312.tar.gz ACQuA-b3b822d187a6402a39d30e471fe90a5dfad64312.zip | |
Before reintroducing extended queries.
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.java | 222 |
1 files changed, 93 insertions, 129 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 index 58d7add..a567699 100644 --- a/src/uk/ac/ox/cs/pagoda/query/rollup/QueryGraph.java +++ b/src/uk/ac/ox/cs/pagoda/query/rollup/QueryGraph.java | |||
| @@ -1,49 +1,11 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.query.rollup; | 1 | package uk.ac.ox.cs.pagoda.query.rollup; |
| 2 | 2 | ||
| 3 | import java.util.HashMap; | 3 | import org.semanticweb.HermiT.model.*; |
| 4 | import java.util.HashSet; | 4 | import org.semanticweb.owlapi.model.*; |
| 5 | import java.util.Iterator; | ||
| 6 | import java.util.Map; | ||
| 7 | import java.util.Set; | ||
| 8 | |||
| 9 | import org.semanticweb.HermiT.model.Atom; | ||
| 10 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 11 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 12 | import org.semanticweb.HermiT.model.Constant; | ||
| 13 | import org.semanticweb.HermiT.model.Individual; | ||
| 14 | import org.semanticweb.HermiT.model.Term; | ||
| 15 | import org.semanticweb.HermiT.model.Variable; | ||
| 16 | import org.semanticweb.owlapi.model.IRI; | ||
| 17 | import org.semanticweb.owlapi.model.OWLAxiom; | ||
| 18 | import org.semanticweb.owlapi.model.OWLClass; | ||
| 19 | import org.semanticweb.owlapi.model.OWLClassExpression; | ||
| 20 | import org.semanticweb.owlapi.model.OWLClassExpressionVisitorEx; | ||
| 21 | import org.semanticweb.owlapi.model.OWLDataAllValuesFrom; | ||
| 22 | import org.semanticweb.owlapi.model.OWLDataExactCardinality; | ||
| 23 | import org.semanticweb.owlapi.model.OWLDataFactory; | ||
| 24 | import org.semanticweb.owlapi.model.OWLDataHasValue; | ||
| 25 | import org.semanticweb.owlapi.model.OWLDataMaxCardinality; | ||
| 26 | import org.semanticweb.owlapi.model.OWLDataMinCardinality; | ||
| 27 | import org.semanticweb.owlapi.model.OWLDataSomeValuesFrom; | ||
| 28 | import org.semanticweb.owlapi.model.OWLIndividual; | ||
| 29 | import org.semanticweb.owlapi.model.OWLLiteral; | ||
| 30 | import org.semanticweb.owlapi.model.OWLNamedIndividual; | ||
| 31 | import org.semanticweb.owlapi.model.OWLObjectAllValuesFrom; | ||
| 32 | import org.semanticweb.owlapi.model.OWLObjectComplementOf; | ||
| 33 | import org.semanticweb.owlapi.model.OWLObjectExactCardinality; | ||
| 34 | import org.semanticweb.owlapi.model.OWLObjectHasSelf; | ||
| 35 | import org.semanticweb.owlapi.model.OWLObjectHasValue; | ||
| 36 | import org.semanticweb.owlapi.model.OWLObjectIntersectionOf; | ||
| 37 | import org.semanticweb.owlapi.model.OWLObjectMaxCardinality; | ||
| 38 | import org.semanticweb.owlapi.model.OWLObjectMinCardinality; | ||
| 39 | import org.semanticweb.owlapi.model.OWLObjectOneOf; | ||
| 40 | import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; | ||
| 41 | import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; | ||
| 42 | import org.semanticweb.owlapi.model.OWLObjectUnionOf; | ||
| 43 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 44 | |||
| 45 | import uk.ac.ox.cs.pagoda.util.Namespace; | 5 | import uk.ac.ox.cs.pagoda.util.Namespace; |
| 46 | 6 | ||
| 7 | import java.util.*; | ||
| 8 | |||
| 47 | public class QueryGraph { | 9 | public class QueryGraph { |
| 48 | 10 | ||
| 49 | Set<Variable> freeVars = new HashSet<Variable>(); | 11 | Set<Variable> freeVars = new HashSet<Variable>(); |
| @@ -82,168 +44,170 @@ public class QueryGraph { | |||
| 82 | 44 | ||
| 83 | rollup(); | 45 | rollup(); |
| 84 | } | 46 | } |
| 85 | |||
| 86 | private void updateExistentiallyVariables(Variable argumentVariable) { | ||
| 87 | if (freeVars.contains(argumentVariable)) return ; | ||
| 88 | existVars.add(argumentVariable); | ||
| 89 | } | ||
| 90 | 47 | ||
| 91 | public void createEdges(Term u, AtomicRole r, Term v) { | 48 | public void createEdges(Term u, AtomicRole r, Term v) { |
| 92 | if (ontology.containsDataPropertyInSignature(IRI.create(r.getIRI()))) { | 49 | if(ontology.containsDataPropertyInSignature(IRI.create(r.getIRI()))) { |
| 93 | // edges.add(u, new DataEdge(r, v)); | 50 | // edges.add(u, new DataEdge(r, v)); |
| 94 | Constant c = (Constant) v; | 51 | Constant c = (Constant) v; |
| 95 | OWLLiteral l = factory.getOWLLiteral(c.getLexicalForm(), c.getDatatypeURI()); | 52 | OWLLiteral l = factory.getOWLLiteral(c.getLexicalForm(), c.getDatatypeURI()); |
| 96 | concepts.add(u, factory.getOWLDataHasValue(factory.getOWLDataProperty(IRI.create(r.getIRI())), l)); | 53 | concepts.add(u, factory.getOWLDataHasValue(factory.getOWLDataProperty(IRI.create(r.getIRI())), l)); |
| 97 | } | 54 | } |
| 98 | else { | 55 | else { |
| 99 | boolean rollable = existVars.contains(u) || existVars.contains(v); | 56 | boolean rollable = existVars.contains(u) || existVars.contains(v); |
| 100 | 57 | ||
| 101 | ObjectEdge edge = new ObjectEdge(r, v, false); | 58 | ObjectEdge edge = new ObjectEdge(r, v, false); |
| 102 | if (rollable) { | 59 | if(rollable) { |
| 103 | rollable_edges.add(u, edge); | 60 | rollable_edges.add(u, edge); |
| 104 | edge = new ObjectEdge(r, u, true); | 61 | edge = new ObjectEdge(r, u, true); |
| 105 | rollable_edges.add(v, edge); | 62 | rollable_edges.add(v, edge); |
| 106 | } | 63 | } |
| 107 | else edges.add(u, edge); | 64 | else edges.add(u, edge); |
| 108 | 65 | ||
| 109 | } | 66 | } |
| 110 | } | 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<OWLAxiom> getAssertions(Map<Variable, Term> assignment) { | ||
| 85 | if(!rollable_edges.isEmpty()) return null; | ||
| 86 | |||
| 87 | OWLIndividual sub; | ||
| 88 | Visitor visitor = new Visitor(factory, assignment); | ||
| 89 | Set<OWLAxiom> axioms = getPropertyAssertions(assignment); | ||
| 90 | for(Map.Entry<Term, Set<OWLClassExpression>> entry : concepts.map.entrySet()) { | ||
| 91 | if(existVars.contains(entry.getKey())) continue; | ||
| 92 | sub = factory.getOWLNamedIndividual(IRI.create(getIndividual(entry.getKey(), assignment).getIRI())); | ||
| 93 | for(OWLClassExpression clsExp : entry.getValue()) { | ||
| 94 | axioms.add(factory.getOWLClassAssertionAxiom(clsExp.accept(visitor), sub)); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | return axioms; | ||
| 98 | } | ||
| 99 | |||
| 100 | private void updateExistentiallyVariables(Variable argumentVariable) { | ||
| 101 | if(freeVars.contains(argumentVariable)) return; | ||
| 102 | existVars.add(argumentVariable); | ||
| 103 | } | ||
| 111 | 104 | ||
| 112 | private void rollup() { | 105 | private void rollup() { |
| 113 | for (boolean updated = true; updated; ) { | 106 | for (boolean updated = true; updated; ) { |
| 114 | updated = false; | 107 | updated = false; |
| 115 | 108 | ||
| 116 | Set<ObjectEdge> set; | 109 | Set<ObjectEdge> set; |
| 117 | for (Variable var: existVars) { | 110 | for (Variable var: existVars) { |
| 118 | if ((set = rollable_edges.map.get(var)) != null && set.size() == 1) { | 111 | if ((set = rollable_edges.map.get(var)) != null && set.size() == 1) { |
| 119 | updated = true; | 112 | updated = true; |
| 120 | ObjectEdge edge = set.iterator().next(); | 113 | ObjectEdge edge = set.iterator().next(); |
| 121 | rollupEdge(edge.v, edge.p.getInverseProperty().getSimplified(), var, true); | 114 | rollupEdge(edge.v, edge.p.getInverseProperty().getSimplified(), var, true); |
| 122 | set.clear(); | 115 | set.clear(); |
| 123 | } | 116 | } |
| 124 | } | 117 | } |
| 125 | if (updated) continue; | 118 | if(updated) continue; |
| 126 | 119 | ||
| 127 | for (Variable var: existVars) { | 120 | for (Variable var: existVars) { |
| 128 | set = rollable_edges.map.get(var); | 121 | set = rollable_edges.map.get(var); |
| 129 | if (set == null) continue; | 122 | if(set == null) continue; |
| 130 | for (Iterator<ObjectEdge> iter = set.iterator(); iter.hasNext(); ) { | 123 | for (Iterator<ObjectEdge> iter = set.iterator(); iter.hasNext(); ) { |
| 131 | ObjectEdge edge = iter.next(); | 124 | ObjectEdge edge = iter.next(); |
| 132 | if (constants.contains(edge.v) || freeVars.contains(edge.v)) { | 125 | if (constants.contains(edge.v) || freeVars.contains(edge.v)) { |
| 133 | updated = true; | 126 | updated = true; |
| 134 | rollupEdge(var, edge.p, edge.v, false); | 127 | rollupEdge(var, edge.p, edge.v, false); |
| 135 | iter.remove(); | 128 | iter.remove(); |
| 136 | } | 129 | } |
| 137 | } | 130 | } |
| 138 | } | 131 | } |
| 139 | } | 132 | } |
| 140 | 133 | ||
| 141 | } | 134 | } |
| 142 | 135 | ||
| 143 | private void rollupEdge(Term u, OWLObjectPropertyExpression op, Term v, boolean inverse) { | 136 | private void rollupEdge(Term u, OWLObjectPropertyExpression op, Term v, boolean inverse) { |
| 144 | if (existVars.contains(v)) { | 137 | if (existVars.contains(v)) { |
| 145 | concepts.add(u, factory.getOWLObjectSomeValuesFrom(op, factory.getOWLObjectIntersectionOf(concepts.get(v)))); | 138 | concepts.add(u, factory.getOWLObjectSomeValuesFrom(op, factory.getOWLObjectIntersectionOf(concepts.get(v)))); |
| 146 | } | 139 | } |
| 147 | else { | 140 | else { |
| 148 | OWLIndividual obj = getOWLIndividual(v); | 141 | OWLIndividual obj = getOWLIndividual(v); |
| 149 | concepts.add(u, factory.getOWLObjectHasValue(op, obj)); | 142 | concepts.add(u, factory.getOWLObjectHasValue(op, obj)); |
| 150 | } | 143 | } |
| 151 | 144 | ||
| 152 | if (inverse) | 145 | if(inverse) |
| 153 | removeRollableEdge(u, op, v); | 146 | removeRollableEdge(u, op, v); |
| 154 | else | 147 | else |
| 155 | removeRollableEdge(v, op.getInverseProperty().getSimplified(), u); | 148 | removeRollableEdge(v, op.getInverseProperty().getSimplified(), u); |
| 156 | } | 149 | } |
| 157 | 150 | ||
| 158 | private void removeRollableEdge(Term u, OWLObjectPropertyExpression op, Term v) { | 151 | private void removeRollableEdge(Term u, OWLObjectPropertyExpression op, Term v) { |
| 159 | Set<ObjectEdge> set = rollable_edges.get(u); | 152 | Set<ObjectEdge> set = rollable_edges.get(u); |
| 160 | ObjectEdge edge; | 153 | ObjectEdge edge; |
| 161 | if (set != null) | 154 | if (set != null) |
| 162 | for (Iterator<ObjectEdge> iter = set.iterator(); iter.hasNext(); ) { | 155 | for (Iterator<ObjectEdge> iter = set.iterator(); iter.hasNext(); ) { |
| 163 | edge = iter.next(); | 156 | edge = iter.next(); |
| 164 | if (edge.p.equals(op) && edge.v.equals(v)) iter.remove(); | 157 | if(edge.p.equals(op) && edge.v.equals(v)) iter.remove(); |
| 165 | } | 158 | } |
| 166 | } | 159 | } |
| 167 | 160 | ||
| 168 | OWLNamedIndividual getOWLIndividual(Term t) { | 161 | OWLNamedIndividual getOWLIndividual(Term t) { |
| 169 | if (freeVars.contains(t)) | 162 | if (freeVars.contains(t)) |
| 170 | return new VariableIndividual((Variable) t); | 163 | return new VariableIndividual((Variable) t); |
| 171 | else if (t instanceof Variable) | 164 | else if (t instanceof Variable) |
| 172 | return null; | 165 | return null; |
| 173 | else | 166 | else |
| 174 | return factory.getOWLNamedIndividual(IRI.create(((Individual) t).getIRI())); | 167 | return factory.getOWLNamedIndividual(IRI.create(((Individual) t).getIRI())); |
| 168 | } | ||
| 169 | |||
| 170 | private Individual getIndividual(Term key, Map<Variable, Term> assignment) { | ||
| 171 | if(key instanceof Individual) | ||
| 172 | return (Individual) key; | ||
| 173 | else | ||
| 174 | return (Individual) assignment.get(key); | ||
| 175 | } | 175 | } |
| 176 | 176 | ||
| 177 | class ObjectEdge { | 177 | class ObjectEdge { |
| 178 | OWLObjectPropertyExpression p; | 178 | OWLObjectPropertyExpression p; |
| 179 | Term v; | 179 | Term v; |
| 180 | 180 | ||
| 181 | public ObjectEdge(AtomicRole r, Term t, boolean inverse) { | 181 | public ObjectEdge(AtomicRole r, Term t, boolean inverse) { |
| 182 | p = factory.getOWLObjectProperty(IRI.create(r.getIRI())); | 182 | p = factory.getOWLObjectProperty(IRI.create(r.getIRI())); |
| 183 | if (inverse) p = p.getInverseProperty(); | 183 | if(inverse) p = p.getInverseProperty(); |
| 184 | v = t; | 184 | v = t; |
| 185 | 185 | ||
| 186 | } | 186 | } |
| 187 | } | 187 | } |
| 188 | 188 | ||
| 189 | class MultiMap<K, V> { | 189 | class MultiMap<K, V> { |
| 190 | 190 | ||
| 191 | HashMap<K, Set<V>> map = new HashMap<K, Set<V>>(); | 191 | HashMap<K, Set<V>> map = new HashMap<K, Set<V>>(); |
| 192 | |||
| 193 | void add(K key, V value) { | ||
| 194 | Set<V> list = map.get(key); | ||
| 195 | if (list == null) | ||
| 196 | map.put(key, list = new HashSet<V>()); | ||
| 197 | list.add(value); | ||
| 198 | } | ||
| 199 | 192 | ||
| 200 | public Set<V> get(K v) { | 193 | public Set<V> get(K v) { |
| 201 | return map.get(v); | 194 | return map.get(v); |
| 202 | } | 195 | } |
| 203 | 196 | ||
| 204 | public boolean isEmpty() { | 197 | public boolean isEmpty() { |
| 205 | for (Map.Entry<K, Set<V>> entry: map.entrySet()) | 198 | for(Map.Entry<K, Set<V>> entry : map.entrySet()) |
| 206 | if (!entry.getValue().isEmpty()) | 199 | if(!entry.getValue().isEmpty()) |
| 207 | return false; | 200 | return false; |
| 208 | return true; | 201 | return true; |
| 209 | } | 202 | } |
| 210 | |||
| 211 | } | ||
| 212 | 203 | ||
| 213 | public Set<OWLAxiom> getPropertyAssertions(Map<Variable, Term> assignment) { | 204 | void add(K key, V value) { |
| 214 | OWLIndividual sub, obj; | 205 | Set<V> list = map.get(key); |
| 215 | Set<OWLAxiom> axioms = new HashSet<OWLAxiom>(); | 206 | if(list == null) |
| 216 | for (Map.Entry<Term, Set<ObjectEdge>> entry: edges.map.entrySet()) { | 207 | map.put(key, list = new HashSet<V>()); |
| 217 | sub = factory.getOWLNamedIndividual(IRI.create(getIndividual(entry.getKey(), assignment).getIRI())); | 208 | list.add(value); |
| 218 | for (ObjectEdge edge: entry.getValue()) { | ||
| 219 | obj = factory.getOWLNamedIndividual(IRI.create(getIndividual(edge.v, assignment).getIRI())); | ||
| 220 | axioms.add(factory.getOWLObjectPropertyAssertionAxiom(edge.p, sub, obj)); | ||
| 221 | } | ||
| 222 | } | ||
| 223 | return axioms; | ||
| 224 | } | ||
| 225 | |||
| 226 | public Set<OWLAxiom> getAssertions(Map<Variable, Term> assignment) { | ||
| 227 | if (!rollable_edges.isEmpty()) return null; | ||
| 228 | |||
| 229 | OWLIndividual sub; | ||
| 230 | Visitor visitor = new Visitor(factory, assignment); | ||
| 231 | Set<OWLAxiom> axioms = getPropertyAssertions(assignment); | ||
| 232 | for (Map.Entry<Term, Set<OWLClassExpression>> entry: concepts.map.entrySet()) { | ||
| 233 | if (existVars.contains(entry.getKey())) continue; | ||
| 234 | sub = factory.getOWLNamedIndividual(IRI.create(getIndividual(entry.getKey(), assignment).getIRI())); | ||
| 235 | for (OWLClassExpression clsExp: entry.getValue()) { | ||
| 236 | axioms.add(factory.getOWLClassAssertionAxiom(clsExp.accept(visitor), sub)); | ||
| 237 | } | ||
| 238 | } | 209 | } |
| 239 | return axioms; | ||
| 240 | } | ||
| 241 | 210 | ||
| 242 | private Individual getIndividual(Term key, Map<Variable, Term> assignment) { | ||
| 243 | if (key instanceof Individual) | ||
| 244 | return (Individual) key; | ||
| 245 | else | ||
| 246 | return (Individual) assignment.get(key); | ||
| 247 | } | 211 | } |
| 248 | } | 212 | } |
| 249 | 213 | ||
