-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.h
416 lines (340 loc) · 9.3 KB
/
simulator.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
/* simulator core header */
#ifndef _SIM_H
#define _SIM_H
#include <list>
#include <limits.h>
#include <stdint.h>
#include <string>
#include <iostream>
#include <queue>
#include <unordered_set>
#include <exception>
#include <cfloat>
using std::cout;
void Run();
double Time();
void InitTime(double start, double end);
double Exponential(double middle);
double Uniform(double low, double high);
double Random();
double Normal(double middle, double sigma);
class Calendar;
class Facility;
class Store;
class GarbageCollector;
class MetaEntity;
class GarbageCollector
{
public:
void addPtr( MetaEntity *ptr ){ database.insert(ptr); }
void removePtr( MetaEntity *ptr ){ database.erase(ptr); }
static GarbageCollector& instance(){
static GarbageCollector instance; // Guaranteed to be destroyed.
return instance;
}
void Free();
bool flag = false;
private:
std::unordered_set<MetaEntity*> database;
explicit GarbageCollector() {}
};
namespace Internal {
extern long double Time; // simulation time
extern long double TimeStop;
int GenerateID();
}
class Statistics
{
unsigned numRecords;
double min;
double max;
double sum;
double sum2x;
std::string name;
static unsigned int id;
public:
Statistics(){Statistics("STATISTICS_" + std::to_string(Statistics::id++));}
Statistics(std::string name_);
double Min() const;
double Max() const;
double Avg() const;
double StdDev();
double NumRecords() { return numRecords; }
void SetNumRecords(unsigned int val) { numRecords = val; }
void Clear() { numRecords = min = max = sum = sum2x = 0; }
void Output();
void Record(double val); // stat recording
};
class Histogram
{
public:
Histogram(std::string _name, double _low, double _step, unsigned _count):
low(_low), step(_step), count(_count), name(_name)
{
data.resize(count+2);
}
void Sample(double x);
double Low() {return low;}
double High() {return low + step*count;}
double Step() {return step;}
int Count() {return count;}
void Output();
private:
std::vector<int> data;
double low;
double step;
int count;
std::string name;
Statistics stat;
};
class TimeStats
{
public:
static unsigned int id;
unsigned int n; // number of samples
double t_0; // start of statistics
double sum; // total
double start_time; // start of statistics
double min, max;
// TODO rozptyl
/*helper*/
double previousTime;
double previousValue;
std::string name;
public:
TimeStats(){
TimeStats("TimeStat_" + std::to_string(TimeStats::id++));
}
TimeStats(std::string name_) ;
double Avg() const;
double Min() const {return min;}
double Max() const {return max;}
double Start() { return t_0; }
void SampleBegin(double x);
void SampleEnd();
};
class MetaEntity
{
protected:
std::string _name;
int id;
uint8_t _prioriy = 0; // priorita procesu
double _activationTime = 0; // next call in calendar == activationtime
public:
typedef void (MetaEntity::*Fptr)();
MetaEntity(uint8_t prio = 0) : _prioriy(prio) { // zaregistrovat do GC alebo nastavit priznak
id = Internal::GenerateID();
_name = std::string("MetaEntity_") + std::to_string(id);
GarbageCollector::instance().addPtr(this);
}
virtual ~MetaEntity(){ }
#define SLOT(x) (static_cast<MetaEntity::Fptr>((&x)))
virtual void Behavior() = 0;
void scheduleAt( double t, Fptr callback );
void scheduleAt( double t = 0 ) { MetaEntity::scheduleAt(t, SLOT(MetaEntity::Behavior)); }
std::string name() { return _name; }
uint8_t GetProcPrio() { return _prioriy; }
double activationTime() {return _activationTime;}
Fptr Passivate();
int referenceCounter = 0;
};
class Process : public MetaEntity
{
public:
Process(uint8_t prio = 0) : MetaEntity::MetaEntity(prio) {
_name = "Process " + std::to_string(id);
}
void Seize(Facility &f, Fptr callback, uint8_t servicePrio = 0);
void Release(Facility &f);
void Enter(Store &s, Fptr callback, int capacity );
void Leave(Store &s, int capacity);
private:
};
class Event : public MetaEntity
{
public:
Event(uint8_t prio = 0) : MetaEntity::MetaEntity(prio) {
_name = "Event " + std::to_string(id);
}
};
/*
class Event:MetaEvent
class Process:MetaEvent
*/
class CalendarItem
{
protected:
double activationTime;
uint8_t priority;
MetaEntity* target_object = nullptr;
MetaEntity::Fptr ptr;
public:
CalendarItem(){}
CalendarItem( MetaEntity &t, MetaEntity::Fptr _ptr, double time, uint8_t _priority = 0 ):
activationTime(time),
priority(_priority),
target_object(&t),
ptr(_ptr)
{;}
virtual ~CalendarItem() {}
MetaEntity& GetTarget() { return *target_object; }
MetaEntity* GetTargetPtr() { return target_object; }
MetaEntity::Fptr GetPtr() { return ptr; }
uint8_t GetPriority() const { return priority; }
double GetTime() const { return activationTime; }
void Execute() { (target_object->*ptr)(); }
inline bool operator<=(const CalendarItem& rhs){
if ( this->GetTime() < rhs.GetTime() )
return true;
else if (this->GetTime() == rhs.GetTime())
{
if (this->GetPriority() <= rhs.GetPriority())
return true;
else
return false;
}
else
return false;
}
};
class Calendar
{
private:
std::list<CalendarItem> calendar;
explicit Calendar() { calendar.clear(); }
public:
static Calendar& instance()
{
static Calendar instance; // Guaranteed to be destroyed.
return instance;
}
bool Empty() {return calendar.empty(); }
void Clear() { calendar.clear(); }
int Length() { return calendar.size(); }
CalendarItem Next() {
CalendarItem tmp = calendar.front();
calendar.pop_front();
return tmp;
}
void Dump();
MetaEntity::Fptr Passivate( MetaEntity* entity);
void Schedule(MetaEntity &t, MetaEntity::Fptr ptr, double time, uint8_t priority = 0 );
};
class QueueItem : public CalendarItem
{
public:
QueueItem(){}
QueueItem( MetaEntity &t, MetaEntity::Fptr _ptr, uint8_t _priority = 0 ):
CalendarItem( t, _ptr, 0, _priority )
{;}
void resetPtr() { target_object = nullptr; }
void setPtr(MetaEntity::Fptr callback) { ptr = callback; }
double remainingTime = 0; // Q2 in facility
int requiredCapacity = 0; // Q in store
double insertTime = 0;
bool operator<=( QueueItem& item2)
{
if ( this->priority < item2.priority ) return false; // servisna priorita
if ( this->priority == item2.priority )
{
if ( this->target_object->GetProcPrio() < item2.GetTarget().GetProcPrio() )
return false;
else
return true;
}
else return true;
}
};
class Queue
{
private:
std::list<QueueItem> queue;
std::string _name;
unsigned int incoming = 0;
unsigned int outcoming = 0;
TimeStats tstats;
Statistics stat;
public:
Queue(){
_name = "Queue_" + std::to_string(Internal::GenerateID());
}
Queue(std::string name): _name(name){ }
bool Empty() {return queue.empty(); }
void Clear() { queue.clear(); }
int Length() { return queue.size(); }
std::string name() {return _name;}
void Insert(QueueItem item);
QueueItem GetFirst(int capacity = 0);
QueueItem& Front();
bool isPresent( MetaEntity &obj );
void Dump();
bool hasOutput (){return incoming;}
void Output();
};
class Facility
{
public :
Facility (std::string name):
_name (name),
Q1(_name + ".Q1"),
Q2(_name + ".Q2")
{}
Facility ():
id (Internal::GenerateID()),
_name ("Facility_" + std::to_string(id)),
Q1(_name + ".Q1"),
Q2(_name + ".Q2")
{}
bool Busy() { return (in.GetTargetPtr() == nullptr )?false:true; }
int QueueLen() { return Q1.Length(); }
void Seize(MetaEntity *obj, MetaEntity::Fptr callback, uint8_t service_prio = 0 );
void Release( MetaEntity *obj );
void Output();
QueueItem in;
private:
int id;
std::string _name;
Queue Q1;
Queue Q2;
Statistics stats;
TimeStats tStats;
};
class Store
{
public:
Store( int _cap = 1 ):
capacity(_cap),
id (Internal::GenerateID()),
_name("Store_" + std::to_string(id)),
Q(_name + ".Q")
{
freeCounter = capacity;
}
Store( std::string name, int _cap = 1):
capacity(_cap),
_name(name),
Q(_name + ".Q")
{
freeCounter = capacity;
id = Internal::GenerateID();
}
int Free(){ return freeCounter; }
int Used(){ return capacity - freeCounter; }
int Capacity(){ return capacity; }
bool Full() { return ( freeCounter == 0 ); }
bool Empty(){ return capacity == freeCounter; }
std::string name(){return _name;}
int QueueLen() { return Q.Length(); }
void Enter(MetaEntity *obj, MetaEntity::Fptr callback, int _capacity );
void Leave( int _capacity );
void Output();
private:
int capacity;
int id;
std::string _name;
int freeCounter;
Queue Q;
TimeStats tstats;
int enterCount = 0;
};
#endif