SUMO - Simulation of Urban MObility
MSEventControl.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
19 // Stores time-dependant events and executes them at the proper time
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <cassert>
29 #include "MSEventControl.h"
31 #include <utils/common/Command.h>
32 #include "MSNet.h"
33 
34 
35 // ===========================================================================
36 // member definitions
37 // ===========================================================================
39  : currentTimeStep(-1), myEvents() {}
40 
41 
43  // delete the events
44  while (!myEvents.empty()) {
45  Event e = myEvents.top();
46  delete e.first;
47  myEvents.pop();
48  }
49 }
50 
51 
52 void
53 MSEventControl::addEvent(Command* operation, SUMOTime execTimeStep) {
54  myEvents.push(Event(operation, execTimeStep));
55 }
56 
57 
58 void
60  // Execute all events that are scheduled for execTime.
61  while (!myEvents.empty()) {
62  Event currEvent = myEvents.top();
63  if (currEvent.second < 0) {
64  currEvent.second = execTime;
65  }
66  if (currEvent.second < execTime + DELTA_T) {
67  Command* command = currEvent.first;
68  myEvents.pop();
69  SUMOTime time = 0;
70  try {
71  time = command->execute(execTime);
72  } catch (...) {
73  delete command;
74  throw;
75  }
76 
77  // Delete nonrecurring events, reinsert recurring ones
78  // with new execution time = execTime + returned offset.
79  if (time <= 0) {
80  if (time < 0) {
81  WRITE_WARNING("Command returned negative repeat number; will be deleted.");
82  }
83  delete currEvent.first;
84  } else {
85  currEvent.second += time;
86  myEvents.push(currEvent);
87  }
88  } else {
89  break;
90  }
91  }
92 }
93 
94 
95 bool
97  return myEvents.empty();
98 }
99 
100 void
102  currentTimeStep = time;
103 }
104 
105 SUMOTime
107  if (currentTimeStep < 0) {
109  }
110  return currentTimeStep;
111 }
112 
113 
114 
115 /****************************************************************************/
116 
long long int SUMOTime
Definition: SUMOTime.h:36
MSEventControl()
Default constructor.
virtual void execute(SUMOTime time)
Executes time-dependant commands.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:165
SUMOTime DELTA_T
Definition: SUMOTime.cpp:35
Base (microsim) event class.
Definition: Command.h:54
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:241
SUMOTime currentTimeStep
The current TimeStep.
virtual SUMOTime execute(SUMOTime currentTime)=0
Executes the command.
void setCurrentTimeStep(SUMOTime time)
Set the current Time.
std::pair< Command *, SUMOTime > Event
Combination of an event and the time it shall be executed at.
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:263
EventCont myEvents
Event-container, holds executable events.
SUMOTime getCurrentTimeStep()
get the Current TimeStep used in addEvent.
virtual ~MSEventControl()
Destructor.
bool isEmpty()
Returns whether events are in the que.