SUMO - Simulation of Urban MObility

SAXWeightsHandler.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // An XML-handler for network weights
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 <utils/options/OptionsCont.h>
00032 #include <utils/common/UtilExceptions.h>
00033 #include <utils/common/MsgHandler.h>
00034 #include <utils/common/ToString.h>
00035 #include <utils/xml/SUMOXMLDefinitions.h>
00036 #include <utils/xml/SUMOSAXHandler.h>
00037 #include "SAXWeightsHandler.h"
00038 
00039 #ifdef CHECK_MEMORY_LEAKS
00040 #include <foreign/nvwa/debug_new.h>
00041 #endif // CHECK_MEMORY_LEAKS
00042 
00043 
00044 // ===========================================================================
00045 // method definitions
00046 // ===========================================================================
00047 // ---------------------------------------------------------------------------
00048 // SAXWeightsHandler::ToRetrieveDefinition methods
00049 // ---------------------------------------------------------------------------
00050 SAXWeightsHandler::ToRetrieveDefinition::ToRetrieveDefinition(const std::string& attributeName,
00051         bool edgeBased, EdgeFloatTimeLineRetriever& destination)
00052     : myAttributeName(attributeName), myAmEdgeBased(edgeBased), myDestination(destination) {
00053 }
00054 
00055 
00056 SAXWeightsHandler::ToRetrieveDefinition::~ToRetrieveDefinition() {
00057 }
00058 
00059 
00060 // ---------------------------------------------------------------------------
00061 // SAXWeightsHandler methods
00062 // ---------------------------------------------------------------------------
00063 SAXWeightsHandler::SAXWeightsHandler(const std::vector<ToRetrieveDefinition*> &defs,
00064                                      const std::string& file)
00065     : SUMOSAXHandler(file), myDefinitions(defs),
00066       myCurrentTimeBeg(-1), myCurrentTimeEnd(-1) {}
00067 
00068 
00069 SAXWeightsHandler::SAXWeightsHandler(ToRetrieveDefinition* def,
00070                                      const std::string& file)
00071     : SUMOSAXHandler(file),
00072       myCurrentTimeBeg(-1), myCurrentTimeEnd(-1) {
00073     myDefinitions.push_back(def);
00074 }
00075 
00076 
00077 SAXWeightsHandler::~SAXWeightsHandler() throw() {
00078     std::vector<ToRetrieveDefinition*>::iterator i;
00079     for (i=myDefinitions.begin(); i!=myDefinitions.end(); ++i) {
00080         delete *i;
00081     }
00082 }
00083 
00084 
00085 void SAXWeightsHandler::myStartElement(int element,
00086                                        const SUMOSAXAttributes& attrs) throw(ProcessError) {
00087     switch (element) {
00088     case SUMO_TAG_INTERVAL: {
00089         bool ok = true;
00090         myCurrentTimeBeg = attrs.getSUMORealReporting(SUMO_ATTR_BEGIN, 0, ok);
00091         myCurrentTimeEnd = attrs.getSUMORealReporting(SUMO_ATTR_END, 0, ok);
00092     }
00093     break;
00094     case SUMO_TAG_EDGE: {
00095         bool ok = true;
00096         myCurrentEdgeID = attrs.getOptStringReporting(SUMO_ATTR_ID, 0, ok, "");
00097         tryParse(attrs, true);
00098     }
00099     break;
00100     case SUMO_TAG_LANE: {
00101         tryParse(attrs, false);
00102     }
00103     break;
00104     default:
00105         break;
00106     }
00107 }
00108 
00109 
00110 void
00111 SAXWeightsHandler::tryParse(const SUMOSAXAttributes& attrs, bool isEdge) {
00112     // !!!! no error handling!
00113     std::vector<ToRetrieveDefinition*>::iterator i;
00114     if (isEdge) {
00115         // process all that want values directly from the edge
00116         for (i=myDefinitions.begin(); i!=myDefinitions.end(); ++i) {
00117             if ((*i)->myAmEdgeBased) {
00118                 if (attrs.hasAttribute((*i)->myAttributeName)) {
00119                     (*i)->myAggValue = attrs.getFloat((*i)->myAttributeName);
00120                     (*i)->myNoLanes = 1;
00121                     (*i)->myHadAttribute = true;
00122                 } else {
00123                     (*i)->myHadAttribute = false;
00124                 }
00125             } else {
00126                 (*i)->myAggValue = 0;
00127                 (*i)->myNoLanes = 0;
00128             }
00129         }
00130     } else {
00131         // process the current lane values
00132         for (i=myDefinitions.begin(); i!=myDefinitions.end(); ++i) {
00133             if (!(*i)->myAmEdgeBased) {
00134                 try {
00135                     (*i)->myAggValue += attrs.getFloat((*i)->myAttributeName);
00136                     ++((*i)->myNoLanes);
00137                     (*i)->myHadAttribute = true;
00138                 } catch (EmptyData&) {
00139                     WRITE_ERROR("Missing value '" + (*i)->myAttributeName + "' in edge '" + myCurrentEdgeID + "'.");
00140                 } catch (NumberFormatException&) {
00141                     WRITE_ERROR("The value should be numeric, but is not.\n In edge '" + myCurrentEdgeID + "' at time step " + toString(myCurrentTimeBeg) + ".");
00142                 }
00143             }
00144         }
00145     }
00146 }
00147 
00148 
00149 void
00150 SAXWeightsHandler::myEndElement(int element) throw() {
00151     if (element==SUMO_TAG_EDGE) {
00152         std::vector<ToRetrieveDefinition*>::iterator i;
00153         for (i=myDefinitions.begin(); i!=myDefinitions.end(); ++i) {
00154             if ((*i)->myHadAttribute) {
00155                 (*i)->myDestination.addEdgeWeight(myCurrentEdgeID,
00156                                                   (*i)->myAggValue/(SUMOReal)(*i)->myNoLanes,
00157                                                   myCurrentTimeBeg, myCurrentTimeEnd);
00158             }
00159         }
00160     }
00161 }
00162 
00163 
00164 
00165 /****************************************************************************/
00166 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines