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.PrintStream;
022:
023: import java.util.StringTokenizer;
024:
025: /**
026: * DOCUMENT ME!
027: */
028: public class Edge {
029: private Node source;
030: private Node next;
031: private String guard;
032: private String action;
033: private Attributes attributes;
034:
035: public Edge(Node s, Node n, String g, String a, Attributes as) {
036: init(s, n, g, a, as);
037: }
038:
039: public Edge(Node s, Node n, String g, String a) {
040: init(s, n, g, a, null);
041: }
042:
043: public Edge(Node s, Node n, String g) {
044: init(s, n, g, "-", null);
045: }
046:
047: public Edge(Node s, Node n) {
048: init(s, n, "-", "-", null);
049: }
050:
051: public Edge(Node s, Edge e) {
052: init(s, e.next, new String(e.guard), new String(e.action),
053: new Attributes(e.attributes));
054: }
055:
056: public Edge(Edge e, Node n) {
057: init(e.source, n, new String(e.guard), new String(e.action),
058: new Attributes(e.attributes));
059: }
060:
061: public Edge(Edge e) {
062: init(e.source, e.next, new String(e.guard),
063: new String(e.action), new Attributes(e.attributes));
064: }
065:
066: public String getAction() {
067: return action;
068: }
069:
070: public synchronized void setAttributes(Attributes a) {
071: attributes = new Attributes(a);
072: }
073:
074: public Attributes getAttributes() {
075: return attributes;
076: }
077:
078: public void setBooleanAttribute(String name, boolean value) {
079: attributes.setBoolean(name, value);
080: }
081:
082: public boolean getBooleanAttribute(String name) {
083: return attributes.getBoolean(name);
084: }
085:
086: public String getGuard() {
087: return guard;
088: }
089:
090: public void setIntAttribute(String name, int value) {
091: attributes.setInt(name, value);
092: }
093:
094: public int getIntAttribute(String name) {
095: return attributes.getInt(name);
096: }
097:
098: public Node getNext() {
099: return next;
100: }
101:
102: public Node getSource() {
103: return source;
104: }
105:
106: public void setStringAttribute(String name, String value) {
107: attributes.setString(name, value);
108: }
109:
110: public String getStringAttribute(String name) {
111: return attributes.getString(name);
112: }
113:
114: public synchronized void remove() {
115: source.removeOutgoingEdge(this );
116: next.removeIncomingEdge(this );
117: }
118:
119: // Modified by robbyjo - Jul 15, 2002
120: void save(PrintStream out, int format) {
121: switch (format) {
122: case Graph.SM_FORMAT:
123: save_sm(out);
124:
125: break;
126:
127: case Graph.FSP_FORMAT:
128: save_fsp(out);
129:
130: break;
131:
132: case Graph.XML_FORMAT:
133: save_xml(out);
134:
135: break;
136:
137: case Graph.SPIN_FORMAT:
138: save_spin(out);
139:
140: break;
141:
142: default:
143: throw new RuntimeException("Unknown format!");
144: }
145: }
146:
147: private void init(Node s, Node n, String g, String a, Attributes as) {
148: source = s;
149: next = n;
150: guard = g;
151: action = a;
152:
153: if (as == null) {
154: attributes = new Attributes();
155: } else {
156: attributes = as;
157: }
158:
159: s.addOutgoingEdge(this );
160: n.addIncomingEdge(this );
161: }
162:
163: // Modified by ckong - Sept 7, 2001
164: private void save_fsp(PrintStream out) {
165: String g;
166: String accs = "";
167:
168: if (guard.equals("-")) {
169: g = "TRUE";
170: } else {
171: g = guard;
172: }
173:
174: int nsets = source.getGraph().getIntAttribute("nsets");
175:
176: if (nsets == 0) {
177: if (getBooleanAttribute("accepting")) {
178: accs = "@";
179: }
180: } else {
181: boolean first = true;
182: StringBuffer sb = new StringBuffer();
183:
184: for (int i = 0; i < nsets; i++) {
185: if (getBooleanAttribute("acc" + i)) {
186: if (first) {
187: first = false;
188: } else {
189: sb.append(",");
190: }
191:
192: sb.append(i);
193: }
194: }
195:
196: if (!first) {
197: accs = "{" + sb.toString() + "}";
198: }
199: }
200:
201: //System.out.print(g + " -" + accs + "-> S" + next.getId());
202: out.print(g + accs + "-> S" + next.getId());
203: }
204:
205: private void save_sm(PrintStream out) {
206: out.print(" ");
207: out.println(next.getId());
208: out.print(" ");
209: out.println(guard);
210: out.print(" ");
211: out.println(action);
212: out.print(" ");
213: out.println(attributes);
214: }
215:
216: // robbyjo's contribution
217: private void save_spin(PrintStream out) {
218: String ln = System.getProperty("line.separator");
219: String g = guard.equals("-") ? "1" : guard;
220: String accs = "";
221:
222: StringTokenizer tok = new StringTokenizer(new String(g), "&");
223: g = "";
224:
225: while (tok.hasMoreTokens()) {
226: g += tok.nextToken();
227:
228: if (tok.hasMoreTokens()) {
229: g += " && ";
230: }
231: }
232:
233: tok = new StringTokenizer(new String(g), "|");
234: g = "";
235:
236: while (tok.hasMoreTokens()) {
237: g += tok.nextToken();
238:
239: if (tok.hasMoreTokens()) {
240: g += " || ";
241: }
242: }
243:
244: int nsets = source.getGraph().getIntAttribute("nsets");
245:
246: if (nsets == 0) {
247: if (getBooleanAttribute("accepting")) {
248: accs = "@";
249: }
250: } else {
251: boolean first = true;
252: StringBuffer sb = new StringBuffer();
253:
254: for (int i = 0; i < nsets; i++) {
255: if (getBooleanAttribute("acc" + i)) {
256: if (first) {
257: first = false;
258: } else {
259: sb.append(",");
260: }
261:
262: sb.append(i);
263: }
264: }
265:
266: if (!first) {
267: accs = "{" + sb.toString() + "}";
268: }
269: }
270:
271: out.print("(" + g + ") " + accs + "-> goto ");
272:
273: if (next.getBooleanAttribute("accepting")) {
274: out.print("accept_");
275: }
276:
277: out.print("S" + next.getId());
278: }
279:
280: private void save_xml(PrintStream out) {
281: out.println("<transition to=\"" + next.getId() + "\">");
282:
283: if (!guard.equals("-")) {
284: out.println("<guard>" + xml_quote(guard) + "</guard>");
285: }
286:
287: if (!action.equals("-")) {
288: out.println("<action>" + xml_quote(action) + "</action>");
289: }
290:
291: attributes.save(out, Graph.XML_FORMAT);
292: out.println("</transition>");
293: }
294:
295: private String xml_quote(String s) {
296: StringBuffer sb = new StringBuffer();
297:
298: for (int i = 0; i < s.length(); i++) {
299: char ch = s.charAt(i);
300:
301: switch (ch) {
302: case '&':
303: sb.append("&");
304:
305: break;
306:
307: case '<':
308: sb.append("<");
309:
310: break;
311:
312: case '>':
313: sb.append(">");
314:
315: break;
316:
317: default:
318: sb.append(ch);
319:
320: break;
321: }
322: }
323:
324: return sb.toString();
325: }
326: }
|