Line data Source code
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 : /****************************************************************************/
14 : /// @file NLDiscreteEventBuilder.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @author Sascha Krieg
18 : /// @author Michael Behrisch
19 : /// @date Sep, 2003
20 : ///
21 : // }
22 : /****************************************************************************/
23 : #include <config.h>
24 :
25 : #include <utils/xml/SUMOXMLDefinitions.h>
26 : #include <microsim/MSNet.h>
27 : #include <microsim/actions/Command_SaveTLSState.h>
28 : #include <microsim/actions/Command_SaveTLSSwitches.h>
29 : #include <microsim/actions/Command_SaveTLSSwitchStates.h>
30 : #include <microsim/actions/Command_SaveTLSProgram.h>
31 : #include <microsim/MSEventControl.h>
32 : #include <microsim/traffic_lights/MSTLLogicControl.h>
33 : #include <microsim/traffic_lights/MSTrafficLightLogic.h>
34 : #include <utils/common/FileHelpers.h>
35 : #include <utils/common/UtilExceptions.h>
36 : #include <utils/iodevices/OutputDevice.h>
37 : #include "NLDiscreteEventBuilder.h"
38 :
39 :
40 : // ===========================================================================
41 : // method definitions
42 : // ===========================================================================
43 35454 : NLDiscreteEventBuilder::NLDiscreteEventBuilder(MSNet& net)
44 35454 : : myNet(net) {
45 35454 : myActions["SaveTLSStates"] = EV_SAVETLSTATE;
46 35454 : myActions["SaveTLSSwitchTimes"] = EV_SAVETLSWITCHES;
47 35454 : myActions["SaveTLSSwitchStates"] = EV_SAVETLSWITCHSTATES;
48 35454 : myActions["SaveTLSProgram"] = EV_SAVETLSPROGRAM;
49 35454 : }
50 :
51 :
52 35454 : NLDiscreteEventBuilder::~NLDiscreteEventBuilder() {}
53 :
54 :
55 : void
56 1334 : NLDiscreteEventBuilder::addAction(const SUMOSAXAttributes& attrs,
57 : const std::string& basePath) {
58 1334 : bool ok = true;
59 2668 : const std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, nullptr, ok, "");
60 : // check whether the type was given
61 1334 : if (type == "" || !ok) {
62 0 : throw InvalidArgument("An action's type is not given.");
63 : }
64 : // get the numerical representation
65 : KnownActions::iterator i = myActions.find(type);
66 1334 : if (i == myActions.end()) {
67 0 : throw InvalidArgument("The action type '" + type + "' is not known.");
68 : }
69 : // build the action
70 1334 : switch ((*i).second) {
71 513 : case EV_SAVETLSTATE:
72 513 : buildSaveTLStateCommand(attrs, basePath);
73 : break;
74 265 : case EV_SAVETLSWITCHES:
75 265 : buildSaveTLSwitchesCommand(attrs, basePath);
76 : break;
77 533 : case EV_SAVETLSWITCHSTATES:
78 533 : buildSaveTLSwitchStatesCommand(attrs, basePath);
79 : break;
80 23 : case EV_SAVETLSPROGRAM:
81 23 : buildSaveTLSProgramCommand(attrs, basePath);
82 : break;
83 : }
84 1334 : }
85 :
86 :
87 : void
88 513 : NLDiscreteEventBuilder::buildSaveTLStateCommand(const SUMOSAXAttributes& attrs,
89 : const std::string& basePath) {
90 513 : bool ok = true;
91 513 : const std::string dest = attrs.getOpt<std::string>(SUMO_ATTR_DEST, nullptr, ok, "");
92 513 : const std::string source = attrs.getOpt<std::string>(SUMO_ATTR_SOURCE, nullptr, ok, "");
93 513 : const bool saveDetectors = attrs.getOpt<bool>(SUMO_ATTR_SAVE_DETECTORS, nullptr, ok, false);
94 513 : const bool saveConditions = attrs.getOpt<bool>(SUMO_ATTR_SAVE_CONDITIONS, nullptr, ok, false);
95 : // check the parameter
96 513 : if (dest == "" || !ok) {
97 0 : throw InvalidArgument("Incomplete description of an 'SaveTLSState'-action occurred.");
98 : }
99 513 : if (source == "") {
100 0 : const std::vector<std::string> ids = myNet.getTLSControl().getAllTLIds();
101 0 : for (std::vector<std::string>::const_iterator tls = ids.begin(); tls != ids.end(); ++tls) {
102 0 : const MSTLLogicControl::TLSLogicVariants& logics = myNet.getTLSControl().get(*tls);
103 0 : new Command_SaveTLSState(logics, OutputDevice::getDevice(FileHelpers::checkForRelativity(dest, basePath)),
104 0 : saveDetectors, saveConditions);
105 : }
106 0 : } else {
107 : // get the logic
108 513 : if (!myNet.getTLSControl().knows(source)) {
109 0 : throw InvalidArgument("The traffic light logic to save (" + source + ") is not known.");
110 : }
111 513 : const MSTLLogicControl::TLSLogicVariants& logics = myNet.getTLSControl().get(source);
112 : // build the action
113 1026 : new Command_SaveTLSState(logics, OutputDevice::getDevice(FileHelpers::checkForRelativity(dest, basePath)),
114 513 : saveDetectors, saveConditions);
115 : }
116 513 : }
117 :
118 :
119 : void
120 265 : NLDiscreteEventBuilder::buildSaveTLSwitchesCommand(const SUMOSAXAttributes& attrs,
121 : const std::string& basePath) {
122 265 : bool ok = true;
123 265 : const std::string dest = attrs.getOpt<std::string>(SUMO_ATTR_DEST, nullptr, ok, "");
124 530 : const std::string source = attrs.getOpt<std::string>(SUMO_ATTR_SOURCE, nullptr, ok, "");
125 : // check the parameter
126 265 : if (dest == "" || !ok) {
127 0 : throw InvalidArgument("Incomplete description of an 'SaveTLSSwitchTimes'-action occurred.");
128 : }
129 265 : if (source == "") {
130 0 : const std::vector<std::string> ids = myNet.getTLSControl().getAllTLIds();
131 0 : for (std::vector<std::string>::const_iterator tls = ids.begin(); tls != ids.end(); ++tls) {
132 0 : const MSTLLogicControl::TLSLogicVariants& logics = myNet.getTLSControl().get(*tls);
133 0 : new Command_SaveTLSSwitches(logics, OutputDevice::getDevice(FileHelpers::checkForRelativity(dest, basePath)));
134 : }
135 0 : } else {
136 : // get the logic
137 265 : if (!myNet.getTLSControl().knows(source)) {
138 0 : throw InvalidArgument("The traffic light logic to save (" + source + ") is not known.");
139 : }
140 265 : const MSTLLogicControl::TLSLogicVariants& logics = myNet.getTLSControl().get(source);
141 : // build the action
142 530 : new Command_SaveTLSSwitches(logics, OutputDevice::getDevice(FileHelpers::checkForRelativity(dest, basePath)));
143 : }
144 265 : }
145 :
146 :
147 : void
148 533 : NLDiscreteEventBuilder::buildSaveTLSwitchStatesCommand(const SUMOSAXAttributes& attrs,
149 : const std::string& basePath) {
150 533 : bool ok = true;
151 533 : const std::string dest = attrs.getOpt<std::string>(SUMO_ATTR_DEST, nullptr, ok, "");
152 1066 : const std::string source = attrs.getOpt<std::string>(SUMO_ATTR_SOURCE, nullptr, ok, "");
153 : // check the parameter
154 533 : if (dest == "" || !ok) {
155 0 : throw InvalidArgument("Incomplete description of an 'SaveTLSSwitchStates'-action occurred.");
156 : }
157 533 : if (source == "") {
158 0 : const std::vector<std::string> ids = myNet.getTLSControl().getAllTLIds();
159 0 : for (std::vector<std::string>::const_iterator tls = ids.begin(); tls != ids.end(); ++tls) {
160 0 : const MSTLLogicControl::TLSLogicVariants& logics = myNet.getTLSControl().get(*tls);
161 0 : new Command_SaveTLSSwitchStates(logics, OutputDevice::getDevice(FileHelpers::checkForRelativity(dest, basePath)));
162 : }
163 0 : } else {
164 : // get the logic
165 533 : if (!myNet.getTLSControl().knows(source)) {
166 0 : throw InvalidArgument("The traffic light logic to save (" + source + ") is not known.");
167 : }
168 533 : const MSTLLogicControl::TLSLogicVariants& logics = myNet.getTLSControl().get(source);
169 : // build the action
170 1066 : new Command_SaveTLSSwitchStates(logics, OutputDevice::getDevice(FileHelpers::checkForRelativity(dest, basePath)));
171 : }
172 533 : }
173 :
174 :
175 : void
176 23 : NLDiscreteEventBuilder::buildSaveTLSProgramCommand(const SUMOSAXAttributes& attrs,
177 : const std::string& basePath) {
178 23 : bool ok = true;
179 23 : const std::string dest = attrs.getOpt<std::string>(SUMO_ATTR_DEST, nullptr, ok, "");
180 46 : const std::string source = attrs.getOpt<std::string>(SUMO_ATTR_SOURCE, nullptr, ok, "");
181 : // check the parameter
182 23 : if (dest == "" || !ok) {
183 0 : throw InvalidArgument("Incomplete description of an 'SaveTLSProgram'-action occurred.");
184 : }
185 23 : if (source == "") {
186 0 : const std::vector<std::string> ids = myNet.getTLSControl().getAllTLIds();
187 0 : for (std::vector<std::string>::const_iterator tls = ids.begin(); tls != ids.end(); ++tls) {
188 0 : const MSTLLogicControl::TLSLogicVariants& logics = myNet.getTLSControl().get(*tls);
189 0 : new Command_SaveTLSProgram(logics, OutputDevice::getDevice(FileHelpers::checkForRelativity(dest, basePath)));
190 : }
191 0 : } else {
192 : // get the logic
193 23 : if (!myNet.getTLSControl().knows(source)) {
194 0 : throw InvalidArgument("The traffic light logic to save (" + source + ") is not known.");
195 : }
196 23 : const MSTLLogicControl::TLSLogicVariants& logics = myNet.getTLSControl().get(source);
197 : // build the action
198 46 : new Command_SaveTLSProgram(logics, OutputDevice::getDevice(FileHelpers::checkForRelativity(dest, basePath)));
199 : }
200 23 : }
201 :
202 :
203 : /****************************************************************************/
|