-
Notifications
You must be signed in to change notification settings - Fork 0
/
Node.java
204 lines (175 loc) · 5.31 KB
/
Node.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* Course: 5DV133
* Written by: Erik Dahlberg c15edg
* @Author Group 15 - 5DV133 vt2016
* @Version 1.0
*/
import java.util.*;
/**
* Class Node represent the nodes in the Network, that hold information
* about Actions and how to find an Action.
* Will only read or send a Message every time the takeNextMessage is called,
* this is the method that will make the Message Move around
* from one node to another to spread information in the NetWork.
*
*/
public class Node {
private Position nodePosition;
private int nodeID;
private LinkedList<Message> toDoList;
private ArrayList<Node> neighbours;
private Hashtable<String, Action> actionTable;
private boolean waitOnTurn = true;
private Message newMessage;
/**
* Create new Node
* @param ID The ID that Node will have
* @param p The Position that Node will have
*/
public Node(int ID, Position p) {
this.nodeID = ID;
this.nodePosition = p;
neighbours = new ArrayList<>();
toDoList = new LinkedList<>();
actionTable = new Hashtable<>();
}
/**
* Used to look at first Message in toDoList
* without removing the Message, will return null if list is Empty
* @return First Message in toDoList without, null if list is Empty
*/
public Message peekAtQueue() {
return toDoList.peekFirst();
}
/**
* Will set ID of the node.
* @param ID Updates NodeID to inserted int
*/
public void setNodeID(int ID) {
nodeID = ID;
}
/**
* Used to get current ID
* @return Nodes ID as an int
*/
public int getNodeID() {
return nodeID;
}
/**
* Will ad Node's Position to Action then insert it to Nodes actionTable
* @param a Action that Node will insert to actionTable
*/
public void setAction(Action a) {
a.setNextPosition(nodePosition);
actionTable.put(a.getActionID(), a);
}
/**
* Will return specified Action from Nodes actionTable
* @param actionID String for the action to return
* @return Action from Nodes actionTable
*/
public Action getAction(String actionID) {
return actionTable.get(actionID);
}
/**
* Will return Nodes actionTable
* @return Node's actonTable ((HashTable)
*/
public Hashtable<String, Action> getActionTable() {
return actionTable;
}
/**
* Method is created to only read or send a Message every time
* the method is called.
*
* It prioritizes sending message over reading. First step is to
* check the first message of the queue, and next timeStep it forwards
* the message by using its move method.
*/
public void takeNextMessage() {
if (waitOnTurn) {
if (!toDoList.isEmpty()) {
newMessage = toDoList.pollFirst();
}
else{
newMessage=null;
}
}
if (!waitOnTurn) {
if (newMessage != null) {
newMessage.move(this);
}
}
if(waitOnTurn){
waitOnTurn = false;
}else{
waitOnTurn = true;
}
}
/**
* Will take in param actionID for the Node it is searching for.
* Locates the the Position for the param Node and returns
* the Node in ActionTable that has the same Position.
* @param actionID The actionID to search for.
* @return neighbor Node that RequestMessage should go to
*/
public Node NextNode(String actionID) {
for (int i = 0; i < neighbours.size(); i++) {
if (neighbours.get(i).getPosition().equals(actionTable.get(actionID).getNextPosition())) {
return neighbours.get(i);
}
}
System.err.println("NextNode could not find that Node in AgentTable");
return null;
}
/**
* Will set Nodes position to inserted param
* @param p -Position to set as Node's Position
*/
public void setPosition(Position p) {
nodePosition = p;
}
/**
* Will return the Position of the Node
* @return - Nodes Position
*/
public Position getPosition() {
return nodePosition ;
}
/**
* Will add AgentMassage to last position and
* RequestMessage to first position in queue toDoList.
* @param e Massage that will be added to queue addToDo
*/
public void addToDo(Message e) {
if (e instanceof RequestMessage) {
toDoList.addFirst(e);
} else {
toDoList.add(e);
}
}
/**
* Will add Node to neighbour list
* @param n Node that will be added to the current nodes neighbour-list.
*/
public void setNeighbour(Node n) {
neighbours.add(n);
}
/**
* Will return list with all the current nodes neighbours.
* @return list of the current nodes neighbours.
*/
public ArrayList<Node> getNeighbours() {
return neighbours;
}
/**
* Method to se if nodes x and y position is equal to param
* @param o object
* @return boolean if objects are equal
*/
public boolean equals(Object o) {
Node n = (Node) o;
return this.nodePosition.getX() == n.nodePosition.getX()
&& this.nodePosition.getY() == n.nodePosition.getY();
}
}