Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/traci/connection/clientCloses/runner.py
169689 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 2010-03-21
18
19
from __future__ import absolute_import
20
from __future__ import print_function
21
22
import os
23
import subprocess
24
import sys
25
26
if "SUMO_HOME" in os.environ:
27
sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))
28
import sumolib # noqa
29
import traci # noqa
30
31
PORT = sumolib.miscutils.getFreeSocketPort()
32
DELTA_T = 1000
33
34
sumoBinary = sumolib.checkBinary(sys.argv[1])
35
if sys.argv[1] == "sumo":
36
addOption = []
37
else:
38
addOption = ["-S", "-Q"]
39
40
41
def runSingle(traciEndTime, sumoEndTime=None):
42
step = 0
43
if sumoEndTime is None:
44
opt = addOption
45
else:
46
opt = addOption + ["--end", str(sumoEndTime)]
47
sumoProcess = subprocess.Popen([sumoBinary, "-c", "sumo.sumocfg", "--remote-port", str(PORT)] + opt,
48
stdout=sys.stdout)
49
traci.init(PORT)
50
while not step > traciEndTime:
51
traci.simulationStep()
52
step += 1
53
print("Print ended at step", traci.simulation.getTime())
54
traci.close()
55
sumoProcess.wait()
56
sys.stdout.flush()
57
58
59
print("=========== long route ===========")
60
fdo = open("input_routes.rou.xml", "w")
61
print('<routes>', file=fdo)
62
print(
63
' <route id="horizontal" edges="2fi 2si 1o 1fi 1si 3o 3fi 3si 4o 4fi 4si"/>', file=fdo)
64
print(' <vehicle id="horiz" route="horizontal" depart="0"/>', file=fdo)
65
print('</routes>', file=fdo)
66
fdo.close()
67
print("----------- SUMO end time is smaller than TraCI's -----------")
68
sys.stdout.flush()
69
runSingle(99, 50)
70
print("----------- SUMO end time is not given -----------")
71
sys.stdout.flush()
72
runSingle(99)
73
74
75
print("=========== empty routes in SUMO ===========")
76
fdo = open("input_routes.rou.xml", "w")
77
print('<routes>', file=fdo)
78
print('</routes>', file=fdo)
79
fdo.close()
80
print("----------- SUMO end time is smaller than TraCI's -----------")
81
sys.stdout.flush()
82
runSingle(99, 50)
83
print("----------- SUMO end time is not given -----------")
84
sys.stdout.flush()
85
runSingle(99)
86
87
88
print("=========== vehicle leaves before TraCI ends ===========")
89
fdo = open("input_routes.rou.xml", "w")
90
print('<routes>', file=fdo)
91
print(' <route id="horizontal" edges="2fi 2si"/>', file=fdo)
92
print(' <vehicle id="horiz" route="horizontal" depart="0"/>', file=fdo)
93
print('</routes>', file=fdo)
94
fdo.close()
95
print("----------- SUMO end time is smaller than TraCI's -----------")
96
sys.stdout.flush()
97
runSingle(99, 50)
98
print("----------- SUMO end time is not given -----------")
99
sys.stdout.flush()
100
runSingle(99)
101
102