Eclipse SUMO - Simulation of Urban MObility
RODFDetectorHandler.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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
18 // A handler for loading detector descriptions
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <string>
33 #include <utils/common/ToString.h>
36 #include "RODFDetectorHandler.h"
37 #include "RODFNet.h"
38 
39 
40 // ===========================================================================
41 // method definitions
42 // ===========================================================================
44  const std::string& file)
45  : SUMOSAXHandler(file),
46  myNet(optNet), myIgnoreErrors(ignoreErrors), myContainer(con) {}
47 
48 
50 
51 
52 void
54  const SUMOSAXAttributes& attrs) {
55  if (element == SUMO_TAG_DETECTOR_DEFINITION || element == SUMO_TAG_E1DETECTOR || element == SUMO_TAG_INDUCTION_LOOP) {
56  try {
57  bool ok = true;
58  // get the id, report an error if not given or empty...
59  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
60  if (!ok) {
61  throw ProcessError();
62  }
63  std::string lane = attrs.get<std::string>(SUMO_ATTR_LANE, id.c_str(), ok);
64  if (!ok) {
65  throw ProcessError();
66  }
67  ROEdge* edge = myNet->getEdge(lane.substr(0, lane.rfind('_')));
68  int laneIndex = StringUtils::toIntSecure(lane.substr(lane.rfind('_') + 1), std::numeric_limits<int>::max());
69  if (edge == nullptr || laneIndex >= edge->getNumLanes()) {
70  throw ProcessError("Unknown lane '" + lane + "' for detector '" + id + "' in '" + getFileName() + "'.");
71  }
72  double pos = attrs.get<double>(SUMO_ATTR_POSITION, id.c_str(), ok);
73  std::string mml_type = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok, "");
74  if (!ok) {
75  throw ProcessError();
76  }
78  if (mml_type == "between") {
79  type = BETWEEN_DETECTOR;
80  } else if (mml_type == "source" || mml_type == "highway_source") { // !!! highway-source is legacy (removed accoring output on 06.08.2007)
81  type = SOURCE_DETECTOR;
82  } else if (mml_type == "sink") {
83  type = SINK_DETECTOR;
84  }
85  RODFDetector* detector = new RODFDetector(id, lane, pos, type);
86  if (!myContainer.addDetector(detector)) {
87  delete detector;
88  throw ProcessError("Could not add detector '" + id + "' (probably the id is already used).");
89  }
90  } catch (ProcessError& e) {
91  if (myIgnoreErrors) {
92  WRITE_WARNING(e.what());
93  } else {
94  throw e;
95  }
96  }
97  }
98 }
99 
100 
101 
102 /****************************************************************************/
103 
RODFDetectorHandler.h
SINK_DETECTOR
Definition: RODFDetector.h:71
SUMO_ATTR_TYPE
Definition: SUMOXMLDefinitions.h:382
ToString.h
ROEdge::getNumLanes
int getNumLanes() const
Returns the number of lanes this edge has.
Definition: ROEdge.h:244
RODFDetectorHandler::RODFDetectorHandler
RODFDetectorHandler(RODFNet *optNet, bool ignoreErrors, RODFDetectorCon &con, const std::string &file)
Constructor.
Definition: RODFDetectorHandler.cpp:43
RODFDetector
Class representing a detector within the DFROUTER.
Definition: RODFDetector.h:82
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:239
RODFDetectorHandler::myContainer
RODFDetectorCon & myContainer
the container to put the detectors into
Definition: RODFDetectorHandler.h:74
SUMOSAXHandler
SAX-handler base for SUMO-files.
Definition: SUMOSAXHandler.h:42
OptionsCont.h
RONet::getEdge
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:153
SUMOSAXAttributes::get
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
Definition: SUMOSAXAttributes.h:493
MsgHandler.h
SUMOSAXHandler.h
RODFDetectorHandler::myIgnoreErrors
bool myIgnoreErrors
whether to ignore errors on parsing
Definition: RODFDetectorHandler.h:71
SUMO_ATTR_ID
Definition: SUMOXMLDefinitions.h:379
RODFDetectorCon
A container for RODFDetectors.
Definition: RODFDetector.h:221
SUMO_ATTR_LANE
Definition: SUMOXMLDefinitions.h:635
SUMO_TAG_DETECTOR_DEFINITION
definition of a detector
Definition: SUMOXMLDefinitions.h:213
RODFDetectorHandler::~RODFDetectorHandler
virtual ~RODFDetectorHandler()
Destructor.
Definition: RODFDetectorHandler.cpp:49
ProcessError
Definition: UtilExceptions.h:40
UtilExceptions.h
SUMOSAXAttributes::getOpt
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.
Definition: SUMOSAXAttributes.h:519
SUMO_ATTR_POSITION
Definition: SUMOXMLDefinitions.h:658
StringUtils::toIntSecure
static int toIntSecure(const std::string &sData, int def)
converts a string into the integer value described by it
Definition: StringUtils.cpp:256
StringUtils.h
RODFDetectorType
RODFDetectorType
Numerical representation of different detector types.
Definition: RODFDetector.h:59
GenericSAXHandler::getFileName
const std::string & getFileName() const
returns the current file name
Definition: GenericSAXHandler.cpp:75
RODFNet
A DFROUTER-network.
Definition: RODFNet.h:45
ROEdge
A basic edge for routing applications.
Definition: ROEdge.h:73
TYPE_NOT_DEFINED
A not yet defined detector.
Definition: RODFDetector.h:61
SOURCE_DETECTOR
A source detector.
Definition: RODFDetector.h:70
config.h
StringTokenizer.h
BETWEEN_DETECTOR
An in-between detector.
Definition: RODFDetector.h:67
RODFNet.h
SUMO_TAG_INDUCTION_LOOP
alternative tag for e1 detector
Definition: SUMOXMLDefinitions.h:66
RODFDetectorHandler::myNet
RODFNet * myNet
the net
Definition: RODFDetectorHandler.h:68
SUMO_TAG_E1DETECTOR
an e1 detector
Definition: SUMOXMLDefinitions.h:64
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:57
RODFDetectorCon::addDetector
bool addDetector(RODFDetector *dfd)
Definition: RODFDetector.cpp:486
SUMOXMLDefinitions.h
RODFDetectorHandler::myStartElement
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
Definition: RODFDetectorHandler.cpp:53