-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCalendar.java
More file actions
262 lines (242 loc) · 7.28 KB
/
Calendar.java
File metadata and controls
262 lines (242 loc) · 7.28 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
import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.io.FileWriter;
import java.io.IOException;
/**
* Instance tridy {@code Calendar} predstavuji prioritni frontu objektu simulace.
*
* @author kolovsky
* @author jmacura
* @version 1.00.000
*/
public class Calendar extends Thread
{
//== KONSTANTNi ATRIBUTY INSTANCi ==========================================
private final PriorityQueue<Process> q;
private final Graph g;
//== PROMeNNe ATRIBUTY INSTANCi ============================================
/**
* Cas simulace v minutach.
*/
public int time;
/**
* Seznam vsech nakladnich aut.
*/
public ArrayList<Car> allCar = new ArrayList<Car>();
/**
* Seznam vsech vrtulniku.
*/
public ArrayList<Helicop> allHelicop = new ArrayList<Helicop>();
private volatile boolean isRun = true;
/**
* Vytvori novy kalendar s prioritni frontou a spusti simulaci.
*
* @param g Mapa uzemi, tj. graf, nad kterym probiha simulace.
*/
public Calendar(Graph g)
{
super("Calendar");
time = 0;
q = new PriorityQueue<Process>(10000, new Comparator<Process>() {
@Override
public int compare(Process o1, Process o2) {
return o1.time - o2.time;
}});
this.g = g;
start();
}
/**
* Vrati mapu uzemi.
* @return Graf, nad kterym probiha simulace.
*/
public Graph getGraph()
{
return this.g;
}
/**
* Vrati prioritni frontu objektu.
* @return Prioritni fronta objektu tridy {@code Process}.
*/
public PriorityQueue<Process> getQueue()
{
return this.q;
}
/**
* metoda pro spusteni simulace
*/
@Override
public synchronized void run()
{
//System.out.println("START!");
Core.log("START!");
if(g != null)
{
addAllNodeToQ();
}
else
{
Core.log("NENI GRAF!");
}
Core.log("Pripravuji data pro simulaci (nejakou dobu to muse trvat)");
g.generatePath();
/*try{
addAllNodeToQ();
}
catch(NullPointerException e){
Core.log("NENI GRAF!");
Core.exceptions.add(e);
//return;
}*/
simulate();
try
{
createStatistics();
}
catch (IOException e)
{
System.out.println("chyba");
Core.exceptions.add(e);
System.out.println(e.toString());
}
Core.summary();
}
/**
* metoda pro pozastavovani simulace
*/
public void pauseNplay()
{
if (isRun) {
Core.log("POZASTAVENO!");
isRun = false;
}
else {
Core.log("SPUSTENO");
isRun = true;
}
}
/**
* metoda ve ktere probiha simulace
*/
public void simulate()
{
Process proc;
while (q.size() != 0) {
if (q.peek().time == time) {
proc = q.poll();
//System.out.println(((Settle)proc).id);
proc.goOn();
//System.out.println("peak time "+q.peek().time);
//System.out.println(proc.time);
//System.out.println(((Airport)proc.node.suppliedFrom.proces).actualFood);
}
else {
if (q.peek().time < time) {
Process pe = q.peek();
//Core.log("peak time "+pe.time + " "+pe.people);
if (pe instanceof Settle) {
Core.log("mesto"+pe.node.people);
}
else if (pe instanceof Car) {
Car pee = (Car) pe;
Core.log("car"+pee.nextWork+" "+pee.path[pee.path.length-1].id+" kolik: "+pee.kolik +" "+ pee.cast);
}
else {
Core.log("ost"+pe.nextWork);
}
}
time++;
Core.log("======"+time+"======");
//Core.log(Thread.currentThread().getName());
}
if (time == 10800) {
break;
}
while (!isRun) {
try{
sleep(1);
}
catch(Exception e){
Core.exceptions.add(e);
//q.clear();
//System.out.println("SHUTDOWN");
}
}
//if(isKillingTime)
//{
// interrupt();
//}
}
}
/**
* vytvareni procesu k vrcholum a plneni fronty procesy
*/
public void addAllNodeToQ(){
//Random r = new Random();
Node node = g.firstNode;
//int pp = 0;
for (int i = 0;i<g.arrayAirport.length ;i++ ) {
//g.arrayAirport[i].proces = new Airport(60);
//g.arrayAirport[i].proces.node = g.arrayAirport[i];
new Airport(60,g.arrayAirport[i]);
q.add(g.arrayAirport[i].proces);
}
while (node != null) {
if (node instanceof SettleNode) {
//node.proces = new Settle(0);
//node.proces.node = node;
new Settle(0,node);
q.add(node.proces);
if (((Settle)node.proces).id == 3000) {
((Settle)node.proces).lastID += 5;
}
//pp++;
}
//if (node instanceof AirportNode) {
//node.proces = new Airport(r.nextInt(1000));
//}
node = node.next;
/*if (pp == 3001) {
break;
}*/
}
}
/**
* vytvori statistiku simulace
* @throws IOEXception Chyba zapisu do souboru.
*/
public void createStatistics() throws IOException{
FileWriter out1 = new FileWriter("zasobovano_z.txt");
FileWriter out2 = new FileWriter("zasobovano_kdy_kolik.txt");
out1.write("ID zasobovano z letiste\n");
Node node = g.firstNode;
while (node != null) {
if (node instanceof SettleNode) {
out1.write(node.id+"");
out1.write(" "+node.suppliedFrom.id+"\n");
out2.write("==== ID "+node.id+" ====\n");
out2.write(node.log.toString()+"\n");
}
node = node.next;
}
out2.close();
out1.close();
//System.out.println(0);
FileWriter out3 = new FileWriter("movingObject.txt");
out3.write("Celkovy pocet nakladnich aut je "+allCar.size()+"\n");
out3.write("Celkovy pocet vrtulniku je "+allHelicop.size()+"\n");
out3.write("=====CAR=====\n");
out3.write("Start End Quant Settle\n");
//System.out.println(1);
for (int i = 0;i<allCar.size() ;i++ ) {
out3.write(allCar.get(i).toString(false));
}
//System.out.println(2);
out3.write("=====HELICOP=====\n");
for (int i = 0;i<allHelicop.size() ;i++ ) {
out3.write(allHelicop.get(i).toString(false));
}
out3.close();
Core.log("Vytvorena statistika");
}
}