SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
NLTriggerBuilder.cpp
Go to the documentation of this file.
1 /****************************************************************************/
12 // Builds trigger objects for microsim
13 /****************************************************************************/
14 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
15 // Copyright (C) 2001-2015 DLR (http://www.dlr.de/) and contributors
16 /****************************************************************************/
17 //
18 // This file is part of SUMO.
19 // SUMO is free software: you can redistribute it and/or modify
20 // it under the terms of the GNU General Public License as published by
21 // the Free Software Foundation, either version 3 of the License, or
22 // (at your option) any later version.
23 //
24 /****************************************************************************/
25 
26 
27 // ===========================================================================
28 // included modules
29 // ===========================================================================
30 #ifdef _MSC_VER
31 #include <windows_config.h>
32 #else
33 #include <config.h>
34 #endif
35 
36 #include <string>
38 #include <microsim/MSLane.h>
39 #include <microsim/MSEdge.h>
40 #include <microsim/MSGlobals.h>
52 #include "NLHandler.h"
53 #include "NLTriggerBuilder.h"
55 #include <utils/xml/XMLSubSys.h>
56 
57 
58 #ifdef HAVE_INTERNAL
59 #include <mesosim/MELoop.h>
60 #include <mesosim/METriggeredCalibrator.h>
61 #endif
62 
63 #ifdef CHECK_MEMORY_LEAKS
64 #include <foreign/nvwa/debug_new.h>
65 #endif // CHECK_MEMORY_LEAKS
66 
67 
68 // ===========================================================================
69 // method definitions
70 // ===========================================================================
72  : myHandler(0) {}
73 
74 
76 
77 void
79  myHandler = handler;
80 }
81 
82 
83 void
85  bool ok = true;
86  // get the id, throw if not given or empty...
87  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
88  if (!ok) {
89  return;
90  }
91  MSEdge* e = MSEdge::dictionary(id);
92  if (e == 0) {
93  WRITE_ERROR("Unknown edge ('" + id + "') referenced in a vaporizer.");
94  return;
95  }
96  SUMOTime begin = attrs.getSUMOTimeReporting(SUMO_ATTR_BEGIN, 0, ok);
97  SUMOTime end = attrs.getSUMOTimeReporting(SUMO_ATTR_END, 0, ok);
98  if (!ok) {
99  return;
100  }
101  if (begin < 0) {
102  WRITE_ERROR("A vaporization begin time is negative (edge id='" + id + "').");
103  return;
104  }
105  if (begin >= end) {
106  WRITE_ERROR("A vaporization ends before it starts (edge id='" + id + "').");
107  return;
108  }
109  if (end >= string2time(OptionsCont::getOptions().getString("begin"))) {
114  }
115 }
116 
117 
118 
119 void
121  const std::string& base) {
122  // get the id, throw if not given or empty...
123  bool ok = true;
124  // get the id, throw if not given or empty...
125  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
126  if (!ok) {
127  return;
128  }
129  // get the file name to read further definitions from
130  std::string file = getFileName(attrs, base, true);
131  std::string objectid = attrs.get<std::string>(SUMO_ATTR_LANES, id.c_str(), ok);
132  if (!ok) {
133  throw InvalidArgument("The lanes to use within MSLaneSpeedTrigger '" + id + "' are not known.");
134  }
135  std::vector<MSLane*> lanes;
136  std::vector<std::string> laneIDs;
137  SUMOSAXAttributes::parseStringVector(objectid, laneIDs);
138  for (std::vector<std::string>::iterator i = laneIDs.begin(); i != laneIDs.end(); ++i) {
139  MSLane* lane = MSLane::dictionary(*i);
140  if (lane == 0) {
141  throw InvalidArgument("The lane to use within MSLaneSpeedTrigger '" + id + "' is not known.");
142  }
143  lanes.push_back(lane);
144  }
145  if (lanes.size() == 0) {
146  throw InvalidArgument("No lane defined for MSLaneSpeedTrigger '" + id + "'.");
147  }
148  try {
149  MSLaneSpeedTrigger* trigger = buildLaneSpeedTrigger(net, id, lanes, file);
150  if (file == "") {
152  }
153  } catch (ProcessError& e) {
154  throw InvalidArgument(e.what());
155  }
156 }
157 
158 
159 void
161  bool ok = true;
162  // get the id, throw if not given or empty...
163  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
164  if (!ok) {
165  throw ProcessError();
166  }
167  // get the lane
168  MSLane* lane = getLane(attrs, "busStop", id);
169  // get the positions
170  SUMOReal frompos = attrs.getOpt<SUMOReal>(SUMO_ATTR_STARTPOS, id.c_str(), ok, 0);
171  SUMOReal topos = attrs.getOpt<SUMOReal>(SUMO_ATTR_ENDPOS, id.c_str(), ok, lane->getLength());
172  const bool friendlyPos = attrs.getOpt<bool>(SUMO_ATTR_FRIENDLY_POS, id.c_str(), ok, false);
173  if (!ok || !myHandler->checkStopPos(frompos, topos, lane->getLength(), POSITION_EPS, friendlyPos)) {
174  throw InvalidArgument("Invalid position for bus stop '" + id + "'.");
175  }
176  // get the lines
177  std::vector<std::string> lines;
178  SUMOSAXAttributes::parseStringVector(attrs.getOpt<std::string>(SUMO_ATTR_LINES, id.c_str(), ok, "", false), lines);
179  // build the bus stop
180  buildBusStop(net, id, lines, lane, frompos, topos);
181 }
182 
183 void
185  bool ok = true;
186  // get the id, throw if not given or empty...
187  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
188  if (!ok) {
189  throw ProcessError();
190  }
191  // get the lane
192  MSLane* lane = getLane(attrs, "containerStop", id);
193  // get the positions
194  SUMOReal frompos = attrs.getOpt<SUMOReal>(SUMO_ATTR_STARTPOS, id.c_str(), ok, 0);
195  SUMOReal topos = attrs.getOpt<SUMOReal>(SUMO_ATTR_ENDPOS, id.c_str(), ok, lane->getLength());
196  const bool friendlyPos = attrs.getOpt<bool>(SUMO_ATTR_FRIENDLY_POS, id.c_str(), ok, false);
197  if (!ok || !myHandler->checkStopPos(frompos, topos, lane->getLength(), POSITION_EPS, friendlyPos)) {
198  throw InvalidArgument("Invalid position for container stop '" + id + "'.");
199  }
200  // get the lines
201  std::vector<std::string> lines;
202  SUMOSAXAttributes::parseStringVector(attrs.getOpt<std::string>(SUMO_ATTR_LINES, id.c_str(), ok, "", false), lines);
203  // build the container stop
204  buildContainerStop(net, id, lines, lane, frompos, topos);
205 }
206 
207 void
209  const std::string& base) {
210  bool ok = true;
211  // get the id, throw if not given or empty...
212  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
213  if (!ok) {
214  throw ProcessError();
215  }
216  // get the file name to read further definitions from
217  MSLane* lane = getLane(attrs, "calibrator", id);
218  const SUMOReal pos = getPosition(attrs, lane, "calibrator", id);
219  const SUMOTime freq = attrs.getOptSUMOTimeReporting(SUMO_ATTR_FREQUENCY, id.c_str(), ok, DELTA_T); // !!! no error handling
220  std::string file = getFileName(attrs, base, true);
221  std::string outfile = attrs.getOpt<std::string>(SUMO_ATTR_OUTPUT, id.c_str(), ok, "");
222  std::string routeProbe = attrs.getOpt<std::string>(SUMO_ATTR_ROUTEPROBE, id.c_str(), ok, "");
223  MSRouteProbe* probe = 0;
224  if (routeProbe != "") {
225  probe = dynamic_cast<MSRouteProbe*>(net.getDetectorControl().getTypedDetectors(SUMO_TAG_ROUTEPROBE).get(routeProbe));
226  }
228 #ifdef HAVE_INTERNAL
229  METriggeredCalibrator* trigger = buildMECalibrator(net, id, &lane->getEdge(), pos, file, outfile, freq, probe);
230  if (file == "") {
231  trigger->registerParent(SUMO_TAG_CALIBRATOR, myHandler);
232  }
233 #endif
234  } else {
235  MSCalibrator* trigger = buildCalibrator(net, id, &lane->getEdge(), pos, file, outfile, freq, probe);
236  if (file == "") {
238  }
239  }
240 }
241 
242 
243 void
245  const std::string& base) {
246  bool ok = true;
247  // get the id, throw if not given or empty...
248  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
249  if (!ok) {
250  throw ProcessError();
251  }
252  // get the file name to read further definitions from
253  std::string file = getFileName(attrs, base, true);
254  std::string objectid = attrs.get<std::string>(SUMO_ATTR_EDGES, id.c_str(), ok);
255  if (!ok) {
256  throw InvalidArgument("The edge to use within MSTriggeredRerouter '" + id + "' is not known.");
257  }
258  MSEdgeVector edges;
259  std::vector<std::string> edgeIDs;
260  SUMOSAXAttributes::parseStringVector(objectid, edgeIDs);
261  for (std::vector<std::string>::iterator i = edgeIDs.begin(); i != edgeIDs.end(); ++i) {
262  MSEdge* edge = MSEdge::dictionary(*i);
263  if (edge == 0) {
264  throw InvalidArgument("The edge to use within MSTriggeredRerouter '" + id + "' is not known.");
265  }
266  edges.push_back(edge);
267  }
268  if (edges.size() == 0) {
269  throw InvalidArgument("No edges found for MSTriggeredRerouter '" + id + "'.");
270  }
271  SUMOReal prob = attrs.getOpt<SUMOReal>(SUMO_ATTR_PROB, id.c_str(), ok, 1);
272  bool off = attrs.getOpt<bool>(SUMO_ATTR_OFF, id.c_str(), ok, false);
273  if (!ok) {
274  throw InvalidArgument("Could not parse MSTriggeredRerouter '" + id + "'.");
275  }
276  MSTriggeredRerouter* trigger = buildRerouter(net, id, edges, prob, file, off);
277  // read in the trigger description
278  if (file == "") {
280  } else if (!XMLSubSys::runParser(*trigger, file)) {
281  throw ProcessError();
282  }
283 }
284 
285 
286 // -------------------------
287 
288 
290 NLTriggerBuilder::buildLaneSpeedTrigger(MSNet& /*net*/, const std::string& id,
291  const std::vector<MSLane*>& destLanes,
292  const std::string& file) {
293  return new MSLaneSpeedTrigger(id, destLanes, file);
294 }
295 
296 
297 #ifdef HAVE_INTERNAL
298 METriggeredCalibrator*
299 NLTriggerBuilder::buildMECalibrator(MSNet& /*net*/, const std::string& id,
300  const MSEdge* edge, SUMOReal pos,
301  const std::string& file,
302  const std::string& outfile,
303  const SUMOTime freq, MSRouteProbe* probe) {
304  return new METriggeredCalibrator(id, edge, pos, file, outfile, freq, MSGlobals::gMesoNet->getSegmentForEdge(*edge, pos)->getLength(), probe);
305 }
306 #endif
307 
308 
310 NLTriggerBuilder::buildCalibrator(MSNet& /*net*/, const std::string& id,
311  MSEdge* edge, SUMOReal pos,
312  const std::string& file,
313  const std::string& outfile,
314  const SUMOTime freq, const MSRouteProbe* probe) {
315  return new MSCalibrator(id, edge, pos, file, outfile, freq, edge->getLength(), probe);
316 }
317 
318 
320 NLTriggerBuilder::buildRerouter(MSNet&, const std::string& id,
321  MSEdgeVector& edges,
322  SUMOReal prob, const std::string& file, bool off) {
323  return new MSTriggeredRerouter(id, edges, prob, file, off);
324 }
325 
326 
327 void
328 NLTriggerBuilder::buildBusStop(MSNet& net, const std::string& id,
329  const std::vector<std::string>& lines,
330  MSLane* lane, SUMOReal frompos, SUMOReal topos) {
331  MSBusStop* stop = new MSBusStop(id, lines, *lane, frompos, topos);
332  if (!net.addBusStop(stop)) {
333  delete stop;
334  throw InvalidArgument("Could not build bus stop '" + id + "'; probably declared twice.");
335  }
336 }
337 
338 
339 void
340 NLTriggerBuilder::buildContainerStop(MSNet& net, const std::string& id,
341  const std::vector<std::string>& lines,
342  MSLane* lane, SUMOReal frompos, SUMOReal topos) {
343  MSContainerStop* stop = new MSContainerStop(id, lines, *lane, frompos, topos);
344  if (!net.addContainerStop(stop)) {
345  delete stop;
346  throw InvalidArgument("Could not build container stop '" + id + "'; probably declared twice.");
347  }
348 }
349 
350 
351 
352 
353 std::string
355  const std::string& base,
356  const bool allowEmpty) {
357  // get the file name to read further definitions from
358  bool ok = true;
359  std::string file = attrs.getOpt<std::string>(SUMO_ATTR_FILE, 0, ok, "");
360  if (file == "") {
361  if (allowEmpty) {
362  return file;
363  }
364  throw InvalidArgument("No filename given.");
365  }
366  // check whether absolute or relative filenames are given
367  if (!FileHelpers::isAbsolute(file)) {
368  return FileHelpers::getConfigurationRelative(base, file);
369  }
370  return file;
371 }
372 
373 
374 MSLane*
376  const std::string& tt,
377  const std::string& tid) {
378  bool ok = true;
379  std::string objectid = attrs.get<std::string>(SUMO_ATTR_LANE, tid.c_str(), ok);
380  MSLane* lane = MSLane::dictionary(objectid);
381  if (lane == 0) {
382  throw InvalidArgument("The lane " + objectid + " to use within the " + tt + " '" + tid + "' is not known.");
383  }
384  return lane;
385 }
386 
387 
388 SUMOReal
390  MSLane* lane,
391  const std::string& tt, const std::string& tid) {
392  bool ok = true;
393  SUMOReal pos = attrs.get<SUMOReal>(SUMO_ATTR_POSITION, 0, ok);
394  const bool friendlyPos = attrs.getOpt<bool>(SUMO_ATTR_FRIENDLY_POS, 0, ok, false);
395  if (!ok) {
396  throw InvalidArgument("Error on parsing a position information.");
397  }
398  if (pos < 0) {
399  pos = lane->getLength() + pos;
400  }
401  if (pos > lane->getLength()) {
402  if (friendlyPos) {
403  pos = lane->getLength() - (SUMOReal) 0.1;
404  } else {
405  throw InvalidArgument("The position of " + tt + " '" + tid + "' lies beyond the lane's '" + lane->getID() + "' length.");
406  }
407  }
408  return pos;
409 }
410 
411 
412 
413 /****************************************************************************/
void setHandler(NLHandler *handler)
Sets the parent handler to use for nested parsing.
void parseAndBuildCalibrator(MSNet &net, const SUMOSAXAttributes &attrs, const std::string &base)
Parses his values and builds a mesoscopic or microscopic calibrator.
virtual void buildBusStop(MSNet &net, const std::string &id, const std::vector< std::string > &lines, MSLane *lane, SUMOReal frompos, SUMOReal topos)
Builds a bus stop.
MSEdge & getEdge() const
Returns the lane's edge.
Definition: MSLane.h:455
virtual MSCalibrator * buildCalibrator(MSNet &net, const std::string &id, MSEdge *edge, SUMOReal pos, const std::string &file, const std::string &outfile, const SUMOTime freq, const MSRouteProbe *probe)
builds a microscopic calibrator
static std::string getConfigurationRelative(const std::string &configPath, const std::string &path)
Returns the second path as a relative path to the first file.
Definition: FileHelpers.cpp:86
A lane area vehicles can halt at and load and unload containers.
Writes routes of vehicles passing a certain edge.
Definition: MSRouteProbe.h:68
SUMOReal getLength() const
Returns the lane's length.
Definition: MSLane.h:370
void parseAndBuildRerouter(MSNet &net, const SUMOSAXAttributes &attrs, const std::string &base)
Parses his values and builds a rerouter.
static bool checkStopPos(SUMOReal &startPos, SUMOReal &endPos, const SUMOReal laneLength, const SUMOReal minLength, const bool friendlyPos)
check start and end position of a stop
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
static bool dictionary(const std::string &id, MSEdge *edge)
Inserts edge into the static dictionary Returns true if the key id isn't already in the dictionary...
Definition: MSEdge.cpp:531
SUMOTime incVaporization(SUMOTime t)
Enables vaporization.
Definition: MSEdge.cpp:278
void parseAndBuildBusStop(MSNet &net, const SUMOSAXAttributes &attrs)
Parses his values and builds a bus stop.
NLTriggerBuilder()
Constructor.
Base (microsim) event class.
Definition: Command.h:61
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything's ok.
Definition: XMLSubSys.cpp:114
virtual ~NLTriggerBuilder()
Destructor.
SUMOTime decVaporization(SUMOTime t)
Disables vaporization.
Definition: MSEdge.cpp:285
The simulated network and simulation perfomer.
Definition: MSNet.h:94
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
bool addBusStop(MSBusStop *busStop)
Adds a bus stop.
Definition: MSNet.cpp:742
SUMOTime getOptSUMOTimeReporting(int attr, const char *objectid, bool &ok, SUMOTime defaultValue, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
Changes the speed allowed on a set of lanes.
T get(const std::string &id) const
Retrieves an item.
const std::string & getID() const
Returns the id.
Definition: Named.h:60
A road/street connecting two junctions.
Definition: MSEdge.h:81
const NamedObjectCont< MSDetectorFileOutput * > & getTypedDetectors(SumoXMLTag type) const
Returns the list of detectors of the given type.
static void parseStringVector(const std::string &def, std::vector< std::string > &into)
Splits the given string.
SUMOReal getLength() const
return the length of the edge
Definition: MSEdge.h:535
SUMOReal getPosition(const SUMOSAXAttributes &attrs, MSLane *lane, const std::string &tt, const std::string &tid)
returns the position on the lane checking it
the edges of a route
Encapsulated SAX-Attributes.
NLHandler * myHandler
The parent handler to set for subhandlers.
static bool isAbsolute(const std::string &path)
Returns the information whether the given path is absolute.
A lane area vehicles can halt at.
Definition: MSBusStop.h:64
void parseAndBuildContainerStop(MSNet &net, const SUMOSAXAttributes &attrs)
Parses his values and builds a container stop.
bool addContainerStop(MSContainerStop *containerStop)
Adds a container stop.
Definition: MSNet.cpp:767
MSEventControl * getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
Definition: MSNet.h:369
A wrapper for a Command function.
void parseAndBuildLaneSpeedTrigger(MSNet &net, const SUMOSAXAttributes &attrs, const std::string &base)
Parses his values and builds a lane speed trigger.
virtual void buildContainerStop(MSNet &net, const std::string &id, const std::vector< std::string > &lines, MSLane *lane, SUMOReal frompos, SUMOReal topos)
Builds a container stop.
void registerParent(const int tag, GenericSAXHandler *handler)
Assigning a parent handler which is enabled when the specified tag is closed.
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:48
#define POSITION_EPS
Definition: config.h:189
SUMOTime getSUMOTimeReporting(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is a SUMOTime.
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition: MSNet.h:339
std::string getFileName(const SUMOSAXAttributes &attrs, const std::string &base, const bool allowEmpty=false)
Helper method to obtain the filename.
virtual SUMOTime addEvent(Command *operation, SUMOTime execTimeStep, AdaptType type)
Adds an Event.
The XML-Handler for network loading.
Definition: NLHandler.h:93
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
Reroutes vehicles passing an edge.
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:852
int SUMOTime
Definition: SUMOTime.h:43
Calibrates the flow on a segment to a specified one.
Definition: MSCalibrator.h:57
A variable speed sign.
Patch the time in a way that it is at least as high as the simulation begin time. ...
#define SUMOReal
Definition: config.h:218
static const bool gUseMesoSim
Definition: MSGlobals.h:102
#define DELTA_T
Definition: SUMOTime.h:50
virtual MSTriggeredRerouter * buildRerouter(MSNet &net, const std::string &id, MSEdgeVector &edges, SUMOReal prob, const std::string &file, bool off)
builds an rerouter
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.
virtual MSLaneSpeedTrigger * buildLaneSpeedTrigger(MSNet &net, const std::string &id, const std::vector< MSLane * > &destLanes, const std::string &file)
Builds a lane speed trigger.
std::vector< MSEdge * > MSEdgeVector
Definition: MSEdge.h:78
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
void buildVaporizer(const SUMOSAXAttributes &attrs)
Builds a vaporization.
MSLane * getLane(const SUMOSAXAttributes &attrs, const std::string &tt, const std::string &tid)
Returns the lane defined by attribute "lane".