1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
10 /// @file    RORouteDef.h
11 /// @author  Daniel Krajzewicz
12 /// @author  Michael Behrisch
13 /// @author  Jakob Erdmann
14 /// @date    Sept 2002
15 /// @version $Id$
16 ///
17 // Base class for a vehicle's route definition
18 /****************************************************************************/
19 #ifndef RORouteDef_h
20 #define RORouteDef_h
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <string>
29 #include <iostream>
30 #include <utils/common/Named.h>
31 #include <utils/router/SUMOAbstractRouter.h>
32 #include "RORoute.h"
33 
34 
35 // ===========================================================================
36 // class declarations
37 // ===========================================================================
38 class ROEdge;
39 class OptionsCont;
40 class ROVehicle;
41 class OutputDevice;
42 
43 
44 // ===========================================================================
45 // class definitions
46 // ===========================================================================
47 /**
48  * @class RORouteDef
49  * @brief Base class for a vehicle's route definition
50  *
51  * This class resembles what a vehicle knows about his route when being loaded
52  *  into a router. Whether it is just the origin and the destination, the whole
53  *  route through the network or even a route with alternatives depends on
54  *  the derived class.
55  */
56 class RORouteDef : public Named {
57 public:
58     /** @brief Constructor
59      *
60      * @param[in] id The id of the route
61      * @param[in] color The color of the route
62      */
63     RORouteDef(const std::string& id, const int lastUsed,
64                const bool tryRepair, const bool mayBeDisconnected);
65 
66 
67     /// @brief Destructor
68     virtual ~RORouteDef();
69 
70 
71     /** @brief Adds a single alternative loaded from the file
72         An alternative may also be generated during DUA */
73     void addLoadedAlternative(RORoute* alternative);
74 
75     /** @brief Adds an alternative loaded from the file */
76     void addAlternativeDef(const RORouteDef* alternative);
77 
78     /** @brief Triggers building of the complete route (via
79      * preComputeCurrentRoute) or returns precomputed route */
80     RORoute* buildCurrentRoute(SUMOAbstractRouter<ROEdge, ROVehicle>& router, SUMOTime begin,
81                                const ROVehicle& veh) const;
82 
83     /** @brief Builds the complete route
84      * (or chooses her from the list of alternatives, when existing) */
85     void preComputeCurrentRoute(SUMOAbstractRouter<ROEdge, ROVehicle>& router, SUMOTime begin,
86                                 const ROVehicle& veh) const;
87 
88     /** @brief Builds the complete route
89      * (or chooses her from the list of alternatives, when existing) */
90     bool repairCurrentRoute(SUMOAbstractRouter<ROEdge, ROVehicle>& router, SUMOTime begin,
91                             const ROVehicle& veh, ConstROEdgeVector oldEdges, ConstROEdgeVector& newEdges) const;
92 
93     /** @brief Adds an alternative to the list of routes
94     *
95      * (This may be the new route) */
96     void addAlternative(SUMOAbstractRouter<ROEdge, ROVehicle>& router,
97                         const ROVehicle* const, RORoute* current, SUMOTime begin);
98 
99     const ROEdge* getDestination() const;
100 
getFirstRoute()101     const RORoute* getFirstRoute() const {
102         if (myAlternatives.empty()) {
103             return 0;
104         }
105         return myAlternatives.front();
106     }
107 
108     /** @brief Saves the built route / route alternatives
109      *
110      * Writes the route into the given stream.
111      *
112      * @param[in] dev The device to write the route into
113      * @param[in] asAlternatives Whether the route shall be saved as route alternatives
114      * @return The same device for further usage
115      */
116     OutputDevice& writeXMLDefinition(OutputDevice& dev, const ROVehicle* const veh,
117                                      bool asAlternatives, bool withExitTimes) const;
118 
119     /** @brief Returns a origin-destination copy of the route definition.
120      *
121      * The resulting route definition contains only a single route with
122      * origin and destination edge copied from this one
123      *
124      * @param[in] id The id for the new route definition
125      * @return the new route definition
126      */
127     RORouteDef* copyOrigDest(const std::string& id) const;
128 
129     /** @brief Returns a deep copy of the route definition.
130      *
131      * The resulting route definition contains copies of all
132      * routes contained in this one
133      *
134      * @param[in] id The id for the new route definition
135      * @param[in] stopOffset The offset time for "until"-stops defined in the original route
136      * @return the new route definition
137      */
138     RORouteDef* copy(const std::string& id, const SUMOTime stopOffset) const;
139 
140     /** @brief Returns the sum of the probablities of the contained routes */
141     double getOverallProb() const;
142 
setUsingJTRR()143     static void setUsingJTRR() {
144         myUsingJTRR = true;
145     }
146 
147 protected:
148     /// @brief precomputed route for out-of-order computation
149     mutable RORoute* myPrecomputed;
150 
151     /// @brief Index of the route used within the last step
152     mutable int myLastUsed;
153 
154     /// @brief The alternatives
155     std::vector<RORoute*> myAlternatives;
156 
157     /// @brief Routes which are deleted someplace else
158     std::set<RORoute*> myRouteRefs;
159 
160     /// @brief Information whether a new route was generated
161     mutable bool myNewRoute;
162 
163     const bool myTryRepair;
164     const bool myMayBeDisconnected;
165 
166     static bool myUsingJTRR;
167 
168 private:
169     /** Function-object for sorting routes from highest to lowest probability. */
170     struct ComparatorProbability {
operatorComparatorProbability171         bool operator()(const RORoute* const a, const RORoute* const b) {
172             return a->getProbability() > b->getProbability();
173         }
174     };
175 
176 private:
177     /// @brief Invalidated copy constructor
178     RORouteDef(const RORouteDef& src);
179 
180     /// @brief Invalidated assignment operator
181     RORouteDef& operator=(const RORouteDef& src);
182 
183 };
184 
185 
186 #endif
187 
188 /****************************************************************************/
189 
190