00001 package gov.nasa.arc.ase.util.graph;
00002
00003 import java.io.PrintStream;
00004
00005
00006
00007
00008
00009 import java.util.Iterator;
00010 import java.util.List;
00011 import java.util.LinkedList;
00012
00013
00014 public class Node {
00015 private Graph graph;
00016 private List outgoingEdges;
00017 private List incomingEdges;
00018 private Attributes attributes;
00019
00020 public Node(Graph g) { init(g, null); }
00021 public Node(Graph g, Attributes a) { init(g, a); }
00022 public Node(Node n) { init(n.graph, new Attributes(n.attributes));
00023 for(Iterator i = n.outgoingEdges.iterator(); i.hasNext(); ) {
00024 new Edge(this, (Edge)i.next());
00025 }
00026 for(Iterator i = n.incomingEdges.iterator(); i.hasNext(); ) {
00027 new Edge((Edge)i.next(), this);
00028 }
00029 }
00030 synchronized void addIncomingEdge(Edge e) { incomingEdges.add(e); }
00031 synchronized void addOutgoingEdge(Edge e) { outgoingEdges.add(e); }
00032 public synchronized void forAllEdges(Visitor v) {
00033 for(Iterator i = new LinkedList(outgoingEdges).iterator(); i.hasNext(); )
00034 v.visitEdge((Edge)i.next());
00035 }
00036 public Attributes getAttributes() { return attributes; }
00037 public boolean getBooleanAttribute(String name) { return attributes.getBoolean(name); }
00038 public Graph getGraph() { return graph; }
00039 public synchronized int getId() { return attributes.getInt("_id"); }
00040 public int getIncomingEdgeCount() { return outgoingEdges.size(); }
00041 public List getIncomingEdges() { return new LinkedList(incomingEdges); }
00042 public int getIntAttribute(String name) { return attributes.getInt(name); }
00043 public int getOutgoingEdgeCount() { return outgoingEdges.size(); }
00044 public List getOutgoingEdges() { return new LinkedList(outgoingEdges); }
00045 public String getStringAttribute(String name) { return attributes.getString(name); }
00046 private void init(Graph g, Attributes a) {
00047 graph = g;
00048 if(a == null)
00049 attributes = new Attributes();
00050 else
00051 attributes = a;
00052
00053 incomingEdges = new LinkedList();
00054 outgoingEdges = new LinkedList();
00055
00056 graph.addNode(this);
00057 }
00058 synchronized public void remove() {
00059 for(Iterator i = new LinkedList(outgoingEdges).iterator(); i.hasNext(); ) {
00060 ((Edge)i.next()).remove();
00061 }
00062 for(Iterator i = new LinkedList(incomingEdges).iterator(); i.hasNext(); ) {
00063 ((Edge)i.next()).remove();
00064 }
00065 graph.removeNode(this);
00066 }
00067 synchronized void removeIncomingEdge(Edge e) { incomingEdges.remove(e); }
00068 synchronized void removeOutgoingEdge(Edge e) { outgoingEdges.remove(e); }
00069 void save(PrintStream out, int format) {
00070 switch(format) {
00071 case Graph.SM_FORMAT: save_sm(out); break;
00072 case Graph.FSP_FORMAT: save_fsp(out); break;
00073 }
00074 }
00075
00076 private void save_fsp(PrintStream out) {
00077
00078 out.print("S" + getId() + "=(");
00079 for(Iterator i = outgoingEdges.iterator(); i.hasNext(); ) {
00080 ((Edge)i.next()).save(out, Graph.FSP_FORMAT);
00081 if(i.hasNext())
00082
00083 out.print(" |");
00084 }
00085
00086
00087 out.print(")");
00088 }
00089 private void save_sm(PrintStream out) {
00090 int id = getId();
00091 out.print(" "); out.println(outgoingEdges.size());
00092 attributes.unset("_id");
00093 out.print(" "); out.println(attributes);
00094 setId(id);
00095 for(Iterator i = outgoingEdges.iterator(); i.hasNext(); )
00096 ((Edge)i.next()).save(out, Graph.SM_FORMAT);
00097 }
00098 synchronized public void setAttributes(Attributes a) {
00099 int id = getId();
00100 attributes = new Attributes(a);
00101 setId(id);
00102 }
00103 synchronized public void setBooleanAttribute(String name, boolean value) {
00104 if(name.equals("_id")) return;
00105 attributes.setBoolean(name, value);
00106 }
00107 synchronized void setId(int id) {
00108 attributes.setInt("_id", id);
00109 }
00110 synchronized public void setIntAttribute(String name, int value) {
00111 if(name.equals("_id")) return;
00112 attributes.setInt(name, value);
00113 }
00114 synchronized public void setStringAttribute(String name, String value) {
00115 if(name.equals("_id")) return;
00116 attributes.setString(name, value);
00117 }
00118 }