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