KickJava   Java API By Example, From Geeks To Geeks.

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


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.ltl.graph;
20
21 import java.io.PrintStream JavaDoc;
22
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  * DOCUMENT ME!
29  */

30 public class Node {
31     private Graph graph;
32
33     private List JavaDoc outgoingEdges;
34
35     private List JavaDoc incomingEdges;
36
37     private Attributes attributes;
38
39     public Node(Graph g, Attributes a) {
40         init(g, a);
41     }
42
43     public Node(Graph g) {
44         init(g, null);
45     }
46
47     public Node(Node n) {
48         init(n.graph, new Attributes(n.attributes));
49
50         for (Iterator JavaDoc i = n.outgoingEdges.iterator(); i.hasNext();) {
51             new Edge(this, (Edge) i.next());
52         }
53
54         for (Iterator JavaDoc i = n.incomingEdges.iterator(); i.hasNext();) {
55             new Edge((Edge) i.next(), this);
56         }
57     }
58
59     public synchronized void setAttributes(Attributes a) {
60         int id = getId();
61         attributes = new Attributes(a);
62         setId(id);
63     }
64
65     public Attributes getAttributes() {
66         return attributes;
67     }
68
69     public synchronized void setBooleanAttribute(String JavaDoc name, boolean value) {
70         if (name.equals("_id")) {
71             return;
72         }
73
74         attributes.setBoolean(name, value);
75     }
76
77     public boolean getBooleanAttribute(String JavaDoc name) {
78         return attributes.getBoolean(name);
79     }
80
81     public Graph getGraph() {
82         return graph;
83     }
84
85     public synchronized int getId() {
86         return attributes.getInt("_id");
87     }
88
89     public int getIncomingEdgeCount() {
90         return outgoingEdges.size();
91     }
92
93     public List JavaDoc getIncomingEdges() {
94         return new LinkedList JavaDoc(incomingEdges);
95     }
96
97     public synchronized void setIntAttribute(String JavaDoc name, int value) {
98         if (name.equals("_id")) {
99             return;
100         }
101
102         attributes.setInt(name, value);
103     }
104
105     public int getIntAttribute(String JavaDoc name) {
106         return attributes.getInt(name);
107     }
108
109     public int getOutgoingEdgeCount() {
110         return outgoingEdges.size();
111     }
112
113     public List JavaDoc getOutgoingEdges() {
114         return new LinkedList JavaDoc(outgoingEdges);
115     }
116
117     public synchronized void setStringAttribute(String JavaDoc name, String JavaDoc value) {
118         if (name.equals("_id")) {
119             return;
120         }
121
122         attributes.setString(name, value);
123     }
124
125     public String JavaDoc getStringAttribute(String JavaDoc name) {
126         return attributes.getString(name);
127     }
128
129     public synchronized void forAllEdges(Visitor v) {
130         for (Iterator JavaDoc i = new LinkedList JavaDoc(outgoingEdges).iterator(); i.hasNext();) {
131             v.visitEdge((Edge) i.next());
132         }
133     }
134
135     public synchronized void remove() {
136         for (Iterator JavaDoc i = new LinkedList JavaDoc(outgoingEdges).iterator(); i.hasNext();) {
137             ((Edge) i.next()).remove();
138         }
139
140         for (Iterator JavaDoc i = new LinkedList JavaDoc(incomingEdges).iterator(); i.hasNext();) {
141             ((Edge) i.next()).remove();
142         }
143
144         graph.removeNode(this);
145     }
146
147     synchronized void setId(int id) {
148         attributes.setInt("_id", id);
149     }
150
151     synchronized void addIncomingEdge(Edge e) {
152         incomingEdges.add(e);
153     }
154
155     synchronized void addOutgoingEdge(Edge e) {
156         outgoingEdges.add(e);
157     }
158
159     synchronized void removeIncomingEdge(Edge e) {
160         incomingEdges.remove(e);
161     }
162
163     synchronized void removeOutgoingEdge(Edge e) {
164         outgoingEdges.remove(e);
165     }
166
167     // Modified by robbyjo - Jul 15, 2002
168
void save(PrintStream JavaDoc out, int format) {
169         switch (format) {
170         case Graph.SM_FORMAT:
171             save_sm(out);
172
173             break;
174
175         case Graph.FSP_FORMAT:
176             save_fsp(out);
177
178             break;
179
180         case Graph.XML_FORMAT:
181             save_xml(out);
182
183             break;
184
185         case Graph.SPIN_FORMAT:
186             save_spin(out);
187
188             break;
189
190         default:
191             throw new RuntimeException JavaDoc("Unknown format!");
192         }
193     }
194
195     private void init(Graph g, Attributes a) {
196         graph = g;
197
198         if (a == null) {
199             attributes = new Attributes();
200         } else {
201             attributes = a;
202         }
203
204         incomingEdges = new LinkedList JavaDoc();
205         outgoingEdges = new LinkedList JavaDoc();
206
207         graph.addNode(this);
208     }
209
210     // Modified by ckong - Sept 7, 2001
211
private void save_fsp(PrintStream JavaDoc out) {
212         ///System.out.print("S" + getId() + "=(");
213
out.print("S" + getId() + "=(");
214
215         for (Iterator JavaDoc i = outgoingEdges.iterator(); i.hasNext();) {
216             ((Edge) i.next()).save(out, Graph.FSP_FORMAT);
217
218             if (i.hasNext()) {
219                 //System.out.print(" |");
220
out.print(" |");
221             }
222         }
223
224         //System.out.print(")");
225
out.print(")");
226     }
227
228     private void save_sm(PrintStream JavaDoc out) {
229         int id = getId();
230         out.print(" ");
231         out.println(outgoingEdges.size());
232         attributes.unset("_id");
233         out.print(" ");
234         out.println(attributes);
235         setId(id);
236
237         for (Iterator JavaDoc i = outgoingEdges.iterator(); i.hasNext();) {
238             ((Edge) i.next()).save(out, Graph.SM_FORMAT);
239         }
240     }
241
242     // robbyjo's contribution
243
private void save_spin(PrintStream JavaDoc out) {
244         String JavaDoc ln = System.getProperty("line.separator");
245         String JavaDoc lntab = ln + " :: ";
246
247         if (getBooleanAttribute("accepting")) {
248             out.print("accept_");
249         }
250
251         out.print("S" + getId() + ":" + ln + " if" + lntab);
252
253         for (Iterator JavaDoc i = outgoingEdges.iterator(); i.hasNext();) {
254             Edge e = (Edge) i.next();
255             e.save(out, Graph.SPIN_FORMAT);
256
257             if (i.hasNext()) {
258                 out.print(lntab);
259             }
260         }
261
262         out.print(ln + " fi;\n");
263     }
264
265     private void save_xml(PrintStream JavaDoc out) {
266         int id = getId();
267         out.println("<node id=\"" + id + "\">");
268         attributes.unset("_id");
269         attributes.save(out, Graph.XML_FORMAT);
270         setId(id);
271
272         for (Iterator JavaDoc i = outgoingEdges.iterator(); i.hasNext();) {
273             ((Edge) i.next()).save(out, Graph.XML_FORMAT);
274         }
275
276         out.println("</node>");
277     }
278 }
Popular Tags