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