aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/endomorph/DependencyGraph.java
diff options
context:
space:
mode:
authoryzhou <yujiao.zhou@gmail.com>2015-04-21 10:34:27 +0100
committeryzhou <yujiao.zhou@gmail.com>2015-04-21 10:34:27 +0100
commit9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8 (patch)
tree47511c0fb89dccff0db4b5990522e04f294d795b /src/uk/ac/ox/cs/pagoda/endomorph/DependencyGraph.java
parentb1ac207612ee8b045244253fb94b866104bc34f2 (diff)
downloadACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.tar.gz
ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.zip
initial version
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/endomorph/DependencyGraph.java')
-rw-r--r--src/uk/ac/ox/cs/pagoda/endomorph/DependencyGraph.java295
1 files changed, 295 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/endomorph/DependencyGraph.java b/src/uk/ac/ox/cs/pagoda/endomorph/DependencyGraph.java
new file mode 100644
index 0000000..307fa33
--- /dev/null
+++ b/src/uk/ac/ox/cs/pagoda/endomorph/DependencyGraph.java
@@ -0,0 +1,295 @@
1 package uk.ac.ox.cs.pagoda.endomorph;
2
3import java.util.Arrays;
4import java.util.Collection;
5import java.util.Comparator;
6import java.util.HashMap;
7import java.util.HashSet;
8import java.util.Iterator;
9import java.util.LinkedList;
10import java.util.Map;
11import java.util.Queue;
12import java.util.Set;
13
14import uk.ac.ox.cs.pagoda.summary.Edge;
15import uk.ac.ox.cs.pagoda.summary.Graph;
16import uk.ac.ox.cs.pagoda.summary.Node;
17import uk.ac.ox.cs.pagoda.summary.NodeTuple;
18import uk.ac.ox.cs.pagoda.util.Timer;
19import uk.ac.ox.cs.pagoda.util.Utility;
20
21public class DependencyGraph {
22
23 Set<Clique> entrances = new HashSet<Clique>();
24
25 public Set<Clique> getEntrances() {
26 return entrances;
27 }
28
29 Set<Clique> exits = new HashSet<Clique>();
30
31 public Set<Clique> getExits() {
32 return exits;
33 }
34
35 Map<Clique, Collection<Clique>> outGoingEdges = new HashMap<Clique, Collection<Clique>>();
36
37 public Map<Clique, Collection<Clique>> getOutGoingEdges() {
38 return outGoingEdges;
39 }
40
41 Map<Clique, Collection<Clique>> inComingEdges = new HashMap<Clique, Collection<Clique>>();
42
43 public Map<Clique, Collection<Clique>> getInComingEdges() {
44 return inComingEdges;
45 }
46
47 Graph graph;
48 EndomorphChecker homomorphismChecker;
49 Set<Clique> cliques = new HashSet<Clique>();
50
51 public DependencyGraph(Graph graph) {
52 this.graph = graph;
53 homomorphismChecker = new EndomorphChecker2(graph);
54// homomorphismChecker = new EndomorphChecker1(graph);
55 }
56
57 private int compareNodeTuple(NodeTuple t1, NodeTuple t2) {
58 Iterator<Node> iter1 = t1.getNodes().iterator();
59 Iterator<Node> iter2 = t2.getNodes().iterator();
60 int ret;
61 int index = 0;
62 Node u, v;
63 while (iter1.hasNext()) {
64 u = iter1.next(); v = iter2.next();
65 if (u == null && v == null) {
66 if ((ret = t1.getAnswerTuple().getGroundTerm(index).toString().compareTo(t2.getAnswerTuple().getGroundTerm(index).toString())) != 0)
67 return ret;
68 }
69 else if (u != null && v != null) {
70 if ((ret = compareNode(u, v)) != 0) return ret;
71 }
72 else if (u == null)
73 return 1;
74 else if (v == null)
75 return -1;
76
77 }
78
79 return 0;
80 }
81
82 private int compareNode(Node o1, Node o2) {
83 int result = o1.getConcepts().size() - o2.getConcepts().size();
84 if (result != 0) return dir(result);
85 Edge[] edge1 = graph.getOutGoingEdges(o1), edge2 = graph.getOutGoingEdges(o2);;
86 int len1 = edge1 == null ? 0 : edge1.length, len2 = edge2 == null ? 0 : edge2.length;
87 result = len1 - len2;
88 if (result != 0) return dir(result);
89
90 edge1 = graph.getInComingEdges(o1); edge2 = graph.getInComingEdges(o2);
91 len1 = edge1 == null ? 0 : edge1.length; len2 = edge2 == null ? 0 : edge2.length;
92 result = len1 - len2;
93 if (result != 0) return dir(result);
94
95 else return o1.getLabel().compareTo(o2.getLabel());
96 }
97
98 private int dir(int flag) { return - flag; }
99
100 public void build(Collection<NodeTuple> nodeTuples) {
101
102 if (nodeTuples.isEmpty()) return ;
103
104 Timer t = new Timer();
105
106 NodeTuple[] nodeTupleArray = new NodeTuple[nodeTuples.size()];
107 nodeTuples.toArray(nodeTupleArray);
108
109 Arrays.sort(nodeTupleArray, new Comparator<NodeTuple>() {
110 @Override
111 public int compare(NodeTuple t1, NodeTuple t2) {
112 return compareNodeTuple(t1, t2);
113 }
114 });
115
116 Utility.logDebug(nodeTupleArray[0].toString(),
117 nodeTupleArray[nodeTupleArray.length - 1].toString());
118
119 for (NodeTuple nodeTuple: nodeTupleArray) addNodeTuple(nodeTuple);
120
121 Utility.logDebug("The number of times to call homomorphism checker: " + homomorphismCheckCounter + "(" + outGoingCheckCounter + "," + inComingCheckCounter + ").",
122 "Time to compute endomorphism relation: " + t.duration());
123
124// print();
125
126 topolocialOrder = null;
127 Utility.logInfo("link: " + link);
128 }
129
130 LinkedList<Clique> topolocialOrder = null;
131
132 public LinkedList<Clique> getTopologicalOrder() {
133 if (topolocialOrder != null) return topolocialOrder;
134
135 topolocialOrder = new LinkedList<Clique>();
136 Queue<Clique> toVisit = new LinkedList<Clique>(entrances);
137 Map<Clique, Integer> toVisitedInComingDegree = new HashMap<Clique, Integer>();
138
139 int count;
140 while (!toVisit.isEmpty()) {
141 Clique cu = toVisit.remove();
142 topolocialOrder.add(cu);
143 if (outGoingEdges.containsKey(cu))
144 for (Clique cv: outGoingEdges.get(cu)) {
145 if (toVisitedInComingDegree.containsKey(cv)) {
146 count = toVisitedInComingDegree.get(cv) - 1;
147 toVisitedInComingDegree.put(cv, count);
148 }
149 else
150 toVisitedInComingDegree.put(cv, count = inComingEdges.get(cv).size() - 1);
151 if (count == 0)
152 toVisit.add(cv);
153 }
154 }
155
156 return topolocialOrder;
157 }
158
159 private void addNodeTuple(NodeTuple u) {
160 Queue<Clique> toCompare = new LinkedList<Clique>(entrances);
161
162 Map<Clique, Integer> toVisitedDegree = new HashMap<Clique, Integer>();
163 Collection<Clique> directSuccessors = new LinkedList<Clique>();
164
165 int count;
166 while (!toCompare.isEmpty()) {
167 Clique cv = toCompare.remove();
168 ++outGoingCheckCounter;
169 if (checkHomomorphism(u, cv.representative)) {
170 directSuccessors.add(cv);
171 }
172 else {
173 if (outGoingEdges.containsKey(cv))
174 for (Clique c: outGoingEdges.get(cv)) {
175 if (toVisitedDegree.containsKey(c)) {
176 count = toVisitedDegree.get(c) - 1;
177 toVisitedDegree.put(c, count);
178 }
179 else
180 toVisitedDegree.put(c, count = inComingEdges.get(c).size() - 1);
181
182 if (count == 0)
183 toCompare.add(c);
184 }
185 }
186 }
187
188 if (directSuccessors.size() == 1) {
189 Clique clique = directSuccessors.iterator().next();
190 if (checkHomomorphism(clique.representative, u)) {
191 clique.addNodeTuple(u);
192 return ;
193 }
194 }
195
196 Clique cu = new Clique(u);
197 cliques.add(cu);
198
199 toCompare.clear();
200 Set<Clique> visited = new HashSet<Clique>();
201
202 if (directSuccessors.size() == 0)
203 toCompare.addAll(exits);
204 else {
205 for (Clique cv: directSuccessors)
206 if (inComingEdges.containsKey(cv))
207 for (Clique cw: inComingEdges.get(cv)) {
208 visited.add(cw);
209 toCompare.add(cw);
210 }
211 }
212
213
214
215 Collection<Clique> directPredecessors = new LinkedList<Clique>();
216
217 while (!toCompare.isEmpty()) {
218 Clique cv = toCompare.remove();
219 ++inComingCheckCounter;
220 if (checkHomomorphism(cv.representative, u))
221 directPredecessors.add(cv);
222 else {
223 if (inComingEdges.containsKey(cv))
224 for (Clique c: inComingEdges.get(cv))
225 if (!visited.contains(c)) {
226 visited.add(c);
227 toCompare.add(c);
228 }
229 }
230 }
231
232 for (Clique cv: directSuccessors) {
233 if (entrances.contains(cv)) entrances.remove(cv);
234 link(cu, cv);
235 }
236
237 for (Clique cw: directPredecessors) {
238 if (exits.contains(cw)) exits.remove(cw);
239 link(cw, cu);
240 }
241
242 if (directPredecessors.size() == 0) entrances.add(cu);
243 if (directSuccessors.size() == 0) exits.add(cu);
244 }
245
246 private int homomorphismCheckCounter = 0;
247 private int outGoingCheckCounter = 0, inComingCheckCounter = 0;
248
249 private boolean checkHomomorphism(NodeTuple u, NodeTuple v) {
250 ++homomorphismCheckCounter;
251 homomorphismChecker.setMapping(u, v);
252 try {
253 Node node1, node2;
254 for (Iterator<Node> iter1 = u.getNodes().iterator(), iter2 = v.getNodes().iterator(); iter1.hasNext(); ) {
255 node1 = iter1.next(); node2 = iter2.next();
256 if (node1 != node2 && !homomorphismChecker.check(node1, node2)) {
257// System.out.println(homomorphismChecker.check(node1, node2));
258 return false;
259 }
260 }
261 return true;
262 } finally {
263 homomorphismChecker.clearMappings();
264 }
265 }
266
267 private void link(Clique u, Clique v) {
268 ++link;
269 addToList(outGoingEdges, u, v);
270 addToList(inComingEdges, v, u);
271 }
272
273 private int link = 0;
274
275 private void addToList(Map<Clique, Collection<Clique>> map, Clique u, Clique v) {
276 Collection<Clique> temp;
277 if ((temp = map.get(u)) == null) {
278 temp = new LinkedList<Clique>();
279 map.put(u, temp);
280 }
281 temp.add(v);
282 }
283
284 public void print() {
285 System.out.println("---------- Dependency Graph -------------");
286 for (Clique u: cliques)
287 if (outGoingEdges.containsKey(u))
288 for (Clique v: outGoingEdges.get(u))
289 System.out.println(u + " -> " + v);
290 System.out.println("-----------------------------------------");
291 }
292
293
294}
295