To load this file without formatting, visit http://whoyouknow.co.uk/ants/AntGraph/java/4/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 *dfdf
   6 * To change this template, choose Tools | Template Manager
   7 * and open the template in the editor.
   8 */
   9
  10package antgraph;
  11
  12
  13import antgraph.Pheromone.Direction;
  14import antgraph.gui.GraphJFrame;
  15import java.awt.Image;
  16import java.awt.image.BufferedImage;
  17import java.util.ArrayList;
  18import java.util.Collections;
  19import java.util.HashMap;
  20import java.util.List;
  21
  22/**
  23 *
  24 * @author James
  25 */
  26public class Graph {
  27
  28    
  29    private List<Node> nodes = new ArrayList<Node>();
  30    private HashMap<Node, List<Node>> adjacencyList = new HashMap<Node, List<Node>>();
  31    private HashMap<String, Edge> edges = new HashMap<String, Edge>();
  32    
  33    private boolean isDirty = false;
  34    
  35    private List<Ant> ants = new ArrayList<Ant>();
  36    
  37    private int c =1;
  38    private char currentChar = 'a' - 1;   
  39    
  40    public Graph() {
  41        System.out.println(this);
  42    }
  43    public void addAnt(Ant listener) {
  44        
  45        ants.add(listener);
  46        System.out.println(ants);
  47    }
  48
  49    
  50    public void addNode(Node n) {
  51        nodes.add(n);
  52    }
  53    
  54    public void addNode(String o) {
  55        this.addNode(new Node(o));
  56    }
  57    
  58    public void addNode() {
  59        this.addNode(new Node(this.getNextChar()));
  60    }
  61    
  62    public void addNode(int x, int y) {
  63        Node n = new Node(this.getNextChar(), x, y);
  64       // n.setLocation(x, y);
  65        this.addNode(n);
  66    }
  67    
  68     public void addNode(String s, int x, int y) {
  69        Node n = new Node(s, x, y);
  70        //n.setLocation(x, y);
  71        this.addNode(n);
  72    }
  73     
  74    public void removeNode(Node n) {
  75        System.out.println("Removing " + n + "...");
  76       
  77        
  78        for(Node node : getNodes()) {
  79            removeEdge(node, n);
  80        }
  81         
  82        nodes.remove(n);
  83        
  84    }
  85    
  86    public int getNumberOfVertices() {
  87        return nodes.size();
  88    }    
  89    
  90    public void addEdge(Node a, Node b) throws NoSuchNodeException {
  91     
  92        
  93        if(hasEdge(a, b) || a.equals(b)) return;
  94           System.out.println("Creating an edge between " + a + " and " + b);
  95        Edge e = new Edge(a, b);
  96    
  97        edges.put(a.toString() + b.toString(), e);
  98      
  99        if(!nodes.contains(a))
 100            throw new NoSuchNodeException("No such node: " + a);
 101        if(!nodes.contains(b))
 102            throw new NoSuchNodeException("No such node: " + b);
 103        
 104        if(!adjacencyList.containsKey(a)) {
 105            adjacencyList.put(a, new ArrayList());
 106        }  
 107         if(!adjacencyList.containsKey(b)) {
 108            adjacencyList.put(b, new ArrayList());
 109        }  
 110
 111        adjacencyList.get(a).add(b);
 112        adjacencyList.get(b).add(a);
 113  
 114    }
 115    
 116    public Edge getEdge(Node a, Node b) {
 117        if(hasEdge(a, b)) {
 118            if(edges.containsKey(a.toString() + b.toString()))
 119                return edges.get(a.toString() + b.toString());
 120            else
 121                return edges.get(b.toString() + a.toString());
 122        }
 123        return null;
 124    }
 125    
 126    public List<Edge> getEdges(Node node) {
 127        List<Edge> edges = new ArrayList();
 128
 129        for(Node n : getAdjacencyList(node)) {
 130            edges.add(getEdge(node, n));
 131        }
 132        return edges;
 133    }
 134
 135    public List<Node> getNodes(Direction direction) {
 136        List<Node> edges = new ArrayList();
 137        for(Edge e : getEdges()) {
 138            if(e.getPheromone().getDirection() == direction) {
 139                if(!edges.contains(e.a()))
 140                    edges.add(e.a());
 141                if(!edges.contains(e.b()))
 142                    edges.add(e.b());                
 143            }
 144        }
 145
 146        return edges;        
 147    }    
 148    
 149    public List<Edge> getEdges(Direction direction) {
 150        List<Edge> edges = new ArrayList();
 151        for(Edge e : getEdges()) {
 152            if(e.getPheromone().getDirection() == direction) edges.add(e);
 153        }
 154
 155        return edges;        
 156    }
 157    
 158    public List<Edge> getEdges() {
 159        return new ArrayList(edges.values());
 160    }
 161
 162    public HashMap<String, Edge> getEdgesMap() {
 163        return edges;
 164    }    
 165    
 166    public boolean hasEdge(Node a, Node b) {
 167        try {
 168            return adjacencyList.get(a).contains(b) || adjacencyList.get(b).contains(a);
 169        }catch (NullPointerException e) {
 170            return false;
 171        }
 172    }
 173    
 174    public void removeEdge(Node a, Node b) {
 175        edges.remove(a.toString() + b.toString());
 176        edges.remove(b.toString() + a.toString());
 177                
 178        getAdjacencyList(a).remove(b);
 179        getAdjacencyList(b).remove(a);        
 180    }
 181    
 182    public void removeEdge(Edge e) {
 183        removeEdge(e.a(), e.b());
 184    }
 185    
 186    public List<Node> getAdjacencyList(Node n) {
 187        return adjacencyList.containsKey(n) ? adjacencyList.get(n) : new ArrayList();
 188    }
 189    
 190    public HashMap<Node, List<Node>> getAdjacencyList() {
 191        return adjacencyList;
 192    }
 193    
 194    public List<Node> getNodes() {
 195        return nodes;
 196    }    
 197    
 198    public List<GraphComponent> getComponents() {
 199        List l = new ArrayList(nodes);
 200        l.addAll(edges.values());
 201        return l;
 202    }
 203    
 204    public void highlight(GraphComponent gc) {
 205        if(gc != null) {
 206            if(gc instanceof Edge) 
 207                for(Edge e : getEdges())
 208                    e.highlight(false);
 209            else
 210                for(Node e : getNodes())
 211                    e.highlight(false);            
 212            gc.highlight(true);
 213        }
 214    }
 215    
 216    public void highlight(List<Node> nodes) {
 217        try {
 218            nodes.add(nodes.get(0));
 219            for(int i = 0; i < nodes.size() - 1; i++) {
 220                getEdge(nodes.get(i), nodes.get(i + 1)).highlight(true);
 221                nodes.get(i).highlight(true);
 222            }
 223            nodes.get(nodes.size()-1).highlight(true);
 224            
 225        }catch (NullPointerException e) {}
 226    }
 227    
 228    public void highlightShortest(Node start, Node end) {
 229        Node pNode = null;
 230        Node node = start;
 231        List<Edge> edges;
 232        
 233        Edge edge = null;
 234        
 235        
 236        for(Edge edge1 : getEdges())
 237            edge1.setSelected(false);
 238        
 239        while(!node.equals(end)) {
 240            edges = getEdges(node);
 241            if(pNode != null) edges.remove(getEdge(pNode, node));
 242            
 243            Collections.sort(edges);
 244            
 245            
 246            edge = edges.get(0);
 247            
 248            
 249            edge.setSelected(true);
 250            node.setSelected(true);
 251            
 252            pNode = node;
 253            node = edge.other(node);
 254            
 255            
 256        }
 257    }
 258    
 259    public void highlightShortest(String a, String b) throws NoSuchNodeException {
 260        highlightShortest(getNode(a), getNode(b));
 261    }
 262    
 263    public void highlightShortest(Node a, String b) throws NoSuchNodeException {
 264        highlightShortest(a, getNode(b));
 265    }
 266
 267    public void highlightShortest(String a, Node b) throws NoSuchNodeException {
 268        highlightShortest(b, getNode(a));
 269    }    
 270    
 271    public void print() {
 272        print(this);
 273    }
 274    
 275    public static void print(Graph g) {
 276
 277        System.out.println(" --- Graph --- ");
 278        
 279        for(Node n : g.getNodes()) {
 280            
 281            System.out.println(n + ": " + g.getAdjacencyList(n));
 282
 283        }
 284        System.out.println(" ------------ ");
 285    }
 286
 287    public void step() {
 288
 289        
 290        for(Ant ant : ants) {
 291            ant.move();
 292        }
 293        
 294        if(getC() % 3 == 0) {
 295            for(Edge edge : getEdges()) {
 296                edge.getPheromone().decrease();
 297            }
 298        }
 299        c++;
 300        
 301        if(getNest().countFood() == GraphJFrame.FOOD) {
 302            //System.out.println(GraphJFrame.getGraph().getNest().countFood());
 303            try {
 304                highlightShortest(getNest(), "l");
 305            }catch (NoSuchNodeException ex) {
 306                System.out.println(ex);
 307            }
 308            
 309            
 310        }
 311    }
 312    
 313    public boolean isDirty() {
 314        return isDirty;
 315    }
 316    
 317    public void setDirty(boolean value) {
 318        isDirty = value;
 319    }
 320    
 321    public char getCurrentChar() {
 322        return currentChar;
 323    }
 324    
 325    public void setCurrentChar(char c) {
 326        currentChar = c;
 327    }
 328    
 329    public char getNextChar() {
 330        currentChar++;
 331        return getCurrentChar();
 332    } 
 333    
 334    public Node getNestNode() {
 335        for(Node n : getNodes()) {
 336            if(n.isNest()) return n;
 337        }
 338        return null;        
 339    }
 340    
 341    public Nest getNest() {
 342        return getNestNode() != null ? getNestNode().getNest() : null;
 343    }
 344    
 345    public Node getNode(String s) throws NoSuchNodeException {
 346        for(Node n : getNodes()) {
 347            if(n.toString().equals(s)) return n;
 348        }
 349        throw new NoSuchNodeException("No Such Node: " + s);
 350    }
 351
 352    public int getC() {
 353        return c;
 354    }
 355}
 356

· Graph.java ends ·

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