Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2001-2026 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file MSRouteHandler.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @author Sascha Krieg
18 : /// @author Michael Behrisch
19 : /// @author Johannes Rummel
20 : /// @author Mirko Barthauer
21 : /// @date Mon, 9 Jul 2001
22 : ///
23 : // Parser and container for routes during their loading
24 : /****************************************************************************/
25 : #include <config.h>
26 :
27 : #include "MSRouteHandler.h"
28 : #include <microsim/transportables/MSTransportableControl.h>
29 : #include <microsim/transportables/MSPModel.h>
30 : #include <microsim/transportables/MSStageDriving.h>
31 : #include <microsim/transportables/MSStageTranship.h>
32 : #include <microsim/transportables/MSStageTrip.h>
33 : #include <microsim/transportables/MSStageWaiting.h>
34 : #include <microsim/transportables/MSStageWalking.h>
35 : #include <microsim/MSEdge.h>
36 : #include <microsim/MSLane.h>
37 : #include <microsim/MSInsertionControl.h>
38 : #include <microsim/MSParkingArea.h>
39 : #include <microsim/MSStoppingPlace.h>
40 : #include <microsim/trigger/MSChargingStation.h>
41 : #include <microsim/MSVehicleControl.h>
42 : #include <microsim/MSEventControl.h>
43 : #include <microsim/Command_RouteReplacement.h>
44 : #include <utils/common/StringTokenizer.h>
45 : #include <utils/common/StringUtils.h>
46 : #include <utils/options/OptionsCont.h>
47 : #include <utils/vehicle/SUMOVehicleParserHelper.h>
48 :
49 : // ===========================================================================
50 : // static members
51 : // ===========================================================================
52 :
53 :
54 : // ===========================================================================
55 : // method definitions
56 : // ===========================================================================
57 80954 : MSRouteHandler::MSRouteHandler(const std::string& file, bool addVehiclesDirectly) :
58 : SUMORouteHandler(file, addVehiclesDirectly ? "" : "routes", true),
59 161908 : MapMatcher(OptionsCont::getOptions().getBool("mapmatch.junctions"),
60 161908 : OptionsCont::getOptions().getBool("mapmatch.taz"),
61 80954 : OptionsCont::getOptions().getFloat("mapmatch.distance"),
62 : MsgHandler::getErrorInstance()),
63 80954 : myActiveRouteRepeat(0),
64 80954 : myActiveRoutePeriod(0),
65 80954 : myActiveRoutePermanent(false),
66 80954 : myActiveType(ObjectTypeEnum::UNDEFINED),
67 80954 : myHaveVia(false),
68 80954 : myActiveTransportablePlan(nullptr),
69 80954 : myAddVehiclesDirectly(addVehiclesDirectly),
70 80954 : myCurrentVTypeDistribution(nullptr),
71 80954 : myCurrentRouteDistribution(nullptr),
72 80954 : myAmLoadingState(false),
73 80954 : myScaleSuffix(OptionsCont::getOptions().getString("scale-suffix")),
74 80954 : myReplayRerouting(OptionsCont::getOptions().getBool("replay-rerouting")),
75 359858 : myStartTriggeredInFlow(false) {
76 80954 : myActiveRoute.reserve(100);
77 80954 : }
78 :
79 :
80 196603 : MSRouteHandler::~MSRouteHandler() {}
81 :
82 :
83 : void
84 761 : MSRouteHandler::deleteActivePlanAndVehicleParameter() {
85 761 : if (myActiveTransportablePlan != nullptr) {
86 474 : for (MSStage* const s : *myActiveTransportablePlan) {
87 285 : delete s;
88 : }
89 189 : delete myActiveTransportablePlan;
90 : }
91 761 : delete myVehicleParameter;
92 761 : resetActivePlanAndVehicleParameter();
93 761 : }
94 :
95 :
96 : void
97 46768 : MSRouteHandler::resetActivePlanAndVehicleParameter() {
98 46768 : myVehicleParameter = nullptr;
99 46768 : myActiveTransportablePlan = nullptr;
100 46768 : myActiveType = ObjectTypeEnum::UNDEFINED;
101 46768 : }
102 :
103 :
104 : void
105 84336 : MSRouteHandler::parseFromViaTo(SumoXMLTag tag, const SUMOSAXAttributes& attrs) {
106 84336 : const std::string element = toString(tag);
107 : myActiveRoute.clear();
108 84336 : bool useTaz = OptionsCont::getOptions().getBool("with-taz");
109 84336 : if (useTaz && !myVehicleParameter->wasSet(VEHPARS_FROM_TAZ_SET) && !myVehicleParameter->wasSet(VEHPARS_TO_TAZ_SET)) {
110 0 : WRITE_WARNINGF(TL("Taz usage was requested but no taz present in % '%'!"), element, myVehicleParameter->id);
111 : useTaz = false;
112 : }
113 84336 : bool ok = true;
114 84390 : const std::string rid = "for " + element + " '" + myVehicleParameter->id + "'";
115 : SUMOVehicleClass vClass = SVC_PASSENGER;
116 84336 : auto& vc = MSNet::getInstance()->getVehicleControl();
117 84336 : if (!vc.getVTypeDistribution(myVehicleParameter->vtypeid)) {
118 83493 : MSVehicleType* const type = vc.getVType(myVehicleParameter->vtypeid, &myParsingRNG);
119 83493 : if (type != nullptr) {
120 83493 : vClass = type->getParameter().vehicleClass;
121 : }
122 : }
123 : // from-attributes
124 103163 : if ((useTaz || (!attrs.hasAttribute(SUMO_ATTR_FROM) && !attrs.hasAttribute(SUMO_ATTR_FROMXY) && !attrs.hasAttribute(SUMO_ATTR_FROMLONLAT))) &&
125 36283 : (attrs.hasAttribute(SUMO_ATTR_FROM_TAZ) || attrs.hasAttribute(SUMO_ATTR_FROM_JUNCTION))) {
126 2396 : const bool useJunction = attrs.hasAttribute(SUMO_ATTR_FROM_JUNCTION);
127 3767 : const std::string tazType = useJunction ? "junction" : "taz";
128 3767 : const std::string tazID = attrs.get<std::string>(useJunction ? SUMO_ATTR_FROM_JUNCTION : SUMO_ATTR_FROM_TAZ, myVehicleParameter->id.c_str(), ok);
129 2428 : const MSEdge* fromTaz = MSEdge::dictionary(tazID + "-source");
130 2396 : if (fromTaz == nullptr) {
131 24 : throw ProcessError("Source " + tazType + " '" + tazID + "' not known " + rid + "!"
132 72 : + (useJunction ? JUNCTION_TAZ_MISSING_HELP : ""));
133 2372 : } else if (fromTaz->getNumSuccessors() == 0 && tag != SUMO_TAG_PERSON) {
134 24 : throw ProcessError("Source " + tazType + " '" + tazID + "' has no outgoing edges " + rid + "!");
135 : } else {
136 2364 : myActiveRoute.push_back(fromTaz);
137 : }
138 81940 : } else if (attrs.hasAttribute(SUMO_ATTR_FROMXY)) {
139 200 : parseGeoEdges(attrs.get<PositionVector>(SUMO_ATTR_FROMXY, myVehicleParameter->id.c_str(), ok), false, vClass, myActiveRoute, rid, true, ok);
140 200 : if (myMapMatchTAZ && ok) {
141 8 : myVehicleParameter->fromTaz = myActiveRoute.back()->getID();
142 8 : myVehicleParameter->parametersSet |= VEHPARS_FROM_TAZ_SET;
143 : }
144 81740 : } else if (attrs.hasAttribute(SUMO_ATTR_FROMLONLAT)) {
145 32 : parseGeoEdges(attrs.get<PositionVector>(SUMO_ATTR_FROMLONLAT, myVehicleParameter->id.c_str(), ok), true, vClass, myActiveRoute, rid, true, ok);
146 32 : if (myMapMatchTAZ && ok) {
147 0 : myVehicleParameter->fromTaz = myActiveRoute.back()->getID();
148 0 : myVehicleParameter->parametersSet |= VEHPARS_FROM_TAZ_SET;
149 : }
150 : } else {
151 163470 : MSEdge::parseEdgesList(attrs.getOpt<std::string>(SUMO_ATTR_FROM, myVehicleParameter->id.c_str(), ok),
152 81708 : myActiveRoute, rid);
153 : }
154 84304 : if (!ok) {
155 16 : throw ProcessError();
156 : }
157 :
158 : // via-attributes
159 84288 : if (!attrs.hasAttribute(SUMO_ATTR_VIA) && !attrs.hasAttribute(SUMO_ATTR_ROUTE)) {
160 74411 : myInsertStopEdgesAt = (int)myActiveRoute.size();
161 : }
162 : ConstMSEdgeVector viaEdges;
163 84288 : if (attrs.hasAttribute(SUMO_ATTR_VIAXY)) {
164 64 : parseGeoEdges(attrs.get<PositionVector>(SUMO_ATTR_VIAXY, myVehicleParameter->id.c_str(), ok), false, vClass, viaEdges, rid, false, ok);
165 84224 : } else if (attrs.hasAttribute(SUMO_ATTR_VIALONLAT)) {
166 8 : parseGeoEdges(attrs.get<PositionVector>(SUMO_ATTR_VIALONLAT, myVehicleParameter->id.c_str(), ok), true, vClass, viaEdges, rid, false, ok);
167 84216 : } else if (attrs.hasAttribute(SUMO_ATTR_VIAJUNCTIONS)) {
168 48 : for (std::string junctionID : attrs.get<std::vector<std::string> >(SUMO_ATTR_VIAJUNCTIONS, myVehicleParameter->id.c_str(), ok)) {
169 32 : const MSEdge* viaSink = MSEdge::dictionary(junctionID + "-sink");
170 32 : if (viaSink == nullptr) {
171 0 : throw ProcessError("Junction-taz '" + junctionID + "' not found." + JUNCTION_TAZ_MISSING_HELP);
172 : } else {
173 32 : viaEdges.push_back(viaSink);
174 : }
175 16 : }
176 : } else {
177 168394 : MSEdge::parseEdgesList(attrs.getOpt<std::string>(SUMO_ATTR_VIA, myVehicleParameter->id.c_str(), ok),
178 : viaEdges, rid);
179 : }
180 84282 : if (!viaEdges.empty()) {
181 927 : myHaveVia = true;
182 : }
183 90462 : for (const MSEdge* e : viaEdges) {
184 6180 : myActiveRoute.push_back(e);
185 6180 : myVehicleParameter->via.push_back(e->getID());
186 : }
187 :
188 : // to-attributes
189 104877 : if ((useTaz || (!attrs.hasAttribute(SUMO_ATTR_TO) && !attrs.hasAttribute(SUMO_ATTR_TOXY) && !attrs.hasAttribute(SUMO_ATTR_TOLONLAT))) &&
190 39421 : (attrs.hasAttribute(SUMO_ATTR_TO_TAZ) || attrs.hasAttribute(SUMO_ATTR_TO_JUNCTION))) {
191 3196 : const bool useJunction = attrs.hasAttribute(SUMO_ATTR_TO_JUNCTION);
192 4965 : const std::string tazType = useJunction ? "junction" : "taz";
193 4965 : const std::string tazID = attrs.get<std::string>(useJunction ? SUMO_ATTR_TO_JUNCTION : SUMO_ATTR_TO_TAZ, myVehicleParameter->id.c_str(), ok, true);
194 3196 : const MSEdge* toTaz = MSEdge::dictionary(tazID + "-sink");
195 3196 : if (toTaz == nullptr) {
196 0 : throw ProcessError("Sink " + tazType + " '" + tazID + "' not known " + rid + "!"
197 0 : + (useJunction ? JUNCTION_TAZ_MISSING_HELP : ""));
198 3196 : } else if (toTaz->getNumPredecessors() == 0 && tag != SUMO_TAG_PERSON) {
199 0 : throw ProcessError("Sink " + tazType + " '" + tazID + "' has no incoming edges " + rid + "!");
200 : } else {
201 3196 : myActiveRoute.push_back(toTaz);
202 : }
203 81086 : } else if (attrs.hasAttribute(SUMO_ATTR_TOXY)) {
204 200 : parseGeoEdges(attrs.get<PositionVector>(SUMO_ATTR_TOXY, myVehicleParameter->id.c_str(), ok, true), false, vClass, myActiveRoute, rid, false, ok);
205 200 : if (myMapMatchTAZ && ok) {
206 8 : myVehicleParameter->toTaz = myActiveRoute.back()->getID();
207 8 : myVehicleParameter->parametersSet |= VEHPARS_TO_TAZ_SET;
208 : }
209 80886 : } else if (attrs.hasAttribute(SUMO_ATTR_TOLONLAT)) {
210 40 : parseGeoEdges(attrs.get<PositionVector>(SUMO_ATTR_TOLONLAT, myVehicleParameter->id.c_str(), ok, true), true, vClass, myActiveRoute, rid, false, ok);
211 40 : if (myMapMatchTAZ && ok) {
212 0 : myVehicleParameter->toTaz = myActiveRoute.back()->getID();
213 0 : myVehicleParameter->parametersSet |= VEHPARS_TO_TAZ_SET;
214 : }
215 : } else {
216 161692 : MSEdge::parseEdgesList(attrs.getOpt<std::string>(SUMO_ATTR_TO, myVehicleParameter->id.c_str(), ok, "", true),
217 80846 : myActiveRoute, rid);
218 : }
219 84282 : myActiveRouteID = "!" + myVehicleParameter->id;
220 84282 : if (myVehicleParameter->routeid == "") {
221 : myVehicleParameter->routeid = myActiveRouteID;
222 : }
223 168570 : }
224 :
225 :
226 : void
227 11234253 : MSRouteHandler::myStartElement(int element, const SUMOSAXAttributes& attrs) {
228 : try {
229 53595 : if (myActiveTransportablePlan != nullptr && myActiveTransportablePlan->empty() && myVehicleParameter->departProcedure == DepartDefinition::TRIGGERED
230 11235193 : && element != SUMO_TAG_RIDE && element != SUMO_TAG_TRANSPORT && element != SUMO_TAG_PARAM) {
231 0 : const std::string mode = myActiveType == ObjectTypeEnum::PERSON ? "ride" : "transport";
232 0 : throw ProcessError("Triggered departure for " + myActiveTypeName + " '" + myVehicleParameter->id + "' requires starting with a " + mode + ".");
233 : }
234 11234253 : if (myVehicleParameter == nullptr) {
235 11016237 : myActiveTypeName = toString((SumoXMLTag)element);
236 11016237 : myHaveVia = false;
237 : }
238 11234253 : SUMORouteHandler::myStartElement(element, attrs);
239 11233917 : switch (element) {
240 1874 : case SUMO_TAG_PERSONFLOW:
241 1874 : addTransportable(attrs, true);
242 : break;
243 312 : case SUMO_TAG_CONTAINERFLOW:
244 312 : addTransportable(attrs, false);
245 : break;
246 20015 : case SUMO_TAG_FLOW:
247 20015 : if (myVehicleParameter) {
248 20015 : parseFromViaTo((SumoXMLTag)element, attrs);
249 : }
250 : break;
251 55288 : case SUMO_TAG_TRIP:
252 55288 : parseFromViaTo((SumoXMLTag)element, attrs);
253 : break;
254 : default:
255 : break;
256 : }
257 382 : } catch (ProcessError&) {
258 382 : deleteActivePlanAndVehicleParameter();
259 382 : throw;
260 382 : }
261 11233871 : }
262 :
263 :
264 : void
265 529 : MSRouteHandler::openVehicleTypeDistribution(const SUMOSAXAttributes& attrs) {
266 529 : bool ok = true;
267 529 : myCurrentVTypeDistributionID = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
268 529 : if (ok) {
269 529 : myCurrentVTypeDistribution = new RandomDistributor<MSVehicleType*>();
270 529 : if (attrs.hasAttribute(SUMO_ATTR_VTYPES)) {
271 : std::vector<double> probs;
272 106 : if (attrs.hasAttribute(SUMO_ATTR_PROBS)) {
273 87 : StringTokenizer st(attrs.get<std::string>(SUMO_ATTR_PROBS, myCurrentVTypeDistributionID.c_str(), ok));
274 273 : while (st.hasNext()) {
275 372 : probs.push_back(StringUtils::toDoubleSecure(st.next(), 1.0));
276 : }
277 87 : }
278 106 : const std::string vTypes = attrs.get<std::string>(SUMO_ATTR_VTYPES, myCurrentVTypeDistributionID.c_str(), ok);
279 106 : StringTokenizer st(vTypes);
280 106 : int probIndex = 0;
281 330 : while (st.hasNext()) {
282 224 : const std::string& vtypeID = st.next();
283 224 : const RandomDistributor<MSVehicleType*>* const dist = MSNet::getInstance()->getVehicleControl().getVTypeDistribution(vtypeID);
284 224 : if (dist != nullptr) {
285 14 : const double distProb = ((int)probs.size() > probIndex ? probs[probIndex] : 1.) / dist->getOverallProb();
286 : std::vector<double>::const_iterator probIt = dist->getProbs().begin();
287 42 : for (MSVehicleType* const type : dist->getVals()) {
288 28 : myCurrentVTypeDistribution->add(type, distProb * *probIt);
289 : probIt++;
290 : }
291 : } else {
292 210 : MSVehicleType* const type = MSNet::getInstance()->getVehicleControl().getVType(vtypeID, &myParsingRNG);
293 210 : if (type == nullptr) {
294 0 : throw ProcessError("Unknown vtype '" + vtypeID + "' in distribution '" + myCurrentVTypeDistributionID + "'.");
295 : }
296 210 : const double prob = ((int)probs.size() > probIndex ? probs[probIndex] : type->getDefaultProbability());
297 210 : myCurrentVTypeDistribution->add(type, prob);
298 : }
299 224 : probIndex++;
300 : }
301 106 : if (probs.size() > 0 && probIndex != (int)probs.size()) {
302 0 : WRITE_WARNING("Got " + toString(probs.size()) + " probabilities for " + toString(probIndex) +
303 : " types in vTypeDistribution '" + myCurrentVTypeDistributionID + "'");
304 : }
305 212 : }
306 : }
307 529 : }
308 :
309 :
310 : void
311 529 : MSRouteHandler::closeVehicleTypeDistribution() {
312 529 : if (myCurrentVTypeDistribution != nullptr) {
313 529 : if (MSGlobals::gStateLoaded && MSNet::getInstance()->getVehicleControl().hasVTypeDistribution(myCurrentVTypeDistributionID)) {
314 28 : myCurrentVTypeDistribution = nullptr;
315 : delete myCurrentVTypeDistribution;
316 28 : return;
317 : }
318 501 : if (myCurrentVTypeDistribution->getOverallProb() == 0) {
319 0 : delete myCurrentVTypeDistribution;
320 0 : throw ProcessError(TLF("Vehicle type distribution '%' is empty.", myCurrentVTypeDistributionID));
321 : }
322 501 : if (!MSNet::getInstance()->getVehicleControl().addVTypeDistribution(myCurrentVTypeDistributionID, myCurrentVTypeDistribution)) {
323 0 : delete myCurrentVTypeDistribution;
324 0 : throw ProcessError(TLF("Another vehicle type (or distribution) with the id '%' exists.", myCurrentVTypeDistributionID));
325 : }
326 501 : myCurrentVTypeDistribution = nullptr;
327 : }
328 : }
329 :
330 :
331 : void
332 154016 : MSRouteHandler::openRoute(const SUMOSAXAttributes& attrs) {
333 : myActiveRoute.clear();
334 154016 : myInsertStopEdgesAt = -1;
335 : // check whether the id is really necessary
336 : std::string rid;
337 154016 : if (myCurrentRouteDistribution != nullptr) {
338 6714 : myActiveRouteID = myCurrentRouteDistributionID + "#" + toString(myCurrentRouteDistribution->getProbs().size()); // !!! document this
339 10071 : rid = "distribution '" + myCurrentRouteDistributionID + "'";
340 150659 : } else if (myVehicleParameter != nullptr) {
341 : // ok, a vehicle is wrapping the route,
342 : // we may use this vehicle's id as default
343 112308 : myActiveRouteID = "!" + myVehicleParameter->id; // !!! document this
344 112308 : if (attrs.hasAttribute(SUMO_ATTR_ID)) {
345 24 : WRITE_WARNINGF(TL("Ids of internal routes are ignored (vehicle '%')."), myVehicleParameter->id);
346 : }
347 : } else {
348 38351 : bool ok = true;
349 38351 : myActiveRouteID = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok, false);
350 38351 : if (!ok) {
351 0 : return;
352 : }
353 115053 : rid = "'" + myActiveRouteID + "'";
354 : }
355 154016 : if (myVehicleParameter != nullptr) { // have to do this here for nested route distributions
356 345681 : rid = "for vehicle '" + myVehicleParameter->id + "'";
357 : }
358 154016 : bool ok = true;
359 154016 : if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
360 307822 : MSEdge::parseEdgesList(attrs.get<std::string>(SUMO_ATTR_EDGES, myActiveRouteID.c_str(), ok), myActiveRoute, rid);
361 : }
362 307956 : myActiveRouteRefID = attrs.getOpt<std::string>(SUMO_ATTR_REFID, myActiveRouteID.c_str(), ok, "");
363 154040 : if (myActiveRouteRefID != "" && MSRoute::dictionary(myActiveRouteRefID, &myParsingRNG) == nullptr) {
364 42 : throw ProcessError(TLF("Invalid reference to route '%' in route %.", myActiveRouteRefID, rid));
365 : }
366 153964 : myActiveRouteProbability = attrs.getOpt<double>(SUMO_ATTR_PROB, myActiveRouteID.c_str(), ok, DEFAULT_VEH_PROB);
367 153964 : myActiveRouteColor = attrs.hasAttribute(SUMO_ATTR_COLOR) ? new RGBColor(attrs.get<RGBColor>(SUMO_ATTR_COLOR, myActiveRouteID.c_str(), ok)) : nullptr;
368 153964 : myActiveRouteRepeat = attrs.getOpt<int>(SUMO_ATTR_REPEAT, myActiveRouteID.c_str(), ok, 0);
369 153964 : myActiveRouteReplacedAtTime = attrs.getOptSUMOTimeReporting(SUMO_ATTR_REPLACED_AT_TIME, myActiveRouteID.c_str(), ok, -1);
370 153964 : myActiveRouteReplacedIndex = attrs.getOpt<int>(SUMO_ATTR_REPLACED_ON_INDEX, myActiveRouteID.c_str(), ok, 0);
371 153964 : myActiveRoutePeriod = attrs.getOptSUMOTimeReporting(SUMO_ATTR_CYCLETIME, myActiveRouteID.c_str(), ok,
372 : // handle obsolete attribute name
373 : attrs.getOptSUMOTimeReporting(SUMO_ATTR_PERIOD, myActiveRouteID.c_str(), ok, 0));
374 153964 : myActiveRoutePermanent = attrs.getOpt<bool>(SUMO_ATTR_STATE, myActiveRouteID.c_str(), ok, false);
375 153964 : if (attrs.hasAttribute(SUMO_ATTR_PERIOD)) {
376 0 : WRITE_WARNING(TL("Attribute 'period' is deprecated for route. Use 'cycleTime' instead."));
377 : }
378 153964 : myCurrentCosts = attrs.getOpt<double>(SUMO_ATTR_COST, myActiveRouteID.c_str(), ok, -1);
379 153964 : if (ok && myCurrentCosts != -1 && myCurrentCosts < 0) {
380 52 : WRITE_WARNING(TLF("Invalid cost for route '%'.", myActiveRouteID));
381 : }
382 : }
383 :
384 :
385 : void
386 10975 : MSRouteHandler::openFlow(const SUMOSAXAttributes& /*attrs*/) {
387 : // Currently unused
388 10975 : }
389 :
390 :
391 : void
392 9040 : MSRouteHandler::openRouteFlow(const SUMOSAXAttributes& /*attrs*/) {
393 : // Currently unused
394 9040 : }
395 :
396 :
397 : void
398 55288 : MSRouteHandler::openTrip(const SUMOSAXAttributes& /*attrs*/) {
399 : // Currently unsued
400 55288 : }
401 :
402 :
403 : void
404 215937 : MSRouteHandler::closeRoute(const bool mayBeDisconnected) {
405 215937 : std::string type = "vehicle";
406 215937 : if (mayBeDisconnected) {
407 61973 : if (myVehicleParameter->repetitionNumber >= 0) {
408 : type = "flow";
409 : } else {
410 : type = "trip";
411 : }
412 : }
413 :
414 : try {
415 215937 : const bool mustReroute = myActiveRoute.size() == 0 && myActiveRouteStops.size() != 0;
416 : if (mustReroute) {
417 : // implicit route from stops
418 120 : for (const SUMOVehicleParameter::Stop& stop : myActiveRouteStops) {
419 96 : myActiveRoute.push_back(MSEdge::dictionary(stop.edge));
420 : }
421 : }
422 215937 : if (myActiveRoute.size() == 0) {
423 60 : delete myActiveRouteColor;
424 60 : myActiveRouteColor = nullptr;
425 60 : if (myActiveRouteRefID != "" && myCurrentRouteDistribution != nullptr) {
426 48 : ConstMSRoutePtr route = MSRoute::dictionary(myActiveRouteRefID, &myParsingRNG);
427 48 : if (route != nullptr) {
428 96 : myCurrentRouteDistribution->add(route, myActiveRouteProbability);
429 : }
430 48 : myActiveRouteID = "";
431 : myActiveRouteRefID = "";
432 : return;
433 : }
434 12 : if (myVehicleParameter != nullptr) {
435 18 : throw ProcessError("The route for " + type + " '" + myVehicleParameter->id + "' has no edges.");
436 : } else {
437 18 : throw ProcessError(TLF("Route '%' has no edges.", myActiveRouteID));
438 : }
439 : }
440 215877 : if (myActiveRoute.size() == 1 && myActiveRoute.front()->isTazConnector()) {
441 0 : throw ProcessError("The routing information for " + type + " '" + myVehicleParameter->id + "' is insufficient.");
442 : }
443 215877 : if (myActiveRouteRepeat > 0) {
444 : // duplicate route
445 977 : ConstMSEdgeVector tmpEdges = myActiveRoute;
446 977 : auto tmpStops = myActiveRouteStops;
447 :
448 977 : if (MSGlobals::gCheckRoutes) {
449 : SUMOVehicleClass vClass = SVC_IGNORING;
450 977 : std::string errSuffix = ".";
451 977 : if (myVehicleParameter != nullptr) {
452 742 : MSVehicleControl& vehControl = MSNet::getInstance()->getVehicleControl();
453 742 : MSVehicleType* vtype = vehControl.getVType(myVehicleParameter->vtypeid, &myParsingRNG);
454 742 : if (vtype != nullptr) {
455 : vClass = vtype->getVehicleClass();
456 2976 : errSuffix = TLF(" for vehicle '%' with vClass %.", myVehicleParameter->id, getVehicleClassNames(vClass));
457 : }
458 : }
459 977 : if (myActiveRoute.size() > 0 && !myActiveRoute.back()->isConnectedTo(*myActiveRoute.front(), vClass)) {
460 16 : if (tmpStops.size() == 0 || tmpStops.back().jump < 0) {
461 32 : throw ProcessError(TLF("Disconnected route '%' when repeating. Last edge '%' is not connected to first edge '%'%",
462 24 : myActiveRouteID, myActiveRoute.back()->getID(), myActiveRoute.front()->getID(), errSuffix));
463 : }
464 : }
465 : }
466 10362 : for (int i = 0; i < myActiveRouteRepeat; i++) {
467 9401 : myActiveRoute.insert(myActiveRoute.begin(), tmpEdges.begin(), tmpEdges.end());
468 11321 : for (SUMOVehicleParameter::Stop stop : tmpStops) {
469 1928 : if (stop.until > 0) {
470 1368 : if (myActiveRoutePeriod <= 0) {
471 8 : const std::string description = myVehicleParameter != nullptr
472 16 : ? "for " + type + " '" + myVehicleParameter->id + "'"
473 24 : : "'" + myActiveRouteID + "'";
474 24 : throw ProcessError(TLF("Cannot repeat stops with 'until' in route % because no cycleTime is defined.", description));
475 : }
476 1360 : stop.until += myActiveRoutePeriod * (i + 1);
477 : }
478 1920 : if (stop.arrival > 0) {
479 25 : if (myActiveRoutePeriod <= 0) {
480 0 : const std::string description = myVehicleParameter != nullptr
481 0 : ? "for " + type + " '" + myVehicleParameter->id + "'"
482 0 : : "'" + myActiveRouteID + "'";
483 0 : throw ProcessError(TLF("Cannot repeat stops with 'arrival' in route % because no cycleTime is defined.", description));
484 : }
485 25 : stop.arrival += myActiveRoutePeriod * (i + 1);
486 : }
487 1920 : stop.index = STOP_INDEX_REPEAT;
488 1920 : myActiveRouteStops.push_back(stop);
489 1928 : }
490 : }
491 961 : if (myActiveRouteStops.size() > 0) {
492 : // never jump on the last stop of a repeating route
493 137 : myActiveRouteStops.back().jump = -1;
494 : }
495 993 : }
496 215861 : MSRoute* const route = new MSRoute(myActiveRouteID, myActiveRoute,
497 431722 : myAmLoadingState ? myActiveRoutePermanent : myVehicleParameter == nullptr,
498 215861 : myActiveRouteColor, myActiveRouteStops,
499 215861 : myActiveRouteReplacedAtTime, myActiveRouteReplacedIndex);
500 215861 : route->setPeriod(myActiveRoutePeriod);
501 215861 : route->setCosts(myCurrentCosts);
502 : route->setReroute(mustReroute);
503 : myActiveRoute.clear();
504 : ConstMSRoutePtr constRoute = std::shared_ptr<const MSRoute>(route);
505 431722 : if (!MSRoute::dictionary(myActiveRouteID, constRoute)) {
506 425 : if (!MSGlobals::gStateLoaded) {
507 26 : if (myVehicleParameter != nullptr) {
508 20 : if (MSNet::getInstance()->getVehicleControl().getVehicle(myVehicleParameter->id) == nullptr) {
509 18 : throw ProcessError("Another route for " + type + " '" + myVehicleParameter->id + "' exists.");
510 : } else {
511 42 : throw ProcessError(TLF("A vehicle with id '%' already exists.", myVehicleParameter->id));
512 : }
513 : } else {
514 18 : throw ProcessError(TLF("Another route (or distribution) with the id '%' exists.", myActiveRouteID));
515 : }
516 : }
517 : } else {
518 215436 : if (myCurrentRouteDistribution != nullptr) {
519 6608 : myCurrentRouteDistribution->add(constRoute, myActiveRouteProbability);
520 : }
521 : }
522 : myActiveRouteID = "";
523 215835 : myActiveRouteColor = nullptr;
524 : myActiveRouteStops.clear();
525 215835 : myActiveRouteRepeat = 0;
526 54 : } catch (ProcessError&) {
527 54 : deleteActivePlanAndVehicleParameter();
528 54 : throw;
529 54 : }
530 : }
531 :
532 :
533 : void
534 1285 : MSRouteHandler::openRouteDistribution(const SUMOSAXAttributes& attrs) {
535 : // check whether the id is really necessary
536 1285 : if (myVehicleParameter != nullptr) {
537 : // ok, a vehicle is wrapping the route,
538 : // we may use this vehicle's id as default
539 1099 : myCurrentRouteDistributionID = "!" + myVehicleParameter->id; // !!! document this
540 : // we have to record this or we cannot remove the distribution later
541 1099 : myVehicleParameter->routeid = myCurrentRouteDistributionID;
542 : } else {
543 186 : bool ok = true;
544 186 : myCurrentRouteDistributionID = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
545 186 : if (!ok) {
546 0 : return;
547 : }
548 : }
549 1285 : myCurrentRouteDistribution = new RandomDistributor<ConstMSRoutePtr>();
550 : std::vector<double> probs;
551 1285 : if (attrs.hasAttribute(SUMO_ATTR_PROBS)) {
552 15 : bool ok = true;
553 15 : StringTokenizer st(attrs.get<std::string>(SUMO_ATTR_PROBS, myCurrentRouteDistributionID.c_str(), ok));
554 45 : while (st.hasNext()) {
555 60 : probs.push_back(StringUtils::toDoubleSecure(st.next(), 1.0));
556 : }
557 15 : }
558 1285 : if (attrs.hasAttribute(SUMO_ATTR_ROUTES)) {
559 22 : bool ok = true;
560 22 : StringTokenizer st(attrs.get<std::string>(SUMO_ATTR_ROUTES, myCurrentRouteDistributionID.c_str(), ok));
561 22 : int probIndex = 0;
562 66 : while (st.hasNext()) {
563 44 : std::string routeID = st.next();
564 44 : ConstMSRoutePtr route = MSRoute::dictionary(routeID, &myParsingRNG);
565 44 : if (route == nullptr) {
566 0 : throw ProcessError("Unknown route '" + routeID + "' in distribution '" + myCurrentRouteDistributionID + "'.");
567 : }
568 44 : const double prob = ((int)probs.size() > probIndex ? probs[probIndex] : 1.0);
569 44 : myCurrentRouteDistribution->add(route, prob, false);
570 44 : probIndex++;
571 : }
572 22 : if (probs.size() > 0 && probIndex != (int)probs.size()) {
573 0 : WRITE_WARNING("Got " + toString(probs.size()) + " probabilities for " + toString(probIndex) +
574 : " routes in routeDistribution '" + myCurrentRouteDistributionID + "'");
575 : }
576 22 : }
577 1285 : }
578 :
579 :
580 : void
581 1271 : MSRouteHandler::closeRouteDistribution() {
582 1271 : if (myCurrentRouteDistribution != nullptr) {
583 1271 : const bool haveSameID = MSRoute::dictionary(myCurrentRouteDistributionID, &myParsingRNG) != nullptr;
584 1271 : if (MSGlobals::gStateLoaded && haveSameID) {
585 4 : delete myCurrentRouteDistribution;
586 2 : myCurrentRouteDistribution = nullptr;
587 2 : return;
588 : }
589 1259 : if (haveSameID) {
590 0 : delete myCurrentRouteDistribution;
591 0 : throw ProcessError(TLF("Another route (or distribution) with the id '%' exists.", myCurrentRouteDistributionID));
592 : }
593 1269 : if (myCurrentRouteDistribution->getOverallProb() == 0) {
594 2 : delete myCurrentRouteDistribution;
595 6 : throw ProcessError(TLF("Route distribution '%' is empty.", myCurrentRouteDistributionID));
596 : }
597 1267 : MSRoute::dictionary(myCurrentRouteDistributionID, myCurrentRouteDistribution, myVehicleParameter == nullptr);
598 1267 : myCurrentRouteDistribution = nullptr;
599 : }
600 : }
601 :
602 :
603 : void
604 320715 : MSRouteHandler::closeVehicle() {
605 : // get nested route
606 320715 : const std::string embeddedRouteID = "!" + myVehicleParameter->id;
607 : ConstMSRoutePtr route = nullptr;
608 320715 : if (myReplayRerouting) {
609 2832 : RandomDistributor<ConstMSRoutePtr>* rDist = MSRoute::distDictionary(embeddedRouteID);
610 2832 : if (rDist != nullptr && rDist->getVals().size() > 0) {
611 : route = rDist->getVals().front();
612 : }
613 : }
614 320715 : if (route == nullptr) {
615 639388 : route = MSRoute::dictionary(embeddedRouteID, &myParsingRNG);
616 : }
617 320715 : MSVehicleControl& vehControl = MSNet::getInstance()->getVehicleControl();
618 320715 : if (myVehicleParameter->departProcedure == DepartDefinition::GIVEN) {
619 : // let's check whether this vehicle had to depart before the simulation starts
620 320119 : if (!(myAddVehiclesDirectly || checkLastDepart()) || (myVehicleParameter->depart <= MSNet::getInstance()->getStateLoaderTime() && !myAmLoadingState)) {
621 1156 : mySkippedVehicles.insert(myVehicleParameter->id);
622 : return;
623 : }
624 : }
625 :
626 : // get the vehicle's type
627 : MSVehicleType* vtype = nullptr;
628 :
629 : try {
630 319559 : if (myVehicleParameter->vtypeid != "") {
631 319559 : vtype = vehControl.getVType(myVehicleParameter->vtypeid, &myParsingRNG);
632 319559 : if (vtype == nullptr) {
633 0 : throw ProcessError("The vehicle type '" + myVehicleParameter->vtypeid + "' for vehicle '" + myVehicleParameter->id + "' is not known.");
634 : }
635 319559 : if (vtype->getVehicleClass() == SVC_PEDESTRIAN) {
636 90 : WRITE_WARNINGF(TL("Vehicle type '%' with vClass=pedestrian should only be used for persons and not for vehicle '%'."), vtype->getID(), myVehicleParameter->id);
637 : }
638 : } else {
639 : // there should be one (at least the default one)
640 0 : vtype = vehControl.getVType(DEFAULT_VTYPE_ID, &myParsingRNG);
641 : }
642 319559 : if (myVehicleParameter->wasSet(VEHPARS_ROUTE_SET)) {
643 : // if the route id was given, prefer that one
644 155939 : if (route != nullptr && !myAmLoadingState) {
645 18 : WRITE_WARNINGF(TL("Ignoring child element 'route' for vehicle '%' because attribute 'route' is set."), myVehicleParameter->id);
646 : }
647 311878 : route = MSRoute::dictionary(myVehicleParameter->routeid, &myParsingRNG);
648 : }
649 319559 : if (route == nullptr) {
650 : // nothing found? -> error
651 21 : if (myVehicleParameter->routeid != "") {
652 45 : throw ProcessError("The route '" + myVehicleParameter->routeid + "' for vehicle '" + myVehicleParameter->id + "' is not known.");
653 : } else {
654 18 : throw ProcessError(TLF("Vehicle '%' has no route.", myVehicleParameter->id));
655 : }
656 : }
657 319538 : myActiveRouteID = "";
658 :
659 21 : } catch (ProcessError&) {
660 21 : deleteActivePlanAndVehicleParameter();
661 21 : throw;
662 21 : }
663 319538 : if (route->mustReroute()) {
664 16 : myVehicleParameter->parametersSet |= VEHPARS_FORCE_REROUTE;
665 16 : if (myVehicleParameter->stops.size() > 0) {
666 16 : route = addVehicleStopsToImplicitRoute(route, false);
667 : }
668 : }
669 319538 : if (myVehicleParameter->departEdgeProcedure != RouteIndexDefinition::DEFAULT) {
670 206 : if ((myVehicleParameter->parametersSet & VEHPARS_FORCE_REROUTE) == 0 &&
671 416 : myVehicleParameter->departEdgeProcedure == RouteIndexDefinition::GIVEN &&
672 182 : myVehicleParameter->departEdge >= (int)route->getEdges().size()) {
673 14 : throw ProcessError("Vehicle '" + myVehicleParameter->id + "' has invalid departEdge index "
674 35 : + toString(myVehicleParameter->departEdge) + " for route with " + toString(route->getEdges().size()) + " edges.");
675 : }
676 : }
677 319531 : if (myVehicleParameter->arrivalEdgeProcedure != RouteIndexDefinition::DEFAULT) {
678 54 : if ((myVehicleParameter->parametersSet & VEHPARS_FORCE_REROUTE) == 0 &&
679 115 : myVehicleParameter->arrivalEdgeProcedure == RouteIndexDefinition::GIVEN &&
680 47 : myVehicleParameter->arrivalEdge >= (int)route->getEdges().size()) {
681 14 : throw ProcessError("Vehicle '" + myVehicleParameter->id + "' has invalid arrivalEdge index "
682 35 : + toString(myVehicleParameter->arrivalEdge) + " for route with " + toString(route->getEdges().size()) + " edges.");
683 : }
684 : }
685 :
686 : // try to build the vehicle
687 : SUMOVehicle* vehicle = nullptr;
688 319524 : if (vehControl.getVehicle(myVehicleParameter->id) == nullptr) {
689 319285 : MSVehicleControl::VehicleDefinitionSource source = (myAmLoadingState) ? MSVehicleControl::VehicleDefinitionSource::STATE : MSVehicleControl::VehicleDefinitionSource::ROUTEFILE;
690 : try {
691 319285 : vehicle = vehControl.buildVehicle(myVehicleParameter, route, vtype, !MSGlobals::gCheckRoutes, source, !myAmLoadingState);
692 109 : } catch (const ProcessError& e) {
693 108 : myVehicleParameter = nullptr;
694 108 : if (!MSGlobals::gCheckRoutes) {
695 15 : WRITE_WARNING(e.what());
696 15 : vehControl.deleteVehicle(vehicle, true);
697 15 : myVehicleParameter = nullptr;
698 : vehicle = nullptr;
699 : return;
700 : } else {
701 93 : throw;
702 : }
703 108 : }
704 319176 : const SUMOTime origDepart = myVehicleParameter->depart;
705 : // maybe we do not want this vehicle to be inserted due to scaling
706 319176 : int quota = myAmLoadingState ? 1 : vehControl.getQuota(vehControl.getScale() * vtype->getParameter().scale);
707 314213 : if (quota > 0) {
708 318219 : registerLastDepart();
709 318219 : myVehicleParameter->depart += MSNet::getInstance()->getInsertionControl().computeRandomDepartOffset();
710 318219 : vehControl.addVehicle(myVehicleParameter->id, vehicle);
711 318219 : if (myReplayRerouting) {
712 2832 : RandomDistributor<ConstMSRoutePtr>* rDist = MSRoute::distDictionary(embeddedRouteID);
713 2832 : if (rDist != nullptr) {
714 2770 : for (int i = 0; i < (int)rDist->getVals().size() - 1; i++) {
715 1749 : SUMOTime replacedAt = rDist->getVals()[i]->getReplacedTime();
716 1749 : auto* cmd = new Command_RouteReplacement(vehicle->getID(), rDist->getVals()[i + 1]);
717 1749 : if (replacedAt >= 0 && replacedAt == myVehicleParameter->depart) {
718 : // routing in the insertion step happens *after* insertion
719 27 : MSNet::getInstance()->getEndOfTimestepEvents()->addEvent(cmd, replacedAt);
720 : } else {
721 1722 : MSNet::getInstance()->getBeginOfTimestepEvents()->addEvent(cmd, replacedAt);
722 : }
723 : }
724 : }
725 : }
726 : int offset = 0;
727 318391 : for (int i = 1; i < quota; i++) {
728 172 : if (vehicle->getParameter().departProcedure == DepartDefinition::GIVEN || vehicle->getParameter().departProcedure == DepartDefinition::BEGIN) {
729 156 : MSNet::getInstance()->getInsertionControl().add(vehicle);
730 : }
731 172 : SUMOVehicleParameter* newPars = new SUMOVehicleParameter(*myVehicleParameter);
732 344 : newPars->id = myVehicleParameter->id + myScaleSuffix + toString(i + offset);
733 178 : while (vehControl.getVehicle(newPars->id) != nullptr) {
734 6 : offset += 1;
735 12 : newPars->id = myVehicleParameter->id + myScaleSuffix + toString(i + offset);
736 : }
737 172 : newPars->depart = origDepart + MSNet::getInstance()->getInsertionControl().computeRandomDepartOffset();
738 172 : if (vehControl.hasVTypeDistribution(myVehicleParameter->vtypeid)) {
739 : // resample type
740 27 : vtype = vehControl.getVType(myVehicleParameter->vtypeid, &myParsingRNG);
741 : }
742 313 : vehicle = vehControl.buildVehicle(newPars, route, vtype, !MSGlobals::gCheckRoutes, source, !myAmLoadingState);
743 172 : vehControl.addVehicle(newPars->id, vehicle);
744 : }
745 318219 : myVehicleParameter = nullptr;
746 : } else {
747 957 : vehControl.deleteVehicle(vehicle, true);
748 957 : myVehicleParameter = nullptr;
749 : vehicle = nullptr;
750 : }
751 : } else {
752 : // strange: another vehicle with the same id already exists
753 239 : if (!MSGlobals::gStateLoaded) {
754 : // and was not loaded while loading a simulation state
755 : // -> error
756 12 : std::string veh_id = myVehicleParameter->id;
757 12 : deleteActivePlanAndVehicleParameter();
758 24 : std::string scaleWarning = "";
759 18 : if (vehControl.getScale() * vtype->getParameter().scale > 1 && veh_id.find(myScaleSuffix) != std::string::npos) {
760 : scaleWarning = "\n (Possibly duplicate id due to using option --scale. Set option --scale-suffix to prevent this)";
761 : }
762 36 : throw ProcessError("Another vehicle with the id '" + veh_id + "' exists." + scaleWarning);
763 : } else {
764 : // ok, it seems to be loaded previously while loading a simulation state
765 : vehicle = nullptr;
766 : }
767 : }
768 : // check whether the vehicle shall be added directly to the network or
769 : // shall stay in the internal buffer
770 318219 : if (vehicle != nullptr) {
771 318219 : if (vehicle->getParameter().departProcedure == DepartDefinition::GIVEN || vehicle->getParameter().departProcedure == DepartDefinition::BEGIN) {
772 317630 : MSNet::getInstance()->getInsertionControl().add(vehicle);
773 : }
774 : }
775 : }
776 :
777 :
778 : ConstMSRoutePtr
779 16 : MSRouteHandler::addVehicleStopsToImplicitRoute(ConstMSRoutePtr route, bool isPermanent) {
780 : // the route was defined without edges and its current edges were
781 : // derived from route-stops.
782 : // We may need to add additional edges for the vehicle-stops
783 : assert(myVehicleParameter->wasSet(VEHPARS_ROUTE_SET));
784 : assert(route->getStops().size() > 0);
785 16 : ConstMSEdgeVector edges = route->getEdges();
786 48 : for (SUMOVehicleParameter::Stop stop : myVehicleParameter->stops) {
787 32 : MSEdge* stopEdge = MSEdge::dictionary(stop.edge);
788 32 : if (stop.index == 0) {
789 16 : if (edges.front() != stopEdge ||
790 0 : route->getStops().front().endPos < stop.endPos) {
791 16 : edges.insert(edges.begin(), stopEdge);
792 : }
793 16 : } else if (stop.index == STOP_INDEX_END) {
794 16 : if (edges.back() != stopEdge ||
795 0 : route->getStops().back().endPos > stop.endPos) {
796 16 : edges.push_back(stopEdge);
797 : }
798 : } else {
799 0 : WRITE_WARNINGF(TL("Could not merge vehicle stops for vehicle '%' into implicitly defined route '%'"), myVehicleParameter->id, route->getID());
800 : }
801 32 : }
802 32 : ConstMSRoutePtr newRoute = std::make_shared<MSRoute>("!" + myVehicleParameter->id, edges,
803 32 : isPermanent, new RGBColor(route->getColor()), route->getStops());
804 32 : if (!MSRoute::dictionary(newRoute->getID(), newRoute)) {
805 0 : throw ProcessError("Could not adapt implicit route for " + std::string(isPermanent ? "flow" : "vehicle") + " '" + myVehicleParameter->id + "'");
806 : }
807 16 : return newRoute;
808 16 : }
809 :
810 :
811 : void
812 43918 : MSRouteHandler::closeTransportable() {
813 : try {
814 43918 : if (myActiveTransportablePlan->size() == 0) {
815 12 : std::string error = myActiveTypeName + " '" + myVehicleParameter->id + "' has no plan.";
816 6 : error[0] = (char)::toupper((char)error[0]);
817 6 : throw ProcessError(error);
818 : }
819 : // let's check whether this transportable had to depart before the simulation starts
820 19771 : if (!(myAddVehiclesDirectly || checkLastDepart())
821 63683 : || (myVehicleParameter->depart <= MSNet::getInstance()->getStateLoaderTime() && !myAmLoadingState)) {
822 31 : deleteActivePlanAndVehicleParameter();
823 : return;
824 : }
825 : // type existence has been checked on opening
826 43881 : MSVehicleType* type = MSNet::getInstance()->getVehicleControl().getVType(myVehicleParameter->vtypeid, &myParsingRNG);
827 43881 : if (myActiveType == ObjectTypeEnum::PERSON
828 41822 : && type->getVehicleClass() != SVC_PEDESTRIAN
829 43942 : && !type->getParameter().wasSet(VTYPEPARS_VEHICLECLASS_SET)) {
830 52 : WRITE_WARNINGF(TL("Person '%' receives type '%' which implicitly uses unsuitable vClass '%'."), myVehicleParameter->id, type->getID(), toString(type->getVehicleClass()));
831 : }
832 43881 : int created = addFlowTransportable(myVehicleParameter->depart, type, myVehicleParameter->id, -1);
833 43853 : registerLastDepart();
834 43853 : if (created > 0) {
835 43829 : resetActivePlanAndVehicleParameter();
836 : } else {
837 24 : deleteActivePlanAndVehicleParameter();
838 : }
839 34 : } catch (ProcessError&) {
840 34 : deleteActivePlanAndVehicleParameter();
841 34 : throw;
842 34 : }
843 : }
844 :
845 :
846 : void
847 41859 : MSRouteHandler::closePerson() {
848 41859 : closeTransportable();
849 41833 : }
850 :
851 :
852 : void
853 2059 : MSRouteHandler::closeContainer() {
854 2059 : closeTransportable();
855 2051 : }
856 :
857 :
858 : void
859 1864 : MSRouteHandler::closePersonFlow() {
860 1864 : closeTransportableFlow();
861 1852 : }
862 :
863 :
864 : void
865 310 : MSRouteHandler::closeContainerFlow() {
866 310 : closeTransportableFlow();
867 310 : }
868 :
869 :
870 : void
871 2174 : MSRouteHandler::closeTransportableFlow() {
872 : try {
873 2174 : const std::string fid = myVehicleParameter->id;
874 2174 : if (myActiveTransportablePlan->size() == 0) {
875 0 : throw ProcessError(myActiveTypeName + "Flow '" + fid + "' has no plan.");
876 : }
877 : // let's check whether this transportable (person/container) had to depart before the simulation starts
878 2174 : if (!(myAddVehiclesDirectly || checkLastDepart())
879 4348 : || (myVehicleParameter->depart <= MSNet::getInstance()->getStateLoaderTime() && !myAmLoadingState)) {
880 0 : deleteActivePlanAndVehicleParameter();
881 : return;
882 : }
883 : // instantiate all persons/containers of this flow
884 : int i = 0;
885 2174 : registerLastDepart();
886 2174 : std::string baseID = myVehicleParameter->id;
887 2174 : if (myVehicleParameter->repetitionProbability > 0) {
888 207 : if (myVehicleParameter->repetitionEnd == SUMOTime_MAX) {
889 0 : throw ProcessError("probabilistic " + myActiveTypeName + "Flow '" + fid + "' must specify end time");
890 : } else {
891 444310 : for (SUMOTime t = myVehicleParameter->depart; t < myVehicleParameter->repetitionEnd; t += TIME2STEPS(1)) {
892 444103 : if (RandHelper::rand(&myParsingRNG) < myVehicleParameter->repetitionProbability) {
893 : // type existence has been checked on opening
894 34795 : MSVehicleType* const type = MSNet::getInstance()->getVehicleControl().getVType(myVehicleParameter->vtypeid, &myParsingRNG);
895 34795 : addFlowTransportable(t, type, baseID, i++);
896 : }
897 : }
898 : }
899 : } else {
900 1967 : SUMOTime depart = myVehicleParameter->depart;
901 1967 : if (myVehicleParameter->repetitionOffset < 0) {
902 : // poisson: randomize first depart
903 99 : myVehicleParameter->incrementFlow(1, &myParsingRNG);
904 : }
905 453127 : for (; i < myVehicleParameter->repetitionNumber && (myVehicleParameter->repetitionNumber != std::numeric_limits<long long int>::max()
906 81858 : || depart + myVehicleParameter->repetitionTotalOffset <= myVehicleParameter->repetitionEnd); i++) {
907 : // type existence has been checked on opening
908 451172 : MSVehicleType* const type = MSNet::getInstance()->getVehicleControl().getVType(myVehicleParameter->vtypeid, &myParsingRNG);
909 451172 : addFlowTransportable(depart + myVehicleParameter->repetitionTotalOffset, type, baseID, i);
910 451160 : myVehicleParameter->incrementFlow(1, &myParsingRNG);
911 : }
912 : }
913 2162 : resetActivePlanAndVehicleParameter();
914 12 : } catch (ProcessError&) {
915 12 : deleteActivePlanAndVehicleParameter();
916 12 : throw;
917 12 : }
918 2162 : myStartTriggeredInFlow = false;
919 : }
920 :
921 :
922 : int
923 529848 : MSRouteHandler::addFlowTransportable(SUMOTime depart, MSVehicleType* type, const std::string& baseID, int i) {
924 : try {
925 : int numCreated = 0;
926 529848 : MSNet* const net = MSNet::getInstance();
927 529848 : MSTransportableControl& tc = myActiveType == ObjectTypeEnum::PERSON ? net->getPersonControl() : net->getContainerControl();
928 529824 : const MSVehicleControl& vc = MSNet::getInstance()->getVehicleControl();
929 : //MSTransportableControl& pc = net->getPersonControl();
930 529824 : const int quota = vc.getQuota(vc.getScale() * type->getParameter().scale, tc.getLoadedNumber());
931 529824 : if (quota == 0) {
932 : tc.addDiscarded();
933 : }
934 1060152 : for (int j = 0; j < quota; j++) {
935 530344 : if (i > 0 || j > 0) {
936 : // copy parameter and plan because the transportable takes over responsibility
937 484337 : SUMOVehicleParameter* copyParam = new SUMOVehicleParameter();
938 484337 : *copyParam = *myVehicleParameter;
939 484337 : myVehicleParameter = copyParam;
940 484337 : MSTransportable::MSTransportablePlan* copyPlan = new MSTransportable::MSTransportablePlan();
941 : MSStage* lastStage = nullptr;
942 1559527 : for (MSStage* const s : *myActiveTransportablePlan) {
943 1075190 : copyPlan->push_back(s->clone());
944 1075190 : if (lastStage != nullptr && s->getStageType() == MSStageType::WALKING && lastStage->getDestinationStop() != nullptr) {
945 0 : MSStageMoving* walk = static_cast<MSStageMoving*>(copyPlan->back());
946 0 : walk->setDepartPos(lastStage->getDestinationStop()->getAccessPos(walk->getEdge(), &myParsingRNG));
947 : }
948 : lastStage = s;
949 : }
950 484337 : myActiveTransportablePlan = copyPlan;
951 484337 : if (myVehicleParameter->departPosProcedure == DepartPosDefinition::RANDOM) {
952 198 : const double initialDepartPos = RandHelper::rand(myActiveTransportablePlan->front()->getDestination()->getLength(), &myParsingRNG);
953 198 : myActiveTransportablePlan->front()->setArrivalPos(initialDepartPos);
954 : }
955 : }
956 530344 : myVehicleParameter->id = (baseID
957 2033174 : + (i >= 0 ? "." + toString(i) : "")
958 1591032 : + (j > 0 ? "." + toString(j) : ""));
959 530344 : if (MSGlobals::gStateLoaded && tc.get(myVehicleParameter->id) != nullptr) {
960 0 : return numCreated;
961 : }
962 530344 : myVehicleParameter->depart = depart += net->getInsertionControl().computeRandomDepartOffset();
963 530344 : MSTransportable* transportable = myActiveType == ObjectTypeEnum::PERSON ?
964 332949 : tc.buildPerson(myVehicleParameter, type, myActiveTransportablePlan, &myParsingRNG) :
965 197395 : tc.buildContainer(myVehicleParameter, type, myActiveTransportablePlan);
966 530344 : numCreated++;
967 530344 : if (!tc.add(transportable)) {
968 16 : std::string error = "Another " + myActiveTypeName + " with the id '" + myVehicleParameter->id + "' exists.";
969 16 : delete transportable;
970 16 : resetActivePlanAndVehicleParameter();
971 16 : throw ProcessError(error);
972 339778 : } else if ((net->hasPersons() && net->getPersonControl().get(myVehicleParameter->id) != nullptr)
973 863277 : && (net->hasContainers() && net->getContainerControl().get(myVehicleParameter->id) != nullptr)) {
974 48 : WRITE_WARNINGF(TL("There exists a person and a container with the same id '%'. Starting with SUMO 1.9.0 this is an error."), myVehicleParameter->id);
975 : }
976 : }
977 529808 : return numCreated;
978 40 : } catch (ProcessError&) {
979 40 : deleteActivePlanAndVehicleParameter();
980 40 : throw;
981 40 : }
982 : }
983 :
984 :
985 : void
986 62862 : MSRouteHandler::closeVType() {
987 : MSVehicleType* vehType = nullptr;
988 62862 : if (myCurrentVTypeRef.empty()) {
989 62780 : vehType = MSVehicleType::build(*myCurrentVType, getFileName());
990 62776 : vehType->check();
991 62776 : if (!MSNet::getInstance()->getVehicleControl().addVType(vehType)) {
992 : const std::string id = vehType->getID();
993 197 : delete vehType;
994 197 : if (!MSGlobals::gStateLoaded) {
995 60 : throw ProcessError(TLF("Another vehicle type (or distribution) with the id '%' exists.", id));
996 : }
997 : }
998 : }
999 62838 : if (myCurrentVTypeDistribution != nullptr) {
1000 1153 : if (!myCurrentVTypeRef.empty()) {
1001 82 : const RandomDistributor<MSVehicleType*>* const dist = MSNet::getInstance()->getVehicleControl().getVTypeDistribution(myCurrentVTypeRef);
1002 82 : if (dist != nullptr) {
1003 32 : const double distProb = (myCurrentVTypeProbability >= 0 ? myCurrentVTypeProbability : 1) / dist->getOverallProb();
1004 : std::vector<double>::const_iterator probIt = dist->getProbs().begin();
1005 48 : for (MSVehicleType* const type : dist->getVals()) {
1006 32 : myCurrentVTypeDistribution->add(type, distProb * *probIt);
1007 : probIt++;
1008 : }
1009 : return;
1010 : } else {
1011 66 : vehType = MSNet::getInstance()->getVehicleControl().getVType(myCurrentVTypeRef);
1012 : }
1013 : }
1014 1137 : double probability = myCurrentVTypeProbability >= 0 ? myCurrentVTypeProbability : vehType->getDefaultProbability();
1015 1137 : myCurrentVTypeDistribution->add(vehType, probability);
1016 : }
1017 : }
1018 :
1019 :
1020 : void
1021 20015 : MSRouteHandler::closeFlow() {
1022 20015 : myInsertStopEdgesAt = -1;
1023 20015 : if (myVehicleParameter->repetitionNumber == 0) {
1024 0 : delete myVehicleParameter;
1025 0 : myVehicleParameter = nullptr;
1026 15 : return;
1027 : }
1028 : // let's check whether vehicles had to depart before the simulation starts
1029 20015 : myVehicleParameter->repetitionsDone = 0;
1030 20015 : if (myVehicleParameter->repetitionProbability < 0) {
1031 15905 : const SUMOTime offsetToBegin = string2time(OptionsCont::getOptions().getString("begin")) - myVehicleParameter->depart;
1032 17238 : while (myVehicleParameter->repetitionTotalOffset < offsetToBegin) {
1033 1348 : myVehicleParameter->incrementFlow(1, &myParsingRNG);
1034 1348 : if (myVehicleParameter->repetitionsDone == myVehicleParameter->repetitionNumber) {
1035 15 : delete myVehicleParameter;
1036 15 : myVehicleParameter = nullptr;
1037 15 : return;
1038 : }
1039 : }
1040 : }
1041 20000 : if (MSNet::getInstance()->getVehicleControl().getVType(myVehicleParameter->vtypeid, &myParsingRNG) == nullptr) {
1042 0 : throw ProcessError("The vehicle type '" + myVehicleParameter->vtypeid + "' for flow '" + myVehicleParameter->id + "' is not known.");
1043 : }
1044 30966 : if (myVehicleParameter->routeid[0] == '!' && MSRoute::dictionary(myVehicleParameter->routeid, &myParsingRNG) == nullptr) {
1045 6731 : myVehicleParameter->parametersSet |= VEHPARS_FORCE_REROUTE;
1046 6731 : closeRoute(true);
1047 : }
1048 20000 : ConstMSRoutePtr route = MSRoute::dictionary(myVehicleParameter->routeid, &myParsingRNG);
1049 20000 : if (route == nullptr) {
1050 0 : throw ProcessError("The route '" + myVehicleParameter->routeid + "' for flow '" + myVehicleParameter->id + "' is not known.");
1051 : }
1052 20000 : if (route->mustReroute()) {
1053 16 : myVehicleParameter->parametersSet |= VEHPARS_FORCE_REROUTE;
1054 16 : if (myVehicleParameter->stops.size() > 0) {
1055 23 : route = addVehicleStopsToImplicitRoute(route, true);
1056 8 : myVehicleParameter->routeid = route->getID();
1057 : }
1058 : }
1059 20000 : if (myVehicleParameter->departEdgeProcedure != RouteIndexDefinition::DEFAULT) {
1060 20 : if ((myVehicleParameter->parametersSet & VEHPARS_FORCE_REROUTE) == 0 &&
1061 49 : myVehicleParameter->departEdgeProcedure == RouteIndexDefinition::GIVEN &&
1062 9 : myVehicleParameter->departEdge >= (int)route->getEdges().size()) {
1063 14 : throw ProcessError("Flow '" + myVehicleParameter->id + "' has invalid departEdge index "
1064 35 : + toString(myVehicleParameter->departEdge) + " for route with " + toString(route->getEdges().size()) + " edges.");
1065 : }
1066 : }
1067 19993 : if (myVehicleParameter->arrivalEdgeProcedure != RouteIndexDefinition::DEFAULT) {
1068 9 : if ((myVehicleParameter->parametersSet & VEHPARS_FORCE_REROUTE) == 0 &&
1069 18 : myVehicleParameter->arrivalEdgeProcedure == RouteIndexDefinition::GIVEN &&
1070 2 : myVehicleParameter->arrivalEdge >= (int)route->getEdges().size()) {
1071 0 : throw ProcessError("Flow '" + myVehicleParameter->id + "' has invalid arrivalEdge index "
1072 0 : + toString(myVehicleParameter->arrivalEdge) + " for route with " + toString(route->getEdges().size()) + " edges.");
1073 : }
1074 : }
1075 19993 : myActiveRouteID = "";
1076 :
1077 : // check whether the vehicle shall be added directly to the network or
1078 : // shall stay in the internal buffer
1079 19993 : if (myAddVehiclesDirectly || checkLastDepart()) {
1080 19993 : if (MSNet::getInstance()->getInsertionControl().addFlow(myVehicleParameter)) {
1081 19954 : registerLastDepart();
1082 : } else {
1083 39 : if (MSGlobals::gStateLoaded) {
1084 39 : delete myVehicleParameter;
1085 : } else {
1086 0 : throw ProcessError(TLF("Another flow with the id '%' exists.", myVehicleParameter->id));
1087 : }
1088 : }
1089 : }
1090 19993 : myVehicleParameter = nullptr;
1091 : }
1092 :
1093 :
1094 : void
1095 55242 : MSRouteHandler::closeTrip() {
1096 55242 : myVehicleParameter->parametersSet |= VEHPARS_FORCE_REROUTE;
1097 55242 : closeRoute(true);
1098 55242 : closeVehicle();
1099 55224 : }
1100 :
1101 : void
1102 5446 : MSRouteHandler::addRide(const SUMOSAXAttributes& attrs) {
1103 5446 : addRideOrTransport(attrs, SUMO_TAG_RIDE);
1104 5406 : }
1105 :
1106 : void
1107 1841 : MSRouteHandler::addTransport(const SUMOSAXAttributes& attrs) {
1108 1841 : addRideOrTransport(attrs, SUMO_TAG_TRANSPORT);
1109 1829 : }
1110 :
1111 : void
1112 7287 : MSRouteHandler::addRideOrTransport(const SUMOSAXAttributes& attrs, const SumoXMLTag modeTag) {
1113 7287 : if (myVehicleParameter == nullptr) {
1114 0 : throw ProcessError(TLF("Cannot define % stage without %.", toString(modeTag), toString(modeTag)));
1115 : }
1116 : try {
1117 9128 : const std::string mode = modeTag == SUMO_TAG_RIDE ? "ride" : "transport";
1118 7339 : std::string agent = "person";
1119 7339 : std::string stop = "bus stop";
1120 7287 : if (myActiveType == ObjectTypeEnum::CONTAINER) {
1121 : agent = "container";
1122 : stop = "container stop";
1123 : }
1124 :
1125 7287 : if (!((myActiveType == ObjectTypeEnum::PERSON && modeTag == SUMO_TAG_RIDE) ||
1126 1841 : (myActiveType == ObjectTypeEnum::CONTAINER && modeTag == SUMO_TAG_TRANSPORT))) {
1127 36 : throw ProcessError("Found " + mode + " inside " + agent + " element");
1128 : }
1129 7275 : const std::string aid = myVehicleParameter->id;
1130 7275 : bool ok = true;
1131 : const MSEdge* from = nullptr;
1132 14590 : const std::string desc = attrs.getOpt<std::string>(SUMO_ATTR_LINES, aid.c_str(), ok, LINE_ANY);
1133 7315 : StringTokenizer st(desc);
1134 14550 : MSStoppingPlace* s = retrieveStoppingPlace(attrs, " in " + agent + " '" + aid + "'");
1135 : MSEdge* to = nullptr;
1136 7275 : if (s != nullptr) {
1137 1882 : to = &s->getLane().getEdge();
1138 : }
1139 7275 : double arrivalPos = attrs.getOpt<double>(SUMO_ATTR_ARRIVALPOS, aid.c_str(), ok,
1140 1882 : s == nullptr ? std::numeric_limits<double>::infinity() : s->getEndLanePosition());
1141 :
1142 : const SUMOVehicleParameter* startVeh = nullptr;
1143 : const MSEdge* startVehFrom = nullptr;
1144 7275 : if (myActiveTransportablePlan->empty() && myVehicleParameter->departProcedure == DepartDefinition::TRIGGERED) {
1145 904 : if (st.size() != 1) {
1146 0 : throw ProcessError("Triggered departure for " + agent + " '" + aid + "' requires a unique lines value.");
1147 : }
1148 : // agent starts
1149 904 : MSVehicleControl& vehControl = MSNet::getInstance()->getVehicleControl();
1150 904 : const std::string vehID = st.front();
1151 904 : SUMOVehicle* sVeh = vehControl.getVehicle(vehID);
1152 904 : if (sVeh == nullptr) {
1153 72 : if (MSNet::getInstance()->hasFlow(vehID)) {
1154 40 : startVeh = MSNet::getInstance()->getInsertionControl().getFlowPars(vehID);
1155 40 : if (startVeh != nullptr) {
1156 40 : ConstMSRoutePtr const route = MSRoute::dictionary(startVeh->routeid);
1157 40 : startVehFrom = route->getEdges().front();
1158 : // flows are inserted at the end of the time step so we
1159 : // do delay the pedestrian event by one time step
1160 40 : myVehicleParameter->depart = startVeh->depart + DELTA_T;
1161 40 : myStartTriggeredInFlow = true;
1162 : }
1163 : }
1164 : } else {
1165 832 : startVeh = &sVeh->getParameter();
1166 832 : startVehFrom = sVeh->getRoute().getEdges().front();
1167 832 : myVehicleParameter->depart = startVeh->depart;
1168 : }
1169 : if (startVeh == nullptr) {
1170 : if (mySkippedVehicles.count(vehID) == 0) {
1171 48 : throw ProcessError("Unknown vehicle '" + vehID + "' in triggered departure for " + agent + " '" + aid + "'.");
1172 : }
1173 : // we cannot simply throw here because we need to parse the rest of the person (just to discard it)
1174 16 : from = MSEdge::getAllEdges().front(); // a dummy edge to keep parsing active
1175 872 : } else if (startVeh->departProcedure == DepartDefinition::TRIGGERED) {
1176 0 : throw ProcessError("Cannot use triggered vehicle '" + vehID + "' in triggered departure for " + agent + " '" + aid + "'.");
1177 : }
1178 : }
1179 :
1180 7259 : if (attrs.hasAttribute(SUMO_ATTR_FROM)) {
1181 4967 : const std::string fromID = attrs.get<std::string>(SUMO_ATTR_FROM, aid.c_str(), ok);
1182 4967 : from = MSEdge::dictionary(fromID);
1183 4967 : if (from == nullptr) {
1184 0 : throw ProcessError("The from edge '" + fromID + "' within a " + mode + " of " + agent + " '" + aid + "' is not known.");
1185 : }
1186 4967 : if (!myActiveTransportablePlan->empty() && myActiveTransportablePlan->back()->getDestination() != from) {
1187 54 : const bool stopWithAccess = (myActiveTransportablePlan->back()->getDestinationStop() != nullptr
1188 54 : && &myActiveTransportablePlan->back()->getDestinationStop()->getLane().getEdge() == from);
1189 54 : const bool transferAtJunction = (from->getFromJunction() == myActiveTransportablePlan->back()->getDestination()->getFromJunction()
1190 54 : || from->getFromJunction() == myActiveTransportablePlan->back()->getDestination()->getToJunction());
1191 54 : if (!(stopWithAccess || transferAtJunction)) {
1192 14 : throw ProcessError("Disconnected plan for " + agent + " '" + aid +
1193 42 : "' (edge '" + fromID + "' != edge '" + myActiveTransportablePlan->back()->getDestination()->getID() + "').");
1194 : }
1195 : }
1196 4953 : if (startVeh != nullptr && startVehFrom != from) {
1197 8 : throw ProcessError("Disconnected plan for triggered " + agent + " '" + aid +
1198 24 : "' (edge '" + fromID + "' != edge '" + startVehFrom->getID() + "').");
1199 : }
1200 2292 : } else if (startVeh != nullptr) {
1201 : from = startVehFrom;
1202 : }
1203 7237 : if (myActiveTransportablePlan->empty()) {
1204 4705 : if (from == nullptr) {
1205 0 : throw ProcessError("The start edge for " + agent + " '" + aid + "' is not known.");
1206 : } else {
1207 4705 : myActiveTransportablePlan->push_back(new MSStageWaiting(
1208 9410 : from, nullptr, -1, myVehicleParameter->depart, myVehicleParameter->departPos, "start", true));
1209 : }
1210 : }
1211 : // given attribute may override given stopping place due access requirements
1212 7237 : if (to == nullptr || attrs.hasAttribute(SUMO_ATTR_TO)) {
1213 5436 : const std::string toID = attrs.get<std::string>(SUMO_ATTR_TO, aid.c_str(), ok);
1214 5436 : to = MSEdge::dictionary(toID);
1215 5436 : if (to == nullptr) {
1216 6 : throw ProcessError("The to edge '" + toID + "' within a " + mode + " of " + agent + " '" + aid + "' is not known.");
1217 : }
1218 : }
1219 7235 : const std::string group = attrs.getOpt<std::string>(SUMO_ATTR_GROUP, aid.c_str(), ok, OptionsCont::getOptions().getString("persontrip.default.group"));
1220 7235 : const std::string intendedVeh = attrs.getOpt<std::string>(SUMO_ATTR_INTENDED, nullptr, ok, "");
1221 7235 : const SUMOTime intendedDepart = attrs.getOptSUMOTimeReporting(SUMO_ATTR_DEPART, nullptr, ok, -1);
1222 14470 : arrivalPos = SUMOVehicleParameter::interpretEdgePos(arrivalPos, to->getLength(), SUMO_ATTR_ARRIVALPOS, agent + " '" + aid + "' takes a " + mode + " to edge '" + to->getID() + "'");
1223 7235 : MSStageDriving* stage = new MSStageDriving(from, to, s, arrivalPos, 0.0, st.getVector(), group, intendedVeh, intendedDepart);
1224 : // for loading from saved state
1225 7235 : stage->setDeparted(attrs.getOptSUMOTimeReporting(SUMO_ATTR_STARTED, nullptr, ok, -1));
1226 7235 : stage->setEnded(attrs.getOptSUMOTimeReporting(SUMO_ATTR_ENDED, nullptr, ok, -1));
1227 7235 : stage->setVehicleID(attrs.getOpt<std::string>(SUMO_ATTR_VEHICLE, nullptr, ok, ""));
1228 7235 : stage->setVehicleDistance(attrs.getOpt<double>(SUMO_ATTR_ROUTELENGTH, nullptr, ok, -1));
1229 7235 : myActiveTransportablePlan->push_back(stage);
1230 7235 : myParamStack.push_back(stage);
1231 7327 : } catch (ProcessError&) {
1232 52 : deleteActivePlanAndVehicleParameter();
1233 52 : throw;
1234 52 : }
1235 7235 : }
1236 :
1237 : MSStoppingPlace*
1238 89102 : MSRouteHandler::retrieveStoppingPlace(const SUMOSAXAttributes& attrs, const std::string& errorSuffix, SUMOVehicleParameter::Stop* stopParam) {
1239 : MSStoppingPlace* toStop = nullptr;
1240 89102 : SumoXMLTag tag = SUMO_TAG_NOTHING;
1241 : std::string id;
1242 89102 : bool ok = SUMOVehicleParserHelper::parseStopTag(attrs, stopParam, tag, id);
1243 89102 : if (tag != SUMO_TAG_NOTHING) {
1244 33075 : toStop = MSNet::getInstance()->getStoppingPlace(id, tag);
1245 33075 : if (toStop == nullptr) {
1246 : ok = false;
1247 42 : WRITE_ERROR(TLF("The % '%' is not known%.", toString(tag), id, errorSuffix));
1248 : }
1249 : }
1250 89102 : if (!ok && MSGlobals::gCheckRoutes) {
1251 18 : throw ProcessError(TLF("Invalid stop definition%.", errorSuffix));
1252 : }
1253 89096 : return toStop;
1254 : }
1255 :
1256 : Parameterised*
1257 38603 : MSRouteHandler::addStop(const SUMOSAXAttributes& attrs) {
1258 : Parameterised* result = nullptr;
1259 : try {
1260 : std::string errorSuffix;
1261 38603 : if (myActiveType == ObjectTypeEnum::PERSON) {
1262 4119 : errorSuffix = " in person '" + myVehicleParameter->id + "'";
1263 37230 : } else if (myActiveType == ObjectTypeEnum::CONTAINER) {
1264 1023 : errorSuffix = " in container '" + myVehicleParameter->id + "'";
1265 36889 : } else if (myVehicleParameter != nullptr) {
1266 100512 : errorSuffix = " in vehicle '" + myVehicleParameter->id + "'";
1267 : } else {
1268 10224 : errorSuffix = " in route '" + myActiveRouteID + "'";
1269 : }
1270 38603 : SUMOVehicleParameter::Stop stop;
1271 77206 : bool ok = parseStop(stop, attrs, errorSuffix, MsgHandler::getErrorInstance());
1272 38603 : if (!ok) {
1273 27 : if (MSGlobals::gCheckRoutes) {
1274 27 : throw ProcessError();
1275 : }
1276 : return result;
1277 : }
1278 38576 : const MSEdge* edge = nullptr;
1279 : // patch chargingStation stop on a parkingArea
1280 38576 : if (stop.chargingStation != "") {
1281 1222 : const MSChargingStation* cs = dynamic_cast<MSChargingStation*>(MSNet::getInstance()->getStoppingPlace(stop.chargingStation, SUMO_TAG_CHARGING_STATION));
1282 1222 : const MSParkingArea* pa = cs->getParkingArea();
1283 1222 : if (pa != nullptr) {
1284 : stop.parkingarea = pa->getID();
1285 18 : stop.parking = ParkingType::OFFROAD;
1286 : }
1287 : }
1288 38576 : MSStoppingPlace* toStop = retrieveStoppingPlace(attrs, errorSuffix, &stop);
1289 : // if one of the previous stops is defined
1290 38570 : if (toStop != nullptr) {
1291 26712 : const MSLane& l = toStop->getLane();
1292 : stop.lane = l.getID();
1293 26712 : if ((stop.parametersSet & STOP_END_SET) == 0) {
1294 26696 : stop.endPos = toStop->getEndLanePosition();
1295 : } else {
1296 16 : stop.endPos = attrs.get<double>(SUMO_ATTR_ENDPOS, nullptr, ok);
1297 : }
1298 26712 : stop.startPos = toStop->getBeginLanePosition();
1299 26712 : edge = &l.getEdge();
1300 : } else {
1301 : // no, the lane and the position should be given directly
1302 : // get the lane
1303 11858 : stop.lane = attrs.getOpt<std::string>(SUMO_ATTR_LANE, nullptr, ok, "");
1304 11858 : stop.edge = attrs.getOpt<std::string>(SUMO_ATTR_EDGE, nullptr, ok, "");
1305 11858 : if (ok && stop.edge != "") { // edge is given directly
1306 1805 : edge = MSEdge::dictionary(stop.edge);
1307 1805 : if (edge == nullptr || (edge->isInternal() && !MSGlobals::gUsingInternalLanes)) {
1308 18 : throw ProcessError(TLF("The edge '%' for a stop is not known%.", stop.edge, errorSuffix));
1309 : }
1310 10053 : } else if (ok && stop.lane != "") { // lane is given directly
1311 9972 : MSLane* stopLane = MSLane::dictionary(stop.lane);
1312 9972 : if (stopLane == nullptr) {
1313 : // check for opposite-direction stop
1314 153 : stopLane = MSBaseVehicle::interpretOppositeStop(stop);
1315 153 : if (stopLane != nullptr) {
1316 145 : edge = MSEdge::dictionary(stop.edge);
1317 : }
1318 : } else {
1319 9819 : edge = &stopLane->getEdge();
1320 : }
1321 9964 : if (stopLane == nullptr || (stopLane->isInternal() && !MSGlobals::gUsingInternalLanes)) {
1322 24 : throw ProcessError(TLF("The lane '%' for a stop is not known%.", stop.lane, errorSuffix));
1323 : }
1324 81 : } else if (ok && ((attrs.hasAttribute(SUMO_ATTR_X) && attrs.hasAttribute(SUMO_ATTR_Y))
1325 41 : || (attrs.hasAttribute(SUMO_ATTR_LON) && attrs.hasAttribute(SUMO_ATTR_LAT)))) {
1326 : Position pos;
1327 : bool geo = false;
1328 56 : if (attrs.hasAttribute(SUMO_ATTR_X) && attrs.hasAttribute(SUMO_ATTR_Y)) {
1329 40 : pos = Position(attrs.get<double>(SUMO_ATTR_X, myVehicleParameter->id.c_str(), ok), attrs.get<double>(SUMO_ATTR_Y, myVehicleParameter->id.c_str(), ok));
1330 : } else {
1331 16 : pos = Position(attrs.get<double>(SUMO_ATTR_LON, myVehicleParameter->id.c_str(), ok), attrs.get<double>(SUMO_ATTR_LAT, myVehicleParameter->id.c_str(), ok));
1332 : geo = true;
1333 : }
1334 56 : PositionVector positions;
1335 56 : positions.push_back(pos);
1336 : ConstMSEdgeVector geoEdges;
1337 : SUMOVehicleClass vClass = SVC_PASSENGER;
1338 56 : auto& vc = MSNet::getInstance()->getVehicleControl();
1339 56 : if (!vc.getVTypeDistribution(myVehicleParameter->vtypeid)) {
1340 56 : MSVehicleType* const type = vc.getVType(myVehicleParameter->vtypeid, &myParsingRNG);
1341 56 : if (type != nullptr) {
1342 56 : vClass = type->getParameter().vehicleClass;
1343 : }
1344 : }
1345 56 : parseGeoEdges(positions, geo, vClass, geoEdges, myVehicleParameter->id, true, ok, true);
1346 56 : if (ok) {
1347 48 : edge = geoEdges.front();
1348 48 : if (geo) {
1349 16 : GeoConvHelper::getFinal().x2cartesian_const(pos);
1350 : }
1351 48 : stop.parametersSet |= STOP_END_SET;
1352 48 : stop.endPos = edge->getLanes()[0]->getShape().nearest_offset_to_point2D(pos, false);
1353 : } else {
1354 24 : throw ProcessError(TLF("Could not map stop position '%' to the network%.", pos, errorSuffix));
1355 : }
1356 64 : } else {
1357 25 : if (myActiveTransportablePlan && !myActiveTransportablePlan->empty()) { // use end of movement before
1358 19 : toStop = myActiveTransportablePlan->back()->getDestinationStop();
1359 19 : if (toStop != nullptr) { // use end of movement before definied as a stopping place
1360 0 : edge = &toStop->getLane().getEdge();
1361 0 : stop.lane = toStop->getLane().getID();
1362 0 : stop.endPos = toStop->getEndLanePosition();
1363 0 : stop.startPos = toStop->getBeginLanePosition();
1364 : } else { // use end of movement before definied as lane/edge
1365 19 : edge = myActiveTransportablePlan->back()->getDestination();
1366 19 : stop.lane = edge->getLanes()[0]->getID();
1367 19 : stop.endPos = myActiveTransportablePlan->back()->unspecifiedArrivalPos() ?
1368 5 : MSStage::ARRIVALPOS_UNSPECIFIED : myActiveTransportablePlan->back()->getArrivalPos();
1369 38 : stop.startPos = MAX2(0., stop.endPos - MIN_STOP_LENGTH);
1370 : }
1371 : } else {
1372 12 : const std::string msg = TLF("A stop must be placed on a busStop, a chargingStation, an overheadWireSegment, a containerStop, a parkingArea, an edge or a lane%.", errorSuffix);
1373 6 : if (MSGlobals::gCheckRoutes) {
1374 0 : throw ProcessError(msg);
1375 : } else {
1376 18 : WRITE_WARNING(msg);
1377 : return result;
1378 : }
1379 : }
1380 : }
1381 11830 : stop.endPos = attrs.getOpt<double>(SUMO_ATTR_ENDPOS, nullptr, ok, edge->getLength());
1382 11830 : if (attrs.hasAttribute(SUMO_ATTR_POSITION)) {
1383 0 : WRITE_WARNINGF(TL("Deprecated attribute 'pos' in description of stop%."), errorSuffix);
1384 0 : stop.endPos = attrs.getOpt<double>(SUMO_ATTR_POSITION, nullptr, ok, stop.endPos);
1385 : }
1386 11830 : if (stop.endPos < 0) {
1387 781 : stop.endPos += edge->getLength();
1388 : }
1389 23586 : stop.startPos = attrs.getOpt<double>(SUMO_ATTR_STARTPOS, nullptr, ok, MAX2(0., stop.endPos - MIN_STOP_LENGTH));
1390 11830 : if (!myAmLoadingState) {
1391 20609 : const bool friendlyPos = attrs.getOpt<bool>(SUMO_ATTR_FRIENDLY_POS, nullptr, ok, !attrs.hasAttribute(SUMO_ATTR_STARTPOS) && !attrs.hasAttribute(SUMO_ATTR_ENDPOS))
1392 11509 : || !MSGlobals::gCheckRoutes;
1393 11509 : if (!ok || (checkStopPos(stop.startPos, stop.endPos, edge->getLength(), 0, friendlyPos) != StopPos::STOPPOS_VALID)) {
1394 0 : throw ProcessError(TLF("Invalid start or end position for stop on %'%.",
1395 0 : stop.lane != "" ? ("lane '" + stop.lane) : ("edge '" + stop.edge), errorSuffix));
1396 : }
1397 : }
1398 : }
1399 38542 : stop.edge = edge->getID();
1400 38542 : if (myActiveTransportablePlan) {
1401 1706 : if (myActiveTransportablePlan->empty()) {
1402 760 : double departPos = toStop == nullptr || myVehicleParameter->wasSet(VEHPARS_DEPARTPOS_SET)
1403 1251 : ? myVehicleParameter->departPos
1404 708 : : (toStop->getBeginLanePosition() + toStop->getEndLanePosition()) / 2;
1405 1199 : myActiveTransportablePlan->push_back(new MSStageWaiting(
1406 2398 : edge, toStop, -1, myVehicleParameter->depart, departPos, "start", true));
1407 507 : } else if (myActiveTransportablePlan->back()->getDestination() != edge) {
1408 44 : if (myActiveTransportablePlan->back()->getDestination()->isTazConnector()) {
1409 8 : myActiveTransportablePlan->back()->setDestination(edge, toStop);
1410 36 : } else if (myActiveTransportablePlan->back()->getJumpDuration() < 0) {
1411 56 : throw ProcessError(TLF("Disconnected plan for % '%' (%!=%).", myActiveTypeName, myVehicleParameter->id,
1412 42 : edge->getID(), myActiveTransportablePlan->back()->getDestination()->getID()));
1413 : }
1414 : }
1415 : // transporting veh stops somewhere
1416 463 : else if (myActiveTransportablePlan->back()->getStageType() == MSStageType::WAITING
1417 463 : && (attrs.hasAttribute(SUMO_ATTR_ENDPOS) || attrs.hasAttribute(SUMO_ATTR_STARTPOS))) {
1418 12 : const double start = SUMOVehicleParameter::interpretEdgePos(stop.startPos, edge->getLength(), SUMO_ATTR_STARTPOS, "stopping at " + edge->getID());
1419 12 : const double end = SUMOVehicleParameter::interpretEdgePos(stop.endPos, edge->getLength(), SUMO_ATTR_ENDPOS, "stopping at " + edge->getID());
1420 12 : const double prevAr = myActiveTransportablePlan->back()->getArrivalPos();
1421 12 : if (start > prevAr + NUMERICAL_EPS || end < prevAr - NUMERICAL_EPS) {
1422 36 : WRITE_WARNINGF(TL("Disconnected plan for % '%' (stop range %-% does not cover previous arrival position %)."),
1423 : myActiveTypeName, myVehicleParameter->id, toString(start), toString(end), toString(prevAr));
1424 : }
1425 : }
1426 1692 : std::string actType = attrs.getOpt<std::string>(SUMO_ATTR_ACTTYPE, nullptr, ok, "");
1427 1692 : double pos = (stop.startPos + stop.endPos) / 2.;
1428 1692 : if (!myActiveTransportablePlan->empty() && myActiveTransportablePlan->back()->getJumpDuration() < 0) {
1429 1670 : pos = myActiveTransportablePlan->back()->unspecifiedArrivalPos() ?
1430 1646 : MSStage::ARRIVALPOS_UNSPECIFIED : myActiveTransportablePlan->back()->getArrivalPos();
1431 : }
1432 1692 : myActiveTransportablePlan->push_back(new MSStageWaiting(edge, toStop, stop.duration, stop.until, pos, actType, false, stop.jump));
1433 1692 : result = myActiveTransportablePlan->back();
1434 :
1435 36836 : } else if (myVehicleParameter != nullptr) {
1436 33451 : myVehicleParameter->stops.push_back(stop);
1437 33451 : result = &myVehicleParameter->stops.back();
1438 : } else {
1439 3385 : myActiveRouteStops.push_back(stop);
1440 : result = &myActiveRouteStops.back();
1441 : }
1442 38528 : if (myInsertStopEdgesAt >= 0) {
1443 : //std::cout << " myInsertStopEdgesAt=" << myInsertStopEdgesAt << " edge=" << edge->getID() << " myRoute=" << toString(myActiveRoute) << "\n";
1444 6979 : if (edge->isInternal()) {
1445 17 : if (myInsertStopEdgesAt > 0 && *(myActiveRoute.begin() + (myInsertStopEdgesAt - 1)) != edge->getNormalBefore()) {
1446 0 : myActiveRoute.insert(myActiveRoute.begin() + myInsertStopEdgesAt, edge->getNormalBefore());
1447 0 : myInsertStopEdgesAt++;
1448 : }
1449 17 : myActiveRoute.insert(myActiveRoute.begin() + myInsertStopEdgesAt, edge->getNormalSuccessor());
1450 17 : myInsertStopEdgesAt++;
1451 : } else {
1452 6962 : myActiveRoute.insert(myActiveRoute.begin() + myInsertStopEdgesAt, edge);
1453 6962 : myInsertStopEdgesAt++;
1454 : }
1455 31549 : } else if (myHaveVia) {
1456 : // vias were loaded, check for consistency
1457 6726 : if (std::find(myActiveRoute.begin(), myActiveRoute.end(), edge) == myActiveRoute.end()) {
1458 24 : WRITE_WARNINGF(TL("Stop edge '%' missing in attribute 'via' for % '%'."),
1459 : edge->getID(), myActiveTypeName, myVehicleParameter->id);
1460 : }
1461 : }
1462 38672 : } catch (ProcessError&) {
1463 69 : deleteActivePlanAndVehicleParameter();
1464 69 : throw;
1465 69 : }
1466 38528 : return result;
1467 : }
1468 :
1469 :
1470 : void
1471 42409 : MSRouteHandler::parseWalkPositions(const SUMOSAXAttributes& attrs, const std::string& personID,
1472 : const MSEdge* fromEdge, const MSEdge*& toEdge,
1473 : double& departPos, double& arrivalPos, MSStoppingPlace*& bs,
1474 : const MSStage* const lastStage, bool& ok) {
1475 : try {
1476 42409 : const std::string description = "person '" + personID + "' walking from edge '" + fromEdge->getID() + "'";
1477 :
1478 42409 : if (attrs.hasAttribute(SUMO_ATTR_DEPARTPOS)) {
1479 0 : WRITE_WARNING(TL("The attribute departPos is no longer supported for walks, please use the person attribute, the arrivalPos of the previous step or explicit stops."));
1480 : }
1481 42409 : departPos = 0.;
1482 42409 : if (lastStage != nullptr) {
1483 33384 : if (lastStage->getDestinationStop() != nullptr) {
1484 313 : departPos = lastStage->getDestinationStop()->getAccessPos(fromEdge, &myParsingRNG);
1485 33071 : } else if (lastStage->getDestination() == fromEdge) {
1486 33014 : departPos = lastStage->getArrivalPos();
1487 57 : } else if (lastStage->getDestination()->getToJunction() == fromEdge->getToJunction()) {
1488 24 : departPos = fromEdge->getLength();
1489 : }
1490 : }
1491 :
1492 42409 : bs = retrieveStoppingPlace(attrs, " " + description);
1493 42409 : if (bs != nullptr) {
1494 6883 : arrivalPos = bs->getAccessPos(toEdge != nullptr ? toEdge : &bs->getLane().getEdge());
1495 3627 : if (arrivalPos < 0) {
1496 0 : throw ProcessError("Bus stop '" + bs->getID() + "' is not connected to arrival edge '" + toEdge->getID() + "' for " + description + ".");
1497 : }
1498 3627 : if (attrs.hasAttribute(SUMO_ATTR_ARRIVALPOS)) {
1499 8 : const double length = toEdge != nullptr ? toEdge->getLength() : bs->getLane().getLength();
1500 8 : const double arrPos = SUMOVehicleParserHelper::parseWalkPos(SUMO_ATTR_ARRIVALPOS, myHardFail, description, length,
1501 8 : attrs.get<std::string>(SUMO_ATTR_ARRIVALPOS, description.c_str(), ok), &myParsingRNG);
1502 8 : if (arrPos >= bs->getBeginLanePosition() && arrPos < bs->getEndLanePosition()) {
1503 4 : arrivalPos = arrPos;
1504 : } else {
1505 12 : WRITE_WARNINGF(TL("Ignoring arrivalPos for % because it is outside the given stop '%'."), description, toString(SUMO_ATTR_BUS_STOP));
1506 4 : arrivalPos = bs->getAccessPos(&bs->getLane().getEdge());
1507 : }
1508 : }
1509 : } else {
1510 38782 : if (toEdge == nullptr) {
1511 0 : throw ProcessError(TLF("No destination edge for %.", description));
1512 : }
1513 38782 : if (attrs.hasAttribute(SUMO_ATTR_ARRIVALPOS)) {
1514 31732 : arrivalPos = SUMOVehicleParserHelper::parseWalkPos(SUMO_ATTR_ARRIVALPOS, myHardFail, description, toEdge->getLength(),
1515 63464 : attrs.get<std::string>(SUMO_ATTR_ARRIVALPOS, description.c_str(), ok), &myParsingRNG);
1516 : } else {
1517 7050 : arrivalPos = toEdge->getLength() / 2.;
1518 : }
1519 : }
1520 0 : } catch (ProcessError&) {
1521 0 : deleteActivePlanAndVehicleParameter();
1522 0 : throw;
1523 0 : }
1524 42409 : }
1525 :
1526 :
1527 : void
1528 9039 : MSRouteHandler::addPersonTrip(const SUMOSAXAttributes& attrs) {
1529 9039 : if (myVehicleParameter == nullptr) {
1530 12 : throw ProcessError(TL("Cannot define person stage without person."));
1531 : }
1532 : try {
1533 : myActiveRoute.clear();
1534 9033 : bool ok = true;
1535 : const char* const id = myVehicleParameter->id.c_str();
1536 : const MSEdge* from = nullptr;
1537 9033 : const MSEdge* to = nullptr;
1538 9033 : parseFromViaTo(SUMO_TAG_PERSON, attrs);
1539 9025 : myInsertStopEdgesAt = -1;
1540 11683 : if (attrs.hasAttribute(SUMO_ATTR_FROM) || attrs.hasAttribute(SUMO_ATTR_FROM_JUNCTION) || attrs.hasAttribute(SUMO_ATTR_FROM_TAZ)
1541 11571 : || attrs.hasAttribute(SUMO_ATTR_FROMXY) || attrs.hasAttribute(SUMO_ATTR_FROMLONLAT)) {
1542 6535 : from = myActiveRoute.front();
1543 2490 : } else if (myActiveTransportablePlan->empty()) {
1544 0 : throw ProcessError(TLF("Start edge not defined for person '%'.", myVehicleParameter->id));
1545 : } else {
1546 2490 : from = myActiveTransportablePlan->back()->getDestination();
1547 : }
1548 13307 : if (attrs.hasAttribute(SUMO_ATTR_TO) || attrs.hasAttribute(SUMO_ATTR_TO_JUNCTION) || attrs.hasAttribute(SUMO_ATTR_TO_TAZ)
1549 12361 : || attrs.hasAttribute(SUMO_ATTR_TOXY) || attrs.hasAttribute(SUMO_ATTR_TOLONLAT)) {
1550 5769 : to = myActiveRoute.back();
1551 : } // else, to may also be derived from stopping place
1552 :
1553 9025 : const SUMOTime duration = attrs.getOptSUMOTimeReporting(SUMO_ATTR_DURATION, id, ok, -1);
1554 9025 : if (attrs.hasAttribute(SUMO_ATTR_DURATION) && duration <= 0) {
1555 0 : throw ProcessError(TLF("Non-positive walking duration for '%'.", myVehicleParameter->id));
1556 : }
1557 :
1558 9025 : double departPos = 0;
1559 9025 : double arrivalPos = 0;
1560 9025 : MSStoppingPlace* stoppingPlace = nullptr;
1561 9025 : parseWalkPositions(attrs, myVehicleParameter->id, from, to, departPos, arrivalPos, stoppingPlace, nullptr, ok);
1562 :
1563 9025 : SVCPermissions modeSet = 0;
1564 9025 : if (attrs.hasAttribute(SUMO_ATTR_MODES)) {
1565 2480 : const std::string modes = attrs.getOpt<std::string>(SUMO_ATTR_MODES, id, ok, "");
1566 : std::string errorMsg;
1567 : // try to parse person modes
1568 2480 : if (!SUMOVehicleParameter::parsePersonModes(modes, "person", id, modeSet, errorMsg)) {
1569 0 : throw InvalidArgument(errorMsg);
1570 : }
1571 : } else {
1572 7785 : modeSet = myVehicleParameter->modes;
1573 : }
1574 9025 : const std::string group = attrs.getOpt<std::string>(SUMO_ATTR_GROUP, id, ok, OptionsCont::getOptions().getString("persontrip.default.group"));
1575 9025 : MSVehicleControl& vehControl = MSNet::getInstance()->getVehicleControl();
1576 27077 : const std::string types = attrs.getOpt<std::string>(SUMO_ATTR_VTYPES, id, ok, myVehicleParameter->vTypes);
1577 18176 : for (StringTokenizer st(types); st.hasNext();) {
1578 128 : const std::string vtypeid = st.next();
1579 128 : const MSVehicleType* const vType = vehControl.getVType(vtypeid);
1580 128 : if (vType == nullptr) {
1581 6 : throw InvalidArgument("The vehicle type '" + vtypeid + "' in a trip for person '" + myVehicleParameter->id + "' is not known.");
1582 : }
1583 252 : modeSet |= (vType->getVehicleClass() == SVC_BICYCLE) ? SVC_BICYCLE : SVC_PASSENGER;
1584 9025 : }
1585 9023 : const double speed = attrs.getOpt<double>(SUMO_ATTR_SPEED, id, ok, -1.);
1586 9023 : if (attrs.hasAttribute(SUMO_ATTR_SPEED) && speed <= 0) {
1587 0 : throw ProcessError(TLF("Non-positive walking speed for '%'.", myVehicleParameter->id));
1588 : }
1589 9023 : const double walkFactor = attrs.getOpt<double>(SUMO_ATTR_WALKFACTOR, id, ok, OptionsCont::getOptions().getFloat("persontrip.walkfactor"));
1590 18046 : const double departPosLat = interpretDepartPosLat(attrs.getOpt<std::string>(SUMO_ATTR_DEPARTPOS_LAT, nullptr, ok, ""), -1, "personTrip");
1591 9023 : if (ok) {
1592 9023 : if (myActiveTransportablePlan->empty()) {
1593 6378 : double initialDepartPos = myVehicleParameter->departPos;
1594 6378 : if (myVehicleParameter->departPosProcedure == DepartPosDefinition::RANDOM) {
1595 : initialDepartPos = RandHelper::rand(from->getLength(), &myParsingRNG);
1596 : }
1597 12756 : myActiveTransportablePlan->push_back(new MSStageWaiting(from, nullptr, -1, myVehicleParameter->depart, initialDepartPos, "start", true));
1598 : }
1599 9023 : myVehicleParameter->parametersSet |= VEHPARS_FORCE_REROUTE;
1600 9023 : MSStoppingPlace* fromStop = myActiveTransportablePlan->empty() ? nullptr : myActiveTransportablePlan->back()->getDestinationStop();
1601 12278 : myActiveTransportablePlan->push_back(new MSStageTrip(from, fromStop, to == nullptr ? &stoppingPlace->getLane().getEdge() : to,
1602 : stoppingPlace, duration, modeSet, types, speed, walkFactor, group,
1603 12278 : departPosLat, attrs.hasAttribute(SUMO_ATTR_ARRIVALPOS), arrivalPos));
1604 9025 : myParamStack.push_back(myActiveTransportablePlan->back());
1605 9023 : if (attrs.hasAttribute(SUMO_ATTR_ARRIVALPOS)) {
1606 1480 : myActiveTransportablePlan->back()->markSet(VEHPARS_ARRIVALPOS_SET);
1607 : }
1608 : }
1609 : myActiveRoute.clear();
1610 10 : } catch (ProcessError&) {
1611 10 : deleteActivePlanAndVehicleParameter();
1612 10 : throw;
1613 10 : }
1614 9023 : }
1615 :
1616 :
1617 : void
1618 39323 : MSRouteHandler::addWalk(const SUMOSAXAttributes& attrs) {
1619 39323 : if (myVehicleParameter == nullptr) {
1620 0 : throw ProcessError(TL("Cannot define person stage without person."));
1621 : }
1622 39323 : myActiveRouteID = "";
1623 39323 : if (attrs.hasAttribute(SUMO_ATTR_EDGES) || attrs.hasAttribute(SUMO_ATTR_ROUTE)) {
1624 : try {
1625 : myActiveRoute.clear();
1626 33392 : bool ok = true;
1627 33392 : const SUMOTime duration = attrs.getOptSUMOTimeReporting(SUMO_ATTR_DURATION, nullptr, ok, -1);
1628 33392 : if (attrs.hasAttribute(SUMO_ATTR_DURATION) && duration <= 0) {
1629 24 : throw ProcessError(TLF("Non-positive walking duration for '%'.", myVehicleParameter->id));
1630 : }
1631 : double speed = -1; // default to vType speed
1632 33384 : if (attrs.hasAttribute(SUMO_ATTR_SPEED)) {
1633 361 : speed = attrs.get<double>(SUMO_ATTR_SPEED, nullptr, ok);
1634 361 : if (speed <= 0) {
1635 0 : throw ProcessError(TLF("Non-positive walking speed for '%'.", myVehicleParameter->id));
1636 : }
1637 : }
1638 33384 : double departPos = 0;
1639 33384 : double arrivalPos = 0;
1640 33384 : MSStoppingPlace* bs = nullptr;
1641 33384 : if (attrs.hasAttribute(SUMO_ATTR_ROUTE)) {
1642 676 : myActiveRouteID = attrs.get<std::string>(SUMO_ATTR_ROUTE, myVehicleParameter->id.c_str(), ok);
1643 676 : ConstMSRoutePtr route = MSRoute::dictionary(myActiveRouteID, &myParsingRNG);
1644 676 : if (route == nullptr) {
1645 0 : throw ProcessError("The route '" + myActiveRouteID + "' for walk of person '" + myVehicleParameter->id + "' is not known.");
1646 : }
1647 676 : myActiveRoute = route->getEdges();
1648 : } else {
1649 65416 : MSEdge::parseEdgesList(attrs.get<std::string>(SUMO_ATTR_EDGES, myVehicleParameter->id.c_str(), ok), myActiveRoute, myActiveRouteID);
1650 : }
1651 33384 : if (myActiveTransportablePlan->empty()) {
1652 32882 : double initialDepartPos = myVehicleParameter->departPos;
1653 32882 : if (myVehicleParameter->departPosProcedure == DepartPosDefinition::RANDOM) {
1654 23 : initialDepartPos = RandHelper::rand(myActiveRoute.front()->getLength(), &myParsingRNG);
1655 : }
1656 65764 : myActiveTransportablePlan->push_back(new MSStageWaiting(myActiveRoute.front(), nullptr, -1, myVehicleParameter->depart, initialDepartPos, "start", true));
1657 : }
1658 33384 : parseWalkPositions(attrs, myVehicleParameter->id, myActiveRoute.front(), myActiveRoute.back(), departPos, arrivalPos, bs, myActiveTransportablePlan->back(), ok);
1659 33384 : if (myActiveRoute.empty()) {
1660 0 : throw ProcessError(TLF("No edges to walk for person '%'.", myVehicleParameter->id));
1661 : }
1662 33676 : if (myActiveTransportablePlan->back()->getDestination() != myActiveRoute.front() &&
1663 33384 : myActiveTransportablePlan->back()->getDestination()->getToJunction() != myActiveRoute.front()->getFromJunction() &&
1664 260 : myActiveTransportablePlan->back()->getDestination()->getToJunction() != myActiveRoute.front()->getToJunction()) {
1665 234 : if (myActiveTransportablePlan->back()->getDestinationStop() == nullptr || myActiveTransportablePlan->back()->getDestinationStop()->getAccessPos(myActiveRoute.front()) < 0.) {
1666 36 : throw ProcessError("Disconnected plan for person '" + myVehicleParameter->id + "' (" + myActiveRoute.front()->getID() + " not connected to " + myActiveTransportablePlan->back()->getDestination()->getID() + ").");
1667 : }
1668 : }
1669 33372 : const int departLane = attrs.getOpt<int>(SUMO_ATTR_DEPARTLANE, nullptr, ok, -1);
1670 66744 : const double departPosLat = interpretDepartPosLat(attrs.getOpt<std::string>(SUMO_ATTR_DEPARTPOS_LAT, nullptr, ok, ""), departLane, "walk");
1671 33372 : MSStageWalking* stage = new MSStageWalking(myVehicleParameter->id, myActiveRoute, bs, duration, speed, departPos, arrivalPos, departPosLat, departLane, myActiveRouteID);
1672 33372 : stage->setDeparted(attrs.getOptSUMOTimeReporting(SUMO_ATTR_STARTED, nullptr, ok, -1));
1673 33372 : stage->setEnded(attrs.getOptSUMOTimeReporting(SUMO_ATTR_ENDED, nullptr, ok, -1));
1674 33372 : stage->setTotalWaitingTime(attrs.getOptSUMOTimeReporting(SUMO_ATTR_WAITINGTIME, nullptr, ok, 0));
1675 33386 : if (attrs.hasAttribute(SUMO_ATTR_EXITTIMES) && OptionsCont::getOptions().getBool("vehroute-output.exit-times")) {
1676 14 : std::vector<SUMOTime>* exitTimes = new std::vector<SUMOTime>();
1677 44 : for (const std::string& tStr : attrs.get<std::vector<std::string> >(SUMO_ATTR_EXITTIMES, nullptr, ok)) {
1678 30 : exitTimes->push_back(string2time(tStr));
1679 14 : }
1680 : stage->setExitTimes(exitTimes);
1681 : }
1682 33372 : if (attrs.hasAttribute(SUMO_ATTR_ARRIVALPOS)) {
1683 : stage->markSet(VEHPARS_ARRIVALPOS_SET);
1684 : }
1685 33372 : myActiveTransportablePlan->push_back(stage);
1686 33372 : myParamStack.push_back(stage);
1687 : myActiveRoute.clear();
1688 20 : } catch (ProcessError&) {
1689 20 : deleteActivePlanAndVehicleParameter();
1690 20 : throw;
1691 20 : }
1692 : } else { // parse walks from->to as person trips
1693 5931 : addPersonTrip(attrs);
1694 : }
1695 39303 : }
1696 :
1697 : double
1698 42395 : MSRouteHandler::interpretDepartPosLat(const std::string& value, int departLane, const std::string& element) {
1699 42395 : double pos = MSPModel::UNSPECIFIED_POS_LAT;
1700 42395 : if (value == "") {
1701 : return pos;
1702 : }
1703 : std::string error;
1704 : DepartPosLatDefinition dpd;
1705 250 : if (SUMOVehicleParameter::parseDepartPosLat(value, element, myVehicleParameter->id, pos, dpd, error)) {
1706 250 : if (dpd != DepartPosLatDefinition::GIVEN) {
1707 4 : const MSLane* lane = MSStageMoving::checkDepartLane(myActiveRoute.front(), SVC_IGNORING, departLane, myVehicleParameter->id);
1708 4 : if (lane == nullptr) {
1709 0 : throw ProcessError(TLF("Could not find departure lane for walk of person '%' when interpreting departPosLat", myVehicleParameter->id));
1710 : }
1711 4 : const double usableWidth = lane->getWidth() - 0.5;
1712 4 : switch (dpd) {
1713 0 : case DepartPosLatDefinition::RIGHT:
1714 0 : pos = -usableWidth / 2;
1715 0 : break;
1716 0 : case DepartPosLatDefinition::LEFT:
1717 0 : pos = usableWidth / 2;
1718 0 : break;
1719 0 : case DepartPosLatDefinition::CENTER:
1720 0 : pos = 0;
1721 0 : break;
1722 4 : case DepartPosLatDefinition::RANDOM:
1723 : case DepartPosLatDefinition::FREE:
1724 : case DepartPosLatDefinition::RANDOM_FREE:
1725 : /// @note must be randomized for every person individually when loading a personFlow
1726 4 : pos = MSPModel::RANDOM_POS_LAT;
1727 4 : break;
1728 : default:
1729 : break;
1730 : }
1731 : }
1732 : } else {
1733 0 : throw ProcessError(error);
1734 : }
1735 250 : return pos;
1736 : }
1737 :
1738 :
1739 : void
1740 46196 : MSRouteHandler::addTransportable(const SUMOSAXAttributes& /*attrs*/, const bool isPerson) {
1741 46196 : myActiveType = isPerson ? ObjectTypeEnum::PERSON : ObjectTypeEnum::CONTAINER;
1742 46196 : if (!MSNet::getInstance()->getVehicleControl().hasVType(myVehicleParameter->vtypeid)) {
1743 0 : const std::string error = TLF("The type '%' for % '%' is not known.", myVehicleParameter->vtypeid, myActiveTypeName, myVehicleParameter->id);
1744 0 : deleteActivePlanAndVehicleParameter();
1745 0 : throw ProcessError(error);
1746 : }
1747 46196 : myActiveTransportablePlan = new MSTransportable::MSTransportablePlan();
1748 46196 : }
1749 :
1750 :
1751 : void
1752 1550 : MSRouteHandler::addTranship(const SUMOSAXAttributes& attrs) {
1753 : try {
1754 : myActiveRoute.clear();
1755 1550 : const std::string cid = myVehicleParameter->id;
1756 1550 : bool ok = true;
1757 1550 : const MSEdge* from = nullptr;
1758 1550 : const MSEdge* to = nullptr;
1759 : MSStoppingPlace* cs = nullptr;
1760 :
1761 : double speed;
1762 1550 : const MSVehicleType* vtype = MSNet::getInstance()->getVehicleControl().getVType(myVehicleParameter->vtypeid);
1763 1550 : if (attrs.hasAttribute(SUMO_ATTR_SPEED)) { // speed is explicitly set
1764 71 : speed = attrs.getOpt<double>(SUMO_ATTR_SPEED, nullptr, ok, -1);
1765 71 : if (!ok) {
1766 0 : throw ProcessError(TLF("Could not read tranship speed for container '%'.", cid));
1767 : }
1768 1479 : } else if (vtype != nullptr && vtype->wasSet(VTYPEPARS_MAXSPEED_SET)) { // speed is set by vtype
1769 : speed = vtype->getMaxSpeed();
1770 : } else { // default speed value
1771 1479 : speed = DEFAULT_CONTAINER_TRANSHIP_SPEED;
1772 : }
1773 1550 : if (speed <= 0) {
1774 0 : throw ProcessError(TLF("Non-positive tranship speed for container '%'.", cid));
1775 : }
1776 : // values from preceding stage:
1777 : const MSEdge* preEdge = nullptr;
1778 : double prePos = 0;
1779 1550 : if (!myActiveTransportablePlan->empty()) {
1780 576 : preEdge = myActiveTransportablePlan->back()->getDestination();
1781 576 : prePos = myActiveTransportablePlan->back()->getArrivalPos();
1782 : }
1783 : // set depart position as given attribute value, arrival position of preceding stage or default (=0)
1784 1550 : double departPos = attrs.getOpt<double>(SUMO_ATTR_DEPARTPOS, cid.c_str(), ok, prePos);
1785 :
1786 1550 : if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
1787 252 : MSEdge::parseEdgesList(attrs.get<std::string>(SUMO_ATTR_EDGES, cid.c_str(), ok), myActiveRoute, myActiveRouteID);
1788 : } else {
1789 : // set 'from':
1790 1424 : if (attrs.hasAttribute(SUMO_ATTR_FROM)) {
1791 917 : const std::string fromID = attrs.get<std::string>(SUMO_ATTR_FROM, cid.c_str(), ok);
1792 917 : from = MSEdge::dictionary(fromID);
1793 917 : if (from == nullptr) {
1794 0 : throw ProcessError("The from edge '" + fromID + "' within a tranship of container '" + cid + "' is not known.");
1795 : }
1796 917 : if (preEdge != nullptr && preEdge != from) {
1797 0 : throw ProcessError("Disconnected plan for container '" + cid + "' (" + from->getID() + "!=" + preEdge->getID() + ").");
1798 : }
1799 507 : } else if (preEdge == nullptr) {
1800 0 : throw ProcessError(TLF("The start edge for container '%' is not known.", cid));
1801 : } else {
1802 507 : from = preEdge;
1803 : }
1804 : // set 'to':
1805 1424 : if (attrs.hasAttribute(SUMO_ATTR_TO)) {
1806 582 : const std::string toID = attrs.get<std::string>(SUMO_ATTR_TO, cid.c_str(), ok);
1807 582 : to = MSEdge::dictionary(toID);
1808 582 : if (to == nullptr) {
1809 0 : throw ProcessError("The to edge '" + toID + "' within a tranship of container '" + cid + "' is not known.");
1810 : }
1811 : } else {
1812 842 : const std::string description = "container '" + cid + "' transhipping from edge '" + from->getID() + "'";
1813 842 : cs = retrieveStoppingPlace(attrs, " " + description);
1814 842 : if (cs != nullptr) {
1815 842 : to = &cs->getLane().getEdge();
1816 : } else {
1817 0 : throw ProcessError(TLF("Inconsistent tranship for container '%', needs either: 'edges', 'to', 'containerStop' (or any other stopping place)", cid));
1818 : }
1819 : }
1820 1424 : myActiveRoute.push_back(from);
1821 1424 : myActiveRoute.push_back(to);
1822 : }
1823 1550 : if (myActiveRoute.empty()) {
1824 0 : throw ProcessError(TLF("No edges to tranship container '%'.", cid));
1825 : }
1826 1550 : if (preEdge == nullptr) { // additional 'stop' to start the container plan
1827 974 : myActiveTransportablePlan->push_back(new MSStageWaiting(
1828 1948 : myActiveRoute.front(), nullptr, -1, myVehicleParameter->depart, departPos, "start", true));
1829 : }
1830 3100 : double arrivalPos = attrs.getOpt<double>(SUMO_ATTR_ARRIVALPOS, cid.c_str(), ok,
1831 1550 : cs == nullptr ? myActiveRoute.back()->getLength() : cs->getEndLanePosition());
1832 1550 : myActiveTransportablePlan->push_back(new MSStageTranship(myActiveRoute, cs, speed, departPos, arrivalPos));
1833 1550 : myParamStack.push_back(myActiveTransportablePlan->back());
1834 : myActiveRoute.clear();
1835 0 : } catch (ProcessError&) {
1836 0 : deleteActivePlanAndVehicleParameter();
1837 0 : throw;
1838 0 : }
1839 1550 : }
1840 :
1841 :
1842 : void
1843 144 : MSRouteHandler::initLaneTree(NamedRTree* tree) {
1844 5856 : for (const auto& edge : MSEdge::getAllEdges()) {
1845 5712 : if (edge->isNormal() || MSGlobals::gUsingInternalLanes) {
1846 10700 : for (MSLane* lane : edge->getLanes()) {
1847 5216 : Boundary b = lane->getShape().getBoxBoundary();
1848 5216 : const float cmin[2] = {(float) b.xmin(), (float) b.ymin()};
1849 5216 : const float cmax[2] = {(float) b.xmax(), (float) b.ymax()};
1850 5216 : tree->Insert(cmin, cmax, lane);
1851 : }
1852 : }
1853 : }
1854 144 : }
1855 :
1856 :
1857 : SumoRNG*
1858 0 : MSRouteHandler::getRNG() {
1859 0 : return &myParsingRNG;
1860 : }
1861 :
1862 :
1863 : const SUMOVTypeParameter*
1864 62807 : MSRouteHandler::getVTypeParameter(const std::string& refid) {
1865 62807 : const auto t = MSNet::getInstance()->getVehicleControl().getVType(refid);
1866 62807 : return t != nullptr ? &t->getParameter() : nullptr;
1867 : }
1868 :
1869 :
1870 : MSEdge*
1871 1120 : MSRouteHandler::retrieveEdge(const std::string& id) {
1872 1120 : return MSEdge::dictionary(id);
1873 : }
1874 :
1875 :
1876 : /****************************************************************************/
|