SUMO - Simulation of Urban MObility

ODDistrictHandler.cpp

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