ODDistrictHandler.cpp
Go to the documentation of this file.00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029
00030 #include <string>
00031 #include <utility>
00032 #include <iostream>
00033 #include <utils/common/UtilExceptions.h>
00034 #include <utils/common/MsgHandler.h>
00035 #include <utils/xml/SUMOSAXHandler.h>
00036 #include <utils/xml/SUMOXMLDefinitions.h>
00037 #include "ODDistrict.h"
00038 #include "ODDistrictCont.h"
00039 #include "ODDistrictHandler.h"
00040
00041 #ifdef CHECK_MEMORY_LEAKS
00042 #include <foreign/nvwa/debug_new.h>
00043 #endif // CHECK_MEMORY_LEAKS
00044
00045
00046
00047
00048
00049 ODDistrictHandler::ODDistrictHandler(ODDistrictCont &cont,
00050 const std::string &file) throw()
00051 : SUMOSAXHandler(file), myContainer(cont), myCurrentDistrict(0) {}
00052
00053
00054 ODDistrictHandler::~ODDistrictHandler() throw() {}
00055
00056
00057 void
00058 ODDistrictHandler::myStartElement(SumoXMLTag element,
00059 const SUMOSAXAttributes &attrs) throw(ProcessError) {
00060 switch (element) {
00061 case SUMO_TAG_DISTRICT:
00062 openDistrict(attrs);
00063 break;
00064 case SUMO_TAG_DSOURCE:
00065 addSource(attrs);
00066 break;
00067 case SUMO_TAG_DSINK:
00068 addSink(attrs);
00069 break;
00070 default:
00071 break;
00072 }
00073 }
00074
00075
00076 void
00077 ODDistrictHandler::myEndElement(SumoXMLTag element) throw(ProcessError) {
00078 if (element==SUMO_TAG_DISTRICT) {
00079 closeDistrict();
00080 }
00081 }
00082
00083
00084 void
00085 ODDistrictHandler::openDistrict(const SUMOSAXAttributes &attrs) throw() {
00086 myCurrentDistrict = 0;
00087
00088 std::string id;
00089 if (!attrs.setIDFromAttributes("district", id)) {
00090 return;
00091 }
00092 myCurrentDistrict = new ODDistrict(id);
00093 }
00094
00095
00096 void
00097 ODDistrictHandler::addSource(const SUMOSAXAttributes &attrs) throw() {
00098 std::pair<std::string, SUMOReal> vals = parseConnection(attrs, "source");
00099 if (vals.second>=0) {
00100 myCurrentDistrict->addSource(vals.first, vals.second);
00101 }
00102 }
00103
00104
00105 void
00106 ODDistrictHandler::addSink(const SUMOSAXAttributes &attrs) throw() {
00107 std::pair<std::string, SUMOReal> vals = parseConnection(attrs, "sink");
00108 if (vals.second>=0) {
00109 myCurrentDistrict->addSink(vals.first, vals.second);
00110 }
00111 }
00112
00113
00114
00115 std::pair<std::string, SUMOReal>
00116 ODDistrictHandler::parseConnection(const SUMOSAXAttributes &attrs, const std::string &type) throw() {
00117
00118 if (myCurrentDistrict==0) {
00119 return std::pair<std::string, SUMOReal>("", -1);
00120 }
00121
00122 std::string id;
00123 if (!attrs.setIDFromAttributes(type.c_str(), id)) {
00124 return std::pair<std::string, SUMOReal>("", -1);
00125 }
00126
00127 bool ok = true;
00128 SUMOReal weight = attrs.getSUMORealReporting(SUMO_ATTR_WEIGHT, type.c_str(), id.c_str(), ok);
00129 if (ok) {
00130 if (weight<0) {
00131 MsgHandler::getErrorInstance()->inform("'probability' must be positive (in definition of " + type + " '" + id + "').");
00132 } else {
00133 return std::pair<std::string, SUMOReal>(id, weight);
00134 }
00135 }
00136 return std::pair<std::string, SUMOReal>("", -1);
00137 }
00138
00139
00140 void
00141 ODDistrictHandler::closeDistrict() throw() {
00142 if (myCurrentDistrict!=0) {
00143 myContainer.add(myCurrentDistrict->getID(), myCurrentDistrict);
00144 }
00145 }
00146
00147
00148
00149
00150