001: //
002: // Copyright (C) 2005 United States Government as represented by the
003: // Administrator of the National Aeronautics and Space Administration
004: // (NASA). All Rights Reserved.
005: //
006: // This software is distributed under the NASA Open Source Agreement
007: // (NOSA), version 1.3. The NOSA has been approved by the Open Source
008: // Initiative. See the file NOSA-1.3-JPF at the top of the distribution
009: // directory tree for the complete NOSA document.
010: //
011: // THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
012: // KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
013: // LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
014: // SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
015: // A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
016: // THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
017: // DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
018: //
019: package gov.nasa.ltl.graph;
020:
021: import java.io.IOException;
022:
023: /**
024: * DOCUMENT ME!
025: */
026: public class SM2Dot {
027: public static void endDigraph() {
028: System.out.println("}");
029: }
030:
031: public static void main(String[] args) {
032: if (args.length != 1) {
033: System.err.println("usage:");
034: System.err.println("\tSM2Dot <filename>");
035: System.err.println();
036: System.exit(1);
037: }
038:
039: try {
040: Graph g = Graph.load(args[0]);
041:
042: startDigraph(args[0]);
043:
044: printInit(g.getInit());
045:
046: g.forAllNodes(new EmptyVisitor() {
047: public void visitNode(Node n) {
048: printNode(n);
049: n.forAllEdges(new EmptyVisitor() {
050: public void visitEdge(Edge e) {
051: printEdge(e);
052: }
053: });
054: }
055: });
056: endDigraph();
057: } catch (IOException e) {
058: System.err.println("Can't load file: " + args[0]);
059: System.exit(1);
060: }
061: }
062:
063: public static void printEdge(Edge e) {
064: int id = e.getSource().getId();
065: int nxt = e.getNext().getId();
066: String guard = e.getGuard();
067: String action = e.getAction();
068: String label = e.getStringAttribute("label");
069:
070: StringBuffer sb = new StringBuffer();
071:
072: if (label != null) {
073: sb.append(label);
074: sb.append("\\n");
075: }
076:
077: if (!guard.equals("-")) {
078: if (!action.equals("-")) {
079: sb.append(guard + "/" + action + "\\n");
080: } else {
081: sb.append(guard + "\\n");
082: }
083: } else if (!action.equals("-")) {
084: sb.append(guard + "/" + action + "\\n");
085: } else {
086: sb.append("true\\n");
087: }
088:
089: int nsets = e.getSource().getGraph().getIntAttribute("nsets");
090: boolean first = true;
091:
092: for (int i = 0; i < nsets; i++) {
093: if (e.getBooleanAttribute("acc" + i)) {
094: if (first) {
095: sb.append("{");
096: first = false;
097: } else {
098: sb.append(",");
099: }
100:
101: sb.append(i);
102: }
103: }
104:
105: if (!first) {
106: sb.append("}");
107: }
108:
109: System.out.println("\t" + id + " -> " + nxt + " [label=\""
110: + sb.toString() + "\"]");
111: }
112:
113: public static void printInit(Node n) {
114: System.out.println("\tinit [color=white, label=\"\"];");
115: System.out.println("\tinit -> " + n.getId() + ";");
116: }
117:
118: public static void printNode(Node n) {
119: int id = n.getId();
120:
121: if (n.getBooleanAttribute("accepting")) {
122: System.out.println("\t" + id + " [shape=doublecircle];");
123: } else {
124: System.out.println("\t" + id + " [shape=circle];");
125: }
126:
127: String label = n.getStringAttribute("label");
128: StringBuffer sb = new StringBuffer();
129:
130: if (label != null) {
131: sb.append(label);
132: sb.append("\\n");
133: }
134:
135: sb.append(id + "\\n");
136:
137: int nsets = n.getGraph().getIntAttribute("nsets");
138: boolean first = true;
139:
140: for (int i = 0; i < nsets; i++) {
141: if (n.getBooleanAttribute("acc" + i)) {
142: if (first) {
143: sb.append("{");
144: first = false;
145: } else {
146: sb.append(",");
147: }
148:
149: sb.append(i);
150: }
151: }
152:
153: if (!first) {
154: sb.append("}");
155: }
156:
157: System.out.println("\t" + id + " [label=\"" + sb.toString()
158: + "\"];");
159: }
160:
161: public static void startDigraph(String name) {
162: if (name.lastIndexOf('/') != -1) {
163: name = name.substring(name.lastIndexOf('/') + 1);
164: }
165:
166: name = name.replace('.', '_');
167: name = name.replace('-', '_');
168:
169: System.out.println("digraph " + name + " {");
170: }
171: }
|