KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > ltl > graph > SM2Dot


1
2 //
3
// Copyright (C) 2005 United States Government as represented by the
4
// Administrator of the National Aeronautics and Space Administration
5
// (NASA). All Rights Reserved.
6
//
7
// This software is distributed under the NASA Open Source Agreement
8
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
9
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
10
// directory tree for the complete NOSA document.
11
//
12
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
13
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
14
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
15
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
16
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
17
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
18
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
19
//
20
package gov.nasa.ltl.graph;
21
22 import java.io.IOException JavaDoc;
23
24
25 /**
26  * DOCUMENT ME!
27  */

28 public class SM2Dot {
29   public static void endDigraph () {
30     System.out.println("}");
31   }
32
33   public static void main (String JavaDoc[] args) {
34     if (args.length != 1) {
35       System.err.println("usage:");
36       System.err.println("\tSM2Dot <filename>");
37       System.err.println();
38       System.exit(1);
39     }
40
41     try {
42       Graph g = Graph.load(args[0]);
43
44       startDigraph(args[0]);
45
46       printInit(g.getInit());
47
48       g.forAllNodes(new EmptyVisitor() {
49         public void visitNode (Node n) {
50           printNode(n);
51           n.forAllEdges(new EmptyVisitor() {
52             public void visitEdge (Edge e) {
53               printEdge(e);
54             }
55           });
56         }
57       });
58       endDigraph();
59     } catch (IOException JavaDoc e) {
60       System.err.println("Can't load file: " + args[0]);
61       System.exit(1);
62     }
63   }
64
65   public static void printEdge (Edge e) {
66     int id = e.getSource().getId();
67     int nxt = e.getNext().getId();
68     String JavaDoc guard = e.getGuard();
69     String JavaDoc action = e.getAction();
70     String JavaDoc label = e.getStringAttribute("label");
71
72     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
73
74     if (label != null) {
75       sb.append(label);
76       sb.append("\\n");
77     }
78
79     if (!guard.equals("-")) {
80       if (!action.equals("-")) {
81         sb.append(guard + "/" + action + "\\n");
82       } else {
83         sb.append(guard + "\\n");
84       }
85     } else if (!action.equals("-")) {
86       sb.append(guard + "/" + action + "\\n");
87     } else {
88       sb.append("true\\n");
89     }
90
91     int nsets = e.getSource().getGraph().getIntAttribute("nsets");
92     boolean first = true;
93
94     for (int i = 0; i < nsets; i++) {
95       if (e.getBooleanAttribute("acc" + i)) {
96         if (first) {
97           sb.append("{");
98           first = false;
99         } else {
100           sb.append(",");
101         }
102
103         sb.append(i);
104       }
105     }
106
107     if (!first) {
108       sb.append("}");
109     }
110
111     System.out.println("\t" + id + " -> " + nxt + " [label=\"" +
112                        sb.toString() + "\"]");
113   }
114
115   public static void printInit (Node n) {
116     System.out.println("\tinit [color=white, label=\"\"];");
117     System.out.println("\tinit -> " + n.getId() + ";");
118   }
119
120   public static void printNode (Node n) {
121     int id = n.getId();
122
123     if (n.getBooleanAttribute("accepting")) {
124       System.out.println("\t" + id + " [shape=doublecircle];");
125     } else {
126       System.out.println("\t" + id + " [shape=circle];");
127     }
128
129     String JavaDoc label = n.getStringAttribute("label");
130     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
131
132     if (label != null) {
133       sb.append(label);
134       sb.append("\\n");
135     }
136
137     sb.append(id + "\\n");
138
139     int nsets = n.getGraph().getIntAttribute("nsets");
140     boolean first = true;
141
142     for (int i = 0; i < nsets; i++) {
143       if (n.getBooleanAttribute("acc" + i)) {
144         if (first) {
145           sb.append("{");
146           first = false;
147         } else {
148           sb.append(",");
149         }
150
151         sb.append(i);
152       }
153     }
154
155     if (!first) {
156       sb.append("}");
157     }
158
159     System.out.println("\t" + id + " [label=\"" + sb.toString() + "\"];");
160   }
161
162   public static void startDigraph (String JavaDoc name) {
163     if (name.lastIndexOf('/') != -1) {
164       name = name.substring(name.lastIndexOf('/') + 1);
165     }
166
167     name = name.replace('.', '_');
168     name = name.replace('-', '_');
169
170     System.out.println("digraph " + name + " {");
171   }
172 }
Popular Tags