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