Eclipse SUMO - Simulation of Urban MObility
GNEChange_TLS.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 /****************************************************************************/
18 // A network change in which a traffic light is created or deleted
19 /****************************************************************************/
20 #include <config.h>
21 
22 
24 #include <netbuild/NBOwnTLDef.h>
25 #include <netedit/GNENet.h>
26 
27 #include "GNEChange_TLS.h"
28 
29 
30 // ===========================================================================
31 // FOX-declarations
32 // ===========================================================================
33 FXIMPLEMENT_ABSTRACT(GNEChange_TLS, GNEChange, nullptr, 0)
34 
35 // ===========================================================================
36 // member method definitions
37 // ===========================================================================
38 
39 
40 GNEChange_TLS::GNEChange_TLS(GNEJunction* junction, NBTrafficLightDefinition* tlDef, bool forward, bool forceInsert, const std::string tlID) :
42  GNEChange(Supermode::NETWORK, forward, false),
43  myJunction(junction),
44  myTlDef(tlDef),
45  myForceInsert(forceInsert) {
46  myJunction->incRef("GNEChange_TLS");
47  if (myTlDef == nullptr) {
48  // check forward
49  if (!forward) {
50  throw ProcessError("If myTlDef is null, forward cannot be false");
51  }
52  // potential memory leak if this change is never executed
54  if (myJunction->getNBNode()->isTLControlled()) {
55  // copy existing type
56  type = (*myJunction->getNBNode()->getControllingTLS().begin())->getType();
57  }
58  myTlDef = new NBOwnTLDef(tlID == "" ? myJunction->getMicrosimID() : tlID, 0, type);
59  }
60 }
61 
62 
64  bool forceInsert, const std::string tlID) :
65  GNEChange(Supermode::NETWORK, forward, false),
66  myJunction(junction),
67  myTlDef(tlDef),
68  myForceInsert(forceInsert) {
69  myJunction->incRef("GNEChange_TLS");
70  if (myTlDef == nullptr) {
71  // check forward
72  if (!forward) {
73  throw ProcessError("If myTlDef is null, forward cannot be false");
74  }
76  // copy existing type
77  type = (*myJunction->getNBNode()->getControllingTLS().begin())->getType();
78  }
79  myTlDef = new NBOwnTLDef(tlID == "" ? myJunction->getMicrosimID() : tlID, 0, type);
80  }
81 }
82 
83 
84 GNEChange_TLS::GNEChange_TLS(GNEJunction* junction, NBTrafficLightDefinition* tlDef, const std::string& newID) :
85  GNEChange(Supermode::NETWORK, true, false),
86  myJunction(junction),
87  myTlDef(tlDef),
88  myForceInsert(false),
89  myOldID(tlDef->getID()),
90  myNewID(newID) {
91  myJunction->incRef("GNEChange_TLS");
92 }
93 
94 
96  myJunction->decRef("GNEChange_TLS");
97  if (myJunction->unreferenced()) {
98  // show extra information for tests
99  WRITE_DEBUG("Deleting unreferenced " + myJunction->getTagStr() + " '" + myJunction->getID() + "' in GNEChange_TLS");
100  delete myJunction;
101  }
102 }
103 
104 
105 void
107  if (myForward) {
108  if (myNewID.empty()) {
109  // show extra information for tests
110  WRITE_DEBUG("Removing TLS from " + myJunction->getTagStr() + " '" + myJunction->getID() + "'");
111  // remove traffic light from junction
113  } else {
114  // show extra information for tests
115  WRITE_DEBUG("Renaming Traffic Light: " + myOldID);
116  // set old ID
118  }
119  } else {
120  if (myNewID.empty()) {
121  // show extra information for tests
122  WRITE_DEBUG("Adding TLS into " + myJunction->getTagStr() + " '" + myJunction->getID() + "'");
123  // add traffic light to junction
125  } else {
126  // show extra information for tests
127  WRITE_DEBUG("Renaming Traffic Light: " + myNewID);
128  // set new ID
130  }
131  }
132  // enable save networkElements
134 }
135 
136 
137 void
139  if (myForward) {
140  if (myNewID.empty()) {
141  // show extra information for tests
142  WRITE_DEBUG("Adding TLS into " + myJunction->getTagStr() + " '" + myJunction->getID() + "'");
143  // add traffic light to junction
145  } else {
146  // show extra information for tests
147  WRITE_DEBUG("Renaming Traffic Light: " + myNewID);
148  // set new ID
150  }
151  } else {
152  if (myNewID.empty()) {
153  // show extra information for tests
154  WRITE_DEBUG("Deleting TLS from " + myJunction->getTagStr() + " '" + myJunction->getID() + "'");
155  // remove traffic light from junction
157  } else {
158  // show extra information for tests
159  WRITE_DEBUG("Renaming Traffic Light: " + myOldID);
160  // set old ID
162  }
163  }
164  // enable save networkElements
166 }
167 
168 
169 std::string
171  if (myForward) {
172  return (TL("Undo create TLS '") + myJunction->getID() + "'");
173  } else {
174  return (TL("Undo delete TLS '") + myJunction->getID() + "'");
175  }
176 }
177 
178 
179 std::string
181  if (myForward) {
182  return (TL("Redo create TLS '") + myJunction->getID() + "'");
183  } else {
184  return (TL("Redo delete TLS '") + myJunction->getID() + "'");
185  }
186 }
Supermode
@brie enum for supermodes
@ NETWORK
Network mode (Edges, junctions, etc..)
#define WRITE_DEBUG(msg)
Definition: MsgHandler.h:306
#define TL(string)
Definition: MsgHandler.h:315
TrafficLightType
const std::string getID() const
get ID (all Attribute Carriers have one)
const std::string & getTagStr() const
get tag assigned to this object in string format
GNENet * getNet() const
get pointer to net
GNEJunction * myJunction
we need the junction because it is the target of our change commands
Definition: GNEChange_TLS.h:86
void undo()
undo action
GNEChange_TLS(GNEJunction *junction, NBTrafficLightDefinition *tlDef, bool forward, bool forceInsert=false, const std::string tlID="")
Constructor for creating/deleting a TLS.
const std::string myNewID
Definition: GNEChange_TLS.h:98
void redo()
redo action
std::string undoName() const
return undoName
~GNEChange_TLS()
Destructor.
const std::string myOldID
variables used for renaming TLS
Definition: GNEChange_TLS.h:97
std::string redoName() const
get Redo name
NBTrafficLightDefinition * myTlDef
the traffic light to be created/deleted. We assume no responsibility for the pointer
Definition: GNEChange_TLS.h:91
const bool myForceInsert
check if forceInsert is enabled
Definition: GNEChange_TLS.h:94
the function-object for an editing operation (abstract base)
Definition: GNEChange.h:56
bool myForward
we group antagonistic commands (create junction/delete junction) and keep them apart by this flag
Definition: GNEChange.h:212
void addTrafficLight(NBTrafficLightDefinition *tlDef, bool forceInsert)
adds a traffic light
NBNode * getNBNode() const
Return net build node.
void removeTrafficLight(NBTrafficLightDefinition *tlDef)
removes a traffic light
void requireSaveNetwork()
inform that network has to be saved
NBTrafficLightLogicCont & getTLLogicCont()
returns the tllcont of the underlying netbuilder
Definition: GNENet.cpp:2142
GNENetHelper::SavingStatus * getSavingStatus() const
get saving status
Definition: GNENet.cpp:129
void decRef(const std::string &debugMsg="")
Decrease reference.
void incRef(const std::string &debugMsg="")
Increase reference.
bool unreferenced()
check if object ins't referenced
const std::string & getMicrosimID() const
Returns the id of the object as known to microsim.
Definition: GUIGlObject.h:143
const std::set< NBTrafficLightDefinition * > & getControllingTLS() const
Returns the traffic lights that were assigned to this node (The set of tls that control this node)
Definition: NBNode.h:336
bool isTLControlled() const
Returns whether this node is controlled by any tls.
Definition: NBNode.h:331
A traffic light logics which must be computed (only nodes/edges are given)
Definition: NBOwnTLDef.h:44
The base class for traffic light logic definitions.
void rename(NBTrafficLightDefinition *tlDef, const std::string &newID)
rename traffic light
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:60
static StringBijection< TrafficLightType > TrafficLightTypes
traffic light types
T get(const std::string &str) const
Definition: json.hpp:4471