SUMO - Simulation of Urban MObility

RORDLoader_TripDefs.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00009 // The basic class for loading trip definitions
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/common/UtilExceptions.h>
00035 #include <utils/common/StringTokenizer.h>
00036 #include <utils/common/MsgHandler.h>
00037 #include <utils/common/ToString.h>
00038 #include "RORouteDef.h"
00039 #include "RONet.h"
00040 #include "RORouteDef_OrigDest.h"
00041 #include "RORDLoader_TripDefs.h"
00042 #include "ROVehicle.h"
00043 #include "RORouteDef_Complete.h"
00044 #include "ROAbstractRouteDefLoader.h"
00045 #include <utils/xml/SUMOVehicleParserHelper.h>
00046 
00047 #ifdef CHECK_MEMORY_LEAKS
00048 #include <foreign/nvwa/debug_new.h>
00049 #endif // CHECK_MEMORY_LEAKS
00050 
00051 
00052 // ===========================================================================
00053 // method definitions
00054 // ===========================================================================
00055 RORDLoader_TripDefs::RORDLoader_TripDefs(RONet& net,
00056         SUMOTime begin, SUMOTime end,
00057         bool emptyDestinationsAllowed, bool withTaz,
00058         const std::string& fileName)
00059     : ROTypedXMLRoutesLoader(net, begin, end, fileName),
00060       myEmptyDestinationsAllowed(emptyDestinationsAllowed),
00061       myWithTaz(withTaz),
00062       myDepartureTime(-1), myCurrentVehicleType(0),
00063       myParameter(0), myHaveWarnedAboutDeprecatedTripDef(false) {}
00064 
00065 
00066 RORDLoader_TripDefs::~RORDLoader_TripDefs() {}
00067 
00068 
00069 void
00070 RORDLoader_TripDefs::myStartElement(int element,
00071                                     const SUMOSAXAttributes& attrs) {
00072     if (element == SUMO_TAG_TRIP__DEPRECATED && !myHaveWarnedAboutDeprecatedTripDef) {
00073         myHaveWarnedAboutDeprecatedTripDef = true;
00074         WRITE_WARNING("'" + toString(SUMO_TAG_TRIP__DEPRECATED) + "' is deprecated; please use '" + toString(SUMO_TAG_TRIP) + "'.");
00075     }
00076     // check whether a trip definition shall be parsed
00077     if (element == SUMO_TAG_TRIP || element == SUMO_TAG_TRIP__DEPRECATED) {
00078         bool ok = true;
00079         // get the vehicle id, the edges, the speed and position and
00080         //  the departure time and other information
00081         std::string id = getVehicleID(attrs);
00082         myDepartureTime = attrs.getSUMOTimeReporting(SUMO_ATTR_DEPART, id.c_str(), ok);
00083         if (myWithTaz) {
00084             myBeginEdge = getEdge(attrs, "origin", SUMO_ATTR_FROM_TAZ, id, false);
00085             myEndEdge = getEdge(attrs, "destination", SUMO_ATTR_TO_TAZ, id, myEmptyDestinationsAllowed);
00086         } else {
00087             myBeginEdge = getEdge(attrs, "origin", SUMO_ATTR_FROM, id, false);
00088             myEndEdge = getEdge(attrs, "destination", SUMO_ATTR_TO, id, myEmptyDestinationsAllowed);
00089         }
00090         myParameter = SUMOVehicleParserHelper::parseVehicleAttributes(attrs, true);
00091         myParameter->id = id;
00092         // recheck attributes
00093         if (!ok) {
00094             return;
00095         }
00096         if (myDepartureTime < 0) {
00097             WRITE_ERROR("The departure time must be positive.");
00098             return;
00099         }
00100     }
00101     // check whether a vehicle type shall be parsed
00102     if (element == SUMO_TAG_VTYPE || element == SUMO_TAG_VTYPE__DEPRECATED) {
00103         myCurrentVehicleType = SUMOVehicleParserHelper::beginVTypeParsing(attrs);
00104     } else if (myCurrentVehicleType != 0) {
00105         SUMOVehicleParserHelper::parseVTypeEmbedded(*myCurrentVehicleType, element, attrs);
00106     }
00107 }
00108 
00109 
00110 std::string
00111 RORDLoader_TripDefs::getVehicleID(const SUMOSAXAttributes& attrs) {
00112     // try to get the id, do not report an error if not given or empty...
00113     bool ok = true;
00114     std::string id = attrs.getOptStringReporting(SUMO_ATTR_ID, 0, ok, "", false);
00115     // get a valid vehicle id
00116     if (id == "") {
00117         return myIdSupplier.getNext();
00118     } else {
00119         return id;
00120     }
00121 }
00122 
00123 
00124 ROEdge*
00125 RORDLoader_TripDefs::getEdge(const SUMOSAXAttributes& attrs,
00126                              const std::string& purpose,
00127                              SumoXMLAttr which, const std::string& vid,
00128                              bool emptyAllowed) {
00129     UNUSED_PARAMETER(purpose);
00130     bool ok = true;
00131     std::string id = attrs.getStringReporting(which, 0, ok, !emptyAllowed);
00132     if (which == SUMO_ATTR_FROM_TAZ) {
00133         id += "-source";
00134     }
00135     if (which == SUMO_ATTR_TO_TAZ) {
00136         id += "-sink";
00137     }
00138     ROEdge* e = myNet.getEdge(id);
00139     if (e == 0 && !emptyAllowed) {
00140         WRITE_ERROR("The edge '" + id + "' is not known.\n Vehicle id='" + vid + "'.");
00141     }
00142     return e;
00143 }
00144 
00145 
00146 void
00147 RORDLoader_TripDefs::myEndElement(int element) {
00148     if ((element == SUMO_TAG_TRIP || element == SUMO_TAG_TRIP__DEPRECATED) &&
00149             !MsgHandler::getErrorInstance()->wasInformed()) {
00150 
00151         if (myDepartureTime < myBegin || myDepartureTime >= myEnd) {
00152             delete myParameter;
00153             return;
00154         }
00155         RGBColor* col = myParameter->wasSet(VEHPARS_COLOR_SET) ? new RGBColor(myParameter->color) : 0;
00156         RORouteDef* route = new RORouteDef_OrigDest(myParameter->id, col, myBeginEdge, myEndEdge);
00157         SUMOVTypeParameter* type = myNet.getVehicleTypeSecure(myParameter->vtypeid);
00158         // check whether any errors occured
00159         if (MsgHandler::getErrorInstance()->wasInformed()) {
00160             return;
00161         }
00162         if (myNet.addRouteDef(route)) {
00163             myNextRouteRead = true;
00164             // build the vehicle
00165             ROVehicle* veh = new ROVehicle(*myParameter, route, type);
00166             myNet.addVehicle(myParameter->id, veh);
00167         } else {
00168             WRITE_ERROR("The vehicle '" + myParameter->id + "' occurs at least twice.");
00169             delete route;
00170         }
00171         delete myParameter;
00172         myParameter = 0;
00173     }
00174     if (element == SUMO_TAG_VTYPE || element == SUMO_TAG_VTYPE__DEPRECATED) {
00175         SUMOVehicleParserHelper::closeVTypeParsing(*myCurrentVehicleType);
00176         myNet.addVehicleType(myCurrentVehicleType);
00177         myCurrentVehicleType = 0;
00178     }
00179 }
00180 
00181 
00182 void
00183 RORDLoader_TripDefs::beginNextRoute() {
00184     myNextRouteRead = false;
00185 }
00186 
00187 
00188 /****************************************************************************/
00189 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines