Eclipse SUMO - Simulation of Urban MObility
ROJTRTurnDefLoader.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2001-2024 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 /****************************************************************************/
20 // Loader for the of turning percentages and source/sink definitions
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <set>
25 #include <string>
27 #include <utils/xml/XMLSubSys.h>
31 #include <utils/common/ToString.h>
35 #include <router/RONet.h>
36 #include "ROJTREdge.h"
37 #include "ROJTRTurnDefLoader.h"
38 
39 
40 // ===========================================================================
41 // method definitions
42 // ===========================================================================
44  SUMOSAXHandler("turn-ratio-file"), myNet(net),
45  myIntervalBegin(0), myIntervalEnd(STEPS2TIME(SUMOTime_MAX)),
46  myEdge(nullptr),
47  mySourcesAreSinks(OptionsCont::getOptions().getBool("sources-are-sinks")),
48  myDiscountSources(OptionsCont::getOptions().getBool("discount-sources")),
49  myHaveWarnedAboutDeprecatedFormat(false)
50 {}
51 
52 
54 
55 
56 void
58  const SUMOSAXAttributes& attrs) {
59  bool ok = true;
60  switch (element) {
61  case SUMO_TAG_INTERVAL:
62  myIntervalBegin = attrs.get<double>(SUMO_ATTR_BEGIN, nullptr, ok);
63  myIntervalEnd = attrs.get<double>(SUMO_ATTR_END, nullptr, ok);
64  break;
65  case SUMO_TAG_FROMEDGE:
68  WRITE_WARNINGF(TL("The turn-file format with elements %, % is deprecated, please use % instead."),
70  }
71  beginFromEdge(attrs);
72  break;
73  case SUMO_TAG_TOEDGE:
74  addToEdge(attrs);
75  break;
76  case SUMO_TAG_EDGEREL:
77  addEdgeRel(attrs);
78  break;
79  case SUMO_TAG_SINK:
80  if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
81  std::string edges = attrs.get<std::string>(SUMO_ATTR_EDGES, nullptr, ok);
83  while (st.hasNext()) {
84  std::string id = st.next();
85  ROEdge* edge = myNet.getEdge(id);
86  if (edge == nullptr) {
87  throw ProcessError(TLF("The edge '%' declared as a sink is not known.", id));
88  }
89  edge->setSink();
90  }
91  }
92  break;
93  case SUMO_TAG_FLOW: {
94  const std::string flowID = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
95  if (attrs.hasAttribute(SUMO_ATTR_FROM)) {
96  const std::string edgeID = attrs.get<std::string>(SUMO_ATTR_FROM, nullptr, ok);
97  ROEdge* edge = myNet.getEdge(edgeID);
98  if (edge == nullptr) {
99  throw ProcessError("The from-edge '" + edgeID + "' in flow '" + flowID + "' is not known.");
100  }
101  if (mySourcesAreSinks) {
102  edge->setSink();
103  }
104  if (myDiscountSources) {
106  int numVehs = 0;
107  if (pars->repetitionProbability > 0) {
108  numVehs = int(STEPS2TIME(pars->repetitionEnd - pars->depart) * pars->repetitionProbability);
109  } else {
110  numVehs = pars->repetitionNumber;
111  }
112  delete pars;
113  static_cast<ROJTREdge*>(edge)->changeSourceFlow(numVehs);
114  }
115  } else {
116  WRITE_WARNINGF(TL("Ignoring flow '%' without 'from'"), flowID);
117  }
118  break;
119  }
120  case SUMO_TAG_SOURCE:
121  if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
122  std::string edges = attrs.get<std::string>(SUMO_ATTR_EDGES, nullptr, ok);
124  while (st.hasNext()) {
125  std::string id = st.next();
126  ROEdge* edge = myNet.getEdge(id);
127  if (edge == nullptr) {
128  throw ProcessError(TLF("The edge '%' declared as a source is not known.", id));
129  }
130  edge->setSource();
131  }
132  }
133  break;
134  default:
135  break;
136  }
137 }
138 
139 
140 void
142  myEdge = nullptr;
143  bool ok = true;
144  // get the id, report an error if not given or empty...
145  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
146  if (!ok) {
147  return;
148  }
149  //
150  myEdge = static_cast<ROJTREdge*>(myNet.getEdge(id));
151  if (myEdge == nullptr) {
152  WRITE_ERRORF(TL("The edge '%' is not known within the network (within a 'from-edge' tag)."), id);
153  return;
154  }
155 }
156 
157 
158 void
160  if (myEdge == nullptr) {
161  return;
162  }
163  bool ok = true;
164  // get the id, report an error if not given or empty...
165  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
166  if (!ok) {
167  return;
168  }
169  //
170  ROJTREdge* edge = static_cast<ROJTREdge*>(myNet.getEdge(id));
171  if (edge == nullptr) {
172  WRITE_ERRORF(TL("The edge '%' is not known within the network (within a 'to-edge' tag)."), id);
173  return;
174  }
175  const double probability = attrs.get<double>(SUMO_ATTR_PROB, id.c_str(), ok);
176  if (ok) {
177  if (probability < 0) {
178  WRITE_ERRORF(TL("'probability' must be positive (in definition of to-edge '%')."), id);
179  } else {
181  }
182  }
183 }
184 
185 
186 void
188  bool ok = true;
189  // get the id, report an error if not given or empty...
190  std::string fromID = attrs.get<std::string>(SUMO_ATTR_FROM, nullptr, ok);
191  std::string toID = attrs.get<std::string>(SUMO_ATTR_TO, nullptr, ok);
192  double probability = attrs.get<double>(
194  (fromID + "->" + toID).c_str(), ok);
195  if (!ok) {
196  return;
197  }
198  //
199  ROJTREdge* from = static_cast<ROJTREdge*>(myNet.getEdge(fromID));
200  if (from == nullptr) {
201  WRITE_ERRORF(TL("The edge '%' is not known."), fromID);
202  return;
203  }
204  ROJTREdge* to = static_cast<ROJTREdge*>(myNet.getEdge(toID));
205  if (to == nullptr) {
206  WRITE_ERRORF(TL("The edge '%' is not known."), toID);
207  return;
208  }
209  if (probability < 0) {
210  WRITE_ERRORF(TL("'probability' must be positive (in edgeRelation from '%' to '%'."), fromID, toID);
211  } else {
212  from->addFollowerProbability(to, myIntervalBegin, myIntervalEnd, probability);
213  }
214 }
215 
216 
217 /****************************************************************************/
#define WRITE_WARNINGF(...)
Definition: MsgHandler.h:296
#define WRITE_ERRORF(...)
Definition: MsgHandler.h:305
#define TL(string)
Definition: MsgHandler.h:315
#define TLF(string,...)
Definition: MsgHandler.h:317
#define STEPS2TIME(x)
Definition: SUMOTime.h:55
#define SUMOTime_MAX
Definition: SUMOTime.h:34
#define TIME2STEPS(x)
Definition: SUMOTime.h:57
@ SUMO_TAG_INTERVAL
an aggreagated-output interval
@ SUMO_TAG_EDGEREL
a relation between two edges
@ SUMO_TAG_TOEDGE
Outgoing edge specification (jtrrouter)
@ SUMO_TAG_FLOW
a flow definition using from and to edges or a route
@ SUMO_TAG_FROMEDGE
Incoming edge specification (jtrrouter)
@ SUMO_TAG_SINK
Sink(s) specification.
@ SUMO_TAG_SOURCE
a source
@ SUMO_ATTR_BEGIN
weights: time range begin
@ SUMO_ATTR_EDGES
the edges of a route
@ SUMO_ATTR_TO
@ SUMO_ATTR_FROM
@ SUMO_ATTR_END
weights: time range end
@ SUMO_ATTR_PROB
@ SUMO_ATTR_ID
@ SUMO_ATTR_COUNT
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
A storage for options typed value containers)
Definition: OptionsCont.h:89
A basic edge for routing applications.
Definition: ROEdge.h:70
void setSource(const bool isSource=true)
Sets whether the edge is a source.
Definition: ROEdge.h:120
void setSink(const bool isSink=true)
Sets whether the edge is a sink.
Definition: ROEdge.h:128
An edge the jtr-router may route through.
Definition: ROJTREdge.h:48
void addFollowerProbability(ROJTREdge *follower, double begTime, double endTime, double probability)
adds the information about the percentage of using a certain follower
Definition: ROJTREdge.cpp:60
~ROJTRTurnDefLoader()
Destructor.
ROJTRTurnDefLoader(RONet &net)
Constructor.
void addEdgeRel(const SUMOSAXAttributes &attrs)
Parses the probability to use a certain incoming-outgoing edge relation.
void addToEdge(const SUMOSAXAttributes &attrs)
Parses the probability to use a certain outgoing edge.
bool myHaveWarnedAboutDeprecatedFormat
whether the warning for the deprecated format has been issued
ROJTREdge * myEdge
The current incoming edge the turning probabilities are set into.
double myIntervalBegin
The begin and the end of the current interval.
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
RONet & myNet
The network to set the information into.
bool mySourcesAreSinks
whether all sources are sinks
void beginFromEdge(const SUMOSAXAttributes &attrs)
Begins the processing of a incoming edge definition.
bool myDiscountSources
whether upstream flows should be discounted from source flows
The router's network representation.
Definition: RONet.h:62
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:157
Encapsulated SAX-Attributes.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
SAX-handler base for SUMO-files.
Structure representing possible vehicle parameter.
double repetitionProbability
The probability for emitting a vehicle per second.
SUMOTime repetitionEnd
The time at which the flow ends (only needed when using repetitionProbability)
static SUMOVehicleParameter * parseFlowAttributes(SumoXMLTag tag, const SUMOSAXAttributes &attrs, const bool hardFail, const bool needID, const SUMOTime beginDefault, const SUMOTime endDefault, const bool allowInternalRoutes=false)
Parses a flow's attributes.
static const int WHITECHARS
identifier for splitting the given string at all whitespace characters