-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjobscheduler.java
More file actions
406 lines (343 loc) · 11.1 KB
/
Copy pathjobscheduler.java
File metadata and controls
406 lines (343 loc) · 11.1 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
public class jobscheduler {
static void predecessor(int JobId, rbt RBT) {
int temp;
if(RBT.contains(JobId) == true) {
int tempExe = RBT.get(JobId).exeTime;
int tempTotal = RBT.get(JobId).totalTime;
RBT.delete(JobId);
temp = RBT.floor(JobId);
RBT.put(JobId, tempExe, tempTotal);
if(temp == 0) {
System.out.println("(" + temp +"," + 0 + "," + 0 + ")");
}
else {
System.out.println("(" + temp +"," + RBT.get(temp).exeTime + "," + RBT.get(temp).totalTime + ")");
}
return;
}
else {
temp = RBT.floor(JobId);
if(temp == 0) {
System.out.println("(" + temp +"," + 0 + "," + 0 + ")");
}
else {
System.out.println("(" + temp +"," + RBT.get(temp).exeTime + "," + RBT.get(temp).totalTime + ")");
}
return;
}
}
static void successor(int JobId, rbt RBT) {
int temp;
if(RBT.contains(JobId) == true) {
int tempExe = RBT.get(JobId).exeTime;
int tempTotal = RBT.get(JobId).totalTime;
RBT.delete(JobId);
temp = RBT.ceiling(JobId);
RBT.put(JobId, tempExe, tempTotal);
if(temp == 0) {
System.out.println("(" + temp +"," + 0 + "," + 0 + ")");
}
else {
System.out.println("(" + temp +"," + RBT.get(temp).exeTime + "," + RBT.get(temp).totalTime + ")");
}
return;
}
else {
temp = RBT.ceiling(JobId);
if(temp == 0) {
System.out.println("(" + temp +"," + 0 + "," + 0 + ")");
}
else {
System.out.println("(" + temp +"," + RBT.get(temp).exeTime + "," + RBT.get(temp).totalTime + ")");
}
return;
}
}
static int getSuccId(int JobId, rbt RBT) {
int temp;
if(RBT.contains(JobId) == true) {
int tempExe = RBT.get(JobId).exeTime;
int tempTotal = RBT.get(JobId).totalTime;
RBT.delete(JobId);
temp = RBT.ceiling(JobId);
RBT.put(JobId, tempExe, tempTotal);
return temp;
}
else {
temp = RBT.ceiling(JobId);
return temp;
}
}
static int getPreviousId(int JobId, rbt RBT) {
int temp;
if(RBT.contains(JobId) == true) {
int tempExe = RBT.get(JobId).exeTime;
int tempTotal = RBT.get(JobId).totalTime;
RBT.delete(JobId);
temp = RBT.floor(JobId);
RBT.put(JobId, tempExe, tempTotal);
return temp;
}
else {
temp = RBT.floor(JobId);
return temp;
}
}
static void Range(int lowId, int highId, rbt RBT) {
/*both low and high in*/
if(RBT.contains(lowId) == true && RBT.contains(highId) == true) {
int temp = lowId;
System.out.println("(" + lowId +"," + RBT.get(lowId).exeTime + "," + RBT.get(lowId).totalTime + ")");
while(true) {
if(getSuccId(temp,RBT) < highId && getSuccId(temp,RBT) != 0) {
temp = getSuccId(temp,RBT);
System.out.print("(" + temp +"," + RBT.get(temp).exeTime + "," + RBT.get(temp).totalTime + ")" + ",");
}
else if(getSuccId(temp,RBT) == highId && getSuccId(temp,RBT) != 0) {
temp = getSuccId(temp,RBT);
System.out.println("(" + temp +"," + RBT.get(temp).exeTime + "," + RBT.get(temp).totalTime + ")");
}
else return;
}
}
/*low in high out*/
else if(RBT.contains(lowId) == true && RBT.contains(highId) == false) {
int temp = lowId;
System.out.println("(" + lowId +"," + RBT.get(lowId).exeTime + "," + RBT.get(lowId).totalTime + ")");
while(true) {
if(getSuccId(temp,RBT) < getPreviousId(highId,RBT) && getSuccId(temp,RBT) != 0) {
temp = getSuccId(temp,RBT);
System.out.print("(" + temp +"," + RBT.get(temp).exeTime + "," + RBT.get(temp).totalTime + ")" + ",");
}
else if(getSuccId(temp,RBT) == getPreviousId(highId,RBT) && getSuccId(temp,RBT) != 0) {
temp = getSuccId(temp,RBT);
System.out.println("(" + temp +"," + RBT.get(temp).exeTime + "," + RBT.get(temp).totalTime + ")");
}
else return;
}
}
/*low out high in*/
else if(RBT.contains(lowId) == false && RBT.contains(highId) == true) {
int temp = lowId;
//System.out.println("(" + lowId +"," + RBT.get(lowId).exeTime + "," + RBT.get(lowId).totalTime + ")");
while(true) {
if(getSuccId(temp,RBT) < highId && getSuccId(temp,RBT) != 0) {
temp = getSuccId(temp,RBT);
System.out.print("(" + temp +"," + RBT.get(temp).exeTime + "," + RBT.get(temp).totalTime + ")" + ",");
}
else if(getSuccId(temp,RBT) == highId) {
temp = getSuccId(temp,RBT);
System.out.println("(" + temp +"," + RBT.get(temp).exeTime + "," + RBT.get(temp).totalTime + ")");
}
else return;
}
}
/*low out high out*/
if(RBT.contains(lowId) == false && RBT.contains(highId) == false) {
int temp = lowId;
int flag = 0;
//System.out.println("(" + lowId +"," + RBT.get(lowId).exeTime + "," + RBT.get(lowId).totalTime + ")");
while(true) {
if(getSuccId(temp,RBT) < getPreviousId(highId,RBT) && getSuccId(temp,RBT) != 0) {
temp = getSuccId(temp,RBT);
System.out.print("(" + temp +"," + RBT.get(temp).exeTime + "," + RBT.get(temp).totalTime + ")" + ",");
flag = 1;
}
else if(getSuccId(temp,RBT) == getPreviousId(highId,RBT) && getSuccId(temp,RBT) != 0) {
temp = getSuccId(temp,RBT);
System.out.println("(" + temp +"," + RBT.get(temp).exeTime + "," + RBT.get(temp).totalTime + ")");
flag = 1;
}
else {
if(flag == 0) {
System.out.println("(" + 0 + "," + 0 + "," + 0 + ")");
}
return;
}
}
}
// if()
// if()
// if()
}
static int parseJobdId(String str) {
int parenStart = str.indexOf("(");
int comma = str.indexOf(",");
return Integer.parseInt(str.substring(parenStart + 1, comma));
}
static int parsetotalTime(String str) {
int comma = str.indexOf(",");
int parenEnd = str.indexOf(")");
return Integer.parseInt(str.substring(comma + 1, parenEnd));
}
static void INSERT(String str,minheap heap, rbt RBT) {
int JobId = parseJobdId(str);
int totalTime = parsetotalTime(str);
RBT.put(JobId,0,totalTime);
heap.insert(JobId,0,totalTime);
}
static void PrintSingleJob(String str, rbt RBT) {
int parenStart = str.indexOf("(");
int parenEnd = str.indexOf(")");
int JobId = Integer.parseInt(str.substring(parenStart + 1, parenEnd));
if(RBT.contains(JobId) == false) {
System.out.println("(" + 0 +"," + 0 + "," + 0 + ")");
}
else {
System.out.println("(" + JobId +"," + RBT.get(JobId).exeTime + "," + RBT.get(JobId).totalTime + ")");
}
}
static void PrintMultiJob(String str, rbt RBT) {
int JobID1;
int JobID2;
int index = str.indexOf("(");
int end = str.indexOf(",");
JobID1 = Integer.parseInt(str.substring(index+1,end));//find first JobId from the command
index = str.indexOf(",");
end = str.indexOf(")");
JobID2 = Integer.parseInt(str.substring(index+1,end));
Range(JobID1,JobID2,RBT);
}
static void PrintNextJob(String str, rbt RBT) {
int parenStart = str.indexOf("(");
int parenEnd = str.indexOf(")");
int JobId = Integer.parseInt(str.substring(parenStart + 1, parenEnd));
// if(RBT.contains(JobId) == false) {
// RBT.put(JobId,0,0);
// }
successor(JobId,RBT);
//RBT.delete(JobId);
}
static void PrintPreviousJob(String str, rbt RBT) {
int parenStart = str.indexOf("(");
int parenEnd = str.indexOf(")");
int JobId = Integer.parseInt(str.substring(parenStart + 1, parenEnd));
// if(RBT.contains(JobId) == false) {
// RBT.put(JobId,0,0);
// }
predecessor(JobId,RBT);
//RBT.delete(JobId);
}
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
int counter = 0;
int index = 0;
int end = 0;
int jobID,execute_time,total_time;
int remainTime = 0;
int execute_time2;
String subStr = null;
String command = null;
boolean profree = true;
minheap heap = new minheap();
rbt RedBlackTree = new rbt();
ArrayList<Integer> heapRoot = new ArrayList<Integer>();
// printToFile output = new printToFile();
try {
File file = new File("output_file.txt");
PrintStream print=new PrintStream("output_file.txt");
System.setOut(print);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
InputStream is = new FileInputStream(args[0]);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String str = null;
str = reader.readLine();
index = str.indexOf(" ");
end = str.indexOf("(");
subStr = str.substring(0,index - 1);
while (true) {
if (counter == Integer.parseInt(subStr)) {
command = str.substring(index+1,end);
switch(command) {
case "Insert": INSERT(str,heap,RedBlackTree);
break;
case "PrintJob":
if(!str.contains(","))
PrintSingleJob(str,RedBlackTree);
if(str.contains(","))
PrintMultiJob(str,RedBlackTree);
break;
case "NextJob":
PrintNextJob(str,RedBlackTree);
break;
case "PreviousJob":
PrintPreviousJob(str,RedBlackTree);
break;
default : break;
}
str = reader.readLine();
if(str == null) {
reader.close();
break;
}
index = str.indexOf(" ");
end = str.indexOf("(");
subStr = str.substring(0,index - 1);
}
counter++;
if(profree == true) {
if (heap.size() == 0) {
continue;
}
heapRoot = heap.pop();
execute_time2 = heapRoot.get(1);
remainTime = heapRoot.get(2) - execute_time2;
heapRoot.set(1,heapRoot.get(1)+1);
// if(rbt.findNode(rootOfHeap.get(0), rbt.getRoot()) == null) {
// rbt.insert(rootOfHeap.get(0),rootOfHeap.get(1),rootOfHeap.get(2));// try
// }
//rbt.findNode(rootOfHeap.get(0), rbt.getRoot()).exeTime = rootOfHeap.get(1);
RedBlackTree.get(heapRoot.get(0)).exeTime = heapRoot.get(1);
/*********modified area**************/
if(heapRoot.get(1) - heapRoot.get(2) == 0) {
RedBlackTree.delete(heapRoot.get(0));
continue;
}
/*************modifiedd area*********/
profree = false;
}
else {
heapRoot.set(1,heapRoot.get(1)+1);
// if(rbt.findNode(rootOfHeap.get(0), rbt.getRoot()) == null) {
// rbt.insert(rootOfHeap.get(0),rootOfHeap.get(1),rootOfHeap.get(2));// try
// }
//rbt.findNode(rootOfHeap.get(0), rbt.getRoot()).exeTime = rootOfHeap.get(1);
RedBlackTree.get(heapRoot.get(0)).exeTime = heapRoot.get(1);
if(remainTime<=5) {
if(heapRoot.get(2)-heapRoot.get(1)==0) {
RedBlackTree.delete(heapRoot.get(0));
profree = true;
}
}
if(remainTime>5) {
if(remainTime-(heapRoot.get(2)-heapRoot.get(1))==5) {
heap.insert(heapRoot.get(0), heapRoot.get(1), heapRoot.get(2));
profree = true;
}
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println("程序运行时间:"+(endTime-startTime)+"ms");
}
}