This repository has been archived by the owner on Feb 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmodel.h
431 lines (361 loc) · 7.71 KB
/
model.h
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/**
* @file model.h
* @author Adam Jez ([email protected])
* @author Roman Blanco ([email protected])
* @date 7.12.2014
* @brief Hlavicky soubor pro tridy, ktere modeluji struktury
* na vodni ceste, ktere nejvice ovlivnuji dobu prepluti
*/
#ifndef MODEL_H
#define MODEL_H
#include "simlib.h"
#include "constants.h"
#include "limits.h"
#include <iostream>
using namespace std;
//{}
void ActivateQueue(Queue *q, u_int max = INT_MAX);
const char *converToAscii(string letter);
/**
* @class Timeout
* @brief Trida slouzeni k vyvolani nejake akce
* bud spusteni procesu nebo aktivovani fronty
*/
class Timeout : public Event {
public:
Timeout() : Event() {}
void Behavior(){
if(_proc != NULL)
{
_proc->Activate();
Passivate();
_proc = NULL;
}
else if(_queue != NULL)
{
ActivateQueue(_queue, _max);
Passivate();
}
}
void SetQueue(Queue * que, int max)
{
_queue = que;
_max = max;
}
void SetProc(Entity * proc)
{
_proc = proc;
}
private:
Entity *_proc = NULL;
Queue *_queue = NULL;
int _max;
};
enum STRUCTURE {
tunnel = 0, // Tunel
chamber, // Plavebni komora
port, // Pristav
channel, // Kanal - stejna rychlost v obou smerech
river, // Ricni usek - ruzna rychlost ve smerech
bridge,
};
/**
* @class CargoShip
* @brief Trida, ktera dedi od procesu ze Simlibu
* predstavuje nakladni lod, ktera ma urcenou pocatecni
* a cilovou destinaci
*/
class CargoShip : public Process {
private:
double _arrived = Time;
int _from;
int _to;
bool _dir;
int _cur = 0; // TO-DO
public:
double Prichod; // atribute of each customer
public:
bool getDirection()
{
return _dir;
}
int getArrivedTime()
{
return _arrived;
}
void Behavior();
void Timeout()
{
Activate();
}
CargoShip(int from, int to)
: _from(from), _to(to)
{
_dir = Random() < 0.5;
}
};
/**
* @class Generator
* @brief Trida slouzeni k vytvareni lodi
* Generator se vytvari zadanim intervalu vytvareni
* a pozice od a do
*/
class Generator : public Event {
private:
int _diff;
int _place1;
int _place2;
public:
Generator(int diff, int place1, int place2)
: _diff(diff), _place1(place1), _place2(place2)
{}
void Behavior() {
int from, to;
if(Random() < 0.5) // Pomer lodi na kazdou stranou pul na pul
{
from = _place1; to = _place2;
}
else
{
from = _place2; to = _place1;
}
(new CargoShip(from, to))->Activate(); // new customer
Activate(Time+(_diff)); //
}
};
/**
* @class WaterItem
* @brief Trida, ktera slouzi k dedeni ostatnim objektum
* jako je reka, pristav atd. vsechno co se nalezne
* na vodni ceste a nejak ovlivnuje cas lodi na ceste
*/
class WaterItem {
protected:
Histogram *_table = NULL;
//int _type = 0;
public:
virtual ~WaterItem() {
if(_table != NULL)
{
_table->Output();
delete _table;
}
Q1->Output();
Q2->Output();
delete Q1;
delete Q2;
};
virtual int getType() = 0;
virtual int getLength() = 0;
double Start()
{
return Time;
}
void End(double time)
{
(*_table)(Time - time);
}
Queue *Q1 = new Queue();
Queue *Q2 = new Queue();
};
/**
* @class Chamber
* @brief Trida, ktera modeluje plavebni komoru. V konstruktoru se nastavi
* jeji nazev, delka a take promenna cekaci doba pred prepustenim nadrze.
* Pote uz jej lze vyuzivat jako Facility ze simlibu.
*/
class Chamber : public WaterItem {
private:
float _height;
int _waitTime;
int _fillTime = 0;
int _performTime = 0;
bool _pos;
Timeout _tm;
CargoShip *in = NULL;
public:
Chamber(string name, float height)
: _height(height), _pos(Random() < 0.5)
{
_table = new Histogram(converToAscii(string("Komora: " +name)),0,200,20);
if (_height > DIFF_CHAMBER_METER)
_fillTime = _height * TIME_METER_LARGE_CHAMBER;
else
_fillTime = _height * TIME_METER_MEDIUM_CHAMBER;
_performTime = TIME_CLOSE_GATE + TIME_OPEN_GATE + TIME_GO_OUT + TIME_GO_IN + _fillTime;
_waitTime = _performTime * 2;
}
virtual int getType()
{
return chamber;
}
virtual int getLength()
{
return 0; // Zanedbavame delku komor - vychazi pote presnejsi vzdalenosti
}
void PerformAction(CargoShip *ship);
void Seize(CargoShip *ship);
void Release();
};
/**
* @class Tunnel
* @brief Trida, ktera modeluje tunel. V konstruktoru se nastavi
* jeho nazev, delka a pote uz jej lze vyuzivat jako Facility ze simlibu.
*/
class Tunnel : public WaterItem {
private:
int _in = 0;
float _len;
int _waitTime;
int _crossTime;
bool _pos;
long _waitFor = 0;
Timeout _tm;
public:
Tunnel(string name, int len)
: _len(len), _pos(Random() < 0.5)
{
//Vytvorime histogram pro tunel
_table = new Histogram(converToAscii(string("Tunel: " +name)),0,500,20);
// Doba za kterou lod prepluje tunel
_crossTime = _len / SPEED_IN_TUNNEL;
// Pro kolik lodi se ceka
_waitFor = _len < DIFF_TUNNEL_METER ? 1 : 1;
_waitTime = 2*_crossTime * _waitFor;
}
virtual int getType()
{
return tunnel;
}
virtual int getLength()
{
return (int)_len;
}
void ChangeDir();
void PerformAction(CargoShip *ship);
void Seize(CargoShip *ship);
void Release();
};
/**
* @class Bridge
* @brief Trida, ktera modeluje most. V konstruktoru se nastavi
* jeho nazev, delka a pote uz jej lze vyuzivat jako Facility ze simlibu.
*/
class Bridge : public WaterItem {
private:
int _in = 0;
float _len;
int _waitTime;
int _crossTime;
bool _pos;
long _waitFor;
Timeout _tm;
public:
Bridge(string name, int len)
: _len(len), _pos(Random() < 0.5)
{
_table = new Histogram(converToAscii(string("Most: " +name)),0,200,20);
_crossTime = _len / SPEED_IN_BRIDGE;
_waitFor = _len < DIFF_BRIDGE_METER ? 1 : 3;
_waitTime = 2*_crossTime * _waitFor;
}
virtual int getType()
{
return tunnel;
}
virtual int getLength()
{
return (int)_len;
}
void ChangeDir();
void PerformAction(CargoShip *ship);
void Seize(CargoShip *ship);
void Release();
};
/**
* @class Channel
* @brief Trida, ktera modeluje kanal. V konstruktoru se nastavi
* jeho delka. Jedina uzitecna funkce je Perform action,
* ktera simuluju propluti kanalem a dane delce.
*/
class Channel : public WaterItem {
private:
int _length;
int _crossTime;
public:
Channel(int length) : _length(length)
{
_crossTime = length / SPEED_IN_CHANNEL;
}
void PerformAction(CargoShip *ship)
{
ship->Wait(_crossTime);
}
virtual int getType()
{
return channel;
}
virtual int getLength()
{
return _length;
}
};
/**
* @class Channel
* @brief Trida, ktera modeluje pristav. Prozatim nema zadnou
* uzitecnou metodu. Slouzi pouze k ulozeni pristavu jako typu
*/
class Port : public WaterItem {
private:
float _prob;
public:
Port(float probability) : _prob(probability)
{ }
void PerformAction(CargoShip *ship)
{
; // Asi neni potreba nic implementovat
}
virtual int getType()
{
return port;
}
virtual int getLength()
{
return 0;
}
};
/**
* @class Reka
* @brief Trida, ktera modeluje reku. Podle smeru, kterym lod
* rekou proplouva je udana delka jejiho prepluti
*/
class River : public WaterItem {
private:
bool _dir;
int _length;
int _crossTimeWithStream;
int _crossTimeUpStream;
public:
River(int length, bool dir) : _dir(dir)
{
_length = length;
_crossTimeWithStream = length / SPEED_WITH_STREAM;
_crossTimeUpStream = length / SPEED_UP_STREAM;
}
void PerformAction(CargoShip *ship)
{
if(ship->getDirection() == _dir)
ship->Wait(_crossTimeWithStream);
else
ship->Wait(_crossTimeUpStream);
}
virtual int getType()
{
return river;
}
virtual int getLength()
{
return _length;
}
};
#endif