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 }
118 catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception)
119 {
120 exception.printStackTrace();
121 }
122 }
123 });
124 }
125
126
127 @Override
128 public final String toString()
129 {
130 return "TestXMLParser []";
131 }
132
133
134
135
136
137
138
139
140
141
142
143
144
145 static class TestXMLModelKPIs extends AbstractOTSModel
146 {
147
148 private static final long serialVersionUID = 20141121L;
149
150
151 private OTSRoadNetwork network;
152
153
154
155
156 TestXMLModelKPIs(final OTSSimulatorInterface simulator)
157 {
158 super(simulator);
159 }
160
161
162 @Override
163 public final void constructModel() throws SimRuntimeException
164 {
165 URL xmlURL = URLResource.getResource("/xml/N201.xml");
166 this.network = new OTSRoadNetwork("Example network", true, getSimulator());
167 try
168 {
169 XmlNetworkLaneParser.build(xmlURL, this.network, false);
170
171 for (TrafficLight tl : this.network.getObjectMap(TrafficLight.class).values())
172 {
173 tl.setTrafficLightColor(TrafficLightColor.GREEN);
174 }
175
176
177
178 FilterDataGtuType filterDataGtuType = new FilterDataGtuType();
179 SpeedLimit speedLimit = new SpeedLimit();
180 Set<ExtendedDataType<?, ?, ?, GtuData>> extendedDataTypes = new LinkedHashSet<>();
181 Set<FilterDataType<?>> filterDataTypes = new LinkedHashSet<>();
182 extendedDataTypes.add(speedLimit);
183 filterDataTypes.add(filterDataGtuType);
184
185 RoadSampler sampler = new RoadSampler(extendedDataTypes, filterDataTypes, this.network, new Frequency(10.0,
186 FrequencyUnit.SI));
187
188 FilterDataSet metaDataSet = new FilterDataSet();
189 Set<GtuTypeDataInterface> gtuTypes = new LinkedHashSet<>();
190 gtuTypes.add(new GtuTypeData(this.network.getGtuType(GTUType.DEFAULTS.CAR)));
191 gtuTypes.add(new GtuTypeData(this.network.getGtuType(GTUType.DEFAULTS.BUS)));
192 metaDataSet.put(filterDataGtuType, gtuTypes);
193
194 Query query = N201ODfactory.getQuery(this.network, sampler, metaDataSet);
195 scheduleKpiEvent(30.0, this.simulator, query);
196 }
197 catch (NetworkException | ParserConfigurationException | SAXException | GTUException | OTSGeometryException
198 | JAXBException | URISyntaxException | XmlParserException | IOException | TrafficControlException exception)
199 {
200 exception.printStackTrace();
201 }
202
203 URL gisURL = URLResource.getResource("/xml/N201/map.xml");
204 System.err.println("GIS-map file: " + gisURL.toString());
205 CoordinateTransform rdto0 = new CoordinateTransformWGS84toRDNew(0, 0);
206 new GisRenderable2D(this.simulator, gisURL, rdto0);
207 }
208
209
210 @Override
211 public OTSRoadNetwork getNetwork()
212 {
213 return this.network;
214 }
215
216
217 @Override
218 public final String toString()
219 {
220 return "TestXMLModel [simulator=" + this.simulator + "]";
221 }
222
223 TotalTravelDistance totalTravelDistance = new TotalTravelDistance();
224
225 TotalTravelTime totalTravelTime = new TotalTravelTime();
226
227 MeanSpeed meanSpeed = new MeanSpeed(this.totalTravelDistance, this.totalTravelTime);
228
229 MeanTravelTimePerDistance meanTravelTimePerDistance = new MeanTravelTimePerDistance(this.meanSpeed);
230
231 MeanTripLength meanTripLength = new MeanTripLength();
232
233 TotalDelay totalDelay = new TotalDelay(new Speed(80.0, SpeedUnit.KM_PER_HOUR));
234
235 TotalNumberOfStops totalNumberOfStops = new TotalNumberOfStops();
236
237 public <G extends GtuDataInterface> void publishKpis(double time, final DEVSSimulatorInterface.TimeDoubleUnit simulator,
238 final Query<G> query)
239 {
240 Time t = new Time(time, TimeUnit.BASE_SECOND);
241 List<TrajectoryGroup<G>> groups = query.getTrajectoryGroups(t);
242 Length tdist = this.totalTravelDistance.getValue(query, t, groups);
243 Duration ttt = this.totalTravelTime.getValue(query, t, groups);
244 Speed ms = this.meanSpeed.getValue(query, t, groups);
245 Duration mttpdist = this.meanTravelTimePerDistance.getValue(query, t, groups);
246 Length mtl = this.meanTripLength.getValue(query, t, groups);
247 Duration tdel = this.totalDelay.getValue(query, t, groups);
248 Dimensionless nos = this.totalNumberOfStops.getValue(query, t, groups);
249 System.out.println("===== @time " + time + " s =====");
250 System.out.println("Total distance " + tdist);
251 System.out.println("Total travel time " + ttt);
252 System.out.println("Mean speed " + ms);
253 System.out.println("Mean travel time " + mttpdist + " (per m)");
254 System.out.println("Mean trip length " + mtl);
255 System.out.println("Total delay " + tdel);
256 System.out.println("Number of stops " + nos);
257 scheduleKpiEvent(time + 30, simulator, query);
258 }
259
260 public void scheduleKpiEvent(double time, final DEVSSimulatorInterface.TimeDoubleUnit simulator, final Query<?> query)
261 {
262 try
263 {
264 simulator.scheduleEventAbs(new Time(time, TimeUnit.BASE_SECOND), this, this, "publishKpis", new Object[] {time,
265 simulator, query});
266 }
267 catch (SimRuntimeException exception)
268 {
269 throw new RuntimeException("Cannot schedule KPI event.", exception);
270 }
271 }
272
273
274 @Override
275 public Serializable getSourceId()
276 {
277 return "TestXMLModelKPIs";
278 }
279 }
280
281 }