SUMO - Simulation of Urban MObility
ODDistrictCont.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A container for districts
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2002-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>
37 #include <utils/xml/XMLSubSys.h>
41 #include "ODDistrict.h"
42 #include "ODDistrictHandler.h"
43 #include "ODDistrictCont.h"
44 
45 
46 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif // CHECK_MEMORY_LEAKS
49 
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
55 
56 
58 
59 
60 std::string
61 ODDistrictCont::getRandomSourceFromDistrict(const std::string& name) const {
62  ODDistrict* district = get(name);
63  if (district == 0) {
64  throw InvalidArgument("There is no district '" + name + "'.");
65  }
66  return district->getRandomSource();
67 }
68 
69 
70 std::string
71 ODDistrictCont::getRandomSinkFromDistrict(const std::string& name) const {
72  ODDistrict* district = get(name);
73  if (district == 0) {
74  throw InvalidArgument("There is no district '" + name + "'.");
75  }
76  return district->getRandomSink();
77 }
78 
79 
80 void
81 ODDistrictCont::loadDistricts(std::vector<std::string> files) {
82  for (std::vector<std::string>::iterator i = files.begin(); i != files.end(); ++i) {
83  const std::string& districtfile = *i;
84  if (!FileHelpers::isReadable(districtfile)) {
85  throw ProcessError("Could not access network file '" + districtfile + "' to load.");
86  }
87  PROGRESS_BEGIN_MESSAGE("Loading districts from '" + districtfile + "'");
88  // build the xml-parser and handler
89  ODDistrictHandler handler(*this, districtfile);
90  if (!XMLSubSys::runParser(handler, districtfile, true)) {
92  } else {
94  }
95  }
96 }
97 
98 
99 void
100 ODDistrictCont::makeDistricts(const std::map<std::string, std::pair<std::vector<std::string>, std::vector<std::string> > >& districts) {
101  for (std::map<std::string, std::pair<std::vector<std::string>, std::vector<std::string> > >::const_iterator it = districts.begin(); it != districts.end(); ++it) {
102  ODDistrict* current = new ODDistrict(it->first);
103  const std::vector<std::string>& sources = it->second.first;
104  for (std::vector<std::string>::const_iterator i = sources.begin(); i != sources.end(); ++i) {
105  current->addSource(*i, 1.);
106  }
107  const std::vector<std::string>& sinks = it->second.second;
108  for (std::vector<std::string>::const_iterator i = sinks.begin(); i != sinks.end(); ++i) {
109  current->addSink(*i, 1.);
110  }
111  add(current->getID(), current);
112  }
113 }
114 
115 
116 /****************************************************************************/
std::string getRandomSource() const
Returns the id of a source to use.
Definition: ODDistrict.cpp:70
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:58
virtual 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
void addSink(const std::string &id, SUMOReal weight)
Adds a sink connection.
Definition: ODDistrict.cpp:64
An XML-Handler for districts.
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:114
#define PROGRESS_FAILED_MESSAGE()
Definition: MsgHandler.h:205
const std::string & getID() const
Returns the id.
Definition: Named.h:66
~ODDistrictCont()
Destructor.
void addSource(const std::string &id, SUMOReal weight)
Adds a source connection.
Definition: ODDistrict.cpp:58
std::string getRandomSourceFromDistrict(const std::string &name) const
Returns the id of a random source from the named district.
ODDistrictCont()
Constructor.
std::string getRandomSink() const
Returns the id of a sink to use.
Definition: ODDistrict.cpp:76
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:202
void loadDistricts(std::vector< std::string > files)
load districts from files
std::string getRandomSinkFromDistrict(const std::string &name) const
Returns the id of a random sink from the named district.
#define PROGRESS_DONE_MESSAGE()
Definition: MsgHandler.h:203
A district (origin/destination)
Definition: ODDistrict.h:52