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>
13 public class GraphNode
implements Comparable
{
14 private int maxNeighbours
;
15 private Hashtable neighbours
;
19 private Double distanceToGoal
;
20 private boolean visited
;
21 private Double distanceTraveled
= Double
.MAX_VALUE
;
22 private GraphNode parent
;
23 private Double evalFuncVal
;
26 * Creates a new GraphNode instance.
28 * @param name The name of the node.
29 * @param x the x-coordinate of this node.
30 * @param y the y-coordinate of this node.
31 * @param maxNeighbours The maximum amount of
32 * neighbours the node can have.
34 public GraphNode(String name
, int x
, int y
, int maxNeighbours
) {
39 this.maxNeighbours
= maxNeighbours
;
40 this.evalFuncVal
= Double
.MIN_VALUE
;
41 neighbours
= new Hashtable(maxNeighbours
);
45 * Adds a neighbour to this node.
47 * @param node The neighbour node to be added.
48 * @param edge The edge between this node and the new neighbour.
50 public void addNeighbour(GraphNode node
, Object edge
) {
51 neighbours
.put(node
, edge
);
55 * Removes the specified neighbour of this node.
57 * @param node The neighbour to remove.
59 public void deleteNeighbour(GraphNode node
) {
60 neighbours
.remove(node
);
64 * Inspects the weight of the potential edge between this
65 * node and one of its neighbours.
67 * @param node The neighbour of this node.
68 * @return The weight of the potential edge.
70 public Object
getEdge(GraphNode node
) {
71 return neighbours
.get(node
);
75 * Visists this node--sets its visited attribute to true.
82 * Inspects whether this node has been visited or not.
84 * @return true if this node has been visited, else false.
86 public boolean isVisited() {
91 * Inspects all the neighbours (nodes) of this node.
93 * @return An Enumeration of all neighbours of this node..
95 public Enumeration
getNeighbours() {
96 return neighbours
.keys();
100 * Inspects the name of this node.
102 * @return The name of this node.
104 public String
getName() {
109 * Sets the distance to this node from the root node. This is
110 * usefull in algorithms.
112 * @param distanceTraveled The distanceTraveled by this node.
114 public void setDistanceTraveled(Double distanceTraveled
) {
115 this.distanceTraveled
= distanceTraveled
;
119 * Inspects the distance from this node to the root node. See "setDistance."
121 * @return The distance from this node to the root node.
123 public Double
getDistanceTraveled() {
124 return this.distanceTraveled
;
128 * Sets this node's parent node. Used in algorithms.
130 * @param parent The parent to be set.
132 public void setParent(GraphNode parent
) {
133 this.parent
= parent
;
137 * Inspects this node's parent.
139 * @return The parent node of this parent.
141 public GraphNode
getParent() {
146 * Access to this nodes x-coordinate.
148 * @return the x-coordinate of this node.
155 * Access to this nodes y-coordinate.
157 * @return the y-coordinate of this node.
164 * Calculate and return the distance to x- y-coordinates
166 * @param goal the Node to calculate distance to.
168 public void setDistanceToGoal(GraphNode goal
) {
169 this.distanceToGoal
= Math
.hypot((this.x
- goal
.getX()),
170 (this.y
- goal
.getY()));
174 * Retun the distance to Goal.
176 * @return the distance to goal.
178 public Double
getDistanceToGoal() {
179 return this.distanceToGoal
;
183 * Sets the value evalFuncVal, this is used in search algorithms.
185 * @param evalFuncVal the evalFuncVal value.
187 public void setEvalFuncVal(Double evalFuncVal
) {
188 this.evalFuncVal
= evalFuncVal
;
192 * Returns the value evalFuncVal, this is used in search
195 * @return The evalFuncVal value.
197 public Double
getEvalFuncVal() {
198 return this.evalFuncVal
;
202 * Compares this node to another graph node. The comparison uses
203 * the nodes evalFuncVal values.
205 * @param node The node to compare this node to.
206 * @return 0 if the addresses match, a positive integer if
207 * this node has a higher NAME address than the other node,
208 * and a negative integer if this node has a lower NAME address
209 * than the other node.
211 public int compareTo(Object node
) {
212 return this.evalFuncVal
.compareTo(((GraphNode
) node
).getEvalFuncVal());
216 * Returns a String representation of this GraphNode.
218 * @return a a String representation of this GraphNode.
220 public String
toString() {
221 String returnString
= name
+ "\n";
222 for (Enumeration e
= getNeighbours(); e
.hasMoreElements();) {
223 GraphNode neighbour
= (GraphNode
) e
.nextElement();
224 returnString
+= " " + neighbour
.getName()
225 + ", traveltime: " + this.getEdge(neighbour
) +"\n";