Eclipse 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-2019 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 /****************************************************************************/
18 // Stores time-dependant events and executes them at the proper time
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <cassert>
28 #include "MSEventControl.h"
30 #include <utils/common/Command.h>
31 #include "MSNet.h"
32 
33 
34 // ===========================================================================
35 // member definitions
36 // ===========================================================================
38  : currentTimeStep(-1), myEvents() {}
39 
40 
42  // delete the events
43  while (!myEvents.empty()) {
44  Event e = myEvents.top();
45  delete e.first;
46  myEvents.pop();
47  }
48 }
49 
50 
51 void
52 MSEventControl::addEvent(Command* operation, SUMOTime execTimeStep) {
53  myEvents.push(Event(operation, execTimeStep));
54 }
55 
56 
57 void
59  // Execute all events that are scheduled for execTime.
60  while (!myEvents.empty()) {
61  Event currEvent = myEvents.top();
62  if (currEvent.second < 0) {
63  currEvent.second = execTime;
64  }
65  if (currEvent.second < execTime + DELTA_T) {
66  Command* command = currEvent.first;
67  myEvents.pop();
68  SUMOTime time = 0;
69  try {
70  time = command->execute(execTime);
71  } catch (...) {
72  delete command;
73  throw;
74  }
75 
76  // Delete nonrecurring events, reinsert recurring ones
77  // with new execution time = execTime + returned offset.
78  if (time <= 0) {
79  if (time < 0) {
80  WRITE_WARNING("Command returned negative repeat number; will be deleted.");
81  }
82  delete currEvent.first;
83  } else {
84  currEvent.second += time;
85  myEvents.push(currEvent);
86  }
87  } else {
88  break;
89  }
90  }
91 }
92 
93 
94 bool
96  return myEvents.empty();
97 }
98 
99 void
101  currentTimeStep = time;
102 }
103 
104 SUMOTime
106  if (currentTimeStep < 0) {
108  }
109  return currentTimeStep;
110 }
111 
112 
113 
114 /****************************************************************************/
115 
MSEventControl::addEvent
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
Definition: MSEventControl.cpp:52
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:275
MSNet.h
MSEventControl::setCurrentTimeStep
void setCurrentTimeStep(SUMOTime time)
Set the current Time.
Definition: MSEventControl.cpp:100
DELTA_T
SUMOTime DELTA_T
Definition: SUMOTime.cpp:36
MsgHandler.h
MSEventControl::myEvents
EventCont myEvents
Event-container, holds executable events.
Definition: MSEventControl.h:131
MSEventControl::Event
std::pair< Command *, SUMOTime > Event
Combination of an event and the time it shall be executed at.
Definition: MSEventControl.h:52
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:34
Command::execute
virtual SUMOTime execute(SUMOTime currentTime)=0
Executes the command.
MSEventControl::~MSEventControl
virtual ~MSEventControl()
Destructor.
Definition: MSEventControl.cpp:41
MSNet::getCurrentTimeStep
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:283
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
MSEventControl::currentTimeStep
SUMOTime currentTimeStep
The current TimeStep.
Definition: MSEventControl.h:128
MSEventControl::isEmpty
bool isEmpty()
Returns whether events are in the que.
Definition: MSEventControl.cpp:95
Command
Base (microsim) event class.
Definition: Command.h:52
MSEventControl::MSEventControl
MSEventControl()
Default constructor.
Definition: MSEventControl.cpp:37
MSEventControl::execute
virtual void execute(SUMOTime time)
Executes time-dependant commands.
Definition: MSEventControl.cpp:58
config.h
Command.h
MSEventControl.h
MSEventControl::getCurrentTimeStep
SUMOTime getCurrentTimeStep()
get the Current TimeStep used in addEvent.
Definition: MSEventControl.cpp:105