Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/routes/runner.py
169678 views
1
#!/usr/bin/env python
2
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
# Copyright (C) 2008-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 runner.py
15
# @author Daniel Krajzewicz
16
# @author Michael Behrisch
17
# @date 2015-02-03
18
19
20
from __future__ import print_function
21
from __future__ import absolute_import
22
import os
23
import subprocess
24
import sys
25
sys.path.append(
26
os.path.join(os.path.dirname(__file__), '..', '..', '..', "tools"))
27
import sumolib # noqa
28
29
30
def runInstance(call, elem, attrSet, childSet, depart):
31
print(elem, attrSet, childSet)
32
sys.stdout.flush()
33
with open("routes.xml", "w") as routes:
34
routes.write('''<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" \
35
xsi:noNamespaceSchemaLocation="http://sumo.dlr.de/xsd/routes_file.xsd">
36
<route id="a" edges="1fi 1si 2o 2fi 2si"/>
37
<%s id="v" %s''' % (elem, depart))
38
for idx, attr in enumerate(attrs):
39
if attrSet & (2 ** idx):
40
routes.write(attr)
41
routes.write('>\n')
42
for idx, child in enumerate(childs):
43
if childSet & (2 ** idx):
44
routes.write((8 * " ") + child)
45
routes.write(' </%s>\n</routes>\n' % elem)
46
c = call + ["-n", "input_net.net.xml", "-r", routes.name]
47
p = subprocess.Popen(c, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
48
p_taz = subprocess.Popen(c + ["--with-taz"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
49
sys.stdout.write(p.communicate()[0] + p_taz.communicate()[0])
50
if p.returncode < 0 or p_taz.returncode < 0:
51
sys.stdout.write(open(routes.name).read())
52
sys.exit()
53
54
55
optParser = sumolib.options.ArgumentParser()
56
optParser.add_option("-e", "--element", help="xml element to choose")
57
optParser.add_option(
58
"-a", "--attr", type=int, default=0, help="attribute set to use")
59
optParser.add_option("--child", type=int, default=0, help="child set to use")
60
options, args = optParser.parse_known_args()
61
62
if len(args) == 0 or args[0] == "sumo":
63
call = [sumolib.checkBinary('sumo'), "--no-step-log", "--no-duration-log", "-a", "input_additional.add.xml"]
64
elif args[0] == "dfrouter":
65
call = [sumolib.checkBinary('dfrouter'), "--detector-files", "input_additional.add.xml"]
66
elif args[0] == "duarouter":
67
call = [sumolib.checkBinary('duarouter'), "--no-step-log", "-o", "dummy.xml", "-a", "input_additional.add.xml"]
68
elif args[0] == "jtrrouter":
69
call = [sumolib.checkBinary('jtrrouter'), "--no-step-log", "-o", "dummy.xml", "-a", "input_additional.add.xml"]
70
else:
71
print("Unsupported application defined", file=sys.stderr)
72
call += args[1:]
73
74
elements = {'vehicle': 'depart="0"',
75
'flow': 'begin="0" end="1" number="1"', 'trip': 'depart="0"'}
76
attrs = [' from="1fi"', ' to="2si"',
77
' fromTaz="1"', ' toTaz="2"', ' route="a"']
78
childs = ['<route edges="1fi 1si 2o 2fi 2si"/>\n', '<stop lane="1fi_0" duration="10"/>\n',
79
'<stop lane="1si_0" duration="10"/>\n', '<stop lane="2si_0" duration="10"/>\n']
80
81
# check route processing
82
if options.element:
83
runInstance(call, options.element, options.attr, options.child, elements[options.element])
84
else:
85
for elem, depart in sorted(elements.items()):
86
for attrSet in range(2 ** len(attrs)):
87
for childSet in range(2 ** len(childs)):
88
runInstance(call, elem, attrSet, childSet, depart)
89
90