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 2001-2010 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,
00052         EdgeFloatTimeLineRetriever &destination)
00053         : myDestination(destination), myAmEdgeBased(edgeBased),
00054         myAttributeName(attributeName) {
00055 }
00056 
00057 
00058 SAXWeightsHandler::ToRetrieveDefinition::~ToRetrieveDefinition() {
00059 }
00060 
00061 
00062 // ---------------------------------------------------------------------------
00063 // SAXWeightsHandler methods
00064 // ---------------------------------------------------------------------------
00065 SAXWeightsHandler::SAXWeightsHandler(const std::vector<ToRetrieveDefinition*> &defs,
00066                                      const std::string &file)
00067         : SUMOSAXHandler(file),
00068         myCurrentTimeBeg(-1), myCurrentTimeEnd(-1),
00069         myDefinitions(defs) {}
00070 
00071 
00072 SAXWeightsHandler::SAXWeightsHandler(ToRetrieveDefinition *def,
00073                                      const std::string &file)
00074         : SUMOSAXHandler(file),
00075         myCurrentTimeBeg(-1), myCurrentTimeEnd(-1) {
00076     myDefinitions.push_back(def);
00077 }
00078 
00079 
00080 SAXWeightsHandler::~SAXWeightsHandler() throw() {
00081     std::vector<ToRetrieveDefinition*>::iterator i;
00082     for (i=myDefinitions.begin(); i!=myDefinitions.end(); ++i) {
00083         delete *i;
00084     }
00085 }
00086 
00087 
00088 void SAXWeightsHandler::myStartElement(SumoXMLTag element,
00089                                        const SUMOSAXAttributes &attrs) throw(ProcessError) {
00090     switch (element) {
00091     case SUMO_TAG_INTERVAL: {
00092         bool ok = true;
00093         myCurrentTimeBeg = attrs.getSUMORealReporting(SUMO_ATTR_BEGIN, "weights/interval", 0, ok);
00094         myCurrentTimeEnd = attrs.getSUMORealReporting(SUMO_ATTR_END, "weights/interval", 0, ok);
00095     }
00096     break;
00097     case SUMO_TAG_EDGE: {
00098         bool ok = true;
00099         myCurrentEdgeID = attrs.getOptStringReporting(SUMO_ATTR_ID, 0, 0, ok, "");
00100         tryParse(attrs, true);
00101     }
00102     break;
00103     case SUMO_TAG_LANE: {
00104         tryParse(attrs, false);
00105     }
00106     break;
00107     default:
00108         break;
00109     }
00110 }
00111 
00112 
00113 void
00114 SAXWeightsHandler::tryParse(const SUMOSAXAttributes &attrs, bool isEdge) {
00115     // !!!! no error handling!
00116     std::vector<ToRetrieveDefinition*>::iterator i;
00117     if (isEdge) {
00118         // process all that want values directly from the edge
00119         for (i=myDefinitions.begin(); i!=myDefinitions.end(); ++i) {
00120             if ((*i)->myAmEdgeBased) {
00121                 if (attrs.hasAttribute((*i)->myAttributeName)) {
00122                     (*i)->myAggValue = attrs.getFloat((*i)->myAttributeName);
00123                     (*i)->myNoLanes = 1;
00124                     (*i)->myHadAttribute = true;
00125                 } else {
00126                     (*i)->myHadAttribute = false;
00127                 }
00128             } else {
00129                 (*i)->myAggValue = 0;
00130                 (*i)->myNoLanes = 0;
00131             }
00132         }
00133     } else {
00134         // process the current lane values
00135         for (i=myDefinitions.begin(); i!=myDefinitions.end(); ++i) {
00136             if (!(*i)->myAmEdgeBased) {
00137                 try {
00138                     (*i)->myAggValue += attrs.getFloat((*i)->myAttributeName);
00139                     ++((*i)->myNoLanes);
00140                     (*i)->myHadAttribute = true;
00141                 } catch (EmptyData &) {
00142                     MsgHandler::getErrorInstance()->inform("Missing value '" + (*i)->myAttributeName + "' in edge '" + myCurrentEdgeID + "'.");
00143                 } catch (NumberFormatException &) {
00144                     MsgHandler::getErrorInstance()->inform("The value should be numeric, but is not.\n In edge '" + myCurrentEdgeID + "' at time step " + toString(myCurrentTimeBeg) + ".");
00145                 }
00146             }
00147         }
00148     }
00149 }
00150 
00151 
00152 void
00153 SAXWeightsHandler::myEndElement(SumoXMLTag element) throw() {
00154     if (element==SUMO_TAG_EDGE) {
00155         std::vector<ToRetrieveDefinition*>::iterator i;
00156         for (i=myDefinitions.begin(); i!=myDefinitions.end(); ++i) {
00157             if ((*i)->myHadAttribute) {
00158                 (*i)->myDestination.addEdgeWeight(myCurrentEdgeID,
00159                                                   (*i)->myAggValue/(SUMOReal)(*i)->myNoLanes,
00160                                                   myCurrentTimeBeg, myCurrentTimeEnd);
00161             }
00162         }
00163     }
00164 }
00165 
00166 
00167 
00168 /****************************************************************************/
00169 

Generated on Wed May 5 00:06:36 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6