SUMO - Simulation of Urban MObility
ODDistrictHandler.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // An XML-Handler for districts
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 
00023 
00024 // ===========================================================================
00025 // included modules
00026 // ===========================================================================
00027 #ifdef _MSC_VER
00028 #include <windows_config.h>
00029 #else
00030 #include <config.h>
00031 #endif
00032 
00033 #include <string>
00034 #include <utility>
00035 #include <iostream>
00036 #include <utils/common/UtilExceptions.h>
00037 #include <utils/common/MsgHandler.h>
00038 #include <utils/common/ToString.h>
00039 #include <utils/xml/SUMOSAXHandler.h>
00040 #include <utils/xml/SUMOXMLDefinitions.h>
00041 #include "ODDistrict.h"
00042 #include "ODDistrictCont.h"
00043 #include "ODDistrictHandler.h"
00044 
00045 #ifdef CHECK_MEMORY_LEAKS
00046 #include <foreign/nvwa/debug_new.h>
00047 #endif // CHECK_MEMORY_LEAKS
00048 
00049 
00050 // ===========================================================================
00051 // method definitions
00052 // ===========================================================================
00053 ODDistrictHandler::ODDistrictHandler(ODDistrictCont& cont,
00054                                      const std::string& file)
00055     : SUMOSAXHandler(file), myContainer(cont), myCurrentDistrict(0),
00056       myHaveWarnedAboutDeprecatedDistrict(false), myHaveWarnedAboutDeprecatedDSource(false), myHaveWarnedAboutDeprecatedDSink(false)  {}
00057 
00058 
00059 ODDistrictHandler::~ODDistrictHandler() {}
00060 
00061 
00062 void
00063 ODDistrictHandler::myStartElement(int element,
00064                                   const SUMOSAXAttributes& attrs) {
00065     switch (element) {
00066         case SUMO_TAG_DISTRICT__DEPRECATED:
00067             if (!myHaveWarnedAboutDeprecatedDistrict) {
00068                 myHaveWarnedAboutDeprecatedDistrict = true;
00069                 WRITE_WARNING("'" + toString(SUMO_TAG_DISTRICT__DEPRECATED) + "' is deprecated, please use '" + toString(SUMO_TAG_TAZ) + "'.");
00070             }
00071         case SUMO_TAG_TAZ:
00072             openDistrict(attrs);
00073             break;
00074         case SUMO_TAG_DSOURCE__DEPRECATED:
00075             if (!myHaveWarnedAboutDeprecatedDSource) {
00076                 myHaveWarnedAboutDeprecatedDSource = true;
00077                 WRITE_WARNING("'" + toString(SUMO_TAG_DSOURCE__DEPRECATED) + "' is deprecated, please use '" + toString(SUMO_TAG_TAZSOURCE) + "'.");
00078             }
00079         case SUMO_TAG_TAZSOURCE:
00080             addSource(attrs);
00081             break;
00082         case SUMO_TAG_DSINK__DEPRECATED:
00083             if (!myHaveWarnedAboutDeprecatedDSink) {
00084                 myHaveWarnedAboutDeprecatedDSink = true;
00085                 WRITE_WARNING("'" + toString(SUMO_TAG_DSINK__DEPRECATED) + "' is deprecated, please use '" + toString(SUMO_TAG_TAZSINK) + "'.");
00086             }
00087         case SUMO_TAG_TAZSINK:
00088             addSink(attrs);
00089             break;
00090         default:
00091             break;
00092     }
00093 }
00094 
00095 
00096 void
00097 ODDistrictHandler::myEndElement(int element) {
00098     if (element == SUMO_TAG_DISTRICT__DEPRECATED || element == SUMO_TAG_TAZ) {
00099         closeDistrict();
00100     }
00101 }
00102 
00103 
00104 void
00105 ODDistrictHandler::openDistrict(const SUMOSAXAttributes& attrs) {
00106     myCurrentDistrict = 0;
00107     // get the id, report an error if not given or empty...
00108     bool ok = true;
00109     std::string id = attrs.getStringReporting(SUMO_ATTR_ID, 0, ok);
00110     if (!ok) {
00111         return;
00112     }
00113     myCurrentDistrict = new ODDistrict(id);
00114 }
00115 
00116 
00117 void
00118 ODDistrictHandler::addSource(const SUMOSAXAttributes& attrs) {
00119     std::pair<std::string, SUMOReal> vals = parseConnection(attrs);
00120     if (vals.second >= 0) {
00121         myCurrentDistrict->addSource(vals.first, vals.second);
00122     }
00123 }
00124 
00125 
00126 void
00127 ODDistrictHandler::addSink(const SUMOSAXAttributes& attrs) {
00128     std::pair<std::string, SUMOReal> vals = parseConnection(attrs);
00129     if (vals.second >= 0) {
00130         myCurrentDistrict->addSink(vals.first, vals.second);
00131     }
00132 }
00133 
00134 
00135 
00136 std::pair<std::string, SUMOReal>
00137 ODDistrictHandler::parseConnection(const SUMOSAXAttributes& attrs) {
00138     // check the current district first
00139     if (myCurrentDistrict == 0) {
00140         return std::pair<std::string, SUMOReal>("", -1);
00141     }
00142     // get the id, report an error if not given or empty...
00143     bool ok = true;
00144     std::string id = attrs.getStringReporting(SUMO_ATTR_ID, 0, ok);
00145     if (!ok) {
00146         return std::pair<std::string, SUMOReal>("", -1);
00147     }
00148     // get the weight
00149     SUMOReal weight = attrs.getSUMORealReporting(SUMO_ATTR_WEIGHT, id.c_str(), ok);
00150     if (ok) {
00151         if (weight < 0) {
00152             WRITE_ERROR("'probability' must be positive (in definition of " +
00153                         attrs.getObjectType() + " '" + id + "').");
00154         } else {
00155             return std::pair<std::string, SUMOReal>(id, weight);
00156         }
00157     }
00158     return std::pair<std::string, SUMOReal>("", -1);
00159 }
00160 
00161 
00162 void
00163 ODDistrictHandler::closeDistrict() {
00164     if (myCurrentDistrict != 0) {
00165         myContainer.add(myCurrentDistrict->getID(), myCurrentDistrict);
00166     }
00167 }
00168 
00169 
00170 
00171 /****************************************************************************/
00172 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines