/****************************************************************************/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 NLJunctionControlBuilder.h14/// @author Daniel Krajzewicz15/// @author Jakob Erdmann16/// @author Michael Behrisch17/// @date Mon, 9 Jul 200118///19// Builder of microsim-junctions and tls20/****************************************************************************/21#pragma once22#include <config.h>2324#include <string>25#include <vector>26#include <microsim/MSJunction.h>27#include <microsim/MSRightOfWayJunction.h>28#include <microsim/MSJunctionControl.h>29#include <utils/geom/Position.h>30#include <utils/geom/PositionVector.h>31#include <microsim/traffic_lights/MSSimpleTrafficLightLogic.h>32#include <microsim/traffic_lights/MSActuatedTrafficLightLogic.h>33#include <microsim/traffic_lights/MSRailSignal.h>34#include <microsim/MSBitSetLogic.h>35#include <microsim/traffic_lights/MSTLLogicControl.h>36#include <utils/common/UtilExceptions.h>373839// ===========================================================================40// class declarations41// ===========================================================================42class OptionsCont;43class NLDetectorBuilder;444546// ===========================================================================47// class definitions48// ===========================================================================49/**50* @class NLJunctionControlBuilder51* @brief Builder of microsim-junctions and tls52*53* NLJunctionControlBuilder is a factory for MSJunction, MSJunctionLogic, WAUT,54* and MSTRafficLightLogic-instances.55*56* @todo Refactor this class - it's too large57* @todo Resort method by one of the topics.58*/59class NLJunctionControlBuilder {60private:61/// @brief Definition of a lane vector62typedef std::vector<MSLane*> LaneVector;6364public:65/** @brief Constructor66*67* Stores default values for extended tls reading them from the given68* options. Builds a MSTLLogicControl instance for myLogicControl.69* @param[in] net The network to fill70* @param[in] db The detector builder to use71*/72NLJunctionControlBuilder(MSNet& net, NLDetectorBuilder& db);737475/** @brief Destructor76*77* Deletes previously allocated "myLogicControl" and "myJunctions" if78* they were not previously returned (this may happen if an error occurred).79*/80virtual ~NLJunctionControlBuilder();818283/** @brief Begins the processing of the named junction84*85* @param[in] id The ID of the junction86* @param[in] key unused?!87* @param[in] type The type of the junction88* @param[in] x x-position of the junction89* @param[in] y y-position of the junction90* @param[in] shape The shape of the junction91* @param[in] incomingLanes List of lanes which end at this intersection92* @param[in] internalLanes List of internal lanes across this intersection93* @exception InvalidArgument If the junction type is not known94* @todo Check why "key" is given95*/96void openJunction(const std::string& id, const std::string& key,97const SumoXMLNodeType type,98const Position pos,99const PositionVector& shape,100const std::vector<MSLane*>& incomingLanes,101const std::vector<MSLane*>& internalLanes,102const std::string& name);103104105/** @brief Closes (ends) the processing of the current junction106*107* This method throws an InvalidArgument when a junction with the same id108* as the current was already added or if the junction type stored in "myType"109* is invalid. It throws a ProcessError if the container to store the110* junction in was not built before.111*112* @exception InvalidArgument If the current id is already used or the junction type was invalid113* @exception ProcessError If the container to store the junction in was not built before114* @todo Throwing ProcessError would get unneeded if the container would be built by default (see prepare)115* @todo The type of the junctions shoould probably be checked when supprted (in openJunction)116*/117void closeJunction(const std::string& basePath);118119120/** @brief Builds the MSJunctionControl which holds all of the simulations junctions121*122* Returns the previously built junction control ("myJunctions"). "myJunctions" is123* set to 0, so that it will not be destroyed by the destructor.124*125* @return The built junction control126*/127MSJunctionControl* build() const;128129130/** @brief Initialises a junction logic131* @param[in] id The id of the row-logic132*/133void initJunctionLogic(const std::string& id);134135136/** @brief Adds a logic item137*138* Adds a logic item to the current processed logic. Throws an InvalidArgument139* if the current request size (myRequestSize) is 0 or lower.140*141* @param[in] request The request (~link) index142* @param[in] response The response (list of higher priorised links)143* @param[in] foes List of foes to the request144* @param[in] cont Whether the request is followed by an internal end145* @todo Recheck "cont"; is the description correct?146*/147void addLogicItem(int request, const std::string& response,148const std::string& foes, bool cont);149150/** @brief Begins the reading of a traffic lights logic151*152* @param[in] id The id of the tls153* @param[in] programID The id of the currently loaded program154* @param[in] type The type of the tls155* @param[in] offset The offset to start with156* @todo Why is the type not verified?157* @todo Recheck, describe usage of detectorOffset (where does the information come from?)158*/159void initTrafficLightLogic(const std::string& id, const std::string& programID,160TrafficLightType type, SUMOTime offset);161162/** @brief Adds a phase to the currently built traffic lights logic163*164* @param[in] phase The new phase165*/166void addPhase(MSPhaseDefinition* phase);167168/** @brief Adds a condition to the currently built traffic lights logic169*170* @param[in] id the condition id171* @param[in] value the condition expression172*/173bool addCondition(const std::string& id, const std::string& value);174175/** @brief Adds an assignment to the currently built traffic lights logic176*177* @param[in] id the condition id178* @param[in] check the check condition that guards the assignment179* @param[in] value the assigned expression180*/181void addAssignment(const std::string& id, const std::string& check, const std::string& value);182183/** @brief adds a switching condition function to the traffic lights logic currently build184*185* @param[in] id the function id186* @param[in] nArgs the number of arguments187*/188void addFunction(const std::string& id, int nArgs);189190/// closes a switching condition function to the traffic lights logic currently build191void closeFunction();192193/** @brief Returns a previously build tls logic194*195* @param[in] id The ID of the tls logic to return196* @return The named logic197* @exception InvalidArgument If the named tls logic was not built before198*/199MSTLLogicControl::TLSLogicVariants& getTLLogic(const std::string& id) const;200201/** @brief Returns the built tls-logic control202*203* Returns the junction control ("myLogicControl") built using MSTLLogicControl::closeNetworkReading.204* "myLogicControl" is set to 0, so that it will not be destroyed by the destructor.205*206* @return The built tls-logic control207* @exception ProcessError If the traffic lights were not correct208* @see MSTLLogicControl::closeNetworkReading209*/210MSTLLogicControl* buildTLLogics();211212/** @brief Ends the building of a traffic lights logic213*214* Builds the correct type of a MSTrafficLightLogic using the stored information.215* Tries to add it to the used tls control. Throws an InvalidArgument if216* this is not possible (another tls logic with the same name exists).217*218* @exception InvalidArgument If another tls logic with the same name as the currently built was loaded before219*/220virtual void closeTrafficLightLogic(const std::string& basePath);221222/** @brief Adds a parameter223*224* @param[in] key The key of the parameter225* @param[in] value The value of the parameter226* @todo Where are these parameter used? Describe!227* @todo Can a parameter be overwritten?228*/229void addParam(const std::string& key, const std::string& value);230231/** @brief Returns the active key232* @return The active key233*/234const std::string& getActiveKey() const;235236/** @brief Returns the active sub key237* @return The active sub key238*/239const std::string& getActiveSubKey() const;240241/** @brief Returns the used tls control242*243* This may be either the internal one ("myLogicControl"), or, if the network has244* been already loaded and we are loading additional tls programs, the net's logic control.245*246* @return The tls control to use247*/248MSTLLogicControl& getTLLogicControlToUse() const;249250/// @brief initialize junctions after all connections have been loaded251void postLoadInitialization();252253/// @brief try to retrieve junction by id254MSJunction* retrieve(const std::string id);255256/// @brief return the phases loaded so far (for error reporting and cleanup)257const MSSimpleTrafficLightLogic::Phases& getLoadedPhases() const {258return myActivePhases;259}260261void netIsLoaded() {262myNetIsLoaded = true;263}264265protected:266/// @name Factory methods, virtual so that other versions of the structures can be built267/// @{268269/** @brief Builds a junction that does not use a logic270*271* Builds a MSNoLogicJunction272*273* @return The built junction274*/275virtual MSJunction* buildNoLogicJunction();276277/** @brief Builds a junction with a logic278*279* Builds a MSRightOfWayJunction. Throws an exception if the logic was not built.280*281* @return The built junction282* @exception InvalidArgument If the logic of the junction was not built before283*/284virtual MSJunction* buildLogicJunction(MSJunctionLogic* const logic);285286/** @brief Builds an internal junction287*288* Builds a MSInternalJunction289*290* @return The built junction291*/292virtual MSJunction* buildInternalJunction();293/// @}294295protected:296/// @brief The net to use297MSNet& myNet;298299/// @brief The detector builder to use300NLDetectorBuilder& myDetectorBuilder;301302/// @brief The switch offset within the tls303SUMOTime myOffset;304305/// @brief The current logic type306TrafficLightType myLogicType;307308/// @brief The right-of-way-logic of the currently chosen bitset-logic309MSBitsetLogic::Logic myActiveLogic;310311/// @brief The description about which lanes disallow other passing the junction simultaneously312MSBitsetLogic::Foes myActiveFoes;313314/// @brief The description about which lanes have an internal follower315std::bitset<SUMO_MAX_CONNECTIONS> myActiveConts;316317/// @brief The current phase definitions for a simple traffic light318MSSimpleTrafficLightLogic::Phases myActivePhases;319320/// @brief The current switching conditions for an actuated traffic light321MSActuatedTrafficLightLogic::ConditionMap myActiveConditions;322323/// @brief The current assignments for an actuated traffic light324MSActuatedTrafficLightLogic::AssignmentMap myActiveAssignments;325326/// @brief The current functions for an actuated traffic light327MSActuatedTrafficLightLogic::FunctionMap myActiveFunctions;328329/// @brief The current function for an actuated traffic light330MSActuatedTrafficLightLogic::Function myActiveFunction;331332/// @brief The size of the request333int myRequestSize;334335/// @brief Counter for the inserted items336int myRequestItemNumber;337338/// @brief The junctions controls339mutable MSJunctionControl* myJunctions;340341/// @brief The list of the incoming lanes of the currently chosen junction342LaneVector myActiveIncomingLanes;343344/// @brief The list of the internal lanes of the currently chosen junction345LaneVector myActiveInternalLanes;346347/// @brief The id of the currently chosen junction348std::string myActiveID;349350/// @brief The key of the currently chosen junction351std::string myActiveKey, myActiveProgram;352353/// @brief The type of the currently chosen junction354SumoXMLNodeType myType;355356/// @brief The position of the junction357Position myPosition;358359/// @brief The absolute duration of a tls-control loop360SUMOTime myAbsDuration;361362/// @brief The shape of the current junction363PositionVector myShape;364365/// @brief the name of the current junction366std::string myActiveName;367368/// @brief Definition of a parameter map (key->value)369typedef Parameterised::Map StringParameterMap;370371/// @brief The container for information which junctions shall be initialised using which values372std::vector<MSTrafficLightLogic*> myNetworkLogics;373std::vector<MSTrafficLightLogic*> myAdditionalLogics;374std::vector<MSTrafficLightLogic*> myRailSignals;375std::map<MSTrafficLightLogic*, StringParameterMap> myLogicParams;376377/// @brief The tls control to use (0 if net's tls control shall be used)378mutable MSTLLogicControl* myLogicControl;379380/// @brief Parameter map (key->value)381StringParameterMap myAdditionalParameter;382383/// @brief Information whether the current logic had an error384bool myCurrentHasError;385386private:387/** @brief invalidated copy operator */388NLJunctionControlBuilder(const NLJunctionControlBuilder& s);389390/** @brief invalidated assignment operator */391NLJunctionControlBuilder& operator=(const NLJunctionControlBuilder& s);392393static const int NO_REQUEST_SIZE;394395/// @brief whether the network has been loaded396bool myNetIsLoaded;397398399};400401402