diff options
| author | Federico Igne <federico.igne@cs.ox.ac.uk> | 2022-05-10 18:17:06 +0100 |
|---|---|---|
| committer | Federico Igne <federico.igne@cs.ox.ac.uk> | 2022-05-11 12:34:47 +0100 |
| commit | 17bd9beaf7f358a44e5bf36a5855fe6727d506dc (patch) | |
| tree | 47e9310a0cff869d9ec017dcb2c81876407782c8 /src/uk/ac/ox/cs/pagoda/owl/QueryRoller.java | |
| parent | 8651164cd632a5db310b457ce32d4fbc97bdc41c (diff) | |
| download | ACQuA-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/owl/QueryRoller.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/owl/QueryRoller.java | 227 |
1 files changed, 0 insertions, 227 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/owl/QueryRoller.java b/src/uk/ac/ox/cs/pagoda/owl/QueryRoller.java deleted file mode 100644 index f486bbf..0000000 --- a/src/uk/ac/ox/cs/pagoda/owl/QueryRoller.java +++ /dev/null | |||
| @@ -1,227 +0,0 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.owl; | ||
| 2 | |||
| 3 | import java.util.HashMap; | ||
| 4 | import java.util.HashSet; | ||
| 5 | import java.util.LinkedList; | ||
| 6 | import java.util.Map; | ||
| 7 | import java.util.Set; | ||
| 8 | |||
| 9 | import org.semanticweb.HermiT.model.AnnotatedEquality; | ||
| 10 | import org.semanticweb.HermiT.model.Atom; | ||
| 11 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 12 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 13 | import org.semanticweb.HermiT.model.DLClause; | ||
| 14 | import org.semanticweb.HermiT.model.DLPredicate; | ||
| 15 | import org.semanticweb.HermiT.model.Equality; | ||
| 16 | import org.semanticweb.HermiT.model.Inequality; | ||
| 17 | import org.semanticweb.HermiT.model.Term; | ||
| 18 | import org.semanticweb.HermiT.model.Variable; | ||
| 19 | import org.semanticweb.owlapi.model.IRI; | ||
| 20 | import org.semanticweb.owlapi.model.OWLClassExpression; | ||
| 21 | import org.semanticweb.owlapi.model.OWLDataFactory; | ||
| 22 | import org.semanticweb.owlapi.model.OWLNamedIndividual; | ||
| 23 | import org.semanticweb.owlapi.model.OWLObjectHasValue; | ||
| 24 | import org.semanticweb.owlapi.model.OWLObjectPropertyExpression; | ||
| 25 | import org.semanticweb.owlapi.model.OWLObjectSomeValuesFrom; | ||
| 26 | |||
| 27 | import uk.ac.ox.cs.pagoda.summary.Summary; | ||
| 28 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 29 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 30 | |||
| 31 | public class QueryRoller { | ||
| 32 | |||
| 33 | private OWLDataFactory factory; | ||
| 34 | |||
| 35 | protected Map<String, LinkedList<String>> edges, concepts; | ||
| 36 | |||
| 37 | public QueryRoller(OWLDataFactory factory) { | ||
| 38 | this.factory = factory; | ||
| 39 | } | ||
| 40 | |||
| 41 | private OWLClassExpression getOWLClassExpression(String u, String father, Set<String> visited) { | ||
| 42 | visited.add(u); | ||
| 43 | String[] temp; | ||
| 44 | Set<OWLClassExpression> exps = new HashSet<OWLClassExpression>(); | ||
| 45 | OWLObjectPropertyExpression prop; | ||
| 46 | if (concepts.containsKey(u)) | ||
| 47 | for (String concept: concepts.get(u)) { | ||
| 48 | exps.add(factory.getOWLClass(IRI.create(concept))); | ||
| 49 | } | ||
| 50 | |||
| 51 | if (edges.containsKey(u)) | ||
| 52 | for (String pair: edges.get(u)) { | ||
| 53 | temp = pair.split(" "); | ||
| 54 | if (temp[0].startsWith("-")) | ||
| 55 | prop = factory.getOWLObjectInverseOf(factory.getOWLObjectProperty(IRI.create(temp[0].substring(1)))); | ||
| 56 | else | ||
| 57 | prop = factory.getOWLObjectProperty(IRI.create(temp[0])); | ||
| 58 | |||
| 59 | if (temp[1].startsWith("@")) | ||
| 60 | exps.add(factory.getOWLObjectHasValue(prop, factory.getOWLNamedIndividual(IRI.create(OWLHelper.removeAngles(temp[1].substring(1)))))); | ||
| 61 | else if (visited.contains(temp[1])) { | ||
| 62 | if (father != null && father.equals(temp[1])) | ||
| 63 | continue; | ||
| 64 | if (!u.equals(temp[1])) { | ||
| 65 | Utility.logError("The query cannot be rolled up!"); | ||
| 66 | return null; | ||
| 67 | } | ||
| 68 | exps.add(factory.getOWLObjectHasSelf(prop)); | ||
| 69 | } | ||
| 70 | else | ||
| 71 | exps.add(factory.getOWLObjectSomeValuesFrom(prop, getOWLClassExpression(temp[1], u, visited))); | ||
| 72 | } | ||
| 73 | |||
| 74 | if (exps.size() == 0) | ||
| 75 | return factory.getOWLThing(); | ||
| 76 | if (exps.size() == 1) | ||
| 77 | return exps.iterator().next(); | ||
| 78 | else | ||
| 79 | return factory.getOWLObjectIntersectionOf(exps); | ||
| 80 | } | ||
| 81 | |||
| 82 | String currentMainVariable; | ||
| 83 | |||
| 84 | public OWLClassExpression rollUp(DLClause query, String var) { | ||
| 85 | currentMainVariable = var; | ||
| 86 | query = removeSameAs(query); | ||
| 87 | edges = new HashMap<String, LinkedList<String>>(); | ||
| 88 | concepts = new HashMap<String, LinkedList<String>>(); | ||
| 89 | String arg1, arg2, predicate; | ||
| 90 | DLPredicate dlPredicate; | ||
| 91 | Term t1, t2; | ||
| 92 | for (Atom atom: query.getBodyAtoms()) { | ||
| 93 | dlPredicate = atom.getDLPredicate(); | ||
| 94 | if (dlPredicate instanceof AtomicRole) { | ||
| 95 | arg1 = (t1 = atom.getArgument(0)).toString(); | ||
| 96 | arg2 = (t2 = atom.getArgument(1)).toString(); | ||
| 97 | predicate = ((AtomicRole) dlPredicate).getIRI(); | ||
| 98 | if (!predicate.equals(Namespace.RDF_TYPE)) { | ||
| 99 | if (t1 instanceof Variable) | ||
| 100 | if (t2 instanceof Variable) { | ||
| 101 | addEntry(edges, arg1, predicate + " " + arg2); | ||
| 102 | addEntry(edges, arg2, "-" + predicate + " " + arg1); | ||
| 103 | } | ||
| 104 | else | ||
| 105 | addEntry(edges, arg1, predicate + " @" + arg2); | ||
| 106 | else | ||
| 107 | addEntry(edges, arg2, "-" + predicate + " @" + arg1); | ||
| 108 | } | ||
| 109 | else { | ||
| 110 | if (t2 instanceof Variable) return null; | ||
| 111 | addEntry(concepts, arg1, arg2); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | else | ||
| 115 | addEntry(concepts, atom.getArgument(0).toString(), ((AtomicConcept) dlPredicate).getIRI()); | ||
| 116 | } | ||
| 117 | |||
| 118 | return getOWLClassExpression(var, null, new HashSet<String>()); | ||
| 119 | } | ||
| 120 | |||
| 121 | private DLClause removeSameAs(DLClause query) { | ||
| 122 | int equalityStatement = 0; | ||
| 123 | |||
| 124 | Map<Term, Term> ufs = new HashMap<Term, Term>(); | ||
| 125 | for (Atom atom: query.getBodyAtoms()) | ||
| 126 | if (isEquality(atom.getDLPredicate())) { | ||
| 127 | ++equalityStatement; | ||
| 128 | merge(ufs, atom.getArgument(0), atom.getArgument(1)); | ||
| 129 | } | ||
| 130 | |||
| 131 | if (equalityStatement == 0) return query; | ||
| 132 | |||
| 133 | Atom[] bodyAtoms = new Atom[query.getBodyLength() - equalityStatement]; | ||
| 134 | int index = 0; | ||
| 135 | for (Atom atom: query.getBodyAtoms()) | ||
| 136 | if (!isEquality(atom.getDLPredicate())) { | ||
| 137 | if (atom.getArity() == 1) | ||
| 138 | bodyAtoms[index++] = Atom.create(atom.getDLPredicate(), find(ufs, atom.getArgument(0))); | ||
| 139 | else | ||
| 140 | bodyAtoms[index++] = Atom.create(atom.getDLPredicate(), find(ufs, atom.getArgument(0)), find (ufs, atom.getArgument(1))); | ||
| 141 | } | ||
| 142 | |||
| 143 | return DLClause.create(query.getHeadAtoms(), bodyAtoms); | ||
| 144 | } | ||
| 145 | |||
| 146 | private boolean isEquality(DLPredicate p) { | ||
| 147 | return p instanceof Equality || p instanceof AnnotatedEquality || p instanceof Inequality | ||
| 148 | || p.toString().equals(Namespace.EQUALITY_QUOTED) | ||
| 149 | || p.toString().equals(Namespace.EQUALITY_ABBR); | ||
| 150 | } | ||
| 151 | |||
| 152 | private Term find(Map<Term, Term> ufs, Term u) { | ||
| 153 | Term v = u, w; | ||
| 154 | while ((w = ufs.get(v)) != null) v = w; | ||
| 155 | while ((w = ufs.get(u)) != null) { | ||
| 156 | ufs.put(u, v); | ||
| 157 | u = w; | ||
| 158 | } | ||
| 159 | return v; | ||
| 160 | } | ||
| 161 | |||
| 162 | private void merge(Map<Term, Term> ufs, Term u, Term v) { | ||
| 163 | u = find(ufs, u); | ||
| 164 | v = find(ufs, v); | ||
| 165 | if (compare(u, v) <= 0) ufs.put(v, u); | ||
| 166 | else ufs.put(u, v); | ||
| 167 | } | ||
| 168 | |||
| 169 | private int compare(Term u, Term v) { | ||
| 170 | int ret = rank(u) - rank(v); | ||
| 171 | if (ret != 0) return ret; | ||
| 172 | else | ||
| 173 | return u.toString().compareTo(v.toString()); | ||
| 174 | } | ||
| 175 | |||
| 176 | private int rank(Term u) { | ||
| 177 | if (u instanceof Variable) { | ||
| 178 | Variable v = (Variable) u; | ||
| 179 | if (v.getName().equals(currentMainVariable)) | ||
| 180 | return 0; | ||
| 181 | else | ||
| 182 | return 2; | ||
| 183 | } | ||
| 184 | return 1; | ||
| 185 | } | ||
| 186 | |||
| 187 | private void addEntry(Map<String, LinkedList<String>> map, String key, String value) { | ||
| 188 | LinkedList<String> list; | ||
| 189 | if ((list = map.get(key)) == null) { | ||
| 190 | list = new LinkedList<String>(); | ||
| 191 | map.put(key, list); | ||
| 192 | } | ||
| 193 | list.add(value); | ||
| 194 | } | ||
| 195 | |||
| 196 | @Deprecated | ||
| 197 | public OWLClassExpression summarise(Summary sum, OWLClassExpression exp) { | ||
| 198 | if (exp == null) return null; | ||
| 199 | |||
| 200 | Set<OWLClassExpression> exps = exp.asConjunctSet(); | ||
| 201 | if (exps.size() == 1) { | ||
| 202 | OWLClassExpression tempExp = exps.iterator().next(); | ||
| 203 | |||
| 204 | // TODO reference: getOWLClassExpression(String) | ||
| 205 | |||
| 206 | if (tempExp instanceof OWLObjectHasValue) { | ||
| 207 | OWLObjectHasValue hasValue = (OWLObjectHasValue) tempExp; | ||
| 208 | OWLNamedIndividual individual = sum.getRepresentativeIndividual(hasValue.getValue().toStringID()); | ||
| 209 | return factory.getOWLObjectHasValue(hasValue.getProperty(), individual); | ||
| 210 | |||
| 211 | } | ||
| 212 | if (tempExp instanceof OWLObjectSomeValuesFrom) { | ||
| 213 | OWLObjectSomeValuesFrom someValuesFrom = (OWLObjectSomeValuesFrom) tempExp; | ||
| 214 | return factory.getOWLObjectSomeValuesFrom(someValuesFrom.getProperty(), summarise(sum, someValuesFrom.getFiller())); | ||
| 215 | } | ||
| 216 | return tempExp; | ||
| 217 | } | ||
| 218 | |||
| 219 | Set<OWLClassExpression> newExps = new HashSet<OWLClassExpression>(); | ||
| 220 | for (OWLClassExpression clsExp: exps) | ||
| 221 | newExps.add(summarise(sum, clsExp)); | ||
| 222 | |||
| 223 | return factory.getOWLObjectIntersectionOf(newExps); | ||
| 224 | } | ||
| 225 | |||
| 226 | |||
| 227 | } | ||
