SUMO - Simulation of Urban MObility
SAXWeightsHandler.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // An XML-handler for network weights
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 <utils/options/OptionsCont.h>
00035 #include <utils/common/UtilExceptions.h>
00036 #include <utils/common/MsgHandler.h>
00037 #include <utils/common/ToString.h>
00038 #include <utils/xml/SUMOXMLDefinitions.h>
00039 #include <utils/xml/SUMOSAXHandler.h>
00040 #include "SAXWeightsHandler.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 // ---------------------------------------------------------------------------
00051 // SAXWeightsHandler::ToRetrieveDefinition methods
00052 // ---------------------------------------------------------------------------
00053 SAXWeightsHandler::ToRetrieveDefinition::ToRetrieveDefinition(const std::string& attributeName,
00054         bool edgeBased, EdgeFloatTimeLineRetriever& destination)
00055     : myAttributeName(attributeName), myAmEdgeBased(edgeBased), myDestination(destination) {
00056 }
00057 
00058 
00059 SAXWeightsHandler::ToRetrieveDefinition::~ToRetrieveDefinition() {
00060 }
00061 
00062 
00063 // ---------------------------------------------------------------------------
00064 // SAXWeightsHandler methods
00065 // ---------------------------------------------------------------------------
00066 SAXWeightsHandler::SAXWeightsHandler(const std::vector<ToRetrieveDefinition*> &defs,
00067                                      const std::string& file)
00068     : SUMOSAXHandler(file), myDefinitions(defs),
00069       myCurrentTimeBeg(-1), myCurrentTimeEnd(-1) {}
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() {
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(int element,
00089                                        const SUMOSAXAttributes& attrs) {
00090     switch (element) {
00091         case SUMO_TAG_INTERVAL: {
00092             bool ok = true;
00093             myCurrentTimeBeg = attrs.getSUMORealReporting(SUMO_ATTR_BEGIN, 0, ok);
00094             myCurrentTimeEnd = attrs.getSUMORealReporting(SUMO_ATTR_END, 0, ok);
00095         }
00096         break;
00097         case SUMO_TAG_EDGE: {
00098             bool ok = true;
00099             myCurrentEdgeID = attrs.getOptStringReporting(SUMO_ATTR_ID, 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                     WRITE_ERROR("Missing value '" + (*i)->myAttributeName + "' in edge '" + myCurrentEdgeID + "'.");
00143                 } catch (NumberFormatException&) {
00144                     WRITE_ERROR("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(int element) {
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 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines