Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/traci/bugs/ticket4432/runner.py
169708 views
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
4
# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.
5
# This program and the accompanying materials are made available under the
6
# terms of the Eclipse Public License 2.0 which is available at
7
# https://www.eclipse.org/legal/epl-2.0/
8
# This Source Code may also be made available under the following Secondary
9
# Licenses when the conditions for such availability set forth in the Eclipse
10
# Public License 2.0 are satisfied: GNU General Public License, version 2
11
# or later which is available at
12
# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
13
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
14
15
# @file runner.py
16
# @author Leonhard Luecken
17
# @author Michael Behrisch
18
# @author Jakob Erdmann
19
# @author Daniel Krajzewicz
20
# @date 2011-03-04
21
22
23
from __future__ import print_function
24
from __future__ import absolute_import
25
import os
26
import sys
27
28
if "SUMO_HOME" in os.environ:
29
sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))
30
31
import traci # noqa
32
import sumolib # noqa
33
34
35
def countWrittenTrips(fname):
36
return len(list(sumolib.xml.parse_fast(fname, 'tripinfo', ['id'])))
37
38
39
def lastLine(fname):
40
with open(fname) as f:
41
lines = f.readlines()
42
return None if len(lines) == 0 else lines[-1].strip()
43
44
45
sumo = sumolib.checkBinary('sumo')
46
opts = [
47
'-n', 'input_net.net.xml',
48
'-r', 'input_routes.rou.xml',
49
'--no-step-log',
50
'--duration-log.statistics',
51
'-S', '-Q',
52
]
53
traci.start([sumo] + opts + ['--tripinfo-output', 'tripinfos.xml', '-l', 'log']
54
# + ['--save-configuration', 'debug.sumocfg']
55
)
56
57
58
while traci.simulation.getMinExpectedNumber() > 0:
59
traci.simulationStep()
60
61
print("tripinfos at last step: %s" % countWrittenTrips('tripinfos.xml'))
62
print("logfile at last step: %s" % lastLine('log'))
63
traci.load(opts + ['--tripinfo-output', 'tripinfos2.xml', '-l', 'log2'])
64
traci.simulationStep()
65
print("tripinfos after load: %s" % countWrittenTrips('tripinfos.xml'))
66
print("logfile after load: %s" % lastLine('log'))
67
68
while traci.simulation.getMinExpectedNumber() > 0:
69
traci.simulationStep()
70
print("tripinfos2 at last step: %s" % countWrittenTrips('tripinfos2.xml'))
71
print("logfile2 at last step: %s" % lastLine('log2'))
72
73
traci.close()
74
print("tripinfos2 after close: %s" % countWrittenTrips('tripinfos2.xml'))
75
print("logfile2 after close: %s" % lastLine('log2'))
76
# done
77
78