Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2009-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 MSDevice_Vehroutes.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Laura Bieker
17 : /// @author Michael Behrisch
18 : /// @author Jakob Erdmann
19 : /// @date Fri, 30.01.2009
20 : ///
21 : // A device which collects info on the vehicle trip
22 : /****************************************************************************/
23 : #include <config.h>
24 :
25 : #include <microsim/MSGlobals.h>
26 : #include <microsim/MSNet.h>
27 : #include <microsim/MSLane.h>
28 : #include <microsim/MSEdge.h>
29 : #include <microsim/MSRoute.h>
30 : #include <microsim/MSStop.h>
31 : #include <microsim/MSVehicle.h>
32 : #include <microsim/MSVehicleType.h>
33 : #include <microsim/transportables/MSTransportableControl.h>
34 : #include <utils/vehicle/SUMOVehicle.h>
35 : #include <utils/options/OptionsCont.h>
36 : #include <utils/iodevices/OutputDevice_String.h>
37 : #include <utils/xml/SUMOSAXAttributes.h>
38 : #include "MSDevice_Vehroutes.h"
39 :
40 :
41 : // ===========================================================================
42 : // static member variables
43 : // ===========================================================================
44 : bool MSDevice_Vehroutes::mySaveExits = false;
45 : bool MSDevice_Vehroutes::myLastRouteOnly = false;
46 : bool MSDevice_Vehroutes::myDUAStyle = false;
47 : bool MSDevice_Vehroutes::myWriteCosts = false;
48 : bool MSDevice_Vehroutes::mySorted = false;
49 : bool MSDevice_Vehroutes::myIntendedDepart = false;
50 : bool MSDevice_Vehroutes::myRouteLength = false;
51 : bool MSDevice_Vehroutes::mySkipPTLines = false;
52 : bool MSDevice_Vehroutes::myIncludeIncomplete = false;
53 : bool MSDevice_Vehroutes::myWriteStopPriorEdges = false;
54 : bool MSDevice_Vehroutes::myWriteInternal = false;
55 : MSDevice_Vehroutes::StateListener MSDevice_Vehroutes::myStateListener;
56 : MSDevice_Vehroutes::SortedRouteInfo MSDevice_Vehroutes::myRouteInfos;
57 :
58 :
59 : // ===========================================================================
60 : // method definitions
61 : // ===========================================================================
62 : // ---------------------------------------------------------------------------
63 : // static initialisation methods
64 : // ---------------------------------------------------------------------------
65 : void
66 41452 : MSDevice_Vehroutes::init() {
67 41452 : const OptionsCont& oc = OptionsCont::getOptions();
68 82904 : if (oc.isSet("vehroute-output")) {
69 12230 : OutputDevice::createDeviceByOption("vehroute-output", "routes", "routes_file.xsd");
70 12212 : OutputDevice& od = OutputDevice::getDeviceByOption("vehroute-output");
71 6106 : if (!od.isXML()) {
72 4 : WRITE_WARNING("Vehroute output does not fully support tabular formats. Sorting and stop output will not work.");
73 : }
74 6106 : mySaveExits = oc.getBool("vehroute-output.exit-times");
75 6106 : myLastRouteOnly = oc.getBool("vehroute-output.last-route");
76 6106 : myDUAStyle = oc.getBool("vehroute-output.dua");
77 6106 : myWriteCosts = oc.getBool("vehroute-output.cost");
78 6106 : mySorted = myDUAStyle || oc.getBool("vehroute-output.sorted");
79 6106 : myIntendedDepart = oc.getBool("vehroute-output.intended-depart");
80 6106 : myRouteLength = oc.getBool("vehroute-output.route-length");
81 6106 : mySkipPTLines = oc.getBool("vehroute-output.skip-ptlines");
82 6106 : myIncludeIncomplete = oc.getBool("vehroute-output.incomplete");
83 6106 : myWriteStopPriorEdges = oc.getBool("vehroute-output.stop-edges");
84 6106 : myWriteInternal = oc.getBool("vehroute-output.internal");
85 6106 : MSNet::getInstance()->addVehicleStateListener(&myStateListener);
86 6106 : myRouteInfos.routeOut = &od;
87 : }
88 41446 : }
89 :
90 :
91 : void
92 44603 : MSDevice_Vehroutes::insertOptions(OptionsCont& oc) {
93 44603 : oc.addOptionSubTopic("Vehroutes Device");
94 89206 : insertDefaultAssignmentOptions("vehroute", "Vehroutes Device", oc);
95 44603 : }
96 :
97 :
98 : MSDevice_Vehroutes*
99 6200057 : MSDevice_Vehroutes::buildVehicleDevices(SUMOVehicle& v, std::vector<MSVehicleDevice*>& into, int maxRoutes) {
100 6200057 : if (maxRoutes < std::numeric_limits<int>::max()) {
101 1889412 : return new MSDevice_Vehroutes(v, "vehroute_" + v.getID(), maxRoutes);
102 : }
103 5255351 : if (mySkipPTLines && v.getParameter().line != "") {
104 : return nullptr;
105 : }
106 5254225 : OptionsCont& oc = OptionsCont::getOptions();
107 10508450 : if (equippedByDefaultAssignmentOptions(oc, "vehroute", v, oc.isSet("vehroute-output"))) {
108 138283 : if (myLastRouteOnly) {
109 : maxRoutes = 0;
110 : }
111 138283 : myStateListener.myDevices[&v] = new MSDevice_Vehroutes(v, "vehroute_" + v.getID(), maxRoutes);
112 138283 : into.push_back(myStateListener.myDevices[&v]);
113 138283 : return myStateListener.myDevices[&v];
114 : }
115 : return nullptr;
116 : }
117 :
118 :
119 : // ---------------------------------------------------------------------------
120 : // MSDevice_Vehroutes::StateListener-methods
121 : // ---------------------------------------------------------------------------
122 : void
123 527912 : MSDevice_Vehroutes::StateListener::vehicleStateChanged(const SUMOVehicle* const vehicle, MSNet::VehicleState to, const std::string& info) {
124 527912 : if (to == MSNet::VehicleState::NEWROUTE) {
125 : const auto& deviceEntry = myDevices.find(vehicle);
126 98703 : if (deviceEntry != myDevices.end()) {
127 97301 : deviceEntry->second->addRoute(info);
128 : }
129 : }
130 527912 : }
131 :
132 :
133 : // ---------------------------------------------------------------------------
134 : // MSDevice_Vehroutes-methods
135 : // ---------------------------------------------------------------------------
136 1082989 : MSDevice_Vehroutes::MSDevice_Vehroutes(SUMOVehicle& holder, const std::string& id, int maxRoutes) :
137 : MSVehicleDevice(holder, id),
138 1082989 : myCurrentRoute(holder.getRoutePtr()),
139 1082989 : myMaxRoutes(maxRoutes),
140 1082989 : myLastSavedAt(nullptr),
141 1082989 : myLastRouteIndex(-1),
142 1082989 : myDepartLane(-1),
143 1082989 : myDepartPos(-1),
144 1082989 : myDepartSpeed(-1),
145 1082989 : myDepartPosLat(0),
146 1082989 : myStopOut(2) {
147 1082989 : }
148 :
149 :
150 2165844 : MSDevice_Vehroutes::~MSDevice_Vehroutes() {
151 1082922 : myStateListener.myDevices.erase(&myHolder);
152 2165844 : }
153 :
154 :
155 : bool
156 14287623 : MSDevice_Vehroutes::notifyEnter(SUMOTrafficObject& veh, MSMoveReminder::Notification reason, const MSLane* enteredLane) {
157 14287623 : if (reason == MSMoveReminder::NOTIFICATION_DEPARTED) {
158 894570 : if (mySorted && myStateListener.myDevices[static_cast<SUMOVehicle*>(&veh)] == this) {
159 4586 : const SUMOTime departure = myIntendedDepart ? myHolder.getParameter().depart : MSNet::getInstance()->getCurrentTimeStep();
160 4586 : myRouteInfos.departureCounts[departure]++;
161 : }
162 894570 : if (!MSGlobals::gUseMesoSim) {
163 : const MSVehicle& vehicle = static_cast<MSVehicle&>(veh);
164 574252 : myDepartLane = vehicle.getLane()->getIndex();
165 574252 : myDepartPosLat = vehicle.getLateralPositionOnLane();
166 : }
167 894570 : myDepartSpeed = veh.getSpeed();
168 894570 : myDepartPos = veh.getPositionOnLane();
169 : }
170 14287623 : if (myWriteStopPriorEdges) {
171 68 : if (MSGlobals::gUseMesoSim) {
172 48 : const MSEdge* e = veh.getEdge();
173 48 : if (myPriorEdges.empty() || myPriorEdges.back() != e) {
174 12 : myPriorEdges.push_back(e);
175 : }
176 : } else {
177 20 : myPriorEdges.push_back(&enteredLane->getEdge());
178 : }
179 : }
180 14287623 : myLastRouteIndex = myHolder.getRoutePosition();
181 14287623 : return true;
182 : }
183 :
184 :
185 : bool
186 14251963 : MSDevice_Vehroutes::notifyLeave(SUMOTrafficObject& veh, double /*lastPos*/, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
187 14251963 : if (mySaveExits && reason != NOTIFICATION_LANE_CHANGE && reason != NOTIFICATION_PARKING && reason != NOTIFICATION_SEGMENT) {
188 307917 : const MSEdge* edge = myWriteInternal ? dynamic_cast<MSBaseVehicle&>(veh).getCurrentEdge() : veh.getEdge();
189 307917 : if (myLastSavedAt != edge) {
190 155282 : myExits.push_back(MSNet::getInstance()->getCurrentTimeStep());
191 155282 : myLastSavedAt = edge;
192 : }
193 : }
194 14251963 : return true;
195 : }
196 :
197 :
198 : void
199 23288 : MSDevice_Vehroutes::notifyStopEnded() {
200 23288 : SUMOVehicleParameter::Stop stop = myHolder.getStops().front().pars;
201 23288 : const bool closeLater = myWriteStopPriorEdges || mySaveExits;
202 23288 : if (mySaveExits) {
203 : // prevent duplicate output
204 1567 : stop.parametersSet &= ~(STOP_STARTED_SET | STOP_ENDED_SET);
205 : }
206 23288 : stop.write(myStopOut, !closeLater);
207 23288 : if (myWriteStopPriorEdges) {
208 : // calculate length
209 16 : double priorEdgesLength = 0;
210 40 : for (int i = 0; i < (int)myPriorEdges.size(); i++) {
211 24 : if (i == 0) {
212 16 : priorEdgesLength += myPriorEdges.at(i)->getLength();
213 8 : } else if (myPriorEdges.at(i)->getID() != myPriorEdges.at(i - 1)->getID()) {
214 8 : priorEdgesLength += myPriorEdges.at(i)->getLength();
215 : }
216 : }
217 16 : myStopOut.writeAttr("priorEdges", myPriorEdges);
218 : myPriorEdges.clear();
219 16 : myStopOut.writeAttr("priorEdgesLength", priorEdgesLength);
220 : }
221 23288 : if (mySaveExits) {
222 1567 : myStopOut.writeAttr(SUMO_ATTR_STARTED, time2string(stop.started));
223 1567 : myStopOut.writeAttr(SUMO_ATTR_ENDED, stop.ended < 0 ? "-1" : time2string(stop.ended));
224 : }
225 23288 : if (closeLater) {
226 3166 : myStopOut.closeTag();
227 : }
228 23288 : }
229 :
230 :
231 : void
232 166782 : MSDevice_Vehroutes::writeXMLRoute(OutputDevice& os, int index) const {
233 9965 : if (index == 0 && !myIncludeIncomplete && myReplacedRoutes[index].route->size() == 2 &&
234 170536 : myReplacedRoutes[index].route->getEdges().front()->isTazConnector() &&
235 0 : myReplacedRoutes[index].route->getEdges().back()->isTazConnector()) {
236 : return;
237 : }
238 : // check if a previous route shall be written
239 : //std::cout << " writeXMLRoute index=" << index << " numReroutes=" << myHolder.getNumberReroutes() << "\n";
240 166782 : const int routesToSkip = myHolder.getParameter().wasSet(VEHPARS_FORCE_REROUTE) ? 1 : 0;
241 166782 : os.openTag(SUMO_TAG_ROUTE);
242 166782 : if (index >= 0) {
243 : assert((int)myReplacedRoutes.size() > index);
244 39402 : if (myDUAStyle || myWriteCosts) {
245 14320 : os.writeAttr(SUMO_ATTR_COST, myReplacedRoutes[index].route->getCosts());
246 : }
247 39402 : if (myWriteCosts) {
248 14320 : os.writeAttr(SUMO_ATTR_SAVINGS, myReplacedRoutes[index].route->getSavings());
249 : }
250 : // write edge on which the vehicle was when the route was valid
251 116844 : os.writeAttr("replacedOnEdge", (myReplacedRoutes[index].edge ?
252 : myReplacedRoutes[index].edge->getID() : ""));
253 39402 : if (myReplacedRoutes[index].lastRouteIndex > 0) {
254 : // do not write the default
255 27272 : os.writeAttr(SUMO_ATTR_REPLACED_ON_INDEX, myReplacedRoutes[index].lastRouteIndex);
256 : }
257 : // write the reason for replacement
258 39402 : os.writeAttr("reason", myReplacedRoutes[index].info);
259 :
260 : // write the time at which the route was replaced
261 39402 : os.writeAttr(SUMO_ATTR_REPLACED_AT_TIME, time2string(myReplacedRoutes[index].time));
262 39402 : os.writeAttr(SUMO_ATTR_PROB, "0");
263 39402 : OutputDevice_String edgesD;
264 : // always write the part that was actually driven and the rest of the current route that wasn't yet driven
265 : int start = 0;
266 189088 : for (int i = routesToSkip; i < index; i++) {
267 149686 : if (myReplacedRoutes[i].edge != nullptr) {
268 148859 : int end = myReplacedRoutes[i].lastRouteIndex;
269 148859 : myReplacedRoutes[i].route->writeEdgeIDs(edgesD, start, end, myWriteInternal, myHolder.getVClass());
270 : }
271 149686 : start = myReplacedRoutes[i].newRouteIndex;
272 : }
273 39402 : myReplacedRoutes[index].route->writeEdgeIDs(edgesD, start, -1, myWriteInternal, myHolder.getVClass());
274 39402 : std::string edgesS = edgesD.getString();
275 : edgesS.pop_back(); // remove last ' '
276 39402 : os.writeAttr(SUMO_ATTR_EDGES, edgesS);
277 39402 : if (myRouteLength) {
278 7121 : const bool includeInternalLengths = MSGlobals::gUsingInternalLanes && MSNet::getInstance()->hasInternalLinks();
279 : ConstMSRoutePtr route = myReplacedRoutes[index].route;
280 7121 : const double routeLength = route->getDistanceBetween(myHolder.getDepartPos(), route->getEdges().back()->getLength(),
281 7121 : route->begin(), route->end(), includeInternalLengths);
282 7121 : os.writeAttr("routeLength", routeLength);
283 : }
284 39402 : } else {
285 127380 : if (myDUAStyle || myWriteCosts) {
286 32936 : os.writeAttr(SUMO_ATTR_COST, myHolder.getRoute().getCosts());
287 : }
288 127380 : if (myWriteCosts) {
289 32930 : os.writeAttr(SUMO_ATTR_SAVINGS, myHolder.getRoute().getSavings());
290 : }
291 127380 : OutputDevice_String edgesD;
292 : int numWritten = 0;
293 : int start = 0;
294 127380 : if (myHolder.getNumberReroutes() > 0) {
295 : assert((int)myReplacedRoutes.size() <= myHolder.getNumberReroutes());
296 96117 : for (int i = routesToSkip; i < (int)myReplacedRoutes.size(); i++) {
297 38596 : if (myReplacedRoutes[i].edge != nullptr) {
298 37454 : int end = myReplacedRoutes[i].lastRouteIndex;
299 37454 : numWritten += myReplacedRoutes[i].route->writeEdgeIDs(edgesD, start, end, myWriteInternal, myHolder.getVClass());
300 : }
301 38596 : start = myReplacedRoutes[i].newRouteIndex;
302 : }
303 : }
304 127380 : numWritten += myCurrentRoute->writeEdgeIDs(edgesD, start, -1, myWriteInternal, myHolder.getVClass());
305 127380 : std::string edgesS = edgesD.getString();
306 : edgesS.pop_back(); // remove last ' '
307 127380 : os.writeAttr(SUMO_ATTR_EDGES, edgesS);
308 :
309 127380 : if (mySaveExits) {
310 : std::vector<std::string> exits;
311 133841 : for (SUMOTime t : myExits) {
312 247374 : exits.push_back(time2string(t));
313 : }
314 : assert(numWritten >= (int)myExits.size());
315 20308 : std::vector<std::string> missing(numWritten - (int)myExits.size(), "-1");
316 10154 : exits.insert(exits.end(), missing.begin(), missing.end());
317 10154 : os.writeAttr(SUMO_ATTR_EXITTIMES, exits);
318 10154 : }
319 127380 : }
320 333564 : os.closeTag();
321 : }
322 :
323 :
324 : void
325 126027 : MSDevice_Vehroutes::generateOutput(OutputDevice* /*tripinfoOut*/) const {
326 126027 : writeOutput(true);
327 126027 : }
328 :
329 :
330 : void
331 127386 : MSDevice_Vehroutes::writeOutput(const bool hasArrived) const {
332 127386 : const OptionsCont& oc = OptionsCont::getOptions();
333 127386 : OutputDevice& routeOut = OutputDevice::getDeviceByOption("vehroute-output");
334 127386 : OutputDevice_String stringOut(1);
335 127386 : OutputDevice& od = mySorted && routeOut.isXML() ? stringOut : routeOut;
336 127386 : SUMOVehicleParameter tmp = myHolder.getParameter();
337 127386 : tmp.depart = myIntendedDepart ? myHolder.getParameter().depart : myHolder.getDeparture();
338 127386 : if (!MSGlobals::gUseMesoSim) {
339 91424 : if (tmp.wasSet(VEHPARS_DEPARTLANE_SET)) {
340 14434 : tmp.departLaneProcedure = DepartLaneDefinition::GIVEN;
341 14434 : tmp.departLane = myDepartLane;
342 : }
343 91424 : if (tmp.wasSet(VEHPARS_DEPARTPOSLAT_SET)) {
344 8 : tmp.departPosLatProcedure = (tmp.departPosLatProcedure == DepartPosLatDefinition::RANDOM
345 4 : ? DepartPosLatDefinition::GIVEN_VEHROUTE
346 : : DepartPosLatDefinition::GIVEN);
347 4 : tmp.departPosLat = myDepartPosLat;
348 : }
349 : }
350 127386 : if (tmp.wasSet(VEHPARS_DEPARTPOS_SET)) {
351 72548 : tmp.departPosProcedure = ((tmp.departPosProcedure != DepartPosDefinition::GIVEN
352 36274 : && tmp.departPosProcedure != DepartPosDefinition::STOP)
353 36274 : ? DepartPosDefinition::GIVEN_VEHROUTE
354 : : DepartPosDefinition::GIVEN);
355 36274 : tmp.departPos = myDepartPos;
356 : }
357 127386 : if (tmp.wasSet(VEHPARS_DEPARTSPEED_SET)) {
358 94446 : tmp.departSpeedProcedure = ((tmp.departSpeedProcedure != DepartSpeedDefinition::GIVEN
359 47223 : && tmp.departSpeedProcedure != DepartSpeedDefinition::LIMIT)
360 47223 : ? DepartSpeedDefinition::GIVEN_VEHROUTE
361 : : DepartSpeedDefinition::GIVEN);
362 47223 : tmp.departSpeed = myDepartSpeed;
363 : }
364 380152 : if (oc.getBool("vehroute-output.speedfactor") ||
365 456303 : (oc.isDefault("vehroute-output.speedfactor") && tmp.wasSet(VEHPARS_DEPARTSPEED_SET))) {
366 49223 : tmp.parametersSet |= VEHPARS_SPEEDFACTOR_SET;
367 49223 : tmp.speedFactor = myHolder.getChosenSpeedFactor();
368 : }
369 :
370 127386 : const std::string typeID = myHolder.getVehicleType().getID() != DEFAULT_VTYPE_ID ? myHolder.getVehicleType().getID() : "";
371 127386 : tmp.write(od, oc, SUMO_TAG_VEHICLE, typeID);
372 127386 : if (hasArrived) {
373 252054 : od.writeAttr("arrival", time2string(MSNet::getInstance()->getCurrentTimeStep()));
374 : }
375 127386 : if (myRouteLength) {
376 951 : const bool includeInternalLengths = MSGlobals::gUsingInternalLanes && MSNet::getInstance()->hasInternalLinks();
377 951 : const double finalPos = hasArrived ? myHolder.getArrivalPos() : myHolder.getPositionOnLane();
378 1902 : const double routeLength = myHolder.getRoute().getDistanceBetween(myHolder.getDepartPos(), finalPos,
379 951 : myHolder.getRoute().begin(), myHolder.getCurrentRouteEdge(), includeInternalLengths);
380 951 : od.writeAttr("routeLength", routeLength);
381 : }
382 127386 : if (myDUAStyle) {
383 12 : const RandomDistributor<ConstMSRoutePtr>* const routeDist = MSRoute::distDictionary("!" + myHolder.getID());
384 12 : if (routeDist != nullptr) {
385 : const std::vector<ConstMSRoutePtr>& routes = routeDist->getVals();
386 6 : unsigned index = 0;
387 6 : while (index < routes.size() && routes[index] != myCurrentRoute) {
388 0 : ++index;
389 : }
390 6 : od.openTag(SUMO_TAG_ROUTE_DISTRIBUTION).writeAttr(SUMO_ATTR_LAST, index);
391 : const std::vector<double>& probs = routeDist->getProbs();
392 18 : for (int i = 0; i < (int)routes.size(); ++i) {
393 12 : od.setPrecision();
394 12 : od.openTag(SUMO_TAG_ROUTE);
395 12 : od.writeAttr(SUMO_ATTR_COST, routes[i]->getCosts());
396 12 : if (myWriteCosts) {
397 0 : od.writeAttr(SUMO_ATTR_SAVINGS, routes[i]->getSavings());
398 : }
399 12 : od.setPrecision(8);
400 12 : od.writeAttr(SUMO_ATTR_PROB, probs[i]);
401 12 : od.setPrecision();
402 12 : OutputDevice_String edgesD;
403 12 : routes[i]->writeEdgeIDs(edgesD, 0, -1, myWriteInternal, myHolder.getVClass());
404 12 : std::string edgesS = edgesD.getString();
405 : edgesS.pop_back(); // remove last ' '
406 12 : od.writeAttr(SUMO_ATTR_EDGES, edgesS);
407 24 : od.closeTag();
408 12 : }
409 12 : od.closeTag();
410 : } else {
411 6 : writeXMLRoute(od);
412 : }
413 : } else {
414 : std::string dummyMsg;
415 127374 : const int routesToSkip = (myHolder.getParameter().wasSet(VEHPARS_FORCE_REROUTE)
416 49332 : && !myIncludeIncomplete
417 49212 : && myReplacedRoutes.size() > 0
418 224462 : && !myHolder.hasValidRoute(dummyMsg, myReplacedRoutes[0].route) ? 1 : 0);
419 127374 : if ((int)myReplacedRoutes.size() > routesToSkip) {
420 22091 : od.openTag(SUMO_TAG_ROUTE_DISTRIBUTION);
421 61493 : for (int i = routesToSkip; i < (int)myReplacedRoutes.size(); ++i) {
422 39402 : writeXMLRoute(od, i);
423 : }
424 22091 : writeXMLRoute(od);
425 44182 : od.closeTag();
426 : } else {
427 105283 : writeXMLRoute(od);
428 : }
429 : }
430 127386 : if (routeOut.isXML()) {
431 254072 : od << myStopOut.getString();
432 : }
433 127386 : myHolder.getParameter().writeParams(od);
434 127386 : od.closeTag();
435 127386 : od.lf();
436 127386 : if (mySorted && routeOut.isXML()) {
437 : // numerical id reflects loading order
438 8844 : writeSortedOutput(&myRouteInfos, tmp.depart, myHolder.getNumericalID(), stringOut.getString());
439 : }
440 127386 : }
441 :
442 :
443 : ConstMSRoutePtr
444 0 : MSDevice_Vehroutes::getRoute(int index) const {
445 0 : if (index < (int)myReplacedRoutes.size()) {
446 0 : return myReplacedRoutes[index].route;
447 : } else {
448 : return nullptr;
449 : }
450 : }
451 :
452 :
453 : void
454 97301 : MSDevice_Vehroutes::addRoute(const std::string& info) {
455 97301 : if (myMaxRoutes > 0) {
456 : //std::cout << SIMTIME << " " << getID() << " departed=" << myHolder.hasDeparted() << " lastIndex=" << myLastRouteIndex << " start=" << myHolder.getRoutePosition() << "\n";
457 437732 : myReplacedRoutes.push_back(RouteReplaceInfo(
458 129827 : myHolder.hasDeparted() ? myHolder.getEdge() : nullptr,
459 : MSNet::getInstance()->getCurrentTimeStep(), myCurrentRoute, info,
460 : myLastRouteIndex,
461 129827 : myHolder.hasDeparted() ? myHolder.getRoutePosition() : 0));
462 89039 : if ((int)myReplacedRoutes.size() > myMaxRoutes) {
463 : myReplacedRoutes.erase(myReplacedRoutes.begin());
464 : }
465 : }
466 97301 : myCurrentRoute = myHolder.getRoutePtr();
467 97301 : }
468 :
469 :
470 : void
471 39692 : MSDevice_Vehroutes::writePendingOutput(const bool includeUnfinished) {
472 39692 : MSNet* const net = MSNet::getInstance();
473 :
474 39692 : if (!includeUnfinished) {
475 39285 : if (mySorted) {
476 117 : for (const auto& routeInfo : myRouteInfos.routeXML) {
477 36 : for (const auto& rouXML : routeInfo.second) {
478 20 : (*myRouteInfos.routeOut) << rouXML.second;
479 : }
480 : }
481 101 : if (net->hasPersons()) {
482 22 : const SortedRouteInfo& personRouteInfos = net->getPersonControl().getRouteInfo();
483 22 : if (personRouteInfos.routeOut != myRouteInfos.routeOut) {
484 0 : for (const auto& routeInfo : personRouteInfos.routeXML) {
485 0 : for (const auto& rouXML : routeInfo.second) {
486 0 : (*personRouteInfos.routeOut) << rouXML.second;
487 : }
488 : }
489 : }
490 : }
491 : }
492 39285 : return;
493 : }
494 1766 : for (const auto& it : myStateListener.myDevices) {
495 1359 : if (it.first->hasDeparted()) {
496 1359 : if (it.first->isStopped()) {
497 728 : it.second->notifyStopEnded();
498 : }
499 1359 : it.second->writeOutput(false);
500 : }
501 : }
502 : // unfinished persons
503 407 : if (net->hasPersons()) {
504 229 : MSTransportableControl& pc = net->getPersonControl();
505 301 : while (pc.loadedBegin() != pc.loadedEnd()) {
506 72 : pc.erase(pc.loadedBegin()->second);
507 : }
508 : }
509 : // Flush any remaining sorted outputs that may still be buffered
510 407 : if (mySorted) {
511 4 : for (const auto& routeInfo : myRouteInfos.routeXML) {
512 4 : for (const auto& rouXML : routeInfo.second) {
513 2 : (*myRouteInfos.routeOut) << rouXML.second;
514 : }
515 : }
516 2 : if (net->hasPersons()) {
517 0 : const SortedRouteInfo& personRouteInfos = net->getPersonControl().getRouteInfo();
518 0 : if (personRouteInfos.routeOut != myRouteInfos.routeOut) {
519 0 : for (const auto& routeInfo : personRouteInfos.routeXML) {
520 0 : for (const auto& rouXML : routeInfo.second) {
521 0 : (*personRouteInfos.routeOut) << rouXML.second;
522 : }
523 : }
524 : }
525 : }
526 : }
527 : }
528 :
529 :
530 : void
531 108 : MSDevice_Vehroutes::registerTransportableDepart(SUMOTime depart) {
532 108 : myRouteInfos.departureCounts[depart]++;
533 108 : }
534 :
535 :
536 : void
537 4544 : MSDevice_Vehroutes::writeSortedOutput(MSDevice_Vehroutes::SortedRouteInfo* routeInfo, SUMOTime depart, const SUMOTrafficObject::NumericalID id, const std::string& xmlOutput) {
538 4544 : if (routeInfo->routeOut == myRouteInfos.routeOut) {
539 : routeInfo = &myRouteInfos;
540 : }
541 4544 : routeInfo->routeXML[depart][id] = xmlOutput;
542 4544 : routeInfo->departureCounts[depart]--;
543 : std::map<const SUMOTime, int>::iterator it = routeInfo->departureCounts.begin();
544 7771 : while (it != routeInfo->departureCounts.end() && it->second == 0) {
545 7749 : for (const auto& rouXML : routeInfo->routeXML[it->first]) {
546 4522 : (*routeInfo->routeOut) << rouXML.second;
547 : }
548 : routeInfo->routeXML.erase(it->first);
549 : it = routeInfo->departureCounts.erase(it);
550 : }
551 4544 : }
552 :
553 :
554 : void
555 3147 : MSDevice_Vehroutes::saveState(OutputDevice& out) const {
556 3147 : out.openTag(SUMO_TAG_DEVICE);
557 3147 : out.writeAttr(SUMO_ATTR_ID, getID());
558 : std::vector<std::string> internals;
559 3147 : if (!MSGlobals::gUseMesoSim) {
560 1835 : internals.push_back(toString(myDepartLane));
561 3670 : internals.push_back(toString(myDepartPosLat));
562 : }
563 3147 : internals.push_back(toString(myDepartSpeed));
564 3147 : internals.push_back(toString(myDepartPos));
565 3147 : internals.push_back(toString(myReplacedRoutes.size()));
566 5984 : for (int i = 0; i < (int)myReplacedRoutes.size(); ++i) {
567 2837 : const std::string replacedOnEdge = myReplacedRoutes[i].edge == nullptr ? "!NULL" : myReplacedRoutes[i].edge->getID();
568 2837 : internals.push_back(replacedOnEdge);
569 5674 : internals.push_back(toString(myReplacedRoutes[i].time));
570 2837 : internals.push_back(myReplacedRoutes[i].route->getID());
571 2837 : internals.push_back(myReplacedRoutes[i].info);
572 2837 : internals.push_back(toString(myReplacedRoutes[i].lastRouteIndex));
573 5674 : internals.push_back(toString(myReplacedRoutes[i].newRouteIndex));
574 : }
575 3147 : out.writeAttr(SUMO_ATTR_STATE, toString(internals));
576 3147 : if (mySaveExits && myExits.size() > 0) {
577 9 : out.writeAttr(SUMO_ATTR_EXITTIMES, myExits);
578 9 : out.writeAttr(SUMO_ATTR_EDGE, myLastSavedAt->getID());
579 : }
580 3147 : out.closeTag();
581 3147 : }
582 :
583 :
584 : void
585 1908 : MSDevice_Vehroutes::loadState(const SUMOSAXAttributes& attrs) {
586 1908 : std::istringstream bis(attrs.getString(SUMO_ATTR_STATE));
587 1908 : if (!MSGlobals::gUseMesoSim) {
588 1304 : bis >> myDepartLane;
589 1304 : bis >> myDepartPosLat;
590 : }
591 1908 : bis >> myDepartSpeed;
592 1908 : bis >> myDepartPos;
593 : int size;
594 1908 : bis >> size;
595 3496 : for (int i = 0; i < size; ++i) {
596 : std::string edgeID;
597 : SUMOTime time;
598 : std::string routeID;
599 : std::string info;
600 : int lastIndex;
601 : int newIndex;
602 1588 : bis >> edgeID;
603 : bis >> time;
604 1588 : bis >> routeID;
605 1588 : bis >> info;
606 1588 : bis >> lastIndex;
607 1588 : bis >> newIndex;
608 :
609 1588 : ConstMSRoutePtr route = MSRoute::dictionary(routeID);
610 1588 : if (route != nullptr) {
611 2814 : myReplacedRoutes.push_back(RouteReplaceInfo(MSEdge::dictionary(edgeID), time, route, info, lastIndex, newIndex));
612 : }
613 : }
614 1908 : if (mySaveExits && attrs.hasAttribute(SUMO_ATTR_EXITTIMES)) {
615 9 : bool ok = true;
616 20 : for (const std::string& t : attrs.get<std::vector<std::string> >(SUMO_ATTR_EXITTIMES, nullptr, ok)) {
617 11 : myExits.push_back(StringUtils::toLong(t));
618 9 : }
619 9 : if (attrs.hasAttribute(SUMO_ATTR_EDGE)) {
620 9 : myLastSavedAt = MSEdge::dictionary(attrs.getString(SUMO_ATTR_EDGE));
621 : }
622 : }
623 1908 : if (myHolder.hasDeparted()) {
624 751 : myLastRouteIndex = myHolder.getRoutePosition();
625 : }
626 1908 : }
627 :
628 :
629 : /****************************************************************************/
|