SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
MSEventControl.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // Stores time-dependant events and executes them at the proper time
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2001-2015 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <cassert>
36 #include "MSEventControl.h"
38 #include <utils/common/Command.h>
39 #include "MSNet.h"
40 
41 #ifdef CHECK_MEMORY_LEAKS
42 #include <foreign/nvwa/debug_new.h>
43 #endif // CHECK_MEMORY_LEAKS
44 
45 
46 // ===========================================================================
47 // member definitions
48 // ===========================================================================
50  : currentTimeStep(-1), myEvents() {}
51 
52 
54  // delete the events
55  while (! myEvents.empty()) {
56  Event e = myEvents.top();
57  delete e.first;
58  myEvents.pop();
59  }
60 }
61 
62 
65  SUMOTime execTimeStep,
66  AdaptType type) {
67  SUMOTime currTimeStep = getCurrentTimeStep();
68  if (type == ADAPT_AFTER_EXECUTION && execTimeStep <= currTimeStep) {
69  execTimeStep = currTimeStep;
70  }
71  Event newEvent = Event(operation, execTimeStep);
72  myEvents.push(newEvent);
73  return execTimeStep;
74 }
75 
76 
77 void
79  // Execute all events that are scheduled for execTime.
80  for (; !myEvents.empty();) {
81  Event currEvent = myEvents.top();
82  if (currEvent.second == execTime || currEvent.second < execTime + DELTA_T) {
83  Command* command = currEvent.first;
84  myEvents.pop();
85  SUMOTime time = 0;
86  try {
87  time = command->execute(execTime);
88  } catch (...) {
89  delete command;
90  throw;
91  }
92 
93  // Delete nonrecurring events, reinsert recurring ones
94  // with new execution time = execTime + returned offset.
95  if (time <= 0) {
96  if (time < 0) {
97  WRITE_WARNING("Command returned negative repeat number; will be deleted.");
98  }
99  delete currEvent.first;
100  } else {
101  currEvent.second += time;
102  myEvents.push(currEvent);
103  }
104  } else {
105  if (currEvent.second < execTime) {
106  // !!! more verbose information
107  WRITE_WARNING("Could not execute scheduled event.");
108  delete currEvent.first;
109  myEvents.pop();
110  } else {
111  break;
112  }
113  }
114  }
115 }
116 
117 
118 bool
120  return myEvents.empty();
121 }
122 
123 void
125  currentTimeStep = time;
126 }
127 
128 SUMOTime
130  if (currentTimeStep < 0) {
132  }
133  return currentTimeStep;
134 }
135 
136 
137 
138 /****************************************************************************/
139 
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:159
Base (microsim) event class.
Definition: Command.h:61
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:235
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
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.
AdaptType
Defines what to do if the insertion time lies before the current simulation time. ...
EventCont myEvents
Event-container, holds executable events.
SUMOTime getCurrentTimeStep()
get the Current TimeStep used in addEvent.
virtual SUMOTime addEvent(Command *operation, SUMOTime execTimeStep, AdaptType type)
Adds an Event.
virtual ~MSEventControl()
Destructor.
bool isEmpty()
Returns whether events are in the que.
int SUMOTime
Definition: SUMOTime.h:43
Patch the time in a way that it is at least as high as the simulation begin time. ...
#define DELTA_T
Definition: SUMOTime.h:50