SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ODDistrictHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // An XML-Handler for districts
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <string>
34 #include <utility>
35 #include <iostream>
38 #include <utils/common/ToString.h>
41 #include "ODDistrict.h"
42 #include "ODDistrictCont.h"
43 #include "ODDistrictHandler.h"
44 
45 #ifdef CHECK_MEMORY_LEAKS
46 #include <foreign/nvwa/debug_new.h>
47 #endif // CHECK_MEMORY_LEAKS
48 
49 
50 // ===========================================================================
51 // method definitions
52 // ===========================================================================
54  const std::string& file)
55  : SUMOSAXHandler(file), myContainer(cont), myCurrentDistrict(0) {}
56 
57 
59 
60 
61 void
63  const SUMOSAXAttributes& attrs) {
64  switch (element) {
65  case SUMO_TAG_TAZ:
66  openDistrict(attrs);
67  break;
68  case SUMO_TAG_TAZSOURCE:
69  addSource(attrs);
70  break;
71  case SUMO_TAG_TAZSINK:
72  addSink(attrs);
73  break;
74  default:
75  break;
76  }
77 }
78 
79 
80 void
82  if (element == SUMO_TAG_TAZ) {
83  closeDistrict();
84  }
85 }
86 
87 
88 void
91  // get the id, report an error if not given or empty...
92  bool ok = true;
93  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
94  if (!ok) {
95  return;
96  }
97  myCurrentDistrict = new ODDistrict(id);
98  if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
99  std::vector<std::string> desc = attrs.getStringVector(SUMO_ATTR_EDGES);
100  for (std::vector<std::string>::const_iterator i = desc.begin(); i != desc.end(); ++i) {
101  myCurrentDistrict->addSource(*i, 1.);
102  myCurrentDistrict->addSink(*i, 1.);
103  }
104  }
105 }
106 
107 
108 void
110  std::pair<std::string, SUMOReal> vals = parseTAZ(attrs);
111  if (vals.second >= 0) {
112  myCurrentDistrict->addSource(vals.first, vals.second);
113  }
114 }
115 
116 
117 void
119  std::pair<std::string, SUMOReal> vals = parseTAZ(attrs);
120  if (vals.second >= 0) {
121  myCurrentDistrict->addSink(vals.first, vals.second);
122  }
123 }
124 
125 
126 
127 std::pair<std::string, SUMOReal>
129  // check the current district first
130  if (myCurrentDistrict == 0) {
131  return std::pair<std::string, SUMOReal>("", -1);
132  }
133  // get the id, report an error if not given or empty...
134  bool ok = true;
135  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
136  if (!ok) {
137  return std::pair<std::string, SUMOReal>("", -1);
138  }
139  // get the weight
140  SUMOReal weight = attrs.get<SUMOReal>(SUMO_ATTR_WEIGHT, id.c_str(), ok);
141  if (ok) {
142  if (weight < 0) {
143  WRITE_ERROR("'probability' must be positive (in definition of " + attrs.getObjectType() + " '" + id + "').");
144  } else {
145  return std::pair<std::string, SUMOReal>(id, weight);
146  }
147  }
148  return std::pair<std::string, SUMOReal>("", -1);
149 }
150 
151 
152 void
154  if (myCurrentDistrict != 0) {
156  }
157 }
158 
159 
160 
161 /****************************************************************************/
162 
~ODDistrictHandler()
Destructor.
virtual bool add(const std::string &id, T item)
Adds an item.
void addSource(const SUMOSAXAttributes &attrs)
Adds a read source to the current district.
void addSink(const std::string &id, SUMOReal weight)
Adds a sink connection.
Definition: ODDistrict.cpp:64
the weight of a district's source or sink
SAX-handler base for SUMO-files.
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called when an opening-tag occurs.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
std::pair< std::string, SUMOReal > parseTAZ(const SUMOSAXAttributes &attrs)
Returns the id and weight for a taz/tazSink/tazSource.
const std::string & getObjectType() const
return the objecttype to which these attributes belong
const std::string & getID() const
Returns the id.
Definition: Named.h:66
the edges of a route
void addSource(const std::string &id, SUMOReal weight)
Adds a source connection.
Definition: ODDistrict.cpp:58
Encapsulated SAX-Attributes.
void myEndElement(int element)
Called when a closing tag occurs.
A container for districts.
void addSink(const SUMOSAXAttributes &attrs)
Adds a read sink to the current district.
ODDistrictHandler(ODDistrictCont &cont, const std::string &file)
Constructor.
virtual std::vector< std::string > getStringVector(int attr) const =0
Tries to read given attribute assuming it is a string vector.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
void closeDistrict()
Closes the processing of the current district.
void openDistrict(const SUMOSAXAttributes &attrs)
Begins the parsing of a district.
ODDistrict * myCurrentDistrict
The currently parsed district.
#define SUMOReal
Definition: config.h:214
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
ODDistrictCont & myContainer
The container to add read districts to.
A district (origin/destination)
Definition: ODDistrict.h:52