Path: blob/main/tests/complex/traci/contextSubscriptions/simulation_5/runner.py
169689 views
#!/usr/bin/env python1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2# Copyright (C) 2008-2025 German Aerospace Center (DLR) and others.3# This program and the accompanying materials are made available under the4# terms of the Eclipse Public License 2.0 which is available at5# https://www.eclipse.org/legal/epl-2.0/6# This Source Code may also be made available under the following Secondary7# Licenses when the conditions for such availability set forth in the Eclipse8# Public License 2.0 are satisfied: GNU General Public License, version 29# or later which is available at10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later1213# @file runner.py14# @author Daniel Krajzewicz15# @author Michael Behrisch16# @date 2012-10-191718from __future__ import absolute_import19from __future__ import print_function2021import os22import sys2324if "SUMO_HOME" in os.environ:25sys.path.append(os.path.join(os.environ["SUMO_HOME"], "tools"))26import sumolib # noqa27import traci # noqa282930def csRes2Str(csr):31return ', '.join(sorted(csr[''].keys()))323334def runSingle(viewRange, domain, domain2):35name = domain._name if hasattr(domain, "_name") else domain.__name__36name2 = domain2._name if hasattr(domain2, "_name") else domain2.__name__37ids = domain.getIDList() if name != "simulation" else [""]38if not ids:39print("No objects for domain '%s' at time %s" %40(name, traci.simulation.getTime()))41return42egoID = ids[0]4344print("trying to subscribe to %s around %s '%s' at time %s" % (45name2, name, egoID, traci.simulation.getTime()))46domain.subscribeContext(egoID, domain2.DOMAIN_ID, viewRange,47[traci.constants.TRACI_ID_LIST])48traci.simulationStep()49responses = domain.getAllContextSubscriptionResults()50print(" found %s objects" % len(responses))5152for i in range(3):53print(i, csRes2Str(responses))5455domain.unsubscribeContext(egoID, domain2.DOMAIN_ID, viewRange)56traci.simulationStep()57responses = domain.getAllContextSubscriptionResults()58if responses:59print("Error: Unsubscribe did not work", responses)60else:61print("Ok: Unsubscribe successful")62sys.stdout.flush()636465# main66traci.start([sumolib.checkBinary(sys.argv[1]),67'-Q', "-c", "sumo.sumocfg",68'-a', 'input_additional.add.xml'])69traci.simulationStep()70for domain2 in traci.DOMAINS:71try:72runSingle(5, traci.simulation, domain2)73except traci.TraCIException:74pass7576traci.close()777879