Eclipse SUMO - Simulation of Urban MObility
ODDistrictHandler.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 /****************************************************************************/
16 // An XML-Handler for districts
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <string>
26 #include <utility>
27 #include <iostream>
30 #include <utils/common/ToString.h>
33 #include "ODDistrict.h"
34 #include "ODDistrictCont.h"
35 #include "ODDistrictHandler.h"
36 
37 
38 // ===========================================================================
39 // method definitions
40 // ===========================================================================
42  const std::string& file)
43  : SUMOSAXHandler(file), myContainer(cont), myCurrentDistrict(nullptr) {}
44 
45 
47 
48 
49 void
51  const SUMOSAXAttributes& attrs) {
52  switch (element) {
53  case SUMO_TAG_TAZ:
54  openDistrict(attrs);
55  break;
56  case SUMO_TAG_TAZSOURCE:
57  addSource(attrs);
58  break;
59  case SUMO_TAG_TAZSINK:
60  addSink(attrs);
61  break;
62  default:
63  break;
64  }
65 }
66 
67 
68 void
70  if (element == SUMO_TAG_TAZ) {
71  closeDistrict();
72  }
73 }
74 
75 
76 void
78  myCurrentDistrict = nullptr;
79  // get the id, report an error if not given or empty...
80  bool ok = true;
81  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
82  if (!ok) {
83  return;
84  }
85  myCurrentDistrict = new ODDistrict(id);
86  if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
87  std::vector<std::string> desc = attrs.getStringVector(SUMO_ATTR_EDGES);
88  for (std::vector<std::string>::const_iterator i = desc.begin(); i != desc.end(); ++i) {
90  myCurrentDistrict->addSink(*i, 1.);
91  }
92  }
93 }
94 
95 
96 void
98  std::pair<std::string, double> vals = parseTAZ(attrs);
99  if (vals.second >= 0) {
100  myCurrentDistrict->addSource(vals.first, vals.second);
101  }
102 }
103 
104 
105 void
107  std::pair<std::string, double> vals = parseTAZ(attrs);
108  if (vals.second >= 0) {
109  myCurrentDistrict->addSink(vals.first, vals.second);
110  }
111 }
112 
113 
114 
115 std::pair<std::string, double>
117  // check the current district first
118  if (myCurrentDistrict == nullptr) {
119  return std::pair<std::string, double>("", -1);
120  }
121  // get the id, report an error if not given or empty...
122  bool ok = true;
123  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
124  if (!ok) {
125  return std::pair<std::string, double>("", -1);
126  }
127  // get the weight
128  double weight = attrs.get<double>(SUMO_ATTR_WEIGHT, id.c_str(), ok);
129  if (ok) {
130  if (weight < 0) {
131  WRITE_ERROR("'probability' must be positive (in definition of " + attrs.getObjectType() + " '" + id + "').");
132  } else {
133  return std::pair<std::string, double>(id, weight);
134  }
135  }
136  return std::pair<std::string, double>("", -1);
137 }
138 
139 
140 void
142  if (myCurrentDistrict != nullptr) {
144  }
145 }
146 
147 
148 
149 /****************************************************************************/
150 
SUMOSAXAttributes::getStringVector
const std::vector< std::string > getStringVector(int attr) const
Tries to read given attribute assuming it is a string vector.
Definition: SUMOSAXAttributes.cpp:113
ToString.h
SUMOSAXAttributes::hasAttribute
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
SUMOSAXHandler
SAX-handler base for SUMO-files.
Definition: SUMOSAXHandler.h:41
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:492
MsgHandler.h
SUMO_TAG_TAZSOURCE
@ SUMO_TAG_TAZSOURCE
a source within a district (connection road)
Definition: SUMOXMLDefinitions.h:135
SUMOSAXHandler.h
SUMOSAXAttributes::getObjectType
const std::string & getObjectType() const
return the objecttype to which these attributes belong
Definition: SUMOSAXAttributes.h:396
ODDistrictHandler::myCurrentDistrict
ODDistrict * myCurrentDistrict
The currently parsed district.
Definition: ODDistrictHandler.h:162
SUMO_ATTR_ID
@ SUMO_ATTR_ID
Definition: SUMOXMLDefinitions.h:378
ODDistrictHandler::addSource
void addSource(const SUMOSAXAttributes &attrs)
Adds a read source to the current district.
Definition: ODDistrictHandler.cpp:97
ODDistrictHandler::addSink
void addSink(const SUMOSAXAttributes &attrs)
Adds a read sink to the current district.
Definition: ODDistrictHandler.cpp:106
ODDistrict::addSink
void addSink(const std::string &id, double weight)
Adds a sink connection.
Definition: ODDistrict.cpp:52
SUMO_ATTR_WEIGHT
@ SUMO_ATTR_WEIGHT
Definition: SUMOXMLDefinitions.h:421
ODDistrictHandler::closeDistrict
void closeDistrict()
Closes the processing of the current district.
Definition: ODDistrictHandler.cpp:141
ODDistrictHandler.h
ODDistrict
A district (origin/destination)
Definition: ODDistrict.h:44
ODDistrict::addSource
void addSource(const std::string &id, double weight)
Adds a source connection.
Definition: ODDistrict.cpp:46
ODDistrictHandler::parseTAZ
std::pair< std::string, double > parseTAZ(const SUMOSAXAttributes &attrs)
Returns the id and weight for a taz/tazSink/tazSource.
Definition: ODDistrictHandler.cpp:116
SUMO_ATTR_EDGES
@ SUMO_ATTR_EDGES
the edges of a route
Definition: SUMOXMLDefinitions.h:427
ODDistrictHandler::myStartElement
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called when an opening-tag occurs.
Definition: ODDistrictHandler.cpp:50
UtilExceptions.h
ODDistrictHandler::ODDistrictHandler
ODDistrictHandler(ODDistrictCont &cont, const std::string &file)
Constructor.
Definition: ODDistrictHandler.cpp:41
ODDistrictCont
A container for districts.
Definition: ODDistrictCont.h:41
SUMO_TAG_TAZ
@ SUMO_TAG_TAZ
a traffic assignment zone
Definition: SUMOXMLDefinitions.h:133
ODDistrictHandler::openDistrict
void openDistrict(const SUMOSAXAttributes &attrs)
Begins the parsing of a district.
Definition: ODDistrictHandler.cpp:77
SUMO_TAG_TAZSINK
@ SUMO_TAG_TAZSINK
a sink within a district (connection road)
Definition: SUMOXMLDefinitions.h:137
ODDistrictHandler::myContainer
ODDistrictCont & myContainer
The container to add read districts to.
Definition: ODDistrictHandler.h:159
ODDistrictHandler::myEndElement
void myEndElement(int element)
Called when a closing tag occurs.
Definition: ODDistrictHandler.cpp:69
ODDistrictCont.h
ODDistrictHandler::~ODDistrictHandler
~ODDistrictHandler()
Destructor.
Definition: ODDistrictHandler.cpp:46
config.h
ODDistrict.h
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:56
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:76
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:283
SUMOXMLDefinitions.h
NamedObjectCont::add
bool add(const std::string &id, T item)
Adds an item.
Definition: NamedObjectCont.h:65