1 17 18 19 package SOFA.SOFAnode.Util.DFSRChecker.visualization; 20 21 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.io.FileNotFoundException ; 25 26 import SOFA.SOFAnode.Util.DFSRChecker.node.ActionRepository; 27 import SOFA.SOFAnode.Util.DFSRChecker.node.EventNode; 28 import SOFA.SOFAnode.Util.DFSRChecker.node.TreeNode; 29 import SOFA.SOFAnode.Util.DFSRChecker.parser.Debug; 30 31 34 public class DotVisualizer { 35 36 37 public DotVisualizer(ActionRepository repository) { 38 this.repository = repository; 39 this.legend = new String (); 40 } 41 42 47 public int visualize(TreeNode node, String outputfile) { 48 try { 49 explicitidx = 0; 50 legend = ""; 51 FileOutputStream of = new FileOutputStream (outputfile); 52 53 of.write("digraph G {\n size = \"7,11\";\n".getBytes()); 54 55 visualizeNode(node, of, 0); 56 57 of.write((" label=\"" + legend + "\";\n").getBytes()); 58 59 of.write("\n}".getBytes()); 60 of.close(); 61 Debug.println("Parse tree written to: " + outputfile); 62 63 return 0; 64 } catch (FileNotFoundException e) { 65 return 1; 66 } catch (SecurityException e) { 67 return 2; 68 } catch (IOException e) { 69 return 3; 70 } 71 } 72 73 79 private int visualizeNode(TreeNode node, FileOutputStream of, int nodeindex) throws IOException { 80 String [] nodenames = node.getTypeName(); 81 String nodename = nodenames[0]; 82 TreeNode[] children; 83 84 String purename = new String (nodename); 85 86 nodename = nodename + nodeindex++; 87 88 if (purename.equals("Event")) { 89 nodenames[1] = repository.getItemString(((EventNode) node).getEventIndex()); 90 of.write((" " + nodename + " [label=\"" + nodenames[1] + "\", fontname=\"Courier\"];\n").getBytes()); 91 } else if (purename.equals("Explicit")) { 92 of.write((" " + nodename + " [label=\"Exp" + explicitidx + "\", fontname=\"Courier\", shape=invhouse, style=filled, fillcolor=\"grey85\"];\n").getBytes()); 93 legend += "Exp" + explicitidx + ": " + node.protocol + "\\n"; 94 95 ++explicitidx; 96 } else 97 of.write((" " + nodename + " [label=\"" + nodenames[1] + "\", fontname=\"Courier-Bold\"];\n").getBytes()); 98 99 children = node.getChildren(); 100 101 for (int i = 0; i < children.length; ++i) { 102 String [] childnames = children[i].getTypeName(); 103 String childname = childnames[0]; 104 105 childname = childname + nodeindex; 106 of.write(("\t" + nodename + " -> " + childname + ";\n").getBytes()); 107 nodeindex = visualizeNode(children[i], of, nodeindex); 108 109 } 110 111 return nodeindex; 112 } 113 114 118 private ActionRepository repository; 119 120 123 private String legend; 124 125 128 private int explicitidx; 129 130 } 131 132 | Popular Tags |