Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/traci_java/subscription/data/Subscription.java
169689 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2017-2025 German Aerospace Center (DLR) and others.
4
// TraaS module
5
// Copyright (C) 2013-2017 Dresden University of Technology
6
// This program and the accompanying materials are made available under the
7
// terms of the Eclipse Public License 2.0 which is available at
8
// https://www.eclipse.org/legal/epl-2.0/
9
// This Source Code may also be made available under the following Secondary
10
// Licenses when the conditions for such availability set forth in the Eclipse
11
// Public License 2.0 are satisfied: GNU General Public License, version 2
12
// or later which is available at
13
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
14
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
15
/****************************************************************************/
16
/// @file Subscription.java
17
/// @author Jakob Erdmann
18
/// @date 2019
19
///
20
//
21
/****************************************************************************/
22
import org.eclipse.sumo.libtraci.*;
23
import java.util.Map;
24
import java.util.AbstractMap;
25
import java.util.TreeMap;
26
27
public class Subscription {
28
29
public static void main(String[] args) {
30
Simulation.preloadLibraries();
31
String sumo_bin = "sumo";
32
String config_file = "data/config.sumocfg";
33
double step_length = 0.1;
34
if (args.length > 0) {
35
sumo_bin = args[0];
36
}
37
if (args.length > 1) {
38
config_file = args[1];
39
}
40
41
Simulation.start(new StringVector(new String[] {sumo_bin,
42
"-c", config_file,
43
"--start"
44
}));
45
46
Simulation.subscribe(new IntVector(new int[] { libtraci.getVAR_DEPARTED_VEHICLES_IDS() }));
47
48
for (int i = 0; i < 10; i++) {
49
Simulation.step();
50
Vehicle.add("v" + i, "r1", "car", "now", "0", "0", "max", "current", "max", "current", "", "", "", 0, 0);
51
System.out.println("Step: " + i);
52
53
TraCIResults ssRes = Simulation.getSubscriptionResults();
54
for (Map.Entry<Integer, TraCIResult> entry : ssRes.entrySet()) {
55
TraCIStringList vehIDs = TraCIStringList.cast(entry.getValue());
56
for (String vehID : vehIDs.getValue()) {
57
System.out.println("Subscription Departed vehicles: " + vehID);
58
Vehicle.subscribe(vehID, new IntVector(new int[] { Constants.VAR_POSITION, Constants.VAR_SPEED }));
59
}
60
}
61
TreeMap<String, TraCIResults> vsRes = sortedMap(Vehicle.getAllSubscriptionResults());
62
for (Map.Entry<String, TraCIResults> vehEntry : vsRes.entrySet()) {
63
System.out.println("Vehicle Subscription: id=" + vehEntry.getKey());
64
vehEntry.getValue().entrySet().stream().sorted(Map.Entry.comparingByKey())
65
.forEach(entry -> System.out.println(" variable id: " + entry.getKey() + " value: " + entry.getValue().getString()));
66
}
67
}
68
69
Simulation.close();
70
}
71
72
public static <K, V> TreeMap<K, V> sortedMap(AbstractMap<K, V> map) {
73
TreeMap<K, V> result = new TreeMap<K, V>();
74
for (Map.Entry<K, V> e : map.entrySet()) {
75
result.put(e.getKey(), e.getValue());
76
}
77
return result;
78
}
79
}
80
81