1 2 package gov.nasa.ltl.graph; 21 22 import java.io.IOException ; 23 24 25 28 public class SM2Dot { 29 public static void endDigraph () { 30 System.out.println("}"); 31 } 32 33 public static void main (String [] 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 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 guard = e.getGuard(); 69 String action = e.getAction(); 70 String label = e.getStringAttribute("label"); 71 72 StringBuffer sb = new StringBuffer (); 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 label = n.getStringAttribute("label"); 130 StringBuffer sb = new StringBuffer (); 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 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 |