1.00.15
C++ Simulation-Oriented Discrete Event Management Library
Toggle main menu visibility
Loading...
Searching...
No Matches
EventQueue.cpp
Go to the documentation of this file.
1
// //////////////////////////////////////////////////////////////////////
2
// Import section
3
// //////////////////////////////////////////////////////////////////////
4
// STL
5
#include <cassert>
6
// StdAir
7
#include <stdair/stdair_exceptions.hpp>
8
#include <stdair/basic/BasConst_Event.hpp>
9
#include <stdair/bom/EventStruct.hpp>
10
#include <stdair/service/Logger.hpp>
11
// SEvMgr
12
#include <
sevmgr/basic/BasConst_EventQueueManager.hpp
>
13
#include <
sevmgr/bom/EventQueue.hpp
>
14
15
namespace
SEVMGR
{
16
17
// //////////////////////////////////////////////////////////////////////
18
EventQueue::EventQueue
()
19
: _key (
DEFAULT_EVENT_QUEUE_ID
), _parent (NULL),
20
_progressStatus (stdair::DEFAULT_PROGRESS_STATUS,
21
stdair::DEFAULT_PROGRESS_STATUS) {
22
}
23
24
// //////////////////////////////////////////////////////////////////////
25
EventQueue::EventQueue
(
const
Key_T
& iKey)
26
:
_key
(iKey),
_parent
(NULL),
27
_progressStatus
(
stdair
::DEFAULT_PROGRESS_STATUS,
28
stdair
::DEFAULT_PROGRESS_STATUS) {
29
}
30
31
// //////////////////////////////////////////////////////////////////////
32
EventQueue::EventQueue
(
const
EventQueue
& iEventQueue)
33
:
_key
(
DEFAULT_EVENT_QUEUE_ID
),
_parent
(NULL),
34
_progressStatus
(
stdair
::DEFAULT_PROGRESS_STATUS,
35
stdair
::DEFAULT_PROGRESS_STATUS) {
36
assert (
false
);
37
}
38
39
// //////////////////////////////////////////////////////////////////////
40
EventQueue::~EventQueue
() {
41
_eventList
.clear();
42
}
43
44
// //////////////////////////////////////////////////////////////////////
45
std::string
EventQueue::toString
()
const
{
46
std::ostringstream oStr;
47
oStr <<
"("
<<
_eventList
.size() <<
") "
48
<<
_progressStatus
.getCurrentNb() <<
"/{"
49
<<
_progressStatus
.getExpectedNb() <<
","
50
<<
_progressStatus
.getActualNb() <<
"}"
;
51
return
oStr.str();
52
}
53
54
// //////////////////////////////////////////////////////////////////////
55
std::string
EventQueue::display
()
const
{
56
std::ostringstream oStr;
57
58
oStr <<
toString
();
59
60
return
oStr.str();
61
}
62
63
// //////////////////////////////////////////////////////////////////////
64
std::string
EventQueue::list
()
const
{
65
std::ostringstream oStr;
66
oStr <<
describeKey
() << std::endl;
67
oStr <<
toString
() << std::endl;
68
69
// Browse the events
70
for
(stdair::EventList_T::const_iterator itEvent =
_eventList
.begin();
71
itEvent !=
_eventList
.end(); ++itEvent) {
72
const
stdair::EventStruct& lEvent = itEvent->second;
73
74
oStr << lEvent.describe();
75
}
76
77
return
oStr.str();
78
}
79
80
// //////////////////////////////////////////////////////////////////////
81
std::string
EventQueue::
82
list
(
const
stdair::EventType::EN_EventType& iType)
const
{
83
std::ostringstream oStr;
84
oStr <<
describeKey
() << std::endl;
85
oStr <<
toString
() << std::endl;
86
oStr <<
"List "
<< stdair::EventType::getLabel(iType)
87
<<
" events:"
<< std::endl;
88
89
// Browse the events
90
for
(stdair::EventList_T::const_iterator itEvent =
_eventList
.begin();
91
itEvent !=
_eventList
.end(); ++itEvent) {
92
const
stdair::EventStruct& lEvent = itEvent->second;
93
94
if
(lEvent.getEventType() == iType) {
95
oStr << lEvent.describe();
96
}
97
}
98
return
oStr.str();
99
}
100
101
// //////////////////////////////////////////////////////////////////////
102
stdair::Count_T
EventQueue::getQueueSize
()
const
{
103
return
_eventList
.size();
104
}
105
106
// //////////////////////////////////////////////////////////////////////
107
bool
EventQueue::isQueueEmpty
()
const
{
108
return
_eventList
.empty();
109
}
110
111
// //////////////////////////////////////////////////////////////////////
112
bool
EventQueue::isQueueDone
()
const
{
113
const
bool
isQueueEmpty
=
_eventList
.empty();
114
return
isQueueEmpty
;
115
}
116
117
// //////////////////////////////////////////////////////////////////////
118
void
EventQueue::reset
() {
119
// Reset only the current number of events, not the expected one
120
_progressStatus
.reset();
121
122
// Empty the list of events
123
_eventList
.clear();
124
125
// Reset the progress statuses for all the event types
126
for
(ProgressStatusMap_T::iterator itProgressStatus =
127
_progressStatusMap
.begin();
128
itProgressStatus !=
_progressStatusMap
.end(); ++itProgressStatus) {
129
stdair::ProgressStatus& lProgressStatus = itProgressStatus->second;
130
lProgressStatus.reset();
131
}
132
}
133
134
// //////////////////////////////////////////////////////////////////////
135
bool
EventQueue::
136
hasProgressStatus
(
const
stdair::EventType::EN_EventType& iType)
const
{
137
138
bool
hasProgressStatus
=
true
;
139
140
// Retrieve the ProgressStatus structure corresponding to the
141
// given event type
142
ProgressStatusMap_T::const_iterator itProgressStatus =
143
_progressStatusMap
.find (iType);
144
if
(itProgressStatus ==
_progressStatusMap
.end()) {
145
//
146
STDAIR_LOG_DEBUG (
"No ProgressStatus structure can be retrieved in the "
147
<<
"EventQueue: "
<<
display
());
148
149
hasProgressStatus
=
false
;
150
}
151
152
return
hasProgressStatus
;
153
}
154
155
// //////////////////////////////////////////////////////////////////////
156
const
stdair::Count_T&
EventQueue::
157
getCurrentNbOfEvents
(
const
stdair::EventType::EN_EventType& iType)
const
{
158
159
// Retrieve the ProgressStatus structure corresponding to the
160
// given event type
161
ProgressStatusMap_T::const_iterator itProgressStatus =
162
_progressStatusMap
.find (iType);
163
if
(itProgressStatus ==
_progressStatusMap
.end()) {
164
//
165
STDAIR_LOG_ERROR (
"No ProgressStatus structure can be retrieved in the "
166
<<
"EventQueue: "
<<
display
());
167
assert (
false
);
168
}
169
170
const
stdair::ProgressStatus& lProgressStatus = itProgressStatus->second;
171
return
lProgressStatus.getCurrentNb();
172
}
173
174
// //////////////////////////////////////////////////////////////////////
175
const
stdair::Count_T&
EventQueue::
176
getExpectedTotalNbOfEvents
(
const
stdair::EventType::EN_EventType& iType)
const
{
177
178
// Retrieve the ProgressStatus structure corresponding to the
179
// given event type
180
ProgressStatusMap_T::const_iterator itProgressStatus =
181
_progressStatusMap
.find (iType);
182
if
(itProgressStatus ==
_progressStatusMap
.end()) {
183
std::ostringstream oStr;
184
oStr <<
"No ProgressStatus structure can be retrieved in the EventQueue '"
185
<<
display
() <<
"'. The EventQueue should be initialised, e.g., by "
186
<<
"calling a buildSampleBom() method."
;
187
//
188
STDAIR_LOG_ERROR (oStr.str());
189
throw
EventQueueException
(oStr.str());
190
}
191
192
const
stdair::ProgressStatus& lProgressStatus = itProgressStatus->second;
193
return
lProgressStatus.getExpectedNb();
194
}
195
196
// //////////////////////////////////////////////////////////////////////
197
const
stdair::Count_T&
EventQueue::
198
getActualTotalNbOfEvents
(
const
stdair::EventType::EN_EventType& iType)
const
{
199
200
// Retrieve the ProgressStatus structure corresponding to the
201
// given event type
202
ProgressStatusMap_T::const_iterator itProgressStatus =
203
_progressStatusMap
.find (iType);
204
if
(itProgressStatus ==
_progressStatusMap
.end()) {
205
//
206
STDAIR_LOG_ERROR (
"No ProgressStatus structure can be retrieved in the "
207
<<
"EventQueue: "
<<
display
());
208
assert (
false
);
209
}
210
211
const
stdair::ProgressStatus& lProgressStatus = itProgressStatus->second;
212
return
lProgressStatus.getActualNb();
213
}
214
215
// //////////////////////////////////////////////////////////////////////
216
void
EventQueue::updateStatus
(
const
stdair::EventType::EN_EventType& iType,
217
const
stdair::ProgressStatus& iProgressStatus) {
218
219
// Retrieve, if existing, the ProgressStatus structure
220
// corresponding to the given event type
221
ProgressStatusMap_T::iterator itProgressStatus =
222
_progressStatusMap
.find (iType);
223
if
(itProgressStatus ==
_progressStatusMap
.end()) {
224
const
bool
hasInsertBeenSuccessful =
225
_progressStatusMap
.insert (
ProgressStatusMap_T
::
226
value_type (iType, iProgressStatus)).second;
227
228
if
(hasInsertBeenSuccessful ==
false
) {
229
STDAIR_LOG_ERROR (
"No progress_status can be inserted "
230
<<
"for the following event type: "
231
<< stdair::EventType::getLabel(iType)
232
<<
". EventQueue: "
<<
toString
());
233
throw
stdair::EventException (
"No progress_status can be inserted for the "
234
"following event type: "
235
+ stdair::EventType::getLabel(iType)
236
+
". EventQueue: "
+
toString
());
237
}
238
239
return
;
240
}
241
242
stdair::ProgressStatus& lProgressStatus = itProgressStatus->second;
243
244
// Update the progress status
245
const
stdair::Count_T& lCurrentNb = iProgressStatus.getCurrentNb();
246
lProgressStatus.setCurrentNb (lCurrentNb);
247
248
const
stdair::Count_T& lExpectedNb = iProgressStatus.getExpectedNb();
249
lProgressStatus.setExpectedNb(lProgressStatus.getExpectedNb() + lExpectedNb);
250
251
const
stdair::Count_T& lActualNb = iProgressStatus.getActualNb();
252
lProgressStatus.setActualNb (lProgressStatus.getActualNb() + lActualNb);
253
}
254
255
// //////////////////////////////////////////////////////////////////////
256
void
EventQueue::
257
addStatus
(
const
stdair::EventType::EN_EventType& iType,
258
const
stdair::NbOfEvents_T& iExpectedTotalNbOfEvents) {
259
260
// Initialise the progress status object
261
const
stdair::Count_T lExpectedTotalNbOfEventsInt =
262
static_cast<
const
stdair::Count_T
>
(std::floor (iExpectedTotalNbOfEvents));
263
const
stdair::ProgressStatus lProgressStatus (lExpectedTotalNbOfEventsInt);
264
265
// Update the progress status for the given event type
266
updateStatus
(iType, lProgressStatus);
267
268
// Update the overall progress status
269
const
stdair::Count_T lExpectedNb =
270
static_cast<
const
stdair::Count_T
>
(
_progressStatus
.getExpectedNb()
271
+ iExpectedTotalNbOfEvents);
272
_progressStatus
.setExpectedNb (lExpectedNb);
273
274
const
stdair::Count_T lActualNb =
275
static_cast<
const
stdair::Count_T
>
(
_progressStatus
.getActualNb()
276
+ iExpectedTotalNbOfEvents);
277
_progressStatus
.setActualNb (lActualNb);
278
279
}
280
281
// //////////////////////////////////////////////////////////////////////
282
void
EventQueue::updateStatus
(
const
stdair::EventType::EN_EventType& iType,
283
const
stdair::NbOfEvents_T& iActualNbOfEvents) {
284
285
// Initialise the progress status object for the type key
286
const
stdair::Count_T lActualNbOfEventsInt =
287
static_cast<
const
stdair::Count_T
>
(std::floor (iActualNbOfEvents));
288
289
// Update the progress status for the corresponding content type key
290
ProgressStatusMap_T::iterator itProgressStatus =
291
_progressStatusMap
.find (iType);
292
if
(itProgressStatus !=
_progressStatusMap
.end()) {
293
294
//
295
stdair::ProgressStatus& lProgressStatus = itProgressStatus->second;
296
297
// Update the overall progress status
298
const
stdair::Count_T& lActualEventTypeNb = lProgressStatus.getActualNb();
299
const
stdair::Count_T lActualEventTypeNbInt =
300
static_cast<
const
stdair::Count_T
>
(std::floor (lActualEventTypeNb));
301
const
stdair::Count_T& lActualTotalNb =
_progressStatus
.getActualNb();
302
const
stdair::Count_T lActualDeltaNb = lActualNbOfEventsInt - lActualEventTypeNbInt;
303
_progressStatus
.setActualNb (lActualTotalNb + lActualDeltaNb);
304
305
// Update the progress status for the corresponding type key
306
lProgressStatus.setActualNb (lActualNbOfEventsInt);
307
}
308
}
309
310
// //////////////////////////////////////////////////////////////////////
311
void
EventQueue::setStatus
(
const
stdair::EventType::EN_EventType& iType,
312
const
stdair::ProgressStatus& iProgressStatus) {
313
314
// Retrieve the ProgressStatus structure corresponding to the
315
// given event type
316
ProgressStatusMap_T::iterator itProgressStatus =
317
_progressStatusMap
.find (iType);
318
// assert (itProgressStatus != _progressStatusMap.end());
319
if
(itProgressStatus !=
_progressStatusMap
.end()) {
320
// Update the ProgressStatus structure
321
itProgressStatus->second = iProgressStatus;
322
}
323
}
324
325
// //////////////////////////////////////////////////////////////////////
326
const
stdair::ProgressStatus&
EventQueue::
327
getStatus
(
const
stdair::EventType::EN_EventType& iType)
const
{
328
329
// Retrieve the ProgressStatus structure corresponding to the
330
// given event type
331
ProgressStatusMap_T::const_iterator itProgressStatus =
332
_progressStatusMap
.find (iType);
333
if
(itProgressStatus ==
_progressStatusMap
.end()) {
334
std::ostringstream oStr;
335
oStr <<
"No ProgressStatus structure can be retrieved in the EventQueue '"
336
<<
display
() <<
"' for the following event type: "
337
<< stdair::EventType::getLabel(iType) <<
"."
;
338
//
339
STDAIR_LOG_ERROR (oStr.str());
340
throw
EventQueueException
(oStr.str());
341
}
342
assert(itProgressStatus !=
_progressStatusMap
.end());
343
344
const
stdair::ProgressStatus& oProgressStatus = itProgressStatus->second;
345
return
oProgressStatus;
346
}
347
348
// //////////////////////////////////////////////////////////////////////
349
stdair::ProgressPercentage_T
EventQueue::
350
calculateProgress
(
const
stdair::EventType::EN_EventType& iType)
const
{
351
352
// Retrieve the ProgressStatus structure corresponding to the
353
// given event type
354
ProgressStatusMap_T::const_iterator itProgressStatus =
355
_progressStatusMap
.find (iType);
356
if
(itProgressStatus ==
_progressStatusMap
.end()) {
357
//
358
STDAIR_LOG_ERROR (
"No ProgressStatus structure can be retrieved in the "
359
<<
"EventQueue: "
<<
display
());
360
assert (
false
);
361
}
362
363
const
stdair::ProgressStatus& lProgressStatus = itProgressStatus->second;
364
return
lProgressStatus.progress();
365
}
366
367
// //////////////////////////////////////////////////////////////////////
368
stdair::ProgressStatusSet
EventQueue::popEvent
(stdair::EventStruct& ioEventStruct) {
369
370
if
(
_eventList
.empty() ==
true
) {
371
std::ostringstream oStr;
372
oStr <<
"The event queue '"
<<
describeKey
() <<
"' is empty. "
373
<<
"No event can be popped."
;
374
//
375
STDAIR_LOG_ERROR (oStr.str());
376
throw
EventQueueException
(oStr.str());
377
}
378
382
// Get an iterator on the first event (sorted by date-time stamps)
383
stdair::EventList_T::iterator itEvent =
_eventList
.begin();
384
391
ioEventStruct = itEvent->second;
392
// Retrieve the event type
393
const
stdair::EventType::EN_EventType& lEventType = ioEventStruct.getEventType();
394
stdair::ProgressStatusSet oProgressStatusSet (lEventType);
395
396
// Update the (current number part of the) overall progress status,
397
// to account for the event that is being popped out of the event
398
// queue.
399
++
_progressStatus
;
400
401
// Remove the event, which has just been retrieved
402
_eventList
.erase (itEvent);
403
404
411
412
// Retrieve the progress status specific to that event type
413
stdair::ProgressStatus lEventTypeProgressStatus =
getStatus
(lEventType);
414
415
// Increase the current number of events
416
++lEventTypeProgressStatus;
417
418
// Store back the progress status
419
setStatus
(lEventType, lEventTypeProgressStatus);
420
421
// Update the progress status of the progress status set, specific to
422
// the event type.
423
oProgressStatusSet.setTypeSpecificStatus (lEventTypeProgressStatus);
424
428
// Update the overall progress status of the progress status set.
429
oProgressStatusSet.setOverallStatus (
_progressStatus
);
430
431
//
432
return
oProgressStatusSet;
433
}
434
435
// //////////////////////////////////////////////////////////////////////
436
bool
EventQueue::addEvent
(stdair::EventStruct& ioEventStruct) {
437
bool
insertionSucceeded =
438
_eventList
.insert (stdair::EventListElement_T (ioEventStruct.getEventTimeStamp(),
439
ioEventStruct)).second;
440
453
const
unsigned
int
idx = 0;
454
while
(insertionSucceeded ==
false
&& idx != 1e3) {
455
// Increment the date-time stamp (expressed in milliseconds)
456
ioEventStruct.incrementEventTimeStamp();
457
458
// Retry to insert into the event queue
459
insertionSucceeded =
460
_eventList
.insert (stdair::EventListElement_T (ioEventStruct.getEventTimeStamp(),
461
ioEventStruct)).second;
462
}
463
assert (idx != 1e3);
464
465
return
insertionSucceeded;
466
}
467
468
// //////////////////////////////////////////////////////////////////////
469
bool
EventQueue::hasEventDateTime
(
const
stdair::DateTime_T& iDateTime) {
470
471
bool
hasSearchEventBeenSucessful =
true
;
472
478
const
stdair::Duration_T lDuration =
479
iDateTime - stdair::DEFAULT_EVENT_OLDEST_DATETIME;
480
const
stdair::LongDuration_T lDateTimeStamp =
481
lDuration.total_milliseconds();
482
483
// Searches the container for an element with iDateTime as key
484
stdair::EventList_T::iterator itEvent =
485
_eventList
.find (lDateTimeStamp);
486
487
// An iterator to map::end means the specified key has not found in the
488
// container.
489
if
(itEvent ==
_eventList
.end()) {
490
hasSearchEventBeenSucessful =
false
;
491
}
492
493
return
hasSearchEventBeenSucessful;
494
495
}
496
497
}
BasConst_EventQueueManager.hpp
EventQueue.hpp
SEVMGR::EventQueueException
Definition
SEVMGR_Exceptions.hpp:28
SEVMGR::EventQueue::hasEventDateTime
bool hasEventDateTime(const stdair::DateTime_T &)
Definition
EventQueue.cpp:469
SEVMGR::EventQueue::display
std::string display() const
Definition
EventQueue.cpp:55
SEVMGR::EventQueue::getStatus
const stdair::ProgressStatus & getStatus() const
Definition
EventQueue.hpp:103
SEVMGR::EventQueue::~EventQueue
~EventQueue()
Definition
EventQueue.cpp:40
SEVMGR::EventQueue::getQueueSize
stdair::Count_T getQueueSize() const
Definition
EventQueue.cpp:102
SEVMGR::EventQueue::isQueueEmpty
bool isQueueEmpty() const
Definition
EventQueue.cpp:107
SEVMGR::EventQueue::_eventList
stdair::EventList_T _eventList
Definition
EventQueue.hpp:399
SEVMGR::EventQueue::_key
Key_T _key
Definition
EventQueue.hpp:382
SEVMGR::EventQueue::getExpectedTotalNbOfEvents
const stdair::Count_T & getExpectedTotalNbOfEvents() const
Definition
EventQueue.hpp:111
SEVMGR::EventQueue::popEvent
stdair::ProgressStatusSet popEvent(stdair::EventStruct &)
Definition
EventQueue.cpp:368
SEVMGR::EventQueue::describeKey
const std::string describeKey() const
Definition
EventQueue.hpp:213
SEVMGR::EventQueue::_parent
BomAbstract * _parent
Definition
EventQueue.hpp:387
SEVMGR::EventQueue::calculateProgress
stdair::ProgressPercentage_T calculateProgress() const
Definition
EventQueue.hpp:338
SEVMGR::EventQueue::EventQueue
EventQueue(const Key_T &)
Definition
EventQueue.cpp:25
SEVMGR::EventQueue::_progressStatusMap
ProgressStatusMap_T _progressStatusMap
Definition
EventQueue.hpp:411
SEVMGR::EventQueue::hasProgressStatus
bool hasProgressStatus(const stdair::EventType::EN_EventType &) const
Definition
EventQueue.cpp:136
SEVMGR::EventQueue::toString
std::string toString() const
Definition
EventQueue.cpp:45
SEVMGR::EventQueue::Key_T
EventQueueKey Key_T
Definition
EventQueue.hpp:77
SEVMGR::EventQueue::_progressStatus
stdair::ProgressStatus _progressStatus
Definition
EventQueue.hpp:404
SEVMGR::EventQueue::isQueueDone
bool isQueueDone() const
Definition
EventQueue.cpp:112
SEVMGR::EventQueue::list
std::string list() const
Definition
EventQueue.cpp:64
SEVMGR::EventQueue::getActualTotalNbOfEvents
const stdair::Count_T & getActualTotalNbOfEvents() const
Definition
EventQueue.hpp:115
SEVMGR::EventQueue::getCurrentNbOfEvents
const stdair::Count_T & getCurrentNbOfEvents() const
Definition
EventQueue.hpp:107
SEVMGR::EventQueue::updateStatus
void updateStatus(const stdair::EventType::EN_EventType &, const stdair::ProgressStatus &iProgressStatus)
Definition
EventQueue.cpp:216
SEVMGR::EventQueue::addEvent
bool addEvent(stdair::EventStruct &)
Definition
EventQueue.cpp:436
SEVMGR::EventQueue::setStatus
void setStatus(const stdair::ProgressStatus &iProgressStatus)
Definition
EventQueue.hpp:141
SEVMGR::EventQueue::reset
void reset()
Definition
EventQueue.cpp:118
SEVMGR::EventQueue::addStatus
void addStatus(const stdair::EventType::EN_EventType &, const stdair::NbOfRequests_T &iExpectedTotalNbOfEvents)
Definition
EventQueue.cpp:257
SEVMGR
Definition
BasConst.cpp:10
SEVMGR::DEFAULT_EVENT_QUEUE_ID
const EventQueueID_T DEFAULT_EVENT_QUEUE_ID
SEVMGR::ProgressStatusMap_T
std::map< stdair::EventType::EN_EventType, stdair::ProgressStatus > ProgressStatusMap_T
Definition
SEVMGR_Types.hpp:35
stdair
Forward declarations.
Definition
EventQueue.hpp:23
Generated on
for SEvMgr by
1.17.0