1 2 package gov.nasa.ltl.graph; 21 22 import java.io.PrintStream ; 23 24 import java.util.StringTokenizer ; 25 26 27 30 public class Edge { 31 private Node source; 32 private Node next; 33 private String guard; 34 private String action; 35 private Attributes attributes; 36 37 public Edge (Node s, Node n, String g, String a, Attributes as) { 38 init(s, n, g, a, as); 39 } 40 41 public Edge (Node s, Node n, String g, String a) { 42 init(s, n, g, a, null); 43 } 44 45 public Edge (Node s, Node n, String g) { 46 init(s, n, g, "-", null); 47 } 48 49 public Edge (Node s, Node n) { 50 init(s, n, "-", "-", null); 51 } 52 53 public Edge (Node s, Edge e) { 54 init(s, e.next, new String (e.guard), new String (e.action), 55 new Attributes(e.attributes)); 56 } 57 58 public Edge (Edge e, Node n) { 59 init(e.source, n, new String (e.guard), new String (e.action), 60 new Attributes(e.attributes)); 61 } 62 63 public Edge (Edge e) { 64 init(e.source, e.next, new String (e.guard), new String (e.action), 65 new Attributes(e.attributes)); 66 } 67 68 public String getAction () { 69 return action; 70 } 71 72 public synchronized void setAttributes (Attributes a) { 73 attributes = new Attributes(a); 74 } 75 76 public Attributes getAttributes () { 77 return attributes; 78 } 79 80 public void setBooleanAttribute (String name, boolean value) { 81 attributes.setBoolean(name, value); 82 } 83 84 public boolean getBooleanAttribute (String name) { 85 return attributes.getBoolean(name); 86 } 87 88 public String getGuard () { 89 return guard; 90 } 91 92 public void setIntAttribute (String name, int value) { 93 attributes.setInt(name, value); 94 } 95 96 public int getIntAttribute (String name) { 97 return attributes.getInt(name); 98 } 99 100 public Node getNext () { 101 return next; 102 } 103 104 public Node getSource () { 105 return source; 106 } 107 108 public void setStringAttribute (String name, String value) { 109 attributes.setString(name, value); 110 } 111 112 public String getStringAttribute (String name) { 113 return attributes.getString(name); 114 } 115 116 public synchronized void remove () { 117 source.removeOutgoingEdge(this); 118 next.removeIncomingEdge(this); 119 } 120 121 void save (PrintStream out, int format) { 123 switch (format) { 124 case Graph.SM_FORMAT: 125 save_sm(out); 126 127 break; 128 129 case Graph.FSP_FORMAT: 130 save_fsp(out); 131 132 break; 133 134 case Graph.XML_FORMAT: 135 save_xml(out); 136 137 break; 138 139 case Graph.SPIN_FORMAT: 140 save_spin(out); 141 142 break; 143 144 default: 145 throw new RuntimeException ("Unknown format!"); 146 } 147 } 148 149 private void init (Node s, Node n, String g, String a, Attributes as) { 150 source = s; 151 next = n; 152 guard = g; 153 action = a; 154 155 if (as == null) { 156 attributes = new Attributes(); 157 } else { 158 attributes = as; 159 } 160 161 s.addOutgoingEdge(this); 162 n.addIncomingEdge(this); 163 } 164 165 private void save_fsp (PrintStream out) { 167 String g; 168 String accs = ""; 169 170 if (guard.equals("-")) { 171 g = "TRUE"; 172 } else { 173 g = guard; 174 } 175 176 int nsets = source.getGraph().getIntAttribute("nsets"); 177 178 if (nsets == 0) { 179 if (getBooleanAttribute("accepting")) { 180 accs = "@"; 181 } 182 } else { 183 boolean first = true; 184 StringBuffer sb = new StringBuffer (); 185 186 for (int i = 0; i < nsets; i++) { 187 if (getBooleanAttribute("acc" + i)) { 188 if (first) { 189 first = false; 190 } else { 191 sb.append(","); 192 } 193 194 sb.append(i); 195 } 196 } 197 198 if (!first) { 199 accs = "{" + sb.toString() + "}"; 200 } 201 } 202 203 204 out.print(g + accs + "-> S" + next.getId()); 206 } 207 208 private void save_sm (PrintStream out) { 209 out.print(" "); 210 out.println(next.getId()); 211 out.print(" "); 212 out.println(guard); 213 out.print(" "); 214 out.println(action); 215 out.print(" "); 216 out.println(attributes); 217 } 218 219 private void save_spin (PrintStream out) { 221 String ln = System.getProperty("line.separator"); 222 String g = guard.equals("-") ? "1" : guard; 223 String accs = ""; 224 225 StringTokenizer tok = new StringTokenizer (new String (g), "&"); 226 g = ""; 227 228 while (tok.hasMoreTokens()) { 229 g += tok.nextToken(); 230 231 if (tok.hasMoreTokens()) { 232 g += " && "; 233 } 234 } 235 236 tok = new StringTokenizer (new String (g), "|"); 237 g = ""; 238 239 while (tok.hasMoreTokens()) { 240 g += tok.nextToken(); 241 242 if (tok.hasMoreTokens()) { 243 g += " || "; 244 } 245 } 246 247 int nsets = source.getGraph().getIntAttribute("nsets"); 248 249 if (nsets == 0) { 250 if (getBooleanAttribute("accepting")) { 251 accs = "@"; 252 } 253 } else { 254 boolean first = true; 255 StringBuffer sb = new StringBuffer (); 256 257 for (int i = 0; i < nsets; i++) { 258 if (getBooleanAttribute("acc" + i)) { 259 if (first) { 260 first = false; 261 } else { 262 sb.append(","); 263 } 264 265 sb.append(i); 266 } 267 } 268 269 if (!first) { 270 accs = "{" + sb.toString() + "}"; 271 } 272 } 273 274 out.print("(" + g + ") " + accs + "-> goto "); 275 276 if (next.getBooleanAttribute("accepting")) { 277 out.print("accept_"); 278 } 279 280 out.print("S" + next.getId()); 281 } 282 283 private void save_xml (PrintStream out) { 284 out.println("<transition to=\"" + next.getId() + "\">"); 285 286 if (!guard.equals("-")) { 287 out.println("<guard>" + xml_quote(guard) + "</guard>"); 288 } 289 290 if (!action.equals("-")) { 291 out.println("<action>" + xml_quote(action) + "</action>"); 292 } 293 294 attributes.save(out, Graph.XML_FORMAT); 295 out.println("</transition>"); 296 } 297 298 private String xml_quote (String s) { 299 StringBuffer sb = new StringBuffer (); 300 301 for (int i = 0; i < s.length(); i++) { 302 char ch = s.charAt(i); 303 304 switch (ch) { 305 case '&': 306 sb.append("&"); 307 308 break; 309 310 case '<': 311 sb.append("<"); 312 313 break; 314 315 case '>': 316 sb.append(">"); 317 318 break; 319 320 default: 321 sb.append(ch); 322 323 break; 324 } 325 } 326 327 return sb.toString(); 328 } 329 } | Popular Tags |