SUMO - Simulation of Urban MObility
RODFDetectorHandler.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00010 // A handler for loading detector descriptions
00011 /****************************************************************************/
00012 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00013 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00014 /****************************************************************************/
00015 //
00016 //   This file is part of SUMO.
00017 //   SUMO is free software: you can redistribute it and/or modify
00018 //   it under the terms of the GNU General Public License as published by
00019 //   the Free Software Foundation, either version 3 of the License, or
00020 //   (at your option) any later version.
00021 //
00022 /****************************************************************************/
00023 
00024 
00025 // ===========================================================================
00026 // included modules
00027 // ===========================================================================
00028 #ifdef _MSC_VER
00029 #include <windows_config.h>
00030 #else
00031 #include <config.h>
00032 #endif
00033 
00034 #include <string>
00035 #include <utils/options/OptionsCont.h>
00036 #include <utils/common/MsgHandler.h>
00037 #include <utils/common/StringTokenizer.h>
00038 #include <utils/common/UtilExceptions.h>
00039 #include <utils/common/TplConvertSec.h>
00040 #include <utils/common/ToString.h>
00041 #include <utils/xml/SUMOSAXHandler.h>
00042 #include <utils/xml/SUMOXMLDefinitions.h>
00043 #include "RODFDetectorHandler.h"
00044 #include "RODFNet.h"
00045 
00046 #ifdef CHECK_MEMORY_LEAKS
00047 #include <foreign/nvwa/debug_new.h>
00048 #endif // CHECK_MEMORY_LEAKS
00049 
00050 
00051 // ===========================================================================
00052 // method definitions
00053 // ===========================================================================
00054 RODFDetectorHandler::RODFDetectorHandler(RODFNet* optNet, bool ignoreErrors, RODFDetectorCon& con,
00055         const std::string& file)
00056     : SUMOSAXHandler(file),
00057       myNet(optNet), myIgnoreErrors(ignoreErrors), myContainer(con),
00058       myHaveWarnedAboutDeprecatedDetectorDefinition(false) {}
00059 
00060 
00061 RODFDetectorHandler::~RODFDetectorHandler() {}
00062 
00063 
00064 void
00065 RODFDetectorHandler::myStartElement(int element,
00066                                     const SUMOSAXAttributes& attrs) {
00067     if (element == SUMO_TAG_DETECTOR_DEFINITION__DEPRECATED && !myHaveWarnedAboutDeprecatedDetectorDefinition) {
00068         myHaveWarnedAboutDeprecatedDetectorDefinition = true;
00069         WRITE_WARNING("Using '" + toString(SUMO_TAG_DETECTOR_DEFINITION__DEPRECATED) + "' is deprecated. Please use '" + toString(SUMO_TAG_DETECTOR_DEFINITION) + "' instead.");
00070     }
00071     if (element == SUMO_TAG_DETECTOR_DEFINITION || element == SUMO_TAG_DETECTOR_DEFINITION__DEPRECATED) {
00072         try {
00073             bool ok = true;
00074             // get the id, report an error if not given or empty...
00075             std::string id = attrs.getStringReporting(SUMO_ATTR_ID, 0, ok);
00076             if (!ok) {
00077                 throw ProcessError();
00078             }
00079             std::string lane = attrs.getStringReporting(SUMO_ATTR_LANE, id.c_str(), ok);
00080             if (!ok) {
00081                 throw ProcessError();
00082             }
00083             ROEdge* edge = myNet->getEdge(lane.substr(0, lane.rfind('_')));
00084             unsigned int laneIndex = TplConvertSec<char>::_2intSec(lane.substr(lane.rfind('_') + 1).c_str(), INT_MAX);
00085             if (edge == 0 || laneIndex >= edge->getLaneNo()) {
00086                 throw ProcessError("Unknown lane '" + lane + "' for detector '" + id + "' in '" + getFileName() + "'.");
00087             }
00088             SUMOReal pos = attrs.getSUMORealReporting(SUMO_ATTR_POSITION, id.c_str(), ok);
00089             std::string mml_type = attrs.getOptStringReporting(SUMO_ATTR_TYPE, id.c_str(), ok, "");
00090             if (!ok) {
00091                 throw ProcessError();
00092             }
00093             RODFDetectorType type = TYPE_NOT_DEFINED;
00094             if (mml_type == "between") {
00095                 type = BETWEEN_DETECTOR;
00096             } else if (mml_type == "source" || mml_type == "highway_source") { // !!! highway-source is legacy (removed accoring output on 06.08.2007)
00097                 type = SOURCE_DETECTOR;
00098             } else if (mml_type == "sink") {
00099                 type = SINK_DETECTOR;
00100             }
00101             RODFDetector* detector = new RODFDetector(id, lane, pos, type);
00102             if (!myContainer.addDetector(detector)) {
00103                 delete detector;
00104                 throw ProcessError("Could not add detector '" + id + "' (probably the id is already used).");
00105             }
00106         } catch (ProcessError& e) {
00107             if (myIgnoreErrors) {
00108                 WRITE_WARNING(e.what());
00109             } else {
00110                 throw e;
00111             }
00112         }
00113     }
00114 }
00115 
00116 
00117 
00118 /****************************************************************************/
00119 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines