SUMO - Simulation of Urban MObility
ODDistrictCont.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // A container for districts
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <string>
36 #include <utils/xml/XMLSubSys.h>
40 #include "ODDistrict.h"
41 #include "ODDistrictHandler.h"
42 #include "ODDistrictCont.h"
43 
44 
45 
46 // ===========================================================================
47 // method definitions
48 // ===========================================================================
50 
51 
53 
54 
55 std::string
56 ODDistrictCont::getRandomSourceFromDistrict(const std::string& name) const {
57  ODDistrict* district = get(name);
58  if (district == 0) {
59  throw InvalidArgument("There is no district '" + name + "'.");
60  }
61  return district->getRandomSource();
62 }
63 
64 
65 std::string
66 ODDistrictCont::getRandomSinkFromDistrict(const std::string& name) const {
67  ODDistrict* district = get(name);
68  if (district == 0) {
69  throw InvalidArgument("There is no district '" + name + "'.");
70  }
71  return district->getRandomSink();
72 }
73 
74 
75 void
76 ODDistrictCont::loadDistricts(std::vector<std::string> files) {
77  for (std::vector<std::string>::iterator i = files.begin(); i != files.end(); ++i) {
78  const std::string& districtfile = *i;
79  if (!FileHelpers::isReadable(districtfile)) {
80  throw ProcessError("Could not access network file '" + districtfile + "' to load.");
81  }
82  PROGRESS_BEGIN_MESSAGE("Loading districts from '" + districtfile + "'");
83  // build the xml-parser and handler
84  ODDistrictHandler handler(*this, districtfile);
85  if (!XMLSubSys::runParser(handler, districtfile, true)) {
87  } else {
89  }
90  }
91 }
92 
93 
94 void
95 ODDistrictCont::makeDistricts(const std::map<std::string, std::pair<std::vector<std::string>, std::vector<std::string> > >& districts) {
96  for (std::map<std::string, std::pair<std::vector<std::string>, std::vector<std::string> > >::const_iterator it = districts.begin(); it != districts.end(); ++it) {
97  ODDistrict* current = new ODDistrict(it->first);
98  const std::vector<std::string>& sources = it->second.first;
99  for (std::vector<std::string>::const_iterator i = sources.begin(); i != sources.end(); ++i) {
100  current->addSource(*i, 1.);
101  }
102  const std::vector<std::string>& sinks = it->second.second;
103  for (std::vector<std::string>::const_iterator i = sinks.begin(); i != sinks.end(); ++i) {
104  current->addSink(*i, 1.);
105  }
106  add(current->getID(), current);
107  }
108 }
109 
110 
111 /****************************************************************************/
std::string getRandomSource() const
Returns the id of a source to use.
Definition: ODDistrict.cpp:65
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:53
std::string getRandomSink() const
Returns the id of a sink to use.
Definition: ODDistrict.cpp:71
void addSink(const std::string &id, double weight)
Adds a sink connection.
Definition: ODDistrict.cpp:59
void makeDistricts(const std::map< std::string, std::pair< std::vector< std::string >, std::vector< std::string > > > &districts)
create districts from description
std::string getRandomSinkFromDistrict(const std::string &name) const
Returns the id of a random sink from the named district.
An XML-Handler for districts.
const std::string & getID() const
Returns the id.
Definition: Named.h:65
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything&#39;s ok.
Definition: XMLSubSys.cpp:109
bool add(const std::string &id, ODDistrict * item)
Adds an item.
#define PROGRESS_FAILED_MESSAGE()
Definition: MsgHandler.h:204
~ODDistrictCont()
Destructor.
ODDistrictCont()
Constructor.
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:201
std::string getRandomSourceFromDistrict(const std::string &name) const
Returns the id of a random source from the named district.
void loadDistricts(std::vector< std::string > files)
load districts from files
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:202
A district (origin/destination)
Definition: ODDistrict.h:51
void addSource(const std::string &id, double weight)
Adds a source connection.
Definition: ODDistrict.cpp:53