Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tools/purgatory/0103to0110.py
169674 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2009-2025 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 0103to0110.py
15
# @author Daniel Krajzewicz
16
# @author Michael Behrisch
17
# @date 2007
18
19
from __future__ import absolute_import
20
from __future__ import print_function
21
import sys
22
from xml.sax import make_parser, handler
23
24
# attributes sorting lists
25
a = {}
26
a['edge'] = ('id', 'from', 'to', 'priority', 'type', 'function', 'inner')
27
a['lane'] = ('id', 'depart', 'vclasses', 'allow',
28
'disallow', 'maxspeed', 'length', 'shape')
29
a['junction'] = ('id', 'type', 'x', 'y', 'incLanes', 'intLanes', 'shape')
30
a['logicitem'] = ('request', 'response', 'foes', 'cont')
31
a['succ'] = ('edge', 'lane', 'junction')
32
a['succlane'] = (
33
'lane', 'via', 'tl', 'linkno', 'yield', 'dir', 'state', 'int_end')
34
a['row-logic'] = ('id', 'requestSize', 'laneNumber')
35
a['tl-logic'] = ('id', 'type', 'programID', 'offset')
36
a['location'] = ('netOffset', 'convBoundary', 'origBoundary', 'projParameter')
37
38
# elements which are single (not using opening/closing tag)
39
c = ('logicitem', 'phase', 'succlane', 'dsource', 'dsink',
40
'junction', 'roundabout', 'location', 'lane', 'timed_event')
41
42
# delay output till this point
43
d = {}
44
d['junction'] = None
45
d['row-logic'] = 'logic'
46
d['tl-logic'] = 'phase'
47
48
# subelements to use as attributes
49
i = {}
50
i['junction'] = (
51
('inclanes', 'incLanes'), ('intlanes', 'intLanes'), ('shape', 'shape'))
52
i['row-logic'] = (('key', 'id'),
53
('requestsize', 'requestSize'), ('lanenumber', 'laneNumber'))
54
i['tl-logic'] = (('key', 'id'), ('subkey', 'programID'), ('phaseno', 'phaseNo'),
55
('offset', 'offset'), ('logicno', 'dismiss'), ('inclanes', 'inclanes'))
56
57
# join these
58
j = ('net-offset', 'conv-boundary', 'orig-boundary', 'orig-proj')
59
60
61
def getBegin(file):
62
fd = open(file)
63
content = ""
64
for line in fd:
65
if line.find("<net>") >= 0:
66
fd.close()
67
return content
68
content = content + line
69
fd.close()
70
return ""
71
72
73
def patchPhase(attrs):
74
state = ""
75
for i in range(0, len(attrs['phase'])):
76
c = 'g'
77
if attrs['phase'][i] == '0':
78
if attrs['yellow'][i] == '1':
79
c = 'y'
80
else:
81
c = 'r'
82
if attrs['brake'][i] == '0':
83
if c == 'y':
84
c = 'Y'
85
if c == 'g':
86
c = 'G'
87
state = c + state
88
return state
89
90
91
class NetConverter(handler.ContentHandler):
92
93
def __init__(self, outFileName, begin):
94
self._out = open(outFileName, "w")
95
self._out.write(begin)
96
self._collect = False
97
self._tree = []
98
self._attributes = {}
99
self._shapePatch = False
100
self._skipping = False
101
self._hadShape = False
102
103
def beginCollect(self):
104
self._collect = True
105
106
def endCollect(self):
107
self._collect = False
108
self.checkWrite(self._buffer)
109
self._buffer = ""
110
111
def checkWrite(self, what, isCharacters=False):
112
cp = None
113
if len(self._tree) > 2:
114
if self._tree[-2] in i:
115
for p in i[self._tree[-2]]:
116
if self._tree[-1] == p[0]:
117
cp = p
118
if cp and isCharacters:
119
if cp[1] in self._attributes:
120
self._attributes[
121
cp[1]] = self._attributes[cp[1]] + what
122
else:
123
self._attributes[cp[1]] = what
124
if len(self._tree) > 1:
125
if self._tree[-1] in d:
126
cp = 1
127
if not cp:
128
self._out.write(what)
129
if not self._skipping:
130
self._attributes = {}
131
132
def flushStored(self, name):
133
if len(self._attributes) == 0:
134
return
135
self._out.write("<" + name)
136
for key in a[name]:
137
if key in self._attributes:
138
self._out.write(' ' + key + '="' + self._attributes[key] + '"')
139
else:
140
self._out.write(' ' + key + '=""')
141
self._attributes = {}
142
if name in c:
143
self._out.write("/>")
144
else:
145
self._out.write(">")
146
147
def endDocument(self):
148
self.checkWrite("\n")
149
self._out.close()
150
151
def startElement(self, name, attrs):
152
if len(self._tree) > 0 and self._tree[-1] in d and d[self._tree[-1]] == name:
153
self.flushStored(self._tree[-1])
154
self._out.write("\n" + self._lastChars)
155
self._tree.append(name)
156
if name in j:
157
self._skipping = True
158
return
159
self.checkWrite("<" + name)
160
if name in d:
161
self._attributes = {}
162
for key in attrs.getNames():
163
self._attributes[key] = attrs[key]
164
elif name == "phase" and 'phase' in attrs:
165
# patch phase definition
166
state = patchPhase(attrs)
167
for key in attrs.getNames():
168
if key == 'phase':
169
self.checkWrite(' state="' + state + '"')
170
elif key != 'yellow' and key != 'brake':
171
self.checkWrite(' ' + key + '="' + attrs[key] + '"')
172
elif name == 'lane' and 'vclasses' in attrs:
173
for key in a['lane']:
174
if key == 'vclasses':
175
allowed = []
176
disallowed = []
177
for clazz in attrs['vclasses'].split(";"):
178
if clazz:
179
if clazz[0] == '-':
180
disallowed.append(clazz[1:])
181
else:
182
allowed.append(clazz)
183
if allowed:
184
self.checkWrite(' allow="%s"' % (" ".join(allowed)))
185
if disallowed:
186
self.checkWrite(' disallow="%s"' %
187
(" ".join(disallowed)))
188
elif key in attrs:
189
self.checkWrite(' ' + key + '="' + attrs[key] + '"')
190
else:
191
if name not in a:
192
for key in attrs.getNames():
193
self.checkWrite(' ' + key + '="' + attrs[key] + '"')
194
else:
195
for key in a[name]:
196
if key in attrs:
197
self.checkWrite(' ' + key + '="' + attrs[key] + '"')
198
if (name != "lane" and name != "poly") or "shape" in attrs:
199
if name in c:
200
self.checkWrite("/")
201
self.checkWrite(">")
202
else:
203
self.checkWrite(" shape=\"")
204
self._shapePatch = True
205
206
def endElement(self, name):
207
if name in d:
208
if not d[name]:
209
self.flushStored(name)
210
else:
211
self._out.write("\n" + self._lastChars + "</" + name + ">")
212
elif name == "orig-proj":
213
self._out.write(
214
"<location netOffset=\"" + self._attributes["net-offset"])
215
self._out.write(
216
"\" convBoundary=\"" + self._attributes["conv-boundary"])
217
self._out.write(
218
"\" origBoundary=\"" + self._attributes["orig-boundary"])
219
self._out.write(
220
"\" projParameter=\"" + self._attributes["orig-proj"])
221
self._out.write("\"/>")
222
self._attributes = {}
223
self._skipping = False
224
elif name in j:
225
name = ""
226
elif self._shapePatch:
227
self.checkWrite("\"/>")
228
self._shapePatch = False
229
elif name not in c:
230
self.checkWrite("</" + name)
231
self.checkWrite(">")
232
self._tree.pop()
233
234
def characters(self, content):
235
if self._skipping:
236
if content.strip() != "":
237
e = self._tree[-1]
238
if e in self._attributes:
239
self._attributes[e] = self._attributes[e] + content
240
else:
241
self._attributes[e] = content
242
return
243
self._lastChars = content
244
self.checkWrite(content, True)
245
246
def ignorableWhitespace(self, content):
247
self.checkWrite(content)
248
249
def skippedEntity(self, content):
250
self.checkWrite(content)
251
252
def processingInstruction(self, target, data):
253
self.checkWrite('<?%s %s?>' % (target, data))
254
255
256
if len(sys.argv) < 2:
257
print("Usage: " + sys.argv[0] + " <net>")
258
sys.exit()
259
beg = getBegin(sys.argv[1])
260
parser = make_parser()
261
net = NetConverter(sys.argv[1] + ".chg", beg)
262
parser.setContentHandler(net)
263
parser.parse(sys.argv[1])
264
265