00001 package gov.nasa.arc.ase.util.graph; 00002 00003 import java.io.IOException; 00004 00005 public class SM2Dot { 00006 public static void endDigraph() { 00007 System.out.println("}"); 00008 } 00009 public static void main(String[] args) { 00010 if(args.length != 1) { 00011 System.err.println("usage:"); 00012 System.err.println("\tSM2Dot <filename>"); 00013 System.err.println(); 00014 System.exit(1); 00015 } 00016 00017 try { 00018 Graph g = Graph.load(args[0]); 00019 00020 startDigraph(args[0]); 00021 00022 printInit(g.getInit()); 00023 00024 g.forAllNodes(new EmptyVisitor() { 00025 public void visitNode(Node n) { 00026 printNode(n); 00027 n.forAllEdges(new EmptyVisitor() { 00028 public void visitEdge(Edge e) { 00029 printEdge(e); 00030 } 00031 }); 00032 } 00033 }); 00034 endDigraph(); 00035 } catch(IOException e) { 00036 System.err.println("Can't load file: " + args[0]); 00037 System.exit(1); 00038 } 00039 00040 } 00041 public static void printEdge(Edge e) { 00042 int id = e.getSource().getId(); 00043 int nxt = e.getNext().getId(); 00044 String guard = e.getGuard(); 00045 String action = e.getAction(); 00046 String label = e.getStringAttribute("label"); 00047 00048 StringBuffer sb = new StringBuffer(); 00049 if(label != null) { 00050 sb.append(label); 00051 sb.append("\\n"); 00052 } 00053 00054 if(!guard.equals("-")) 00055 if(!action.equals("-")) 00056 sb.append(guard + "/" + action + "\\n"); 00057 else 00058 sb.append(guard + "\\n"); 00059 else 00060 if(!action.equals("-")) 00061 sb.append(guard + "/" + action + "\\n"); 00062 else 00063 sb.append("true\\n"); 00064 00065 int nsets = e.getSource().getGraph().getIntAttribute("nsets"); 00066 boolean first = true; 00067 for(int i = 0; i < nsets; i++) 00068 if(e.getBooleanAttribute("acc"+i)) { 00069 if(first) { 00070 sb.append("{"); 00071 first = false; 00072 } else 00073 sb.append(","); 00074 sb.append(i); 00075 } 00076 if(!first) 00077 sb.append("}"); 00078 00079 System.out.println( 00080 "\t" + id + " -> " + nxt + 00081 " [label=\"" + sb.toString() +"\"]"); 00082 } 00083 public static void printInit(Node n) { 00084 System.out.println("\tinit [color=white, label=\"\"];"); 00085 System.out.println("\tinit -> " + n.getId() + ";"); 00086 } 00087 public static void printNode(Node n) { 00088 int id = n.getId(); 00089 00090 if(n.getBooleanAttribute("accepting")) 00091 System.out.println("\t" + id + " [shape=doublecircle];"); 00092 else 00093 System.out.println("\t" + id + " [shape=circle];"); 00094 00095 String label = n.getStringAttribute("label"); 00096 StringBuffer sb = new StringBuffer(); 00097 00098 if(label != null) { 00099 sb.append(label); 00100 sb.append("\\n"); 00101 } 00102 00103 sb.append(id + "\\n"); 00104 00105 int nsets = n.getGraph().getIntAttribute("nsets"); 00106 boolean first = true; 00107 for(int i = 0; i < nsets; i++) 00108 if(n.getBooleanAttribute("acc"+i)) { 00109 if(first) { 00110 sb.append("{"); 00111 first = false; 00112 } else 00113 sb.append(","); 00114 sb.append(i); 00115 } 00116 if(!first) 00117 sb.append("}"); 00118 00119 System.out.println("\t" + id + " [label=\"" + sb.toString() + "\"];"); 00120 } 00121 public static void startDigraph(String name) { 00122 if(name.lastIndexOf('/') != -1) 00123 name = name.substring(name.lastIndexOf('/') + 1); 00124 name = name.replace('.', '_'); 00125 name = name.replace('-', '_'); 00126 00127 System.out.println("digraph " + name + " {"); 00128 } 00129 }