Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSEventControl.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
22// Stores time-dependant events and executes them at the proper time
23/****************************************************************************/
24#include <config.h>
25
26#include <cassert>
27#include "MSEventControl.h"
30#include "MSNet.h"
31
32
33// ===========================================================================
34// member definitions
35// ===========================================================================
37 myEvents() {}
38
39
41 // delete the events
42 for (const Event& e : myEvents) {
43 delete e.first;
44 }
45}
46
47
48void
49MSEventControl::addEvent(Command* operation, SUMOTime execTimeStep) {
50 myEvents.emplace_back(Event(operation, execTimeStep));
51 std::push_heap(myEvents.begin(), myEvents.end(), MSEventControl::eventCompare);
52}
53
54
55void
57 // Execute all events that are scheduled for execTime.
58 while (!myEvents.empty()) {
59 Event currEvent = myEvents.front();
60 if (currEvent.second < 0) {
61 currEvent.second = execTime;
62 }
63 if (currEvent.second < execTime + DELTA_T) {
64 Command* command = currEvent.first;
65 std::pop_heap(myEvents.begin(), myEvents.end(), eventCompare);
66 myEvents.pop_back();
67 SUMOTime time = 0;
68 try {
69 time = command->execute(execTime);
70 } catch (...) {
71 delete command;
72 throw;
73 }
74
75 // Delete nonrecurring events, reinsert recurring ones
76 // with new execution time = execTime + returned offset.
77 if (time <= 0) {
78 if (time < 0) {
79 WRITE_WARNING("Command returned negative repeat number; will be deleted.");
80 }
81 delete currEvent.first;
82 } else {
83 addEvent(currEvent.first, currEvent.second + time);
84 }
85 } else {
86 break;
87 }
88 }
89}
90
91
92bool
94 return myEvents.empty();
95}
96
97bool
99 return e1.second == e2.second ? e1.first->priority < e2.first->priority : e1.second > e2.second;
100}
101
102void
104 for (auto eventIt = myEvents.begin(); eventIt != myEvents.end();) {
105 eventIt->second = eventIt->first->shiftTime(currentTime, eventIt->second, newTime);
106 if (eventIt->second >= 0) {
107 ++eventIt;
108 } else {
109 delete eventIt->first;
110 eventIt = myEvents.erase(eventIt);
111 }
112 }
113 std::make_heap(myEvents.begin(), myEvents.end(), eventCompare);
114}
115
116
117/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define WRITE_WARNING(msg)
Definition MsgHandler.h:295
SUMOTime DELTA_T
Definition SUMOTime.cpp:38
Base (microsim) event class.
Definition Command.h:50
virtual SUMOTime execute(SUMOTime currentTime)=0
Executes the command.
virtual void execute(SUMOTime time)
Executes time-dependant commands.
static bool eventCompare(const Event &e1, const Event &e2)
compares two events
bool isEmpty()
Returns whether events are in the que.
std::pair< Command *, SUMOTime > Event
Combination of an event and the time it shall be executed at.
MSEventControl()
Default constructor.
void clearState(SUMOTime currentTime, SUMOTime newTime)
Remove all events before quick-loading state.
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
virtual ~MSEventControl()
Destructor.
std::vector< Event > myEvents
Event-container, holds executable events.