1 import java
.util
.Vector
;
2 import java
.util
.Hashtable
;
3 import java
.util
.Enumeration
;
6 * GraphNode, used by Graph to keep track of the nodes
7 * in a network and to make a routing table.
9 * @author "Anton Johansson" <anton.johansson@gmail.com>
10 * @author "Victor Zamanian" <victor.zamanian@gmail.com>
12 public class GraphNode
implements Comparable
{
13 private int maxNeighbours
;
14 private Hashtable neighbours
;
18 private Double distanceToGoal
;
19 private boolean visited
;
20 private Double distanceTraveled
;
21 private GraphNode parent
;
22 private Double evalFuncVal
;
25 * Creates a new GraphNode instance.
27 * @param name The name of the node.
28 * @param maxNeighbours The maximum amount of
29 * neighbours the node can have.
31 public GraphNode(String name
, int x
, int y
, int maxNeighbours
) {
36 this.maxNeighbours
= maxNeighbours
;
37 this.evalFuncVal
= Double
.MIN_VALUE
;
38 neighbours
= new Hashtable(maxNeighbours
);
42 * Adds a neighbour to this node.
44 * @param node The neighbour node to be added.
45 * @param weight The weight of the potential edge between
46 * this node and the new neighbour.
48 public void addNeighbour(GraphNode node
, Object edge
) {
49 neighbours
.put(node
, edge
);
52 public void deleteNeighbour(GraphNode node
) {
53 neighbours
.remove(node
);
57 * Inspects the weight of the potential edge between this
58 * node and one of its neighbours.
60 * @param node The neighbour of this node.
61 * @return The weight of the potential edge.
63 public Object
getEdge(GraphNode node
) {
64 return neighbours
.get(node
);
68 * Visists this node--sets its visited attribute to true.
75 * Inspects whether this node has been visited or not.
77 * @return true if this node has been visited, else false.
79 public boolean isVisited() {
84 * Inspects all the neighbours (nodes) of this node.
86 * @return An Enumeration of all neighbours of this node..
88 public Enumeration
getNeighbours() {
89 return neighbours
.keys();
93 * Inspects the NAME address of this node.
95 * @return The NAME address of this node.
97 public String
getName() {
102 * Sets the distance to this node from the root node in
103 * the process of Dijkstras shortest-path algorithm.
105 * @param distance The distance to this node.
107 public void setDistanceTraveled(Double distanceTraveled
) {
108 this.distanceTraveled
= distanceTraveled
;
112 * Inspects the distance from this node to the root node. See "setDistance."
114 * @return The distance from this node to the root node.
116 public Double
getDistanceTraveled() {
117 return this.distanceTraveled
;
121 * Sets this node's parent node. Used in Dijkstras algorithm.
123 * @param parent The parent to be set.
125 public void setParent(GraphNode parent
) {
126 this.parent
= parent
;
130 * Inspects this node's parent.
132 * @return The parent node of this parent.
134 public GraphNode
getParent() {
139 * Access to this nodes x-coordinate.
141 * @return the x-coordinate of this node.
148 * Access to this nodes y-coordinate.
150 * @return the y-coordinate of this node.
157 * Calculate and return the distance to x- y-coordinates
159 * @param node the Node to calculate distance to
160 * @return distance to the node provided
162 public void setDistanceToGoal(GraphNode goal
) {
163 this.distanceToGoal
= Math
.hypot((this.x
- goal
.getX()),
164 (this.y
- goal
.getY()));
168 * Retun the distance to Goal.
170 * @return the distance to goal.
172 public Double
getDistanceToGoal() {
173 return this.distanceToGoal
;
176 public void setEvalFuncVal(Double evalFuncVal
) {
177 this.evalFuncVal
= evalFuncVal
;
180 public Double
getEvalFuncVal() {
181 return this.evalFuncVal
;
185 * Compares this node to another graph node.
187 * @param node The node to compare this node to.
188 * @return 0 if the addresses match, a positive integer if
189 * this node has a higher NAME address than the other node,
190 * and a negative integer if this node has a lower NAME address
191 * than the other node.
193 public int compareTo(Object node
) {
194 return this.evalFuncVal
.compareTo(((GraphNode
) node
).getEvalFuncVal());
197 public String
toString() {
198 String returnString
= name
+ "\n";
199 for (Enumeration e
= getNeighbours(); e
.hasMoreElements();) {
200 GraphNode neighbour
= (GraphNode
) e
.nextElement();
201 returnString
+= " " + neighbour
.getName()
202 + ", traveltime: " + this.getEdge(neighbour
) +"\n";