Main Page   Packages   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

Graph.java

00001 package gov.nasa.arc.ase.util.graph;
00002 
00003 import java.io.IOException;
00004 import java.io.BufferedReader;
00005 import java.io.FileReader;
00006 import java.io.PrintStream;
00007 import java.io.FileOutputStream;
00008 import java.io.InputStreamReader;
00009 //#ifdef JDK11
00010 
00011 
00012 
00013 //#else JDK11
00014 import java.util.List;
00015 import java.util.Iterator;
00016 import java.util.LinkedList;
00017 //#endif JDK11
00018 
00019 public class Graph {
00020   public static final int SM_FORMAT = 0;
00021   public static final int FSP_FORMAT = 1;
00022 
00023   private List nodes;
00024   private Node init;
00025   private Attributes attributes;
00026 
00027   public Graph()        { init(null); }  
00028   public Graph(Attributes a)    { init(a); }  
00029   synchronized void addNode(Node n) {
00030     nodes.add(n);
00031     if(init == null) init = n;
00032 
00033     number();
00034   }  
00035   private synchronized void dfs(Node n, Visitor v) {
00036     final Visitor visitor = v;
00037 
00038     if(n.getBooleanAttribute("_reached")) return;
00039     n.setBooleanAttribute("_reached", true);
00040 
00041     v.visitNode(n);
00042 
00043     n.forAllEdges(new EmptyVisitor() {
00044     public void visitEdge(Edge e) {
00045       dfs(e.getNext(), visitor);
00046     }
00047     });
00048   }  
00049   public synchronized void dfs(Visitor v) {
00050     if(init == null) return;
00051 
00052     forAllNodes(new EmptyVisitor() {
00053     public void visitNode(Node n) {
00054       n.setBooleanAttribute("_reached", false);
00055     }
00056     });
00057 
00058     dfs(init, v);
00059 
00060     forAllNodes(new EmptyVisitor() {
00061     public void visitNode(Node n) {
00062       n.setBooleanAttribute("_reached", false);
00063     }
00064     });
00065 
00066   }  
00067   public synchronized void forAll(Visitor v) {
00068     for(Iterator i = new LinkedList(nodes).iterator(); i.hasNext(); ) {
00069       Node n = (Node)i.next();
00070 
00071       v.visitNode(n);
00072 
00073       n.forAllEdges(v);
00074     }
00075   }  
00076   public synchronized void forAllEdges(Visitor v) {
00077     for(Iterator i = new LinkedList(nodes).iterator(); i.hasNext(); ) {
00078       Node n = (Node)i.next();
00079 
00080       n.forAllEdges(v);
00081     }
00082   }  
00083   public synchronized void forAllNodes(Visitor v) {
00084     for(Iterator i = new LinkedList(nodes).iterator(); i.hasNext(); ) {
00085       Node n = (Node)i.next();
00086       v.visitNode(n);
00087     }
00088   }  
00089   public boolean getBooleanAttribute(String name) { return attributes.getBoolean(name); }  
00090   public int getEdgeCount() {
00091     int count = 0;
00092     
00093     for(Iterator i = new LinkedList(nodes).iterator(); i.hasNext(); )
00094       count += ((Node)i.next()).getOutgoingEdgeCount();
00095 
00096     return count;
00097   }  
00098   public Node getInit() { return init; }  
00099   public int getIntAttribute(String name) { return attributes.getInt(name); }  
00100   public Node getNode(int id) { 
00101     for(Iterator i = nodes.iterator(); i.hasNext(); ) {
00102       Node n = (Node)i.next();
00103       if(n.getId() == id) return n;
00104     }
00105 
00106     return null;
00107   }  
00108   public int getNodeCount() { return nodes.size(); }  
00109   public List getNodes() { return new LinkedList(nodes); }  
00110   public String getStringAttribute(String name) { return attributes.getString(name); }  
00111   private void init(Attributes a) {
00112     if(a == null)
00113       attributes = new Attributes();
00114     else
00115       attributes = a;
00116     nodes = new LinkedList();
00117     init = null;
00118   }  
00119   public static Graph load() throws IOException { return load(new BufferedReader(new InputStreamReader(System.in))); }  
00120   private static Graph load(BufferedReader in) throws IOException {
00121     int ns = readInt(in);
00122     Node[] nodes = new Node[ns];
00123 
00124     Graph g = new Graph(readAttributes(in));
00125 
00126     for(int i = 0; i < ns; i++) {
00127       int nt = readInt(in);
00128       if(nodes[i] == null)
00129     nodes[i] = new Node(g, readAttributes(in));
00130       else
00131     nodes[i].setAttributes(readAttributes(in));
00132 
00133       for(int j = 0; j < nt; j++) {
00134     int nxt = readInt(in);
00135     String gu = readString(in);
00136     String ac = readString(in);
00137 
00138     if(nodes[nxt] == null) nodes[nxt] = new Node(g);
00139 
00140     new Edge(nodes[i], nodes[nxt], gu, ac, readAttributes(in));
00141       }
00142     }
00143 
00144     g.number();
00145 
00146     return g;
00147   }  
00148   public static Graph load(String fname) throws IOException { return load(new BufferedReader(new FileReader(fname))); }  
00149   private synchronized void number() {
00150     int cnt;
00151 
00152     if(init != null) {
00153       init.setId(0);
00154       cnt = 1;
00155     } else
00156       cnt = 0;
00157 
00158     for(Iterator i = nodes.iterator(); i.hasNext(); ) {
00159       Node n = (Node)i.next();
00160 
00161       if(n != init) n.setId(cnt++);
00162     }
00163   }  
00164   private static Attributes readAttributes(BufferedReader in) throws IOException {
00165     return new Attributes(readLine(in));
00166   }  
00167   private static int readInt(BufferedReader in) throws IOException {
00168     return Integer.parseInt(readLine(in));
00169   }  
00170   private static String readLine(BufferedReader in) throws IOException {
00171     String line;
00172 
00173     do {
00174       line = in.readLine();
00175       int idx = line.indexOf('#');
00176       if(idx != -1) line = line.substring(0, idx);
00177       line = line.trim();
00178     } while(line.length() == 0);
00179 
00180     return line;
00181   }  
00182   private static String readString(BufferedReader in) throws IOException {
00183     return readLine(in);
00184   }  
00185   synchronized void removeNode(Node n) {
00186     nodes.remove(n);
00187 
00188     if(init == n)
00189       if(nodes.size() != 0)
00190     init = (Node)nodes.get(0);
00191       else
00192     init = null;
00193 
00194     number();
00195   }  
00196   public synchronized void save() { save(System.out, SM_FORMAT); }  
00197   public synchronized void save(int format) { save(System.out, format); }  
00198   private synchronized void save(PrintStream out, int format) {
00199     switch(format) {
00200       case SM_FORMAT: save_sm(out); break;
00201       case FSP_FORMAT: save_fsp(out); break;
00202     }
00203   }  
00204   public synchronized void save(String fname) throws IOException { save(new PrintStream(new FileOutputStream(fname)), SM_FORMAT); }  
00205   public synchronized void save(String fname, int format) throws IOException { save(new PrintStream(new FileOutputStream(fname)), format); }  
00206   // Modified by ckong - Sept 7, 2001
00207   private synchronized void save_fsp(PrintStream out) {
00208     if(init != null) out.println("RES = S" + init.getId());
00209     
00210     for(Iterator i = nodes.iterator(); i.hasNext(); ) {
00211       Node n = (Node)i.next();
00212 
00213       n.save(out, FSP_FORMAT);
00214       if(i.hasNext())
00215     //System.out.println(",");
00216     out.println(",");
00217       else
00218     //System.out.println(".");
00219     out.println(".");
00220     }
00221     int nsets = getIntAttribute("nsets");
00222     if(nsets == 0) {
00223       boolean first = true;
00224 
00225       //System.out.print("AS = { ");
00226       out.print("AS = { ");
00227       for(Iterator i = nodes.iterator(); i.hasNext(); ) {
00228     Node n = (Node)i.next();
00229 
00230     if(n.getBooleanAttribute("accepting")) {
00231       if(!first)
00232         //System.out.print(", ");
00233         out.print(", ");
00234       else
00235         first = false;
00236       //System.out.print("S" + n.getId());
00237       out.print("S" + n.getId());
00238     }
00239       }
00240       //System.out.println(" }");
00241       out.println(" }");
00242     } else {
00243       for(int k = 0; k < nsets; k++) {
00244     boolean first = true;
00245 
00246     //System.out.print("AS"+k+" = { ");
00247     out.print("AS"+k+" = { ");
00248     for(Iterator i = nodes.iterator(); i.hasNext(); ) {
00249       Node n = (Node)i.next();
00250 
00251       if(n.getBooleanAttribute("acc"+k)) {
00252         if(!first)
00253           //System.out.print(", ");
00254           out.print(", ");
00255         else
00256           first = false;
00257         //System.out.print("S" + n.getId());
00258         out.print("S" + n.getId());
00259       }
00260     }
00261     //System.out.println(" }");
00262     out.println(" }");
00263       }
00264     }
00265     if (out != System.out)
00266       out.close();
00267   }  
00268   private synchronized void save_sm(PrintStream out) {
00269     out.println(nodes.size());
00270     out.println(attributes);
00271     if(init != null) init.save(out, SM_FORMAT);
00272     for(Iterator i = nodes.iterator(); i.hasNext(); ) {
00273       Node n = (Node)i.next();
00274 
00275       if(n != init) n.save(out, SM_FORMAT);
00276     }
00277   }  
00278   synchronized public void setAttributes(Attributes a) { attributes = new Attributes(a); }  
00279   public synchronized void setBooleanAttribute(String name, boolean value) { attributes.setBoolean(name, value); }  
00280   public synchronized void setInit(Node n) {
00281     if(nodes.contains(n)) {
00282       init = n;
00283       number();
00284     }
00285   }  
00286   public synchronized void setIntAttribute(String name, int value) { attributes.setInt(name, value); }  
00287   public synchronized void setStringAttribute(String name, String value) { attributes.setString(name, value); }  
00288 }

Generated at Thu Feb 7 06:46:04 2002 for Bandera by doxygen1.2.10 written by Dimitri van Heesch, © 1997-2001