SUMO - Simulation of Urban MObility
Main Page
Related Pages
Modules
Data Structures
Files
File List
Globals
All
Data Structures
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
RORDLoader_SUMOBase.cpp
Go to the documentation of this file.
1
/****************************************************************************/
9
// The base class for SUMO-native route handlers
10
/****************************************************************************/
11
// SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12
// Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
13
/****************************************************************************/
14
//
15
// This file is part of SUMO.
16
// SUMO is free software: you can redistribute it and/or modify
17
// it under the terms of the GNU General Public License as published by
18
// the Free Software Foundation, either version 3 of the License, or
19
// (at your option) any later version.
20
//
21
/****************************************************************************/
22
23
24
// ===========================================================================
25
// included modules
26
// ===========================================================================
27
#ifdef _MSC_VER
28
#include <
windows_config.h
>
29
#else
30
#include <
config.h
>
31
#endif
32
33
#include "
RORDLoader_SUMOBase.h
"
34
#include <
utils/common/SUMOVTypeParameter.h
>
35
#include "
RORouteDef.h
"
36
#include "
RONet.h
"
37
#include <
utils/common/UtilExceptions.h
>
38
#include <
utils/common/MsgHandler.h
>
39
#include <
utils/common/StringTokenizer.h
>
40
#include <
utils/common/ToString.h
>
41
#include "
ROVehicle.h
"
42
#include "
RORouteDef_Alternatives.h
"
43
#include "
RORouteDef_Complete.h
"
44
#include "
RORoute.h
"
45
#include <
utils/xml/SUMOVehicleParserHelper.h
>
46
47
#ifdef CHECK_MEMORY_LEAKS
48
#include <
foreign/nvwa/debug_new.h
>
49
#endif // CHECK_MEMORY_LEAKS
50
51
52
// ===========================================================================
53
// method definitions
54
// ===========================================================================
55
RORDLoader_SUMOBase::RORDLoader_SUMOBase
(
RONet
& net,
56
SUMOTime
begin,
SUMOTime
end,
const
int
maxRouteNumber,
const
bool
tryRepair,
57
const
bool
withTaz,
const
bool
keepRoutes,
58
const
bool
skipRouteCalculation,
const
std::string& file)
59
:
ROTypedXMLRoutesLoader
(net, begin, end, file),
60
myVehicleParameter(0), myCurrentIsOk(true), myAltIsValid(true),
61
myCurrentAlternatives(0), myMaxRouteNumber(maxRouteNumber),
62
myCurrentRoute(0), myTryRepair(tryRepair), myWithTaz(withTaz), myKeepRoutes(keepRoutes),
63
mySkipRouteCalculation(skipRouteCalculation), myColor(0), myCurrentVType(0),
64
myHaveWarnedAboutDeprecatedVType(false), myHaveWarnedAboutDeprecatedRoute(false) {
65
}
66
67
68
RORDLoader_SUMOBase::~RORDLoader_SUMOBase
() {
69
// clean up (on failure)
70
delete
myCurrentAlternatives
;
71
delete
myCurrentRoute
;
72
delete
myVehicleParameter
;
73
delete
myColor
;
74
}
75
76
77
void
78
RORDLoader_SUMOBase::myStartElement
(
int
element,
79
const
SUMOSAXAttributes
& attrs) {
80
switch
(element) {
81
case
SUMO_TAG_ROUTE
:
82
startRoute
(attrs);
83
break
;
84
case
SUMO_TAG_VEHICLE
:
85
// try to parse the vehicle definition
86
delete
myVehicleParameter
;
87
myVehicleParameter
= 0;
88
myVehicleParameter
=
SUMOVehicleParserHelper::parseVehicleAttributes
(attrs);
89
if
(
myVehicleParameter
!= 0) {
90
myCurrentDepart
=
myVehicleParameter
->
depart
;
91
}
92
myCurrentIsOk
=
myVehicleParameter
!= 0;
93
break
;
94
case
SUMO_TAG_VTYPE__DEPRECATED
:
95
if
(!
myHaveWarnedAboutDeprecatedVType
) {
96
myHaveWarnedAboutDeprecatedVType
=
true
;
97
WRITE_WARNING
(
"'"
+
toString
(
SUMO_TAG_VTYPE__DEPRECATED
) +
"' is deprecated; please use '"
+
toString
(
SUMO_TAG_VTYPE
) +
"'."
);
98
}
99
case
SUMO_TAG_VTYPE
:
100
myCurrentVType
=
SUMOVehicleParserHelper::beginVTypeParsing
(attrs);
101
break
;
102
case
SUMO_TAG_ROUTE_DISTRIBUTION
:
103
myAltIsValid
=
true
;
104
startAlternative
(attrs);
105
if
(!
myCurrentIsOk
) {
106
myAltIsValid
=
false
;
107
}
108
break
;
109
default
:
110
break
;
111
}
112
// parse embedded vtype information
113
if
(
myCurrentVType
!= 0 && element !=
SUMO_TAG_VTYPE
&& element !=
SUMO_TAG_VTYPE__DEPRECATED
) {
114
SUMOVehicleParserHelper::parseVTypeEmbedded
(*
myCurrentVType
, element, attrs);
115
return
;
116
}
117
if
(!
myCurrentIsOk
) {
118
throw
ProcessError
();
119
}
120
}
121
122
123
void
124
RORDLoader_SUMOBase::startRoute
(
const
SUMOSAXAttributes
& attrs) {
125
delete
myColor
;
126
myColor
= 0;
127
if
(!
myAltIsValid
) {
128
return
;
129
}
130
if
(
myCurrentAlternatives
== 0) {
131
myCurrentIsOk
=
true
;
132
if
(
myVehicleParameter
!= 0) {
133
if
(attrs.
hasAttribute
(
SUMO_ATTR_ID
)) {
134
WRITE_ERROR
(
"Internal routes do not have an id (vehicle '"
+
myVehicleParameter
->
id
+
"')."
);
135
myCurrentIsOk
=
false
;
136
return
;
137
}
138
myCurrentRouteName
=
"!"
+
myVehicleParameter
->
id
;
139
}
else
{
140
myCurrentRouteName
= attrs.
getStringReporting
(
SUMO_ATTR_ID
, 0,
myCurrentIsOk
);
141
}
142
}
else
{
143
// parse route alternative...
144
myCost
= attrs.
getOptSUMORealReporting
(
SUMO_ATTR_COST
,
myCurrentAlternatives
->
getID
().c_str(),
myCurrentIsOk
, -1);
145
myProbability
= attrs.
getSUMORealReporting
(
SUMO_ATTR_PROB
,
myCurrentAlternatives
->
getID
().c_str(),
myCurrentIsOk
);
146
if
(
myCurrentIsOk
&&
myCost
< 0 &&
myCost
!= -1) {
147
WRITE_ERROR
(
"Invalid cost in alternative for route '"
+
myCurrentAlternatives
->
getID
() +
"' ("
+ toString<SUMOReal>(
myCost
) +
")."
);
148
myCurrentIsOk
=
false
;
149
return
;
150
}
151
if
(
myCurrentIsOk
&&
myProbability
< 0) {
152
WRITE_ERROR
(
"Invalid probability in alternative for route '"
+
myCurrentAlternatives
->
getID
() +
"' ("
+ toString<SUMOReal>(
myProbability
) +
")."
);
153
myCurrentIsOk
=
false
;
154
return
;
155
}
156
}
157
if
(attrs.
hasAttribute
(
SUMO_ATTR_COLOR
)) {
158
myColor
=
new
RGBColor
(
RGBColor::parseColorReporting
(
159
attrs.
getString
(
SUMO_ATTR_COLOR
),
160
attrs.
getObjectType
(),
myCurrentRouteName
.c_str(),
true
,
myCurrentIsOk
));
161
}
162
if
(attrs.
hasAttribute
(
SUMO_ATTR_EDGES
)) {
163
myCharacters
(
SUMO_TAG_ROUTE
, attrs.
getStringReporting
(
SUMO_ATTR_EDGES
,
myCurrentRouteName
.c_str(),
myCurrentIsOk
));
164
}
else
{
165
if
(!
myHaveWarnedAboutDeprecatedRoute
) {
166
WRITE_WARNING
(
"Defining routes as a nested string is deprecated, use the edges attribute instead."
);
167
myHaveWarnedAboutDeprecatedRoute
=
true
;
168
}
169
}
170
}
171
172
173
void
174
RORDLoader_SUMOBase::startAlternative
(
const
SUMOSAXAttributes
& attrs) {
175
// try to get the id
176
myCurrentIsOk
=
true
;
177
std::string id;
178
if
(
myVehicleParameter
!= 0) {
179
id
=
myVehicleParameter
->
id
;
180
if
(
id
==
""
) {
181
WRITE_ERROR
(
"Missing 'id' of a routeDistribution."
);
182
myCurrentIsOk
=
false
;
183
return
;
184
}
185
id
=
"!"
+ id;
186
}
else
{
187
id
= attrs.
getStringReporting
(
SUMO_ATTR_ID
, 0,
myCurrentIsOk
);
188
if
(!
myCurrentIsOk
) {
189
return
;
190
}
191
}
192
// try to get the index of the last element
193
int
index = attrs.
getIntReporting
(
SUMO_ATTR_LAST
,
id
.c_str(),
myCurrentIsOk
);
194
if
(
myCurrentIsOk
&& index < 0) {
195
WRITE_ERROR
(
"Negative index of a route alternative (id='"
+
id
+
"')."
);
196
myCurrentIsOk
=
false
;
197
return
;
198
}
199
// build the alternative cont
200
myCurrentAlternatives
=
new
RORouteDef_Alternatives
(
id
, index,
201
myMaxRouteNumber
,
myKeepRoutes
,
mySkipRouteCalculation
);
202
}
203
204
void
205
RORDLoader_SUMOBase::myCharacters
(
int
element,
206
const
std::string& chars) {
207
// process routes only, all other elements do
208
// not have embedded characters
209
if
(element !=
SUMO_TAG_ROUTE
) {
210
return
;
211
}
212
if
(!
myAltIsValid
) {
213
return
;
214
}
215
if
(
myCurrentRoute
!= 0) {
216
return
;
217
}
218
// check whether the costs and the probability are valid
219
if
(
myCurrentAlternatives
!= 0 && !
myCurrentIsOk
) {
220
return
;
221
}
222
// build the list of edges
223
std::vector<const ROEdge*> *list =
new
std::vector<const ROEdge*>();
224
if
(
myWithTaz
&&
myVehicleParameter
->
wasSet
(
VEHPARS_TAZ_SET
)) {
225
ROEdge
* edge =
myNet
.
getEdge
(
myVehicleParameter
->
fromTaz
+
"-source"
);
226
if
(edge != 0) {
227
list->push_back(edge);
228
}
else
{
229
WRITE_ERROR
(
"The vehicle '"
+
myVehicleParameter
->
id
+
"' contains the unknown zone '"
+
myVehicleParameter
->
fromTaz
+
"'."
);
230
myCurrentIsOk
=
false
;
231
}
232
}
233
StringTokenizer
st(chars);
234
while
(
myCurrentIsOk
&& st.
hasNext
()) {
// !!! too slow !!!
235
const
std::string
id
= st.
next
();
236
ROEdge
* edge =
myNet
.
getEdge
(
id
);
237
if
(edge != 0) {
238
list->push_back(edge);
239
}
else
{
240
if
(!
myTryRepair
) {
241
std::string rid =
myCurrentAlternatives
!= 0 ?
myCurrentAlternatives
->
getID
() :
myCurrentRouteName
;
242
WRITE_ERROR
(
"The route '"
+ rid +
"' contains the unknown edge '"
+
id
+
"'."
);
243
myCurrentIsOk
=
false
;
244
}
245
}
246
}
247
if
(
myWithTaz
&&
myVehicleParameter
->
wasSet
(
VEHPARS_TAZ_SET
)) {
248
ROEdge
* edge =
myNet
.
getEdge
(
myVehicleParameter
->
toTaz
+
"-sink"
);
249
if
(edge != 0) {
250
list->push_back(edge);
251
}
else
{
252
WRITE_ERROR
(
"The vehicle '"
+
myVehicleParameter
->
id
+
"' contains the unknown zone '"
+
myVehicleParameter
->
toTaz
+
"'."
);
253
myCurrentIsOk
=
false
;
254
}
255
}
256
if
(
myCurrentIsOk
) {
257
if
(
myCurrentAlternatives
!= 0) {
258
myCurrentAlternatives
->
addLoadedAlternative
(
259
new
RORoute
(
myCurrentAlternatives
->
getID
(),
myCost
,
myProbability
, *list,
myColor
));
260
}
else
{
261
myCurrentRoute
=
new
RORouteDef_Complete
(
myCurrentRouteName
,
myColor
, *list,
myTryRepair
);
262
}
263
myColor
= 0;
264
}
265
delete
list;
266
}
267
268
269
void
270
RORDLoader_SUMOBase::myEndElement
(
int
element) {
271
switch
(element) {
272
case
SUMO_TAG_ROUTE
:
273
if
(!
myAltIsValid
) {
274
return
;
275
}
276
if
(
myCurrentRoute
!= 0 &&
myCurrentIsOk
) {
277
if
(
myCurrentAlternatives
== 0) {
278
myNet
.
addRouteDef
(
myCurrentRoute
);
279
myCurrentRoute
= 0;
280
}
281
if
(
myVehicleParameter
== 0) {
282
myNextRouteRead
=
true
;
283
}
284
myCurrentRoute
= 0;
285
}
286
break
;
287
case
SUMO_TAG_ROUTE_DISTRIBUTION
:
288
if
(!
myCurrentIsOk
) {
289
return
;
290
}
291
if
(
myVehicleParameter
== 0) {
292
myNextRouteRead
=
true
;
293
}
294
myNet
.
addRouteDef
(
myCurrentAlternatives
);
295
myCurrentRoute
= 0;
296
myCurrentAlternatives
= 0;
297
break
;
298
case
SUMO_TAG_VEHICLE
:
299
closeVehicle
();
300
delete
myVehicleParameter
;
301
myVehicleParameter
= 0;
302
myNextRouteRead
=
true
;
303
break
;
304
case
SUMO_TAG_VTYPE__DEPRECATED
:
305
case
SUMO_TAG_VTYPE
: {
306
SUMOVehicleParserHelper::closeVTypeParsing
(*
myCurrentVType
);
307
myNet
.
addVehicleType
(
myCurrentVType
);
308
myCurrentVType
= 0;
309
}
310
default
:
311
break
;
312
}
313
if
(!
myCurrentIsOk
) {
314
throw
ProcessError
();
315
}
316
}
317
318
319
bool
320
RORDLoader_SUMOBase::closeVehicle
() {
321
// get the vehicle id
322
if
(
myVehicleParameter
->
depart
<
myBegin
||
myVehicleParameter
->
depart
>=
myEnd
) {
323
myCurrentIsOk
=
false
;
324
return
false
;
325
}
326
// get vehicle type
327
SUMOVTypeParameter
* type =
myNet
.
getVehicleTypeSecure
(
myVehicleParameter
->
vtypeid
);
328
// get the route
329
RORouteDef
* route =
myNet
.
getRouteDef
(
myVehicleParameter
->
routeid
);
330
if
(route == 0) {
331
route =
myNet
.
getRouteDef
(
"!"
+
myVehicleParameter
->
id
);
332
}
333
if
(route == 0) {
334
WRITE_ERROR
(
"The route of the vehicle '"
+
myVehicleParameter
->
id
+
"' is not known."
);
335
myCurrentIsOk
=
false
;
336
return
false
;
337
}
338
// build the vehicle
339
if
(!
MsgHandler::getErrorInstance
()->wasInformed()) {
340
ROVehicle
* veh =
new
ROVehicle
(*
myVehicleParameter
, route, type);
341
myNet
.
addVehicle
(
myVehicleParameter
->
id
, veh);
342
return
true
;
343
}
344
return
false
;
345
}
346
347
348
349
/****************************************************************************/
350
build
buildd
sumo-0.15.0~dfsg
src
router
RORDLoader_SUMOBase.cpp
Generated on Wed Jul 18 2012 22:58:36 for SUMO - Simulation of Urban MObility by
1.8.1.1