diff options
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/constraints/PredicateDependency.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/constraints/PredicateDependency.java | 235 |
1 files changed, 235 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/constraints/PredicateDependency.java b/src/uk/ac/ox/cs/pagoda/constraints/PredicateDependency.java new file mode 100644 index 0000000..b201918 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/constraints/PredicateDependency.java | |||
| @@ -0,0 +1,235 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.constraints; | ||
| 2 | |||
| 3 | import java.util.Collection; | ||
| 4 | import java.util.HashMap; | ||
| 5 | import java.util.HashSet; | ||
| 6 | import java.util.LinkedList; | ||
| 7 | import java.util.Map; | ||
| 8 | import java.util.Queue; | ||
| 9 | import java.util.Set; | ||
| 10 | |||
| 11 | import org.semanticweb.HermiT.model.AnnotatedEquality; | ||
| 12 | import org.semanticweb.HermiT.model.AtLeastConcept; | ||
| 13 | import org.semanticweb.HermiT.model.AtLeastDataRange; | ||
| 14 | import org.semanticweb.HermiT.model.Atom; | ||
| 15 | import org.semanticweb.HermiT.model.AtomicConcept; | ||
| 16 | import org.semanticweb.HermiT.model.AtomicNegationConcept; | ||
| 17 | import org.semanticweb.HermiT.model.AtomicRole; | ||
| 18 | import org.semanticweb.HermiT.model.DLClause; | ||
| 19 | import org.semanticweb.HermiT.model.DLPredicate; | ||
| 20 | import org.semanticweb.HermiT.model.Equality; | ||
| 21 | import org.semanticweb.HermiT.model.Inequality; | ||
| 22 | import org.semanticweb.HermiT.model.InverseRole; | ||
| 23 | |||
| 24 | import uk.ac.ox.cs.pagoda.rules.OverApproxExist; | ||
| 25 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 26 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 27 | |||
| 28 | |||
| 29 | public class PredicateDependency extends DependencyGraph<DLPredicate> { | ||
| 30 | |||
| 31 | Collection<DLClause> m_clauses; | ||
| 32 | Map<PredicatePair, LinkedList<DLClause>> edgeLabels = new HashMap<PredicatePair, LinkedList<DLClause>>(); | ||
| 33 | |||
| 34 | public PredicateDependency(Collection<DLClause> clauses) { | ||
| 35 | m_clauses = clauses; | ||
| 36 | build(); | ||
| 37 | } | ||
| 38 | |||
| 39 | @Override | ||
| 40 | protected void build() { | ||
| 41 | update(m_clauses); | ||
| 42 | |||
| 43 | addLink(equality, AtomicConcept.NOTHING); | ||
| 44 | addLink(inequality, AtomicConcept.NOTHING); | ||
| 45 | } | ||
| 46 | |||
| 47 | private void addEdgeLabel(DLPredicate body, DLPredicate head, DLClause clause) { | ||
| 48 | PredicatePair key = new PredicatePair(body, head); | ||
| 49 | LinkedList<DLClause> value; | ||
| 50 | if ((value = edgeLabels.get(key)) == null) | ||
| 51 | edgeLabels.put(key, value = new LinkedList<DLClause>()); | ||
| 52 | value.add(clause); | ||
| 53 | } | ||
| 54 | |||
| 55 | private void addLinks4Negation(AtomicConcept c, DLClause clause) { | ||
| 56 | addLink(c, AtomicConcept.NOTHING); | ||
| 57 | addEdgeLabel(c, AtomicConcept.NOTHING, clause); | ||
| 58 | String iri = c.getIRI(); | ||
| 59 | addLink(c = AtomicConcept.create(iri.substring(0, iri.length() - 4)), AtomicConcept.NOTHING); | ||
| 60 | addEdgeLabel(c, AtomicConcept.NOTHING, clause); | ||
| 61 | } | ||
| 62 | |||
| 63 | public Set<DLPredicate> collectPredicate(Atom[] atoms) { | ||
| 64 | Set<DLPredicate> predicates = new HashSet<DLPredicate>(); | ||
| 65 | for (Atom atom: atoms) | ||
| 66 | predicates.addAll(getAtomicPredicates(atom.getDLPredicate())); | ||
| 67 | return predicates; | ||
| 68 | } | ||
| 69 | |||
| 70 | private static final DLPredicate equality = AtomicRole.create(Namespace.EQUALITY); | ||
| 71 | private static final DLPredicate inequality = AtomicRole.create(Namespace.INEQUALITY); | ||
| 72 | |||
| 73 | private Set<DLPredicate> getAtomicPredicates(DLPredicate predicate) { | ||
| 74 | Set<DLPredicate> predicates = new HashSet<DLPredicate>(); | ||
| 75 | if (predicate instanceof AtLeastConcept) | ||
| 76 | predicates.addAll(getAtomicPredicates((AtLeastConcept) predicate)); | ||
| 77 | else { | ||
| 78 | if ((predicate = getAtomicPredicate(predicate)) != null) | ||
| 79 | predicates.add(predicate); | ||
| 80 | } | ||
| 81 | return predicates; | ||
| 82 | } | ||
| 83 | |||
| 84 | private Set<DLPredicate> getAtomicPredicates(AtLeastConcept alc) { | ||
| 85 | Set<DLPredicate> set = new HashSet<DLPredicate>(); | ||
| 86 | if (alc.getOnRole() instanceof AtomicRole) | ||
| 87 | set.add((AtomicRole) alc.getOnRole()); | ||
| 88 | else | ||
| 89 | set.add(((InverseRole) alc.getOnRole()).getInverseOf()); | ||
| 90 | |||
| 91 | if (alc.getToConcept() instanceof AtomicConcept) | ||
| 92 | if (alc.getToConcept().equals(AtomicConcept.THING)); | ||
| 93 | else set.add((AtomicConcept) alc.getToConcept()); | ||
| 94 | else | ||
| 95 | set.add(OverApproxExist.getNegationConcept(((AtomicNegationConcept) alc.getToConcept()).getNegatedAtomicConcept())); | ||
| 96 | return set; | ||
| 97 | } | ||
| 98 | |||
| 99 | private DLPredicate getAtomicPredicate(DLPredicate p) { | ||
| 100 | if (p instanceof Equality || p instanceof AnnotatedEquality) | ||
| 101 | return equality; | ||
| 102 | if (p instanceof Inequality) | ||
| 103 | return inequality; | ||
| 104 | if (p instanceof AtomicConcept) | ||
| 105 | if (p.equals(AtomicConcept.THING)) | ||
| 106 | return null; | ||
| 107 | else return p; | ||
| 108 | if (p instanceof AtomicRole) | ||
| 109 | return p; | ||
| 110 | if (p instanceof AtLeastDataRange) { | ||
| 111 | AtLeastDataRange aldr = (AtLeastDataRange) p; | ||
| 112 | if (aldr.getOnRole() instanceof AtomicRole) | ||
| 113 | return (AtomicRole) aldr.getOnRole(); | ||
| 114 | else | ||
| 115 | return ((InverseRole) aldr.getOnRole()).getInverseOf(); | ||
| 116 | } | ||
| 117 | Utility.logDebug("Unknown DLPredicate in PredicateDependency: " + p); | ||
| 118 | return null; | ||
| 119 | } | ||
| 120 | |||
| 121 | public Set<DLClause> pathTo(DLPredicate p) { | ||
| 122 | Set<DLClause> rules = new HashSet<DLClause>(); | ||
| 123 | Set<DLPredicate> visited = new HashSet<DLPredicate>(); | ||
| 124 | |||
| 125 | Queue<DLPredicate> queue = new LinkedList<DLPredicate>(); | ||
| 126 | queue.add(p); | ||
| 127 | visited.add(p); | ||
| 128 | |||
| 129 | Set<DLPredicate> edge; | ||
| 130 | Collection<DLClause> clauses; | ||
| 131 | |||
| 132 | while (!queue.isEmpty()) { | ||
| 133 | if ((edge = reverseEdges.get(p = queue.poll())) != null) { | ||
| 134 | for (DLPredicate pred: edge) { | ||
| 135 | if (!visited.contains(pred)) { | ||
| 136 | queue.add(pred); | ||
| 137 | visited.add(pred); | ||
| 138 | } | ||
| 139 | clauses = edgeLabelsBetween(pred, p); | ||
| 140 | if (clauses != null) rules.addAll(clauses); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | } | ||
| 144 | return rules; | ||
| 145 | } | ||
| 146 | |||
| 147 | private LinkedList<DLClause> edgeLabelsBetween(DLPredicate p, DLPredicate q) { | ||
| 148 | PredicatePair pair = new PredicatePair(p, q); | ||
| 149 | return edgeLabels.get(pair); | ||
| 150 | } | ||
| 151 | |||
| 152 | Set<DLPredicate> reachableToBottom = null; | ||
| 153 | |||
| 154 | public Set<DLClause> pathToBottom(DLPredicate p) { | ||
| 155 | if (reachableToBottom == null) { | ||
| 156 | reachableToBottom = getAncesters(AtomicConcept.NOTHING); | ||
| 157 | reachableToBottom.add(AtomicConcept.NOTHING); | ||
| 158 | } | ||
| 159 | |||
| 160 | Set<DLClause> rules = new HashSet<DLClause>(); | ||
| 161 | Set<DLPredicate> visited = new HashSet<DLPredicate>(); | ||
| 162 | |||
| 163 | Queue<DLPredicate> queue = new LinkedList<DLPredicate>(); | ||
| 164 | queue.add(p); | ||
| 165 | visited.add(p); | ||
| 166 | |||
| 167 | Set<DLPredicate> edge; | ||
| 168 | Collection<DLClause> clauses; | ||
| 169 | |||
| 170 | while (!queue.isEmpty()) { | ||
| 171 | if ((edge = edges.get(p = queue.poll())) != null) { | ||
| 172 | for (DLPredicate next: edge) | ||
| 173 | if (reachableToBottom.contains(next)) { | ||
| 174 | if (!visited.contains(next)) { | ||
| 175 | queue.add(next); | ||
| 176 | visited.add(next); | ||
| 177 | } | ||
| 178 | clauses = edgeLabelsBetween(p, next); | ||
| 179 | if (clauses != null) rules.addAll(clauses); | ||
| 180 | } | ||
| 181 | } | ||
| 182 | } | ||
| 183 | return rules; | ||
| 184 | } | ||
| 185 | |||
| 186 | public void update(Collection<DLClause> clauses) { | ||
| 187 | Set<DLPredicate> headPredicates, bodyPredicates; | ||
| 188 | |||
| 189 | for (DLClause clause: clauses) { | ||
| 190 | headPredicates = collectPredicate(clause.getHeadAtoms()); | ||
| 191 | bodyPredicates = collectPredicate(clause.getBodyAtoms()); | ||
| 192 | |||
| 193 | for (DLPredicate body: bodyPredicates) | ||
| 194 | for (DLPredicate head: headPredicates) { | ||
| 195 | addLink(body, head); | ||
| 196 | addEdgeLabel(body, head, clause); | ||
| 197 | |||
| 198 | if (body instanceof AtomicConcept && body.toString().contains("_neg")) | ||
| 199 | addLinks4Negation((AtomicConcept) body, clause); | ||
| 200 | if (head instanceof AtomicConcept && head.toString().contains("_neg")) | ||
| 201 | addLinks4Negation((AtomicConcept) head, clause); | ||
| 202 | } | ||
| 203 | |||
| 204 | for (DLPredicate body: bodyPredicates) | ||
| 205 | addLink(equality, body); | ||
| 206 | |||
| 207 | for (DLPredicate head: headPredicates) | ||
| 208 | addLink(equality, head); | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | } | ||
| 213 | |||
| 214 | class PredicatePair { | ||
| 215 | |||
| 216 | DLPredicate p, q; | ||
| 217 | |||
| 218 | public PredicatePair(DLPredicate p, DLPredicate q) { | ||
| 219 | this.p = p; this.q = q; | ||
| 220 | } | ||
| 221 | |||
| 222 | public int hashCode() { | ||
| 223 | return p.hashCode() * 1997 + q.hashCode(); | ||
| 224 | } | ||
| 225 | |||
| 226 | public boolean equals(Object o) { | ||
| 227 | if (!(o instanceof PredicatePair)) return false; | ||
| 228 | PredicatePair thatPair = (PredicatePair) o; | ||
| 229 | return p.equals(thatPair.p) && q.equals(thatPair.q); | ||
| 230 | } | ||
| 231 | |||
| 232 | public String toString() { | ||
| 233 | return "<" + p.toString() + "," + q.toString() + ">"; | ||
| 234 | } | ||
| 235 | } | ||
