KickJava   Java API By Example, From Geeks To Geeks.

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


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.PrintStream JavaDoc;
23
24 import java.util.StringTokenizer JavaDoc;
25
26
27 /**
28  * DOCUMENT ME!
29  */

30 public class Edge {
31   private Node source;
32   private Node next;
33   private String JavaDoc guard;
34   private String JavaDoc action;
35   private Attributes attributes;
36
37   public Edge (Node s, Node n, String JavaDoc g, String JavaDoc a, Attributes as) {
38     init(s, n, g, a, as);
39   }
40
41   public Edge (Node s, Node n, String JavaDoc g, String JavaDoc a) {
42     init(s, n, g, a, null);
43   }
44
45   public Edge (Node s, Node n, String JavaDoc 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 JavaDoc(e.guard), new String JavaDoc(e.action),
55          new Attributes(e.attributes));
56   }
57
58   public Edge (Edge e, Node n) {
59     init(e.source, n, new String JavaDoc(e.guard), new String JavaDoc(e.action),
60          new Attributes(e.attributes));
61   }
62
63   public Edge (Edge e) {
64     init(e.source, e.next, new String JavaDoc(e.guard), new String JavaDoc(e.action),
65          new Attributes(e.attributes));
66   }
67
68   public String JavaDoc 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 JavaDoc name, boolean value) {
81     attributes.setBoolean(name, value);
82   }
83
84   public boolean getBooleanAttribute (String JavaDoc name) {
85     return attributes.getBoolean(name);
86   }
87
88   public String JavaDoc getGuard () {
89     return guard;
90   }
91
92   public void setIntAttribute (String JavaDoc name, int value) {
93     attributes.setInt(name, value);
94   }
95
96   public int getIntAttribute (String JavaDoc 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 JavaDoc name, String JavaDoc value) {
109     attributes.setString(name, value);
110   }
111
112   public String JavaDoc getStringAttribute (String JavaDoc name) {
113     return attributes.getString(name);
114   }
115
116   public synchronized void remove () {
117     source.removeOutgoingEdge(this);
118     next.removeIncomingEdge(this);
119   }
120
121   // Modified by robbyjo - Jul 15, 2002
122
void save (PrintStream JavaDoc 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 JavaDoc("Unknown format!");
146     }
147   }
148
149   private void init (Node s, Node n, String JavaDoc g, String JavaDoc 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   // Modified by ckong - Sept 7, 2001
166
private void save_fsp (PrintStream JavaDoc out) {
167     String JavaDoc g;
168     String JavaDoc 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 JavaDoc sb = new StringBuffer JavaDoc();
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     //System.out.print(g + " -" + accs + "-> S" + next.getId());
205
out.print(g + accs + "-> S" + next.getId());
206   }
207
208   private void save_sm (PrintStream JavaDoc 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   // robbyjo's contribution
220
private void save_spin (PrintStream JavaDoc out) {
221     String JavaDoc ln = System.getProperty("line.separator");
222     String JavaDoc g = guard.equals("-") ? "1" : guard;
223     String JavaDoc accs = "";
224
225     StringTokenizer JavaDoc tok = new StringTokenizer JavaDoc(new String JavaDoc(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 JavaDoc(new String JavaDoc(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 JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc 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 JavaDoc xml_quote (String JavaDoc s) {
299     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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("&amp;");
307
308         break;
309
310       case '<':
311         sb.append("&lt;");
312
313         break;
314
315       case '>':
316         sb.append("&gt;");
317
318         break;
319
320       default:
321         sb.append(ch);
322
323         break;
324       }
325     }
326
327     return sb.toString();
328   }
329 }
Popular Tags