Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/tests/complex/traci_java/api/data/APITest.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 APITest.java
17
/// @author Mario Krumnow
18
/// @author Jakob Erdmann
19
/// @date 2013
20
///
21
//
22
/****************************************************************************/
23
import org.eclipse.sumo.libtraci.*;
24
25
public class APITest {
26
27
public static void main(String[] args) {
28
Simulation.preloadLibraries();
29
String sumo_bin = "sumo";
30
String config_file = "data/config.sumocfg";
31
double step_length = 1;
32
if (args.length > 0) {
33
sumo_bin = args[0];
34
}
35
if (args.length > 1) {
36
config_file = args[1];
37
}
38
39
Simulation.start(new StringVector(new String[] {sumo_bin,
40
"-c", config_file,
41
"--start"
42
}));
43
44
45
double deltaT = Simulation.getDeltaT();
46
System.out.println("deltaT:" + deltaT);
47
TraCIStage stage = Simulation.findRoute("gneE0", "gneE2", "car", 0, 0);
48
System.out.println("findRoute result stage:");
49
for (String s : stage.getEdges()) {
50
System.out.println(" " + s);
51
}
52
TraCIStageVector stages = Simulation.findIntermodalRoute("gneE0", "gneE2", "", 0, 0,
53
1.5, 1, 50, 50, 0, "", "", "");
54
System.out.println("findIntermodalRoute result stages:");
55
for (TraCIStage s2 : stages) {
56
for (String s : s2.getEdges()) {
57
System.out.println(" " + s);
58
}
59
60
}
61
StringVector walk = new StringVector();
62
walk.add("gneE2");
63
walk.add("gneE3");
64
for (int i = 0; i < 36; i++) {
65
66
Simulation.step();
67
Vehicle.add("v" + i, "r1", "car", "now", "0", "0", "max", "current", "max", "current", "", "", "", 0, 0);
68
double timeSeconds = Simulation.getTime();
69
int tlsPhase = TrafficLight.getPhase("gneJ1");
70
String tlsPhaseName = TrafficLight.getPhaseName("gneJ1");
71
System.out.println(String.format("Step %s, tlsPhase %s (%s)", timeSeconds, tlsPhase, tlsPhaseName));
72
73
TraCIVehicleDataVector vehData = InductionLoop.getVehicleData("loop1");
74
for (TraCIVehicleData d : vehData) {
75
System.out.println(String.format(" veh=%s len=%s entry=%s leave=%s type=%s",
76
d.getId(), d.getLength(), d.getEntryTime(), d.getLeaveTime(), d.getTypeID()));
77
}
78
if (i == 10) {
79
Vehicle.setStop("v0", "gneE0", 200, 0, 10, 0, 10, -1);
80
}
81
Person.add("p" + i, "gneE2", 10.0, libtraci.getDEPARTFLAG_NOW(), "DEFAULT_PEDTYPE");
82
Person.appendWalkingStage("p" + i, walk, 50.0, -1, -1.0, "");
83
}
84
StringVector vehIDs = Vehicle.getIDList();
85
String vehIDsStr = "";
86
for (String id : vehIDs) {
87
vehIDsStr += (id + " ");
88
}
89
System.out.println("VehicleIDs: " + vehIDsStr);
90
91
Vehicle.setParameter("v0", "vehParam", "vehValue");
92
System.out.println("vehicle.getParameter: " + Vehicle.getParameter("v0", "vehParam"));
93
94
Vehicle.setAdaptedTraveltime("v0", "gneE0", 200, 100, 200);
95
System.out.println("VehicleType.getAdaptedTraveltime: " + Vehicle.getAdaptedTraveltime("v0", 150, "gneE0"));
96
97
System.out.println("Vehicle.getRoute: " + Vehicle.getRoute("v0"));
98
StringVector route = new StringVector();
99
route.add("gneE0");
100
Vehicle.setRoute("v0", route);
101
System.out.println("Vehicle.getRoute: " + Vehicle.getRoute("v0"));
102
Vehicle.setRouteID("v0", "r1");
103
System.out.println("Vehicle.getRoute: " + Vehicle.getRoute("v0"));
104
105
TraCINextTLSVector ntlsV = Vehicle.getNextTLS("v0");
106
for (TraCINextTLSData t : ntlsV) {
107
System.out.println("Vehicle.getNextTLS: id=" + t.getId() + " dist=" + t.getDist() + " state=" + t.getState());
108
}
109
110
VehicleType.setParameter("car", "vehtypeParam", "vehtypeValue");
111
System.out.println("VehicleType.getParameter: " + VehicleType.getParameter("car", "vehtypeParam"));
112
113
StringVector personIDs = Person.getIDList();
114
String personIDsStr = "";
115
for (String id : personIDs) {
116
personIDsStr += (id + " ");
117
}
118
System.out.println("PersonIDs: " + personIDsStr);
119
120
TraCIStage personStage = Person.getStage("p0", 0);
121
System.out.println("Person.getStage: " + personStage.getType());
122
123
Edge.setParameter("gneE0", "edgeParam", "edgeValue");
124
System.out.println("Edge.getParameter: " + Edge.getParameter("gneE0", "edgeParam"));
125
126
Edge.adaptTraveltime("gneE0", 123, 2000, 4000);
127
System.out.println("Edge.getAdaptatedTraveltime: " + Edge.getAdaptedTraveltime("gneE0", 3000));
128
System.out.println("Edge.getAdaptatedTraveltime: " + Edge.getAdaptedTraveltime("gneE0", 1000));
129
System.out.println("Edge.getAdaptatedTraveltime: " + Edge.getAdaptedTraveltime("gneE0", 5000));
130
131
Edge.setEffort("gneE0", 246, 2000, 4000);
132
System.out.println("Edge.getEffort: " + Edge.getEffort("gneE0", 3000));
133
System.out.println("Edge.getEffort: " + Edge.getEffort("gneE0", 1000));
134
System.out.println("Edge.getEffort: " + Edge.getEffort("gneE0", 5000));
135
136
Lane.setParameter("gneE0_1", "laneParam", "laneValue");
137
System.out.println("Lane.getParameter: " + Lane.getParameter("gneE0_1", "laneParam"));
138
139
Route.setParameter("r1", "routeParam", "routeValue");
140
System.out.println("Route.getParameter: " + Route.getParameter("r1", "routeParam"));
141
142
POI.setParameter("t0", "poiParam", "poiValue");
143
System.out.println("Poi.getParameter: " + POI.getParameter("t0", "poiParam"));
144
145
StringVector controlledJunctions = TrafficLight.getControlledJunctions("gneJ1");
146
System.out.println("TrafficLight.getControlledJunctions: " + controlledJunctions);
147
148
TrafficLight.setParameter("gneJ1", "tlsParam", "tlsValue");
149
System.out.println("TrafficLight.getParameter: " + TrafficLight.getParameter("gneJ1", "tlsParam"));
150
151
TrafficLight.setPhaseDuration("gneJ1", 15);
152
153
TraCILogicVector tlsc = TrafficLight.getCompleteRedYellowGreenDefinition("gneJ1");
154
for (TraCILogic tls : tlsc) {
155
System.out.println("TrafficLight.getCompleteRedYellowGreenDefinition: " + tls.getProgramID());
156
}
157
158
TraCILogic tlsp = tlsc.get(0);
159
TrafficLight.setCompleteRedYellowGreenDefinition("gneJ1", tlsp);
160
161
System.out.println("TrafficLight.getPhaseDuration: " + (double)TrafficLight.getPhaseDuration("gneJ1"));
162
System.out.println("TrafficLight.getNextSwitch: " + (double)TrafficLight.getNextSwitch("gneJ1"));
163
164
TraCIRoadPosition roadPos = Simulation.convertRoad(505.38, -42.08, false, "passenger");
165
System.out.println("Simulation.convertRoad: " + roadPos.getEdgeID() + ", " + roadPos.getPos() + ", " + roadPos.getLaneIndex());
166
167
TraCIPosition geoPos = Simulation.convertGeo(505.38, -42.08, false);
168
System.out.println("Simulation.convertGeo: " + geoPos.getX() + ", " + geoPos.getY());
169
170
for (TraCIConnection c : Lane.getLinks(":gneJ1_6_0")) {
171
System.out.println("Lane.getLinks to=" + c.getApproachedLane() + " via=" + c.getApproachedInternal() + " prio=" + c.getHasPrio() + " open=" + c.getIsOpen());
172
}
173
174
Simulation.close();
175
176
Simulation.start(new StringVector(new String[] {sumo_bin,
177
"-c", config_file,
178
"--start"
179
}));
180
181
Simulation.saveState("file-state-now");
182
Simulation.close();
183
}
184
185
}
186
187