/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file MSEventControl.cpp14/// @author Christian Roessel15/// @author Daniel Krajzewicz16/// @author Jakob Erdmann17/// @author Michael Behrisch18/// @author Matthias Heppner19/// @date Mon, 12 Mar 200120///21// Stores time-dependant events and executes them at the proper time22/****************************************************************************/23#include <config.h>2425#include <cassert>26#include "MSEventControl.h"27#include <utils/common/MsgHandler.h>28#include <utils/common/Command.h>29#include "MSNet.h"303132// ===========================================================================33// member definitions34// ===========================================================================35MSEventControl::MSEventControl() :36myEvents() {}373839MSEventControl::~MSEventControl() {40// delete the events41for (const Event& e : myEvents) {42delete e.first;43}44}454647void48MSEventControl::addEvent(Command* operation, SUMOTime execTimeStep) {49myEvents.emplace_back(Event(operation, execTimeStep));50std::push_heap(myEvents.begin(), myEvents.end(), MSEventControl::eventCompare);51}525354void55MSEventControl::execute(SUMOTime execTime) {56// Execute all events that are scheduled for execTime.57while (!myEvents.empty()) {58Event currEvent = myEvents.front();59if (currEvent.second < 0) {60currEvent.second = execTime;61}62if (currEvent.second < execTime + DELTA_T) {63Command* command = currEvent.first;64std::pop_heap(myEvents.begin(), myEvents.end(), eventCompare);65myEvents.pop_back();66SUMOTime time = 0;67try {68time = command->execute(execTime);69} catch (...) {70delete command;71throw;72}7374// Delete nonrecurring events, reinsert recurring ones75// with new execution time = execTime + returned offset.76if (time <= 0) {77if (time < 0) {78WRITE_WARNING("Command returned negative repeat number; will be deleted.");79}80delete currEvent.first;81} else {82addEvent(currEvent.first, currEvent.second + time);83}84} else {85break;86}87}88}899091bool92MSEventControl::isEmpty() {93return myEvents.empty();94}9596bool97MSEventControl::eventCompare(const Event& e1, const Event& e2) {98return e1.second == e2.second ? e1.first->priority < e2.first->priority : e1.second > e2.second;99}100101void102MSEventControl::clearState(SUMOTime currentTime, SUMOTime newTime) {103for (auto eventIt = myEvents.begin(); eventIt != myEvents.end();) {104eventIt->second = eventIt->first->shiftTime(currentTime, eventIt->second, newTime);105if (eventIt->second >= 0) {106++eventIt;107} else {108delete eventIt->first;109eventIt = myEvents.erase(eventIt);110}111}112std::make_heap(myEvents.begin(), myEvents.end(), eventCompare);113}114115116/****************************************************************************/117118119