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-2018 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 /****************************************************************************/
17 // A container for districts
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <string>
30 #include <utils/xml/XMLSubSys.h>
34 #include "ODDistrict.h"
35 #include "ODDistrictHandler.h"
36 #include "ODDistrictCont.h"
37 
38 
39 
40 // ===========================================================================
41 // method definitions
42 // ===========================================================================
44 
45 
47 
48 
49 std::string
50 ODDistrictCont::getRandomSourceFromDistrict(const std::string& name) const {
51  ODDistrict* district = get(name);
52  if (district == nullptr) {
53  throw InvalidArgument("There is no district '" + name + "'.");
54  }
55  return district->getRandomSource();
56 }
57 
58 
59 std::string
60 ODDistrictCont::getRandomSinkFromDistrict(const std::string& name) const {
61  ODDistrict* district = get(name);
62  if (district == nullptr) {
63  throw InvalidArgument("There is no district '" + name + "'.");
64  }
65  return district->getRandomSink();
66 }
67 
68 
69 void
70 ODDistrictCont::loadDistricts(std::vector<std::string> files) {
71  for (std::vector<std::string>::iterator i = files.begin(); i != files.end(); ++i) {
72  const std::string& districtfile = *i;
73  if (!FileHelpers::isReadable(districtfile)) {
74  throw ProcessError("Could not access network file '" + districtfile + "' to load.");
75  }
76  PROGRESS_BEGIN_MESSAGE("Loading districts from '" + districtfile + "'");
77  // build the xml-parser and handler
78  ODDistrictHandler handler(*this, districtfile);
79  if (!XMLSubSys::runParser(handler, districtfile, true)) {
81  } else {
83  }
84  }
85 }
86 
87 
88 void
89 ODDistrictCont::makeDistricts(const std::map<std::string, std::pair<std::vector<std::string>, std::vector<std::string> > >& districts) {
90  for (std::map<std::string, std::pair<std::vector<std::string>, std::vector<std::string> > >::const_iterator it = districts.begin(); it != districts.end(); ++it) {
91  ODDistrict* current = new ODDistrict(it->first);
92  const std::vector<std::string>& sources = it->second.first;
93  for (std::vector<std::string>::const_iterator i = sources.begin(); i != sources.end(); ++i) {
94  current->addSource(*i, 1.);
95  }
96  const std::vector<std::string>& sinks = it->second.second;
97  for (std::vector<std::string>::const_iterator i = sinks.begin(); i != sinks.end(); ++i) {
98  current->addSink(*i, 1.);
99  }
100  add(current->getID(), current);
101  }
102 }
103 
104 
105 /****************************************************************************/
std::string getRandomSource() const
Returns the id of a source to use.
Definition: ODDistrict.cpp:59
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:47
std::string getRandomSink() const
Returns the id of a sink to use.
Definition: ODDistrict.cpp:65
void addSink(const std::string &id, double weight)
Adds a sink connection.
Definition: ODDistrict.cpp:53
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:78
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:113
bool add(const std::string &id, ODDistrict * item)
Adds an item.
#define PROGRESS_FAILED_MESSAGE()
Definition: MsgHandler.h:246
~ODDistrictCont()
Destructor.
ODDistrictCont()
Constructor.
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:243
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:244
A district (origin/destination)
Definition: ODDistrict.h:45
void addSource(const std::string &id, double weight)
Adds a source connection.
Definition: ODDistrict.cpp:47