Eclipse SUMO - Simulation of Urban MObility
NLDiscreteEventBuilder.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2020 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 /****************************************************************************/
21 // }
22 /****************************************************************************/
23 #include <config.h>
24 
26 #include <microsim/MSNet.h>
37 #include "NLDiscreteEventBuilder.h"
38 
39 
40 // ===========================================================================
41 // method definitions
42 // ===========================================================================
44  : myNet(net) {
45  myActions["SaveTLSStates"] = EV_SAVETLSTATE;
46  myActions["SaveTLSSwitchTimes"] = EV_SAVETLSWITCHES;
47  myActions["SaveTLSSwitchStates"] = EV_SAVETLSWITCHSTATES;
48  myActions["SaveTLSProgram"] = EV_SAVETLSPROGRAM;
49 }
50 
51 
53 
54 
55 void
57  const std::string& basePath) {
58  bool ok = true;
59  const std::string type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, nullptr, ok, "");
60  // check whether the type was given
61  if (type == "" || !ok) {
62  throw InvalidArgument("An action's type is not given.");
63  }
64  // get the numerical representation
65  KnownActions::iterator i = myActions.find(type);
66  if (i == myActions.end()) {
67  throw InvalidArgument("The action type '" + type + "' is not known.");
68  }
69  // build the action
70  switch ((*i).second) {
71  case EV_SAVETLSTATE:
72  buildSaveTLStateCommand(attrs, basePath);
73  break;
74  case EV_SAVETLSWITCHES:
75  buildSaveTLSwitchesCommand(attrs, basePath);
76  break;
78  buildSaveTLSwitchStatesCommand(attrs, basePath);
79  break;
80  case EV_SAVETLSPROGRAM:
81  buildSaveTLSProgramCommand(attrs, basePath);
82  break;
83  }
84 }
85 
86 
87 void
89  const std::string& basePath) {
90  bool ok = true;
91  const std::string dest = attrs.getOpt<std::string>(SUMO_ATTR_DEST, nullptr, ok, "");
92  const std::string source = attrs.getOpt<std::string>(SUMO_ATTR_SOURCE, nullptr, ok, "");
93  // check the parameter
94  if (dest == "" || !ok) {
95  throw InvalidArgument("Incomplete description of an 'SaveTLSState'-action occurred.");
96  }
97  if (source == "") {
98  const std::vector<std::string> ids = myNet.getTLSControl().getAllTLIds();
99  for (std::vector<std::string>::const_iterator tls = ids.begin(); tls != ids.end(); ++tls) {
102  }
103  } else {
104  // get the logic
105  if (!myNet.getTLSControl().knows(source)) {
106  throw InvalidArgument("The traffic light logic to save (" + source + ") is not known.");
107  }
108  const MSTLLogicControl::TLSLogicVariants& logics = myNet.getTLSControl().get(source);
109  // build the action
111  }
112 }
113 
114 
115 void
117  const std::string& basePath) {
118  bool ok = true;
119  const std::string dest = attrs.getOpt<std::string>(SUMO_ATTR_DEST, nullptr, ok, "");
120  const std::string source = attrs.getOpt<std::string>(SUMO_ATTR_SOURCE, nullptr, ok, "");
121  // check the parameter
122  if (dest == "" || !ok) {
123  throw InvalidArgument("Incomplete description of an 'SaveTLSSwitchTimes'-action occurred.");
124  }
125  if (source == "") {
126  const std::vector<std::string> ids = myNet.getTLSControl().getAllTLIds();
127  for (std::vector<std::string>::const_iterator tls = ids.begin(); tls != ids.end(); ++tls) {
130  }
131  } else {
132  // get the logic
133  if (!myNet.getTLSControl().knows(source)) {
134  throw InvalidArgument("The traffic light logic to save (" + source + ") is not known.");
135  }
136  const MSTLLogicControl::TLSLogicVariants& logics = myNet.getTLSControl().get(source);
137  // build the action
139  }
140 }
141 
142 
143 void
145  const std::string& basePath) {
146  bool ok = true;
147  const std::string dest = attrs.getOpt<std::string>(SUMO_ATTR_DEST, nullptr, ok, "");
148  const std::string source = attrs.getOpt<std::string>(SUMO_ATTR_SOURCE, nullptr, ok, "");
149  // check the parameter
150  if (dest == "" || !ok) {
151  throw InvalidArgument("Incomplete description of an 'SaveTLSSwitchStates'-action occurred.");
152  }
153  if (source == "") {
154  const std::vector<std::string> ids = myNet.getTLSControl().getAllTLIds();
155  for (std::vector<std::string>::const_iterator tls = ids.begin(); tls != ids.end(); ++tls) {
158  }
159  } else {
160  // get the logic
161  if (!myNet.getTLSControl().knows(source)) {
162  throw InvalidArgument("The traffic light logic to save (" + source + ") is not known.");
163  }
164  const MSTLLogicControl::TLSLogicVariants& logics = myNet.getTLSControl().get(source);
165  // build the action
167  }
168 }
169 
170 
171 void
173  const std::string& basePath) {
174  bool ok = true;
175  const std::string dest = attrs.getOpt<std::string>(SUMO_ATTR_DEST, nullptr, ok, "");
176  const std::string source = attrs.getOpt<std::string>(SUMO_ATTR_SOURCE, nullptr, ok, "");
177  // check the parameter
178  if (dest == "" || !ok) {
179  throw InvalidArgument("Incomplete description of an 'SaveTLSProgram'-action occurred.");
180  }
181  if (source == "") {
182  const std::vector<std::string> ids = myNet.getTLSControl().getAllTLIds();
183  for (std::vector<std::string>::const_iterator tls = ids.begin(); tls != ids.end(); ++tls) {
186  }
187  } else {
188  // get the logic
189  if (!myNet.getTLSControl().knows(source)) {
190  throw InvalidArgument("The traffic light logic to save (" + source + ") is not known.");
191  }
192  const MSTLLogicControl::TLSLogicVariants& logics = myNet.getTLSControl().get(source);
193  // build the action
195  }
196 }
197 
198 
199 /****************************************************************************/
@ SUMO_ATTR_SOURCE
@ SUMO_ATTR_DEST
@ SUMO_ATTR_TYPE
Writes the switch times of a tls into a file when the tls switches.
Writes the state of the tls to a file (in each second)
Writes the switch times of a tls into a file when the tls switches.
Writes information about the green durations of a tls.
static std::string checkForRelativity(const std::string &filename, const std::string &basePath)
Returns the path from a configuration so that it is accessable from the current working directory.
The simulated network and simulation perfomer.
Definition: MSNet.h:89
MSTLLogicControl & getTLSControl()
Returns the tls logics control.
Definition: MSNet.h:444
Storage for all programs of a single tls.
std::vector< std::string > getAllTLIds() const
bool knows(const std::string &id) const
Returns the information whether the named tls is stored.
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
NLDiscreteEventBuilder(MSNet &net)
Constructor.
void buildSaveTLSwitchesCommand(const SUMOSAXAttributes &attrs, const std::string &basePath)
Builds an action which saves the switch times of links into a file.
void addAction(const SUMOSAXAttributes &attrs, const std::string &basePath)
Builds an action and saves it for further use.
void buildSaveTLSProgramCommand(const SUMOSAXAttributes &attrs, const std::string &basePath)
Builds an action which saves the tls states as a loadable program into a file.
KnownActions myActions
Build actions that shall be executed during the simulation.
void buildSaveTLStateCommand(const SUMOSAXAttributes &attrs, const std::string &basePath)
Builds an action which saves the state of a certain tls into a file.
void buildSaveTLSwitchStatesCommand(const SUMOSAXAttributes &attrs, const std::string &basePath)
Builds an action which saves the switch times and states of tls into a file.
@ EV_SAVETLSWITCHSTATES
"SaveTLSSwitchStates"
@ EV_SAVETLSWITCHES
"SaveTLSSwitchTimes"
@ EV_SAVETLSPROGRAM
"SaveTLSProgram"
static OutputDevice & getDevice(const std::string &name)
Returns the described OutputDevice.
Encapsulated SAX-Attributes.
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue, bool report=true) const
Tries to read given attribute assuming it is an int.