SUMO - Simulation of Urban MObility

RORDLoader_TripDefs.cpp

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