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-2017 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 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
51 
52 
54 
55 
56 std::string
57 ODDistrictCont::getRandomSourceFromDistrict(const std::string& name) const {
58  ODDistrict* district = get(name);
59  if (district == 0) {
60  throw InvalidArgument("There is no district '" + name + "'.");
61  }
62  return district->getRandomSource();
63 }
64 
65 
66 std::string
67 ODDistrictCont::getRandomSinkFromDistrict(const std::string& name) const {
68  ODDistrict* district = get(name);
69  if (district == 0) {
70  throw InvalidArgument("There is no district '" + name + "'.");
71  }
72  return district->getRandomSink();
73 }
74 
75 
76 void
77 ODDistrictCont::loadDistricts(std::vector<std::string> files) {
78  for (std::vector<std::string>::iterator i = files.begin(); i != files.end(); ++i) {
79  const std::string& districtfile = *i;
80  if (!FileHelpers::isReadable(districtfile)) {
81  throw ProcessError("Could not access network file '" + districtfile + "' to load.");
82  }
83  PROGRESS_BEGIN_MESSAGE("Loading districts from '" + districtfile + "'");
84  // build the xml-parser and handler
85  ODDistrictHandler handler(*this, districtfile);
86  if (!XMLSubSys::runParser(handler, districtfile, true)) {
88  } else {
90  }
91  }
92 }
93 
94 
95 void
96 ODDistrictCont::makeDistricts(const std::map<std::string, std::pair<std::vector<std::string>, std::vector<std::string> > >& districts) {
97  for (std::map<std::string, std::pair<std::vector<std::string>, std::vector<std::string> > >::const_iterator it = districts.begin(); it != districts.end(); ++it) {
98  ODDistrict* current = new ODDistrict(it->first);
99  const std::vector<std::string>& sources = it->second.first;
100  for (std::vector<std::string>::const_iterator i = sources.begin(); i != sources.end(); ++i) {
101  current->addSource(*i, 1.);
102  }
103  const std::vector<std::string>& sinks = it->second.second;
104  for (std::vector<std::string>::const_iterator i = sinks.begin(); i != sinks.end(); ++i) {
105  current->addSink(*i, 1.);
106  }
107  add(current->getID(), current);
108  }
109 }
110 
111 
112 /****************************************************************************/
std::string getRandomSource() const
Returns the id of a source to use.
Definition: ODDistrict.cpp:66
static bool isReadable(std::string path)
Checks whether the given file is readable.
Definition: FileHelpers.cpp:54
virtual bool add(const std::string &id, ODDistrict * item)
Adds an item.
std::string getRandomSink() const
Returns the id of a sink to use.
Definition: ODDistrict.cpp:72
void addSink(const std::string &id, double weight)
Adds a sink connection.
Definition: ODDistrict.cpp:60
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:66
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:110
#define PROGRESS_FAILED_MESSAGE()
Definition: MsgHandler.h:205
~ODDistrictCont()
Destructor.
ODDistrictCont()
Constructor.
#define PROGRESS_BEGIN_MESSAGE(msg)
Definition: MsgHandler.h:202
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:203
A district (origin/destination)
Definition: ODDistrict.h:52
void addSource(const std::string &id, double weight)
Adds a source connection.
Definition: ODDistrict.cpp:54