To load this file without formatting, visit http://whoyouknow.co.uk/ants/AntGraph/java/1/src/antgraph/Graph.java. This is a spam-protection measure; sorry for the inconvenience.

· Graph.java ·

   1/*
   2 * Graph.java
   3 *
   4 * Created on 02 March 2007, 22:52
   5 *
   6 * To change this template, choose Tools | Template Manager
   7 * and open the template in the editor.
   8 */
   9
  10package antgraph;
  11
  12
  13import java.io.FileInputStream;
  14import java.io.FileOutputStream;
  15import java.io.ObjectInputStream;
  16import java.io.ObjectOutputStream;
  17import java.io.Serializable;
  18import java.util.ArrayList;
  19import java.util.HashMap;
  20import java.util.List;
  21
  22/**
  23 *
  24 * @author James
  25 */
  26public class Graph implements Serializable {
  27
  28    public static final String EXTENSION = "antz";
  29    
  30    private List<Node> nodes = new ArrayList<Node>();
  31    private HashMap<Node, List<Node>> adjacencyList = new HashMap<Node, List<Node>>();
  32    private HashMap<String, Edge> edges = new HashMap<String, Edge>();
  33    
  34    public Graph() {
  35        
  36    }
  37    
  38    public void addNode(Node n) {
  39        nodes.add(n);
  40    }
  41    
  42    public void addNode(Object o) {
  43        this.addNode(new Node(o));
  44    }
  45    
  46    public void removeNode(Node n) {
  47        System.out.println("Removing " + n + "...");
  48        nodes.remove(n);
  49        
  50        for(Node node : adjacencyList.get(n)) {
  51            edges.remove(n.toString()       + node.toString());
  52            edges.remove(node.toString()    + n.toString());
  53        }
  54        
  55        adjacencyList.remove(n);
  56        
  57        for(List l : adjacencyList.values()) {
  58            l.remove(n);
  59        }
  60        
  61        
  62    }
  63    
  64    public int getNumberOfVertices() {
  65        return nodes.size();
  66    }    
  67    
  68    public void addEdge(Node a, Node b) throws NoSuchNodeException {
  69        System.out.println("Creating an edge between " + a + " and " + b);
  70        
  71        if(hasEdge(a, b) || a.equals(b)) return;
  72        
  73        Edge e = new Edge(a, b);
  74        Edge e1 = new Edge(b, a);
  75        
  76        edges.put(a.toString() + b.toString(), e);
  77        edges.put(b.toString() + a.toString(), e1);
  78        
  79        if(!nodes.contains(a))
  80            throw new NoSuchNodeException("No such node: " + a);
  81        if(!nodes.contains(b))
  82            throw new NoSuchNodeException("No such node: " + b);
  83        
  84        if(!adjacencyList.containsKey(a)) {
  85            adjacencyList.put(a, new ArrayList());
  86        }  
  87         if(!adjacencyList.containsKey(b)) {
  88            adjacencyList.put(b, new ArrayList());
  89        }  
  90
  91        adjacencyList.get(a).add(b);
  92        adjacencyList.get(b).add(a);
  93       System.out.println(adjacencyList.get(a));
  94    }
  95    
  96    public Edge getEdge(Node a, Node b) {
  97        return edges.get(a.toString() + b.toString());
  98    }
  99    
 100    public List<Edge> getEdges(Node node) {
 101        List<Edge> edges = new ArrayList();
 102        for(Edge e : getEdges()) {
 103            if(e.from(node)) edges.add(e);
 104        }
 105        return edges;
 106    }
 107    
 108    public List<Edge> getEdges() {
 109        return new ArrayList(edges.values());
 110    }
 111    
 112    public boolean hasEdge(Node a, Node b) {
 113        try {
 114            return adjacencyList.get(a).contains(b) || adjacencyList.get(b).contains(a);
 115        }catch (NullPointerException e) {
 116            return false;
 117        }
 118    }
 119    
 120    public List<Node> getAdjacencyList(Node n) {
 121        return adjacencyList.containsKey(n) ? adjacencyList.get(n) : new ArrayList();
 122    }
 123    
 124    public HashMap<Node, List<Node>> getAdjacencyList() {
 125        return adjacencyList;
 126    }
 127    
 128    public void print() {
 129        print(this);
 130    }
 131    
 132    public List<Node> getNodes() {
 133        return nodes;
 134    }
 135
 136    
 137    public static void print(Graph g) {
 138
 139        System.out.println(" --- Graph --- ");
 140        
 141        for(Node n : g.getNodes()) {
 142            
 143            System.out.println(n + ": " + g.getAdjacencyList(n));
 144
 145        }
 146        System.out.println(" ------------ ");
 147    }
 148
 149    private boolean isDirty = false;
 150    
 151    public boolean isDirty() {
 152        return isDirty;
 153    }
 154    
 155    public void setDirty(boolean value) {
 156        isDirty = value;
 157    }
 158    
 159    public void save(String sFileName) {
 160        try {
 161            
 162            FileOutputStream f_out = new FileOutputStream(sFileName);
 163
 164            ObjectOutputStream obj_out = new ObjectOutputStream(f_out);
 165            obj_out.writeObject(this);
 166            setDirty(false);
 167        }catch (Exception e) {
 168            System.out.println(e);
 169        }
 170    }  
 171    
 172    public Graph load(String sFileName) {
 173
 174        try {
 175            FileInputStream f_in = new FileInputStream(sFileName);
 176        
 177            ObjectInputStream obj_in = new ObjectInputStream (f_in);
 178            
 179            Graph graph = (Graph)obj_in.readObject();
 180
 181            setDirty(false);
 182        } catch (Exception e) {
 183            System.out.println(e);
 184        }
 185        
 186        return this;
 187    }
 188
 189    public void save() {
 190        save("unnamed_graph");
 191    }    
 192}
 193

· Graph.java ends ·

Generated by CHIP: Code Highlighting in PHP, version 2.7.0.