KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Util > DFSRChecker > visualization > DotVisualizer


1 /*
2  * $Id: DotVisualizer.java,v 1.4 2005/07/08 12:04:12 kofron Exp $
3  *
4  * Copyright 2004
5  * Distributed Systems Research Group
6  * Department of Software Engineering
7  * Faculty of Mathematics and Physics
8  * Charles University, Prague
9  *
10  * Copyright 2005
11  * Formal Methods In Software Engineering Group
12  * Institute of Computer Science
13  * Academy of Sciences of the Czech Republic
14  *
15  * This code was developed by Jan Kofron <kofron@nenya.ms.mff.cuni.cz>
16  */

17
18
19 package SOFA.SOFAnode.Util.DFSRChecker.visualization;
20
21
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.FileNotFoundException JavaDoc;
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 /**
32  * This class creates a dot-visualization of given parse tree.
33  */

34 public class DotVisualizer {
35
36     /** Creates a new instance of DotVisualizer */
37     public DotVisualizer(ActionRepository repository) {
38         this.repository = repository;
39         this.legend = new String JavaDoc();
40     }
41
42     /**
43      * Outputs the tree into image
44      *
45      * @return 0 if no error occur, else non-zero
46      */

47     public int visualize(TreeNode node, String JavaDoc outputfile) {
48         try {
49             explicitidx = 0;
50             legend = "";
51             FileOutputStream JavaDoc of = new FileOutputStream JavaDoc(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 JavaDoc e) {
65             return 1;
66         } catch (SecurityException JavaDoc e) {
67             return 2;
68         } catch (IOException JavaDoc e) {
69             return 3;
70         }
71     }
72
73     /**
74      * This method writes down information about single node and call itself for
75      * its children.
76      *
77      * @return ErrorCode, 0 if no error occur
78      */

79     private int visualizeNode(TreeNode node, FileOutputStream JavaDoc of, int nodeindex) throws IOException JavaDoc {
80         String JavaDoc[] nodenames = node.getTypeName();
81         String JavaDoc nodename = nodenames[0];
82         TreeNode[] children;
83
84         String JavaDoc purename = new String JavaDoc(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 JavaDoc[] childnames = children[i].getTypeName();
103             String JavaDoc 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     //-----------------------------------------------------------------------------
115
/**
116      * The action repository to visualize the event nodes
117      */

118     private ActionRepository repository;
119
120     /**
121      * String for explicit automata legend
122      */

123     private String JavaDoc legend;
124
125     /**
126      * Explicit automata index
127      */

128     private int explicitidx;
129
130 }
131
132
Popular Tags