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