Eclipse 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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // A container for districts
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <string>
28 #include <utils/xml/XMLSubSys.h>
32 #include "ODDistrict.h"
33 #include "ODDistrictHandler.h"
34 #include "ODDistrictCont.h"
35 
36 
37 
38 // ===========================================================================
39 // method definitions
40 // ===========================================================================
42 
43 
45 
46 
47 std::string
48 ODDistrictCont::getRandomSourceFromDistrict(const std::string& name) const {
49  ODDistrict* district = get(name);
50  if (district == nullptr) {
51  throw InvalidArgument("There is no district '" + name + "'.");
52  }
53  return district->getRandomSource();
54 }
55 
56 
57 std::string
58 ODDistrictCont::getRandomSinkFromDistrict(const std::string& name) const {
59  ODDistrict* district = get(name);
60  if (district == nullptr) {
61  throw InvalidArgument("There is no district '" + name + "'.");
62  }
63  return district->getRandomSink();
64 }
65 
66 
67 void
68 ODDistrictCont::loadDistricts(std::vector<std::string> files) {
69  for (std::vector<std::string>::iterator i = files.begin(); i != files.end(); ++i) {
70  const std::string& districtfile = *i;
71  if (!FileHelpers::isReadable(districtfile)) {
72  throw ProcessError("Could not access network file '" + districtfile + "' to load.");
73  }
74  PROGRESS_BEGIN_MESSAGE("Loading districts from '" + districtfile + "'");
75  // build the xml-parser and handler
76  ODDistrictHandler handler(*this, districtfile);
77  if (!XMLSubSys::runParser(handler, districtfile, true)) {
79  } else {
81  }
82  }
83 }
84 
85 
86 void
87 ODDistrictCont::makeDistricts(const std::map<std::string, std::pair<std::vector<std::string>, std::vector<std::string> > >& districts) {
88  for (std::map<std::string, std::pair<std::vector<std::string>, std::vector<std::string> > >::const_iterator it = districts.begin(); it != districts.end(); ++it) {
89  ODDistrict* current = new ODDistrict(it->first);
90  const std::vector<std::string>& sources = it->second.first;
91  for (std::vector<std::string>::const_iterator i = sources.begin(); i != sources.end(); ++i) {
92  current->addSource(*i, 1.);
93  }
94  const std::vector<std::string>& sinks = it->second.second;
95  for (std::vector<std::string>::const_iterator i = sinks.begin(); i != sinks.end(); ++i) {
96  current->addSink(*i, 1.);
97  }
98  add(current->getID(), current);
99  }
100 }
101 
102 
103 /****************************************************************************/
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:280
#define PROGRESS_FAILED_MESSAGE()
Definition: MsgHandler.h:283
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:279
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:48
const std::string & getID() const
Returns the id.
Definition: Named.h:73
ODDistrict * get(const std::string &id) const
Retrieves an item.
bool add(const std::string &id, ODDistrict * item)
Adds an item.
void makeDistricts(const std::map< std::string, std::pair< std::vector< std::string >, std::vector< std::string > > > &districts)
create districts from description
std::string getRandomSourceFromDistrict(const std::string &name) const
Returns the id of a random source from the named district.
std::string getRandomSinkFromDistrict(const std::string &name) const
Returns the id of a random sink from the named district.
~ODDistrictCont()
Destructor.
void loadDistricts(std::vector< std::string > files)
load districts from files
ODDistrictCont()
Constructor.
An XML-Handler for districts.
A district (origin/destination)
Definition: ODDistrict.h:42
void addSource(const std::string &id, double weight)
Adds a source connection.
Definition: ODDistrict.cpp:45
void addSink(const std::string &id, double weight)
Adds a sink connection.
Definition: ODDistrict.cpp:51
std::string getRandomSink() const
Returns the id of a sink to use.
Definition: ODDistrict.cpp:63
std::string getRandomSource() const
Returns the id of a source to use.
Definition: ODDistrict.cpp:57
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false, const bool isRoute=false)
Runs the given handler on the given file; returns if everything's ok.
Definition: XMLSubSys.cpp:148