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