1 package org.opentrafficsim.demo.network.xml;
2
3 import java.awt.Dimension;
4 import java.io.IOException;
5 import java.io.Serializable;
6 import java.net.URISyntaxException;
7 import java.net.URL;
8 import java.rmi.RemoteException;
9 import java.util.LinkedHashSet;
10 import java.util.List;
11 import java.util.Set;
12
13 import javax.naming.NamingException;
14 import javax.swing.SwingUtilities;
15 import javax.xml.bind.JAXBException;
16 import javax.xml.parsers.ParserConfigurationException;
17
18 import org.djunits.unit.FrequencyUnit;
19 import org.djunits.unit.SpeedUnit;
20 import org.djunits.unit.TimeUnit;
21 import org.djunits.value.vdouble.scalar.Dimensionless;
22 import org.djunits.value.vdouble.scalar.Duration;
23 import org.djunits.value.vdouble.scalar.Frequency;
24 import org.djunits.value.vdouble.scalar.Length;
25 import org.djunits.value.vdouble.scalar.Speed;
26 import org.djunits.value.vdouble.scalar.Time;
27 import org.djutils.io.URLResource;
28 import org.opentrafficsim.core.dsol.AbstractOTSModel;
29 import org.opentrafficsim.core.dsol.OTSAnimator;
30 import org.opentrafficsim.core.dsol.OTSModelInterface;
31 import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
32 import org.opentrafficsim.core.geometry.OTSGeometryException;
33 import org.opentrafficsim.core.gis.CoordinateTransformWGS84toRDNew;
34 import org.opentrafficsim.core.gtu.GTUException;
35 import org.opentrafficsim.core.gtu.GTUType;
36 import org.opentrafficsim.core.network.NetworkException;
37 import org.opentrafficsim.draw.core.OTSDrawingException;
38 import org.opentrafficsim.kpi.interfaces.GtuDataInterface;
39 import org.opentrafficsim.kpi.interfaces.GtuTypeDataInterface;
40 import org.opentrafficsim.kpi.sampling.Query;
41 import org.opentrafficsim.kpi.sampling.TrajectoryGroup;
42 import org.opentrafficsim.kpi.sampling.data.ExtendedDataType;
43 import org.opentrafficsim.kpi.sampling.indicator.MeanSpeed;
44 import org.opentrafficsim.kpi.sampling.indicator.MeanTravelTimePerDistance;
45 import org.opentrafficsim.kpi.sampling.indicator.MeanTripLength;
46 import org.opentrafficsim.kpi.sampling.indicator.TotalDelay;
47 import org.opentrafficsim.kpi.sampling.indicator.TotalNumberOfStops;
48 import org.opentrafficsim.kpi.sampling.indicator.TotalTravelDistance;
49 import org.opentrafficsim.kpi.sampling.indicator.TotalTravelTime;
50 import org.opentrafficsim.kpi.sampling.meta.FilterDataGtuType;
51 import org.opentrafficsim.kpi.sampling.meta.FilterDataSet;
52 import org.opentrafficsim.kpi.sampling.meta.FilterDataType;
53 import org.opentrafficsim.road.network.OTSRoadNetwork;
54 import org.opentrafficsim.road.network.factory.xml.XmlParserException;
55 import org.opentrafficsim.road.network.factory.xml.parser.XmlNetworkLaneParser;
56 import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight;
57 import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor;
58 import org.opentrafficsim.road.network.sampling.GtuData;
59 import org.opentrafficsim.road.network.sampling.GtuTypeData;
60 import org.opentrafficsim.road.network.sampling.RoadSampler;
61 import org.opentrafficsim.road.network.sampling.data.SpeedLimit;
62 import org.opentrafficsim.swing.gui.OTSAnimationPanel;
63 import org.opentrafficsim.swing.gui.OTSSimulationApplication;
64 import org.opentrafficsim.trafficcontrol.TrafficControlException;
65 import org.xml.sax.SAXException;
66
67 import nl.javel.gisbeans.io.esri.CoordinateTransform;
68 import nl.tudelft.simulation.dsol.SimRuntimeException;
69 import nl.tudelft.simulation.dsol.animation.D2.GisRenderable2D;
70 import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
71 import nl.tudelft.simulation.language.DSOLException;
72
73
74
75
76
77
78
79
80
81
82 public class TestXMLParserKPIs extends OTSSimulationApplication<OTSModelInterface>
83 {
84
85 private static final long serialVersionUID = 1L;
86
87
88
89
90
91
92 public TestXMLParserKPIs(final OTSModelInterface model, final OTSAnimationPanel animationPanel) throws OTSDrawingException
93 {
94 super(model, animationPanel);
95 }
96
97
98
99
100
101
102 public static void main(final String[] args) throws SimRuntimeException
103 {
104 SwingUtilities.invokeLater(new Runnable()
105 {
106 @Override
107 public void run()
108 {
109 try
110 {
111 OTSAnimator simulator = new OTSAnimator("TestXMLParserKPIs");
112 TestXMLModelKPIs xmlModel = new TestXMLModelKPIs(simulator);
113 simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), xmlModel);
114 OTSAnimationPanel animationPanel = new OTSAnimationPanel(xmlModel.getNetwork().getExtent(), new Dimension(
115 800, 600), simulator, xmlModel, DEFAULT_COLORER, xmlModel.getNetwork());
116 new TestXMLParserKPIs(xmlModel, animationPanel);
117 animationPanel.enableSimulationControlButtons();
118 }
119 catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
120 {
121 exception.printStackTrace();
122 }
123 }
124 });
125 }
126
127
128 @Override
129 public final String toString()
130 {
131 return "TestXMLParser []";
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145
146 static class TestXMLModelKPIs extends AbstractOTSModel
147 {
148
149 private static final long serialVersionUID = 20141121L;
150
151
152 private OTSRoadNetwork network;
153
154
155
156
157 TestXMLModelKPIs(final OTSSimulatorInterface simulator)
158 {
159 super(simulator);
160 }
161
162
163 @Override
164 public final void constructModel() throws SimRuntimeException
165 {
166 URL xmlURL = URLResource.getResource("/xml/N201.xml");
167 this.network = new OTSRoadNetwork("Example network", true, getSimulator());
168 try
169 {
170 XmlNetworkLaneParser.build(xmlURL, this.network, false);
171
172 for (TrafficLight tl : this.network.getObjectMap(TrafficLight.class).values())
173 {
174 tl.setTrafficLightColor(TrafficLightColor.GREEN);
175 }
176
177
178
179 FilterDataGtuType filterDataGtuType = new FilterDataGtuType();
180 SpeedLimit speedLimit = new SpeedLimit();
181 Set<ExtendedDataType<?, ?, ?, GtuData>> extendedDataTypes = new LinkedHashSet<>();
182 Set<FilterDataType<?>> filterDataTypes = new LinkedHashSet<>();
183 extendedDataTypes.add(speedLimit);
184 filterDataTypes.add(filterDataGtuType);
185
186 RoadSampler sampler = new RoadSampler(extendedDataTypes, filterDataTypes, this.network, new Frequency(10.0,
187 FrequencyUnit.SI));
188
189 FilterDataSet metaDataSet = new FilterDataSet();
190 Set<GtuTypeDataInterface> gtuTypes = new LinkedHashSet<>();
191 gtuTypes.add(new GtuTypeData(this.network.getGtuType(GTUType.DEFAULTS.CAR)));
192 gtuTypes.add(new GtuTypeData(this.network.getGtuType(GTUType.DEFAULTS.BUS)));
193 metaDataSet.put(filterDataGtuType, gtuTypes);
194
195 Query query = N201ODfactory.getQuery(this.network, sampler, metaDataSet);
196 scheduleKpiEvent(30.0, this.simulator, query);
197 }
198 catch (NetworkException | ParserConfigurationException | SAXException | GTUException | OTSGeometryException
199 | JAXBException | URISyntaxException | XmlParserException | IOException | TrafficControlException exception)
200 {
201 exception.printStackTrace();
202 }
203
204 URL gisURL = URLResource.getResource("/xml/N201/map.xml");
205 System.err.println("GIS-map file: " + gisURL.toString());
206 CoordinateTransform rdto0 = new CoordinateTransformWGS84toRDNew(0, 0);
207 new GisRenderable2D(this.simulator, gisURL, rdto0);
208 }
209
210
211 @Override
212 public OTSRoadNetwork getNetwork()
213 {
214 return this.network;
215 }
216
217
218 @Override
219 public final String toString()
220 {
221 return "TestXMLModel [simulator=" + this.simulator + "]";
222 }
223
224 TotalTravelDistance totalTravelDistance = new TotalTravelDistance();
225
226 TotalTravelTime totalTravelTime = new TotalTravelTime();
227
228 MeanSpeed meanSpeed = new MeanSpeed(this.totalTravelDistance, this.totalTravelTime);
229
230 MeanTravelTimePerDistance meanTravelTimePerDistance = new MeanTravelTimePerDistance(this.meanSpeed);
231
232 MeanTripLength meanTripLength = new MeanTripLength();
233
234 TotalDelay totalDelay = new TotalDelay(new Speed(80.0, SpeedUnit.KM_PER_HOUR));
235
236 TotalNumberOfStops totalNumberOfStops = new TotalNumberOfStops();
237
238 public <G extends GtuDataInterface> void publishKpis(double time, final DEVSSimulatorInterface.TimeDoubleUnit simulator,
239 final Query<G> query)
240 {
241 Time t = new Time(time, TimeUnit.BASE_SECOND);
242 List<TrajectoryGroup<G>> groups = query.getTrajectoryGroups(t);
243 Length tdist = this.totalTravelDistance.getValue(query, t, groups);
244 Duration ttt = this.totalTravelTime.getValue(query, t, groups);
245 Speed ms = this.meanSpeed.getValue(query, t, groups);
246 Duration mttpdist = this.meanTravelTimePerDistance.getValue(query, t, groups);
247 Length mtl = this.meanTripLength.getValue(query, t, groups);
248 Duration tdel = this.totalDelay.getValue(query, t, groups);
249 Dimensionless nos = this.totalNumberOfStops.getValue(query, t, groups);
250 System.out.println("===== @time " + time + " s =====");
251 System.out.println("Total distance " + tdist);
252 System.out.println("Total travel time " + ttt);
253 System.out.println("Mean speed " + ms);
254 System.out.println("Mean travel time " + mttpdist + " (per m)");
255 System.out.println("Mean trip length " + mtl);
256 System.out.println("Total delay " + tdel);
257 System.out.println("Number of stops " + nos);
258 scheduleKpiEvent(time + 30, simulator, query);
259 }
260
261 public void scheduleKpiEvent(double time, final DEVSSimulatorInterface.TimeDoubleUnit simulator, final Query<?> query)
262 {
263 try
264 {
265 simulator.scheduleEventAbs(new Time(time, TimeUnit.BASE_SECOND), this, this, "publishKpis", new Object[] {time,
266 simulator, query});
267 }
268 catch (SimRuntimeException exception)
269 {
270 throw new RuntimeException("Cannot schedule KPI event.", exception);
271 }
272 }
273
274
275 @Override
276 public Serializable getSourceId()
277 {
278 return "TestXMLModelKPIs";
279 }
280 }
281
282 }