1 package gov.nasa.ltl.graph; 20 21 import java.io.PrintStream ; 22 23 import java.util.Iterator ; 24 import java.util.LinkedList ; 25 import java.util.List ; 26 27 30 public class Node { 31 private Graph graph; 32 33 private List outgoingEdges; 34 35 private List 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 i = n.outgoingEdges.iterator(); i.hasNext();) { 51 new Edge(this, (Edge) i.next()); 52 } 53 54 for (Iterator 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 name, boolean value) { 70 if (name.equals("_id")) { 71 return; 72 } 73 74 attributes.setBoolean(name, value); 75 } 76 77 public boolean getBooleanAttribute(String 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 getIncomingEdges() { 94 return new LinkedList (incomingEdges); 95 } 96 97 public synchronized void setIntAttribute(String name, int value) { 98 if (name.equals("_id")) { 99 return; 100 } 101 102 attributes.setInt(name, value); 103 } 104 105 public int getIntAttribute(String name) { 106 return attributes.getInt(name); 107 } 108 109 public int getOutgoingEdgeCount() { 110 return outgoingEdges.size(); 111 } 112 113 public List getOutgoingEdges() { 114 return new LinkedList (outgoingEdges); 115 } 116 117 public synchronized void setStringAttribute(String name, String value) { 118 if (name.equals("_id")) { 119 return; 120 } 121 122 attributes.setString(name, value); 123 } 124 125 public String getStringAttribute(String name) { 126 return attributes.getString(name); 127 } 128 129 public synchronized void forAllEdges(Visitor v) { 130 for (Iterator i = new LinkedList (outgoingEdges).iterator(); i.hasNext();) { 131 v.visitEdge((Edge) i.next()); 132 } 133 } 134 135 public synchronized void remove() { 136 for (Iterator i = new LinkedList (outgoingEdges).iterator(); i.hasNext();) { 137 ((Edge) i.next()).remove(); 138 } 139 140 for (Iterator i = new LinkedList (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 void save(PrintStream 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 ("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 (); 205 outgoingEdges = new LinkedList (); 206 207 graph.addNode(this); 208 } 209 210 private void save_fsp(PrintStream out) { 212 out.print("S" + getId() + "=("); 214 215 for (Iterator i = outgoingEdges.iterator(); i.hasNext();) { 216 ((Edge) i.next()).save(out, Graph.FSP_FORMAT); 217 218 if (i.hasNext()) { 219 out.print(" |"); 221 } 222 } 223 224 out.print(")"); 226 } 227 228 private void save_sm(PrintStream 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 i = outgoingEdges.iterator(); i.hasNext();) { 238 ((Edge) i.next()).save(out, Graph.SM_FORMAT); 239 } 240 } 241 242 private void save_spin(PrintStream out) { 244 String ln = System.getProperty("line.separator"); 245 String 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 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 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 i = outgoingEdges.iterator(); i.hasNext();) { 273 ((Edge) i.next()).save(out, Graph.XML_FORMAT); 274 } 275 276 out.println("</node>"); 277 } 278 } | Popular Tags |