changes to get a global flow graph
[IRC.git] / Robust / src / Analysis / SSJava / FlowGraph.java
1 package Analysis.SSJava;
2
3 import java.io.BufferedWriter;
4 import java.io.FileWriter;
5 import java.io.IOException;
6 import java.util.HashMap;
7 import java.util.HashSet;
8 import java.util.Iterator;
9 import java.util.Map;
10 import java.util.Set;
11
12 import IR.ClassDescriptor;
13 import IR.Descriptor;
14 import IR.FieldDescriptor;
15 import IR.MethodDescriptor;
16 import IR.VarDescriptor;
17
18 public class FlowGraph {
19
20   MethodDescriptor md;
21
22   Set<FlowNode> returnNodeSet;
23   FlowNode thisVarNode;
24
25   Map<FlowNode, Set<FlowEdge>> mapFlowNodeToOutEdgeSet;
26
27   Map<NTuple<Location>, FlowNode> mapLocTupleToFlowNode;
28   Map<FlowNode, NTuple<Location>> mapFlowNodeToLocTuple;
29
30   // maps the composite representation of field/var descriptors to infer nodes
31   Map<NTuple<Descriptor>, FlowNode> mapDescTupleToInferNode;
32
33   // maps a paramter descriptor to its index
34   Map<Descriptor, Integer> mapParamDescToIdx;
35
36   // DS for the lattice generation
37   Map<Integer, FlowNode> mapIdxToFlowNode;
38
39   public static int interseed = 0;
40
41   boolean debug = true;
42
43   public FlowGraph(MethodDescriptor md, Map<Descriptor, Integer> mapParamDescToIdx) {
44     this.md = md;
45     this.mapFlowNodeToLocTuple = new HashMap<FlowNode, NTuple<Location>>();
46     this.mapLocTupleToFlowNode = new HashMap<NTuple<Location>, FlowNode>();
47     this.mapDescTupleToInferNode = new HashMap<NTuple<Descriptor>, FlowNode>();
48     this.mapParamDescToIdx = new HashMap<Descriptor, Integer>();
49     this.mapParamDescToIdx.putAll(mapParamDescToIdx);
50     this.returnNodeSet = new HashSet<FlowNode>();
51     this.mapIdxToFlowNode = new HashMap<Integer, FlowNode>();
52     this.mapFlowNodeToOutEdgeSet = new HashMap<FlowNode, Set<FlowEdge>>();
53
54     if (!md.isStatic()) {
55       // create a node for 'this' varialbe
56       // NTuple<Descriptor> thisDescTuple = new NTuple<Descriptor>();
57       // thisDescTuple.add(md.getThis());
58
59       NTuple<Descriptor> thisVarTuple = new NTuple<Descriptor>();
60       thisVarTuple.add(md.getThis());
61       FlowNode thisNode = createNewFlowNode(thisVarTuple);
62       thisNode.setSkeleton(true);
63       thisVarNode = thisNode;
64     }
65
66     setupMapIdxToDesc();
67
68   }
69
70   public Map<NTuple<Descriptor>, FlowNode> getMapDescTupleToInferNode() {
71     return mapDescTupleToInferNode;
72   }
73
74   public void setMapDescTupleToInferNode(Map<NTuple<Descriptor>, FlowNode> in) {
75     this.mapDescTupleToInferNode.putAll(in);
76   }
77
78   public Map<NTuple<Location>, FlowNode> getMapLocTupleToFlowNode() {
79     return mapLocTupleToFlowNode;
80   }
81
82   public void setMapLocTupleToFlowNode(Map<NTuple<Location>, FlowNode> in) {
83     this.mapLocTupleToFlowNode.putAll(in);
84   }
85
86   public void setReturnNodeSet(Set<FlowNode> in) {
87     this.returnNodeSet.addAll(in);
88   }
89
90   public void setThisVarNode(FlowNode thisVarNode) {
91     this.thisVarNode = thisVarNode;
92   }
93
94   public Map<Descriptor, Integer> getMapParamDescToIdx() {
95     return mapParamDescToIdx;
96   }
97
98   public FlowNode createIntermediateNode() {
99     NTuple<Descriptor> tuple = new NTuple<Descriptor>();
100     Descriptor interDesc = new InterDescriptor(LocationInference.INTERLOC + interseed);
101     tuple.add(interDesc);
102     interseed++;
103
104     FlowNode newNode = new FlowNode(tuple);
105     newNode.setIntermediate(true);
106
107     mapDescTupleToInferNode.put(tuple, newNode);
108     // nodeSet.add(newNode);
109
110     System.out.println("create new intermediate node= " + newNode);
111
112     return newNode;
113   }
114
115   private void setupMapIdxToDesc() {
116
117     Set<Descriptor> descSet = mapParamDescToIdx.keySet();
118     for (Iterator iterator = descSet.iterator(); iterator.hasNext();) {
119       Descriptor paramDesc = (Descriptor) iterator.next();
120       int idx = mapParamDescToIdx.get(paramDesc);
121       NTuple<Descriptor> descTuple = new NTuple<Descriptor>();
122       descTuple.add(paramDesc);
123       FlowNode paramNode = getFlowNode(descTuple);
124       mapIdxToFlowNode.put(idx, paramNode);
125       paramNode.setSkeleton(true);
126     }
127
128   }
129
130   public int getNumParameters() {
131     return mapIdxToFlowNode.keySet().size();
132   }
133
134   public FlowNode getParamFlowNode(int idx) {
135     return mapIdxToFlowNode.get(idx);
136   }
137
138   public Set<FlowNode> getNodeSet() {
139     Set<FlowNode> set = new HashSet<FlowNode>();
140     set.addAll(mapDescTupleToInferNode.values());
141     return set;
142   }
143
144   public MethodDescriptor getMethodDescriptor() {
145     return md;
146   }
147
148   public boolean isParamDesc(Descriptor desc) {
149
150     if (mapParamDescToIdx.containsKey(desc)) {
151       int idx = mapParamDescToIdx.get(desc);
152       if (!md.isStatic() && idx == 0) {
153         return false;
154       }
155       return true;
156     }
157
158     return false;
159   }
160
161   public boolean hasEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
162
163     FlowNode fromNode = mapDescTupleToInferNode.get(fromDescTuple);
164     FlowNode toNode = mapDescTupleToInferNode.get(toDescTuple);
165
166     Set<FlowEdge> fromNodeOutEdgeSet = getOutEdgeSet(fromNode);
167     for (Iterator iterator = fromNodeOutEdgeSet.iterator(); iterator.hasNext();) {
168       FlowEdge flowEdge = (FlowEdge) iterator.next();
169       if (flowEdge.getDst().equals(toNode)) {
170         return true;
171       } else {
172         if (hasEdge(flowEdge.getDst().getDescTuple(), toDescTuple)) {
173           return true;
174         }
175       }
176     }
177
178     return false;
179   }
180
181   public Set<FlowEdge> getOutEdgeSetStartingFrom(FlowNode startNode) {
182
183     Descriptor prefixDesc = startNode.getCurrentDescTuple().get(0);
184
185     // returns the set of edges that share the same prefix of startNode
186     Set<FlowEdge> edgeSet = new HashSet<FlowEdge>();
187
188     for (Iterator<Set<FlowEdge>> iter = mapFlowNodeToOutEdgeSet.values().iterator(); iter.hasNext();) {
189       Set<FlowEdge> nodeEdgeSet = iter.next();
190       for (Iterator<FlowEdge> iter2 = nodeEdgeSet.iterator(); iter2.hasNext();) {
191         FlowEdge edge = iter2.next();
192         if (edge.getInitTuple().get(0).equals(prefixDesc)) {
193           edgeSet.add(edge);
194         }
195       }
196     }
197
198     return edgeSet;
199   }
200
201   public Set<FlowEdge> getOutEdgeSet(FlowNode node) {
202     if (!mapFlowNodeToOutEdgeSet.containsKey(node)) {
203       mapFlowNodeToOutEdgeSet.put(node, new HashSet<FlowEdge>());
204     }
205     return mapFlowNodeToOutEdgeSet.get(node);
206   }
207
208   public void addValueFlowEdge(NTuple<Descriptor> fromDescTuple, NTuple<Descriptor> toDescTuple) {
209
210     FlowNode fromNode = getFlowNode(fromDescTuple);
211     FlowNode toNode = getFlowNode(toDescTuple);
212
213     // System.out.println("create an edge from " + fromNode + " to " + toNode);
214
215     int fromTupleSize = fromDescTuple.size();
216     NTuple<Descriptor> curFromTuple = new NTuple<Descriptor>();
217     for (int i = 0; i < fromTupleSize; i++) {
218       Descriptor desc = fromDescTuple.get(i);
219       curFromTuple.add(desc);
220       int toTupleSize = toDescTuple.size();
221       NTuple<Descriptor> curToTuple = new NTuple<Descriptor>();
222       for (int k = 0; k < toTupleSize; k++) {
223         Descriptor toDesc = toDescTuple.get(k);
224         curToTuple.add(toDesc);
225         addFlowEdge(getFlowNode(curFromTuple), getFlowNode(curToTuple), fromDescTuple, toDescTuple);
226       }
227     }
228
229     // int fromTupleSize = fromDescTuple.size();
230     // NTuple<Descriptor> curTuple = new NTuple<Descriptor>();
231     // for (int i = 0; i < fromTupleSize; i++) {
232     // Descriptor desc = fromDescTuple.get(i);
233     // curTuple.add(desc);
234     // addFlowEdge(getFlowNode(curTuple), toNode, fromDescTuple, toDescTuple);
235     // }
236     //
237     // int toTupleSize = toDescTuple.size();
238     // curTuple = new NTuple<Descriptor>();
239     // for (int i = 0; i < toTupleSize; i++) {
240     // Descriptor desc = toDescTuple.get(i);
241     // curTuple.add(desc);
242     // addFlowEdge(fromNode, getFlowNode(curTuple), fromDescTuple, toDescTuple);
243     // }
244
245   }
246
247   private void addFlowEdge(FlowNode fromNode, FlowNode toNode, NTuple<Descriptor> initTuple,
248       NTuple<Descriptor> endTuple) {
249
250     FlowEdge edge = new FlowEdge(fromNode, toNode, initTuple, endTuple);
251     addOutEdge(fromNode, edge);
252
253     // System.out.println("add a new edge=" + edge);
254   }
255
256   private void addOutEdge(FlowNode fromNode, FlowEdge edge) {
257     if (!mapFlowNodeToOutEdgeSet.containsKey(fromNode)) {
258       mapFlowNodeToOutEdgeSet.put(fromNode, new HashSet<FlowEdge>());
259     }
260     mapFlowNodeToOutEdgeSet.get(fromNode).add(edge);
261   }
262
263   public FlowNode getFlowNode(NTuple<Descriptor> descTuple) {
264     if (!mapDescTupleToInferNode.containsKey(descTuple)) {
265       FlowNode node = createNewFlowNode(descTuple);
266       mapDescTupleToInferNode.put(descTuple, node);
267     }
268     return mapDescTupleToInferNode.get(descTuple);
269   }
270
271   public FlowNode getThisVarNode() {
272     return thisVarNode;
273   }
274
275   public FlowNode createNewFlowNode(NTuple<Descriptor> tuple) {
276
277     if (!mapDescTupleToInferNode.containsKey(tuple)) {
278       FlowNode node = new FlowNode(tuple);
279       mapDescTupleToInferNode.put(tuple, node);
280       // nodeSet.add(node);
281
282       mapLocTupleToFlowNode.put(getLocationTuple(node), node);
283
284       if (tuple.size() > 1) {
285         NTuple<Descriptor> baseTuple = tuple.subList(0, tuple.size() - 1);
286         getFlowNode(baseTuple).addFieldNode(node);
287       }
288
289       // System.out.println("Creating new node=" + node);
290       return node;
291     } else {
292       return mapDescTupleToInferNode.get(tuple);
293     }
294
295   }
296
297   public void addReturnFlowNode(NTuple<Descriptor> tuple) {
298
299     if (!mapDescTupleToInferNode.containsKey(tuple)) {
300       createNewFlowNode(tuple);
301     }
302
303     FlowNode node = mapDescTupleToInferNode.get(tuple);
304     node.setReturn(true);
305
306     returnNodeSet.add(node);
307   }
308
309   public Set<FlowNode> getReturnNodeSet() {
310     return returnNodeSet;
311   }
312
313   public Set<FlowNode> getLocalReachFlowNodeSetFrom(FlowNode fn) {
314     Set<FlowNode> set = new HashSet<FlowNode>();
315     recurLocalReachFlowNodeSet(fn, set);
316     return set;
317   }
318
319   private void recurLocalReachFlowNodeSet(FlowNode fn, Set<FlowNode> visited) {
320
321     Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
322     for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
323       FlowEdge edge = (FlowEdge) iterator.next();
324       FlowNode dstNode = edge.getDst();
325
326       if (!visited.contains(dstNode)) {
327         visited.add(dstNode);
328         recurLocalReachFlowNodeSet(dstNode, visited);
329       }
330     }
331
332   }
333
334   private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
335
336     Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
337     for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
338       FlowEdge edge = (FlowEdge) iterator.next();
339
340       if (fn.equals(getFlowNode(edge.getInitTuple()))) {
341         FlowNode dstNode = getFlowNode(edge.getEndTuple());
342         if (!visited.contains(dstNode)) {
343           visited.add(dstNode);
344           getReachFlowNodeSetFrom(dstNode, visited);
345         }
346       }
347     }
348
349   }
350
351   public Set<FlowNode> getReachFlowNodeSetFrom(FlowNode fn) {
352     Set<FlowNode> set = new HashSet<FlowNode>();
353     getReachFlowNodeSetFrom(fn, set);
354     return set;
355   }
356
357   // private void getReachFlowNodeSetFrom(FlowNode fn, Set<FlowNode> visited) {
358   //
359   // for (Iterator iterator = fn.getOutEdgeSet().iterator();
360   // iterator.hasNext();) {
361   // FlowEdge edge = (FlowEdge) iterator.next();
362   //
363   // if (fn.equals(getFlowNode(edge.getInitTuple()))) {
364   //
365   // FlowNode dstNode = getFlowNode(edge.getEndTuple());
366   //
367   // if (!visited.contains(dstNode)) {
368   // visited.add(dstNode);
369   // getReachFlowNodeSetFrom(dstNode, visited);
370   // }
371   // }
372   // }
373   //
374   // }
375
376   public Set<NTuple<Location>> getReachableFlowTupleSet(Set<NTuple<Location>> visited, FlowNode fn) {
377
378     Set<FlowEdge> outEdgeSet = getOutEdgeSet(fn);
379
380     for (Iterator iterator = outEdgeSet.iterator(); iterator.hasNext();) {
381       FlowEdge edge = (FlowEdge) iterator.next();
382
383       if (fn.getDescTuple().equals(edge.getInitTuple())) {
384         FlowNode dstNode = getFlowNode(edge.getEndTuple());
385         NTuple<Location> dstTuple = getLocationTuple(dstNode);
386
387         if (!visited.contains(dstTuple)) {
388           visited.add(dstTuple);
389           visited.addAll(getReachableFlowTupleSet(visited, dstNode));
390         }
391
392       }
393     }
394     return visited;
395   }
396
397   public NTuple<Location> getLocationTuple(NTuple<Descriptor> descTuple) {
398     return getLocationTuple(getFlowNode(descTuple));
399   }
400
401   public NTuple<Location> getLocationTuple(FlowNode fn) {
402
403     if (!mapFlowNodeToLocTuple.containsKey(fn)) {
404       NTuple<Descriptor> descTuple = fn.getDescTuple();
405       NTuple<Location> locTuple = new NTuple<Location>();
406       ClassDescriptor cd = null;
407
408       Descriptor localDesc = fn.getDescTuple().get(0);
409
410       if (fn.isIntermediate()) {
411         Location interLoc = new Location(md, localDesc.getSymbol());
412         interLoc.setLocDescriptor(localDesc);
413         locTuple.add(interLoc);
414       } else if (localDesc.getSymbol().equals(LocationInference.TOPLOC)) {
415         Location topLoc = new Location(md, Location.TOP);
416         topLoc.setLocDescriptor(LocationInference.TOPDESC);
417         locTuple.add(topLoc);
418       } else if (localDesc.getSymbol().equals(LocationInference.GLOBALLOC)) {
419         Location globalLoc = new Location(md, LocationInference.GLOBALLOC);
420         globalLoc.setLocDescriptor(LocationInference.GLOBALDESC);
421         locTuple.add(globalLoc);
422       } else {
423         // normal case
424         for (int i = 0; i < descTuple.size(); i++) {
425           Descriptor curDesc = descTuple.get(i);
426           Location loc;
427           if (i == 0) {
428             loc = new Location(md, curDesc.getSymbol());
429             loc.setLocDescriptor(curDesc);
430             cd = ((VarDescriptor) curDesc).getType().getClassDesc();
431           } else {
432             loc = new Location(cd, curDesc.getSymbol());
433             loc.setLocDescriptor(curDesc);
434
435             if (curDesc instanceof FieldDescriptor) {
436               cd = ((FieldDescriptor) curDesc).getType().getClassDesc();
437             } else {
438               cd = ((LocationDescriptor) curDesc).getEnclosingClassDesc();
439             }
440
441           }
442           locTuple.add(loc);
443         }
444       }
445
446       mapFlowNodeToLocTuple.put(fn, locTuple);
447     }
448     return mapFlowNodeToLocTuple.get(fn);
449   }
450
451   public Set<FlowNode> getIncomingFlowNodeSet(FlowNode node) {
452     Set<FlowNode> set = new HashSet<FlowNode>();
453     getIncomingFlowNodeSet(node, set);
454     return set;
455   }
456
457   public void getIncomingFlowNodeSet(FlowNode node, Set<FlowNode> visited) {
458
459     for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
460       FlowNode curNode = (FlowNode) iterator.next();
461       Set<FlowEdge> edgeSet = getOutEdgeSet(curNode);
462
463       for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
464         FlowEdge flowEdge = (FlowEdge) iterator2.next();
465
466         if (node.equals(getFlowNode(flowEdge.getEndTuple()))) {
467           FlowNode incomingNode = getFlowNode(flowEdge.getInitTuple());
468
469           if (!visited.contains(incomingNode)) {
470             visited.add(incomingNode);
471             getIncomingFlowNodeSet(incomingNode, visited);
472           }
473         }
474       }
475     }
476
477   }
478
479   public Set<NTuple<Location>> getIncomingFlowTupleSet(FlowNode fn) {
480
481     NTuple<Descriptor> dstTuple = fn.getDescTuple();
482
483     Set<NTuple<Location>> set = new HashSet<NTuple<Location>>();
484
485     ClassDescriptor cd = null;
486
487     for (Iterator iterator = getNodeSet().iterator(); iterator.hasNext();) {
488       FlowNode node = (FlowNode) iterator.next();
489
490       Set<FlowEdge> edgeSet = getOutEdgeSet(node);
491       for (Iterator iterator2 = edgeSet.iterator(); iterator2.hasNext();) {
492         FlowEdge flowEdge = (FlowEdge) iterator2.next();
493         if (dstTuple.equals(flowEdge.getEndTuple())) {
494           NTuple<Descriptor> initTuple = flowEdge.getInitTuple();
495           NTuple<Location> locTuple = new NTuple<Location>();
496           for (int i = 0; i < initTuple.size(); i++) {
497             Descriptor d = initTuple.get(i);
498             Location loc;
499             if (i == 0) {
500               loc = new Location(md, d.getSymbol());
501               cd = ((VarDescriptor) d).getType().getClassDesc();
502             } else {
503               loc = new Location(cd, d.getSymbol());
504               cd = ((FieldDescriptor) d).getType().getClassDesc();
505             }
506             locTuple.add(loc);
507           }
508           set.add(locTuple);
509         }
510       }
511     }
512     return set;
513   }
514
515   public boolean isParameter(NTuple<Descriptor> tuple) {
516     // return true if a descriptor tuple is started with a parameter descriptor
517     Descriptor firstIdxDesc = tuple.get(0);
518     return mapParamDescToIdx.containsKey(firstIdxDesc);
519   }
520
521   public int getParamIdx(NTuple<Descriptor> tuple) {
522     Descriptor firstIdxDesc = tuple.get(0);
523     return mapParamDescToIdx.get(firstIdxDesc);
524   }
525
526   public FlowGraph clone() {
527     FlowGraph clone = new FlowGraph(md, mapParamDescToIdx);
528
529     // clone.setNodeSet(getNodeSet());
530     clone.setMapLocTupleToFlowNode(getMapLocTupleToFlowNode());
531     clone.setMapFlowNodeToLocTuple(getMapFlowNodeToLocTuple());
532     clone.setMapDescTupleToInferNode(getMapDescTupleToInferNode());
533
534     clone.setMapFlowNodeToOutEdgeSet(getMapFlowNodeToOutEdgeSet());
535     clone.setReturnNodeSet(getReturnNodeSet());
536     clone.setThisVarNode(getThisVarNode());
537
538     return clone;
539   }
540
541   public Map<FlowNode, NTuple<Location>> getMapFlowNodeToLocTuple() {
542     return mapFlowNodeToLocTuple;
543   }
544
545   public void setMapFlowNodeToLocTuple(Map<FlowNode, NTuple<Location>> in) {
546     this.mapFlowNodeToLocTuple.putAll(in);
547   }
548
549   public Map<FlowNode, Set<FlowEdge>> getMapFlowNodeToOutEdgeSet() {
550     return mapFlowNodeToOutEdgeSet;
551   }
552
553   public void setMapFlowNodeToOutEdgeSet(Map<FlowNode, Set<FlowEdge>> inMap) {
554     Set<FlowNode> keySet = inMap.keySet();
555     for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {
556       FlowNode key = (FlowNode) iterator.next();
557       Set<FlowEdge> newEdgeSet = new HashSet<FlowEdge>();
558       newEdgeSet.addAll(inMap.get(key));
559       mapFlowNodeToOutEdgeSet.put(key, newEdgeSet);
560     }
561   }
562
563   private void drawEdges(FlowNode node, BufferedWriter bw, Set<FlowNode> addedNodeSet,
564       Set<FlowEdge> addedEdgeSet) throws IOException {
565
566     Set<FlowEdge> edgeSet = getOutEdgeSet(node);
567
568     for (Iterator<FlowEdge> iterator = edgeSet.iterator(); iterator.hasNext();) {
569       FlowEdge flowEdge = iterator.next();
570
571       FlowNode u = flowEdge.getSrc();
572       FlowNode v = flowEdge.getDst();
573
574       if (u.getDescTuple().equals(flowEdge.getInitTuple())
575           && v.getDescTuple().equals(flowEdge.getEndTuple())) {
576         // only draw an edge of the actual value flow
577
578         if (!addedEdgeSet.contains(flowEdge)) {
579
580           if (!addedNodeSet.contains(u)) {
581             drawNode(u, bw);
582             addedNodeSet.add(u);
583           }
584           if (!addedNodeSet.contains(v)) {
585             drawNode(v, bw);
586             addedNodeSet.add(v);
587           }
588
589           bw.write("" + u.getID() + " -> " + v.getID() + ";\n");
590           addedEdgeSet.add(flowEdge);
591         }
592       }
593
594     }
595
596   }
597
598   private void drawNode(FlowNode node, BufferedWriter bw) throws IOException {
599     bw.write(node.getID() + " [label=\"" + node.getPrettyID() + "\"]" + ";\n");
600   }
601
602   public void writeGraph() throws java.io.IOException {
603     writeGraph("");
604   }
605
606   public void writeGraph(String suffix) throws java.io.IOException {
607
608     String graphName = "flowgraph_" + md.toString() + suffix;
609     graphName = graphName.replaceAll("[\\W]", "");
610
611     BufferedWriter bw = new BufferedWriter(new FileWriter(graphName + ".dot"));
612     bw.write("digraph " + graphName + " {\n");
613     bw.write("compound=true;\n");
614
615     // then visit every flow node
616
617     // Iterator<FlowNode> iter = nodeSet.iterator();
618     Iterator<FlowNode> iter = getNodeSet().iterator();
619
620     Set<FlowEdge> addedEdgeSet = new HashSet<FlowEdge>();
621     Set<FlowNode> addedNodeSet = new HashSet<FlowNode>();
622
623     while (iter.hasNext()) {
624       FlowNode node = iter.next();
625
626       if (node.getDescTuple().size() == 1) {
627         // here, we just care about the local variable
628         if (node.getFieldNodeSet().size() > 0) {
629           drawSubgraph(node, bw, addedEdgeSet);
630         }
631       }
632       drawEdges(node, bw, addedNodeSet, addedEdgeSet);
633
634     }
635
636     bw.write("}\n");
637     bw.close();
638
639   }
640
641   public boolean constainsNode(FlowNode node) {
642     return getNodeSet().contains(node);
643   }
644
645   private void drawSubgraph(FlowNode node, BufferedWriter bw, Set<FlowEdge> addedSet)
646       throws IOException {
647
648     bw.write("subgraph cluster_" + node.getID() + "{\n");
649     bw.write("label=\"" + node.getPrettyID() + "\";\n");
650
651     Set<FlowNode> fieldNodeSet = node.getFieldNodeSet();
652     for (Iterator iterator = fieldNodeSet.iterator(); iterator.hasNext();) {
653       FlowNode fieldNode = (FlowNode) iterator.next();
654       if (fieldNode.getFieldNodeSet().size() > 0) {
655         drawSubgraph(fieldNode, bw, addedSet);
656       } else {
657         Descriptor desc = fieldNode.getDescTuple().getLastElement();
658         if (desc instanceof VarDescriptor) {
659           VarDescriptor varDesc = (VarDescriptor) desc;
660           if (varDesc.getType().isPrimitive()) {
661             bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
662           }
663         } else if (desc instanceof FieldDescriptor) {
664           FieldDescriptor fieldDesc = (FieldDescriptor) desc;
665           if (fieldDesc.getType().isPrimitive()) {
666             bw.write(fieldNode.getID() + " [label=\"" + fieldNode.getPrettyID() + "\"];\n");
667           }
668         }
669       }
670     }
671
672     bw.write("}\n");
673   }
674 }