Path: blob/main/tests/complex/traci_java/subscription/data/Subscription.java
169689 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2017-2025 German Aerospace Center (DLR) and others.3// TraaS module4// Copyright (C) 2013-2017 Dresden University of Technology5// This program and the accompanying materials are made available under the6// terms of the Eclipse Public License 2.0 which is available at7// https://www.eclipse.org/legal/epl-2.0/8// This Source Code may also be made available under the following Secondary9// Licenses when the conditions for such availability set forth in the Eclipse10// Public License 2.0 are satisfied: GNU General Public License, version 211// or later which is available at12// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html13// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later14/****************************************************************************/15/// @file Subscription.java16/// @author Jakob Erdmann17/// @date 201918///19//20/****************************************************************************/21import org.eclipse.sumo.libtraci.*;22import java.util.Map;23import java.util.AbstractMap;24import java.util.TreeMap;2526public class Subscription {2728public static void main(String[] args) {29Simulation.preloadLibraries();30String sumo_bin = "sumo";31String config_file = "data/config.sumocfg";32double step_length = 0.1;33if (args.length > 0) {34sumo_bin = args[0];35}36if (args.length > 1) {37config_file = args[1];38}3940Simulation.start(new StringVector(new String[] {sumo_bin,41"-c", config_file,42"--start"43}));4445Simulation.subscribe(new IntVector(new int[] { libtraci.getVAR_DEPARTED_VEHICLES_IDS() }));4647for (int i = 0; i < 10; i++) {48Simulation.step();49Vehicle.add("v" + i, "r1", "car", "now", "0", "0", "max", "current", "max", "current", "", "", "", 0, 0);50System.out.println("Step: " + i);5152TraCIResults ssRes = Simulation.getSubscriptionResults();53for (Map.Entry<Integer, TraCIResult> entry : ssRes.entrySet()) {54TraCIStringList vehIDs = TraCIStringList.cast(entry.getValue());55for (String vehID : vehIDs.getValue()) {56System.out.println("Subscription Departed vehicles: " + vehID);57Vehicle.subscribe(vehID, new IntVector(new int[] { Constants.VAR_POSITION, Constants.VAR_SPEED }));58}59}60TreeMap<String, TraCIResults> vsRes = sortedMap(Vehicle.getAllSubscriptionResults());61for (Map.Entry<String, TraCIResults> vehEntry : vsRes.entrySet()) {62System.out.println("Vehicle Subscription: id=" + vehEntry.getKey());63vehEntry.getValue().entrySet().stream().sorted(Map.Entry.comparingByKey())64.forEach(entry -> System.out.println(" variable id: " + entry.getKey() + " value: " + entry.getValue().getString()));65}66}6768Simulation.close();69}7071public static <K, V> TreeMap<K, V> sortedMap(AbstractMap<K, V> map) {72TreeMap<K, V> result = new TreeMap<K, V>();73for (Map.Entry<K, V> e : map.entrySet()) {74result.put(e.getKey(), e.getValue());75}76return result;77}78}798081