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.Iterator;
024: import java.util.LinkedList;
025: import java.util.List;
026:
027: /**
028: * DOCUMENT ME!
029: */
030: public class Node {
031: private Graph graph;
032:
033: private List outgoingEdges;
034:
035: private List incomingEdges;
036:
037: private Attributes attributes;
038:
039: public Node(Graph g, Attributes a) {
040: init(g, a);
041: }
042:
043: public Node(Graph g) {
044: init(g, null);
045: }
046:
047: public Node(Node n) {
048: init(n.graph, new Attributes(n.attributes));
049:
050: for (Iterator i = n.outgoingEdges.iterator(); i.hasNext();) {
051: new Edge(this , (Edge) i.next());
052: }
053:
054: for (Iterator i = n.incomingEdges.iterator(); i.hasNext();) {
055: new Edge((Edge) i.next(), this );
056: }
057: }
058:
059: public synchronized void setAttributes(Attributes a) {
060: int id = getId();
061: attributes = new Attributes(a);
062: setId(id);
063: }
064:
065: public Attributes getAttributes() {
066: return attributes;
067: }
068:
069: public synchronized void setBooleanAttribute(String name,
070: boolean value) {
071: if (name.equals("_id")) {
072: return;
073: }
074:
075: attributes.setBoolean(name, value);
076: }
077:
078: public boolean getBooleanAttribute(String name) {
079: return attributes.getBoolean(name);
080: }
081:
082: public Graph getGraph() {
083: return graph;
084: }
085:
086: public synchronized int getId() {
087: return attributes.getInt("_id");
088: }
089:
090: public int getIncomingEdgeCount() {
091: return outgoingEdges.size();
092: }
093:
094: public List getIncomingEdges() {
095: return new LinkedList(incomingEdges);
096: }
097:
098: public synchronized void setIntAttribute(String name, int value) {
099: if (name.equals("_id")) {
100: return;
101: }
102:
103: attributes.setInt(name, value);
104: }
105:
106: public int getIntAttribute(String name) {
107: return attributes.getInt(name);
108: }
109:
110: public int getOutgoingEdgeCount() {
111: return outgoingEdges.size();
112: }
113:
114: public List getOutgoingEdges() {
115: return new LinkedList(outgoingEdges);
116: }
117:
118: public synchronized void setStringAttribute(String name,
119: String value) {
120: if (name.equals("_id")) {
121: return;
122: }
123:
124: attributes.setString(name, value);
125: }
126:
127: public String getStringAttribute(String name) {
128: return attributes.getString(name);
129: }
130:
131: public synchronized void forAllEdges(Visitor v) {
132: for (Iterator i = new LinkedList(outgoingEdges).iterator(); i
133: .hasNext();) {
134: v.visitEdge((Edge) i.next());
135: }
136: }
137:
138: public synchronized void remove() {
139: for (Iterator i = new LinkedList(outgoingEdges).iterator(); i
140: .hasNext();) {
141: ((Edge) i.next()).remove();
142: }
143:
144: for (Iterator i = new LinkedList(incomingEdges).iterator(); i
145: .hasNext();) {
146: ((Edge) i.next()).remove();
147: }
148:
149: graph.removeNode(this );
150: }
151:
152: synchronized void setId(int id) {
153: attributes.setInt("_id", id);
154: }
155:
156: synchronized void addIncomingEdge(Edge e) {
157: incomingEdges.add(e);
158: }
159:
160: synchronized void addOutgoingEdge(Edge e) {
161: outgoingEdges.add(e);
162: }
163:
164: synchronized void removeIncomingEdge(Edge e) {
165: incomingEdges.remove(e);
166: }
167:
168: synchronized void removeOutgoingEdge(Edge e) {
169: outgoingEdges.remove(e);
170: }
171:
172: // Modified by robbyjo - Jul 15, 2002
173: void save(PrintStream out, int format) {
174: switch (format) {
175: case Graph.SM_FORMAT:
176: save_sm(out);
177:
178: break;
179:
180: case Graph.FSP_FORMAT:
181: save_fsp(out);
182:
183: break;
184:
185: case Graph.XML_FORMAT:
186: save_xml(out);
187:
188: break;
189:
190: case Graph.SPIN_FORMAT:
191: save_spin(out);
192:
193: break;
194:
195: default:
196: throw new RuntimeException("Unknown format!");
197: }
198: }
199:
200: private void init(Graph g, Attributes a) {
201: graph = g;
202:
203: if (a == null) {
204: attributes = new Attributes();
205: } else {
206: attributes = a;
207: }
208:
209: incomingEdges = new LinkedList();
210: outgoingEdges = new LinkedList();
211:
212: graph.addNode(this );
213: }
214:
215: // Modified by ckong - Sept 7, 2001
216: private void save_fsp(PrintStream out) {
217: ///System.out.print("S" + getId() + "=(");
218: out.print("S" + getId() + "=(");
219:
220: for (Iterator i = outgoingEdges.iterator(); i.hasNext();) {
221: ((Edge) i.next()).save(out, Graph.FSP_FORMAT);
222:
223: if (i.hasNext()) {
224: //System.out.print(" |");
225: out.print(" |");
226: }
227: }
228:
229: //System.out.print(")");
230: out.print(")");
231: }
232:
233: private void save_sm(PrintStream out) {
234: int id = getId();
235: out.print(" ");
236: out.println(outgoingEdges.size());
237: attributes.unset("_id");
238: out.print(" ");
239: out.println(attributes);
240: setId(id);
241:
242: for (Iterator i = outgoingEdges.iterator(); i.hasNext();) {
243: ((Edge) i.next()).save(out, Graph.SM_FORMAT);
244: }
245: }
246:
247: // robbyjo's contribution
248: private void save_spin(PrintStream out) {
249: String ln = System.getProperty("line.separator");
250: String lntab = ln + " :: ";
251:
252: if (getBooleanAttribute("accepting")) {
253: out.print("accept_");
254: }
255:
256: out.print("S" + getId() + ":" + ln + " if" + lntab);
257:
258: for (Iterator i = outgoingEdges.iterator(); i.hasNext();) {
259: Edge e = (Edge) i.next();
260: e.save(out, Graph.SPIN_FORMAT);
261:
262: if (i.hasNext()) {
263: out.print(lntab);
264: }
265: }
266:
267: out.print(ln + " fi;\n");
268: }
269:
270: private void save_xml(PrintStream out) {
271: int id = getId();
272: out.println("<node id=\"" + id + "\">");
273: attributes.unset("_id");
274: attributes.save(out, Graph.XML_FORMAT);
275: setId(id);
276:
277: for (Iterator i = outgoingEdges.iterator(); i.hasNext();) {
278: ((Edge) i.next()).save(out, Graph.XML_FORMAT);
279: }
280:
281: out.println("</node>");
282: }
283: }
|