#include <config.h>
#include <string>
#include <utils/common/MsgHandler.h>
#include <utils/common/UtilExceptions.h>
#include <utils/common/NamedObjectCont.h>
#include <utils/xml/XMLSubSys.h>
#include <utils/common/RandHelper.h>
#include <utils/common/FileHelpers.h>
#include <utils/options/OptionsCont.h>
#include "ODDistrict.h"
#include "ODDistrictHandler.h"
#include "ODDistrictCont.h"
ODDistrictCont::ODDistrictCont() {}
ODDistrictCont::~ODDistrictCont() {}
std::string
ODDistrictCont::getRandomSourceFromDistrict(const std::string& name) const {
ODDistrict* district = get(name);
if (district == nullptr) {
throw InvalidArgument("There is no district '" + name + "'.");
}
std::string randomSource;
try {
randomSource = district->getRandomSource();
} catch (OutOfBoundsException&) {
throw ProcessError(TLF("District '%' does not provide any valid source.", name));
}
return randomSource;
}
std::string
ODDistrictCont::getRandomSinkFromDistrict(const std::string& name) const {
ODDistrict* district = get(name);
if (district == nullptr) {
throw InvalidArgument("There is no district '" + name + "'.");
}
std::string randomSink;
try {
randomSink = district->getRandomSink();
} catch (OutOfBoundsException&) {
throw ProcessError(TLF("District '%' does not provide any valid sink.", name));
}
return randomSink;
}
void
ODDistrictCont::loadDistricts(std::vector<std::string> files) {
for (std::vector<std::string>::iterator i = files.begin(); i != files.end(); ++i) {
const std::string& districtfile = *i;
if (!FileHelpers::isReadable(districtfile)) {
throw ProcessError(TLF("Could not access network file '%' to load.", districtfile));
}
PROGRESS_BEGIN_MESSAGE("Loading districts from '" + districtfile + "'");
ODDistrictHandler handler(*this, districtfile);
if (!XMLSubSys::runParser(handler, districtfile, true)) {
PROGRESS_FAILED_MESSAGE();
} else {
PROGRESS_DONE_MESSAGE();
}
}
}
void
ODDistrictCont::makeDistricts(const std::map<std::string, std::pair<std::vector<std::string>, std::vector<std::string> > >& districts) {
for (std::map<std::string, std::pair<std::vector<std::string>, std::vector<std::string> > >::const_iterator it = districts.begin(); it != districts.end(); ++it) {
ODDistrict* current = new ODDistrict(it->first);
const std::vector<std::string>& sources = it->second.first;
for (std::vector<std::string>::const_iterator i = sources.begin(); i != sources.end(); ++i) {
current->addSource(*i, 1.);
}
const std::vector<std::string>& sinks = it->second.second;
for (std::vector<std::string>::const_iterator i = sinks.begin(); i != sinks.end(); ++i) {
current->addSink(*i, 1.);
}
add(current->getID(), current);
}
}