-
Notifications
You must be signed in to change notification settings - Fork 1
/
RoutingMapTree.java
310 lines (260 loc) · 8.62 KB
/
RoutingMapTree.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import java.util.*;
import java.io.*;
public class RoutingMapTree {
Exchange root;
Myset switchedOffPhones = new Myset();
public RoutingMapTree(){
root = new Exchange(0);
Myset switchedOffPhones = new Myset();
}
public RoutingMapTree (Exchange e){
root = e;
Myset switchedOffPhones = new Myset();
}
// add Exchange with int a as a child of int b
public void addExchange(int a,int b){
if(findExchange(b) == null)
throw new NoSuchElementException();
if(findExchange(a) != null)
throw new IllegalArgumentException();
Exchange B = findExchange(b);
Exchange A = new Exchange(a);
//B.makeChild(a);//making 'a' b's child and b a's parent
A.parent = B;
B.childList().listAddMember(A);
}
public Exchange findExchange(int a){
if(root == null)
return null;
if(root.id == a)
return root;
for (int i = 0; i < root.numChildren(); i++) {
RoutingMapTree t = root.subtree(i);
Exchange e=t.findExchange(a);
if(e!=null)
return e;
}
return null;
}
//switches On the mobilePhone numbered a
public void switchOn(MobilePhone a,Exchange b){
if(b == null)
throw new NoSuchElementException();
//location of a to b
a.location = b;
a.status = true;
//putting 'a' in mobilephoneset of every ancestor
while(b != null ){
b.residentSet().MobileInsert(a);
b = b.parent();
}
if(switchedOffPhones.IsMember(a.mobnum) == true)
switchedOffPhones.Delete(a.mobnum);
}
public void switchOff(MobilePhone a){
switchedOffPhones.Insert(a.mobnum);
Exchange b = a.location();
b.residentSet().MobileDelete(a);
while(b.parent() != null){
b = b.parent();
b.residentSet().MobileDelete(a);
}
}
public void movePhone( MobilePhone a, Exchange b){
switchOff(a);
switchOn(a,b);
}
public Exchange findPhone(MobilePhone m){
return m.location;
}
//Checking if tree contains node 'a'
public boolean isNodePresent(Exchange a){
if(a == null || root == null)
return false;
if (root.equals(a)) {
return true;
}
boolean temp = false;
if(root.numChildren() == 0)
return false;
for (int i = 0; i < root.numChildren(); i++) {
RoutingMapTree t = root.subtree(i);
temp = t.isNodePresent(a);
if (temp == true)
return true;
}
return false;
}
//returns the mobilePhone which has number = a
public MobilePhone findMobile(int a){
return root.residentSet().findMobileInSet(a);
}
public Exchange lowestRouter(Exchange a, Exchange b){
ExchangeList aAncestors = new ExchangeList();
ExchangeList bAncestors = new ExchangeList();
Exchange temp = a;//a is an ancestor of itself
while(temp.parent() != null){
aAncestors.listAddMember(temp);
temp = temp.parent();
}
aAncestors.listAddMember(root);
temp = b;
while(temp.parent() != null){
bAncestors.listAddMember(temp);
temp = temp.parent();
}
bAncestors.listAddMember(root);
return aAncestors.firstCommonExchange(bAncestors);
}
//list of call path
public ExchangeList routeCall(MobilePhone a, MobilePhone b){
Exchange A = a.location;
Exchange B = b.location;
ExchangeList callPath = new ExchangeList();
Exchange temp = A;//a is an sncestor of itself
while(temp != lowestRouter(A,B) ){
callPath.listAddMember(temp);
temp = temp.parent;
}
callPath.listAddMember(lowestRouter(A,B));
temp = lowestRouter(A,B);
for (int i = 0; i < temp.numChildren(); i++) {
if(temp.subtree(i).isNodePresent(B)){
callPath.listAddMember(temp.subtree(i).root);
temp = temp.subtree(i).root;
}
}
callPath.listAddMember(B);
return callPath;
}
public String performAction(String actionMessage){
String res = "";
String[] w = actionMessage.split(" ");
//addExchange make b the child of a
if (w[0].equals("addExchange")) {
int a,b;
a = Integer.parseInt(w[1]);
b = Integer.parseInt(w[2]);
try{
addExchange(b,a);
}
catch(NoSuchElementException e){
res = res + "addExchange: Error - No exchange with identifier "+a+" found in the network";
}
catch(IllegalArgumentException ea){
res = res + "addExchange: Error - No exchange with identifier "+b+" found in the network";
}
}
//switchOnMobile
else if (w[0].equals("switchOnMobile")) {
int a = Integer.parseInt(w[1]);
int b = Integer.parseInt(w[2]);
Exchange B = findExchange(b);
if(findMobile(a) != null){
res = "switchOnMobile: Error - There is already a mobile with number "+a+" in the network.";
}
else if(switchedOffPhones.IsMember(a))
switchedOffPhones.Delete(a);
else if(findMobile(a) == null){
MobilePhone A = new MobilePhone(a);
A.switchOn();
try{
switchOn(A,B);
}
catch(NoSuchElementException e){
res = "switchOnMobile: Error - No exchange with identifier "+b+" found in the network";
}
}
}
//switchOffMobile
else if (w[0].equals("switchOffMobile")) {
int a = Integer.parseInt(w[1]);
if(switchedOffPhones.IsMember(a))
res = "This phone is already switched off.";
else if(findMobile(a) == null)
res = "switchOffMobile: Error - No mobile Phone with identifier "+a+" found in the network";
else switchOff(findMobile(a));
}
//queryNthChild bth child of a
else if (w[0].equals("queryNthChild")) {
int a = Integer.parseInt(w[1]);
int b = Integer.parseInt(w[2]);
try{
res = "queryNthChild "+a+" "+b+": "+ findExchange(a).childList().listNthChild(b).id ;
}
catch(NoSuchElementException e){
res = "queryNthChild: Error - No exchange with identifier "+a+" found in the network";
}
catch(NullPointerException ea){
res = "queryNthChild: Error - "+b+"th child of Exchange "+a+" does not exist";
}
}
//queryMobilePhoneSet
else if(w[0].equals("queryMobilePhoneSet")) {
int a = Integer.parseInt(w[1]);
if(findExchange(a) == null)
res = "queryMobilePhoneSet: Error - No exchange with identifier "+a+" found in the network";
else{
try{
res = findExchange(a).printAllMobiles( a );
}
catch(NoSuchElementException e){
res = "queryMobilePhoneSet: Error - No exchange with identifier "+a+" found in the network";
}
}
}
//queryfindPhone with number a
else if(w[0].equals("findPhone")){
int a = Integer.parseInt(w[1]);
if(switchedOffPhones.IsMember(a))
res = "queryfindPhone "+a+": Error - Mobile phone with identifier "+a+" is currently switched off";
else try{
res = "queryFindPhone "+a+": "+findPhone(findMobile(a)).id;
}
catch(NoSuchElementException e){
res = "queryFindPhone "+a+": Error - No mobile phone with identifier "+a+" found in the network";
}
catch(NullPointerException ae){
res = "queryFindPhone "+a+": Error - No mobile phone with identifier "+a+" found in the network";
}
}
//movePhone of a to b
else if(w[0].equals("movePhone")){
int a = Integer.parseInt(w[1]);
int b = Integer.parseInt(w[2]);
if(findExchange(b) == null)
res = "movePhone: Error - No exchange with identifier "+b+" found in the network";
if(findMobile(a) == null)
res = "movePhone: Error - No mobile phone with identifier "+a+" found in the network";
if(switchedOffPhones.IsMember(a))
res = "movePhone: Error - mobile phone with identifier "+a+" is currently switched off";
else movePhone(findMobile(a),findExchange(b));
}
//querylowestrouter
else if (w[0].equals("lowestRouter")) {
int a = Integer.parseInt(w[1]);
int b = Integer.parseInt(w[2]);
if(findExchange(a) == null)
res = "queryLowestRouter: Error - No exchange with identifier "+a+" found in the network";
if(findExchange(b) == null)
res = "queryLowestRouter: Error - No exchange with identifier "+b+" found in the network";
else res = "queryLowestRouter "+a+" "+b+": "+lowestRouter(findExchange(a),findExchange(b)).id;
}
//queryFindCallPath
else if (w[0].equals("findCallPath")) {
int a = Integer.parseInt(w[1]);
int b = Integer.parseInt(w[2]);
if (switchedOffPhones.IsMember(a))
res = "queryFindCallPath "+a+" "+b+": Error - Mobile phone with identifier "+a+" is currently switched off";
if (switchedOffPhones.IsMember(b))
res = "queryFindCallPath "+a+" "+b+": Error - Mobile phone with identifier "+b+" is currently switched off";
else if(findMobile(a) == null)
res = "queryFindCallPath "+a+" "+b+": Error - No mobile phone with identifier "+a+" found in the network";
else if(findMobile(b) == null)
res = "queryFindCallPath "+a+" "+b+": Error - No mobile phone with identifier "+b+" found in the network";
else res = res + routeCall(findMobile(a),findMobile(b)).printAllExchanges(a,b);
}
System.out.println(res);
return res;
}
}