View Javadoc
1   package org.opentrafficsim.imb.demo;
2   
3   import java.awt.geom.Point2D;
4   import java.awt.geom.Rectangle2D;
5   import java.awt.geom.Rectangle2D.Double;
6   import java.io.IOException;
7   import java.io.InputStream;
8   import java.io.Serializable;
9   import java.net.URL;
10  import java.util.ArrayList;
11  import java.util.List;
12  
13  import javax.naming.NamingException;
14  import javax.swing.JPanel;
15  import javax.swing.SwingUtilities;
16  import javax.xml.parsers.ParserConfigurationException;
17  
18  import nl.javel.gisbeans.io.esri.CoordinateTransform;
19  import nl.tudelft.simulation.dsol.SimRuntimeException;
20  import nl.tudelft.simulation.dsol.animation.D2.GisRenderable2D;
21  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
22  import nl.tudelft.simulation.language.Throw;
23  import nl.tudelft.simulation.language.io.URLResource;
24  
25  import org.djunits.unit.TimeUnit;
26  import org.djunits.value.vdouble.scalar.Duration;
27  import org.djunits.value.vdouble.scalar.Time;
28  import org.opentrafficsim.base.modelproperties.CompoundProperty;
29  import org.opentrafficsim.base.modelproperties.Property;
30  import org.opentrafficsim.base.modelproperties.PropertyException;
31  import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
32  import org.opentrafficsim.core.dsol.OTSModelInterface;
33  import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
34  import org.opentrafficsim.core.geometry.OTSGeometryException;
35  import org.opentrafficsim.core.gis.TransformWGS84DutchRDNew;
36  import org.opentrafficsim.core.gtu.GTUException;
37  import org.opentrafficsim.core.gtu.animation.GTUColorer;
38  import org.opentrafficsim.core.network.NetworkException;
39  import org.opentrafficsim.core.network.OTSNetwork;
40  import org.opentrafficsim.imb.IMBException;
41  import org.opentrafficsim.imb.connector.OTSIMBConnector;
42  import org.opentrafficsim.imb.transceiver.urbanstrategy.GTUTransceiver;
43  import org.opentrafficsim.imb.transceiver.urbanstrategy.LaneGTUTransceiver;
44  import org.opentrafficsim.imb.transceiver.urbanstrategy.LinkGTUTransceiver;
45  import org.opentrafficsim.imb.transceiver.urbanstrategy.NetworkTransceiver;
46  import org.opentrafficsim.imb.transceiver.urbanstrategy.NodeTransceiver;
47  import org.opentrafficsim.imb.transceiver.urbanstrategy.SensorGTUTransceiver;
48  import org.opentrafficsim.imb.transceiver.urbanstrategy.SimulatorTransceiver;
49  import org.opentrafficsim.imb.transceiver.urbanstrategy.StatisticsGTULaneTransceiver;
50  import org.opentrafficsim.kpi.sampling.Query;
51  import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser;
52  import org.opentrafficsim.road.network.sampling.RoadSampler;
53  import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
54  import org.opentrafficsim.simulationengine.OTSSimulationException;
55  import org.opentrafficsim.simulationengine.SimpleAnimator;
56  import org.opentrafficsim.simulationengine.SimpleSimulatorInterface;
57  import org.xml.sax.SAXException;
58  
59  /**
60   * <p>
61   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
62   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
63   * <p>
64   * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
65   * initial version Oct 17, 2014 <br>
66   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
67   */
68  public class N201IMB extends AbstractWrappableAnimation
69  {
70      /** */
71      private static final long serialVersionUID = 20161007L;
72  
73      /** The model. */
74      private N201Model model;
75  
76      /**
77       * Main program.
78       * @param args String[]; the command line arguments (not used)
79       * @throws SimRuntimeException should never happen
80       */
81      public static void main(final String[] args) throws SimRuntimeException
82      {
83          SwingUtilities.invokeLater(new Runnable()
84          {
85              @Override
86              public void run()
87              {
88                  try
89                  {
90                      N201IMB n201Model = new N201IMB();
91                      // 1 hour simulation run for testing
92                      n201Model.buildAnimator(new Time(0.0, TimeUnit.SECOND), new Duration(0.0, TimeUnit.SECOND), new Duration(
93                              10.0, TimeUnit.HOUR), new ArrayList<Property<?>>(), null, true);
94                  }
95                  catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
96                  {
97                      exception.printStackTrace();
98                  }
99              }
100         });
101     }
102 
103     /** {@inheritDoc} */
104     @Override
105     public final String shortName()
106     {
107         return "Model N201";
108     }
109 
110     /** {@inheritDoc} */
111     @Override
112     public final String description()
113     {
114         return "Model N201 - IMB";
115     }
116 
117     /** {@inheritDoc} */
118     @Override
119     public final void stopTimersThreads()
120     {
121         super.stopTimersThreads();
122     }
123 
124     /** {@inheritDoc} */
125     @Override
126     protected final JPanel makeCharts(final SimpleSimulatorInterface simulator)
127     {
128         return null;
129     }
130 
131     /** {@inheritDoc} */
132     @Override
133     protected final OTSModelInterface makeModel(final GTUColorer colorer)
134     {
135         System.out.println("N201IMB.makeModel called");
136         this.model = new N201Model(getSavedUserModifiedProperties(), colorer, new OTSNetwork("N201 network"));
137         return this.model;
138     }
139 
140     /**
141      * @return the saved user properties for a next run
142      */
143     private List<Property<?>> getSavedUserModifiedProperties()
144     {
145         return this.savedUserModifiedProperties;
146     }
147 
148     /** {@inheritDoc} */
149     @Override
150     protected final Double makeAnimationRectangle()
151     {
152         return new Rectangle2D.Double(103000, 478000, 5500, 5000);
153     }
154 
155     /** {@inheritDoc} */
156     @Override
157     public final String toString()
158     {
159         return "N201 network - IMB []";
160     }
161 
162     /**
163      * Model to test the XML parser.
164      * <p>
165      * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
166      * All rights reserved. BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim
167      * License</a>.
168      * <p>
169      * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
170      * initial version un 27, 2015 <br>
171      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
172      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
173      */
174     class N201Model implements OTSModelInterface
175     {
176         /** */
177         private static final long serialVersionUID = 20141121L;
178 
179         /** The simulator. */
180         private OTSDEVSSimulatorInterface simulator;
181 
182         /** User settable properties. */
183         private List<Property<?>> modelProperties = null;
184 
185         /** the network as created by the AbstractWrappableIMBAnimation. */
186         private final OTSNetwork network;
187 
188         /** Connector to the IMB hub. */
189         OTSIMBConnector imbConnector;
190 
191         /**
192          * @param modelProperties ArrayList&lt;AbstractProperty&lt;?&gt;&gt;; the properties
193          * @param gtuColorer the default and initial GTUColorer, e.g. a DefaultSwitchableTUColorer.
194          * @param network Network; the network
195          */
196         N201Model(final List<Property<?>> modelProperties, final GTUColorer gtuColorer, final OTSNetwork network)
197         {
198             this.modelProperties = modelProperties;
199             this.network = network;
200         }
201 
202         /** {@inheritDoc} */
203         @Override
204         public final void constructModel(final SimulatorInterface<Time, Duration, OTSSimTimeDouble> pSimulator)
205                 throws SimRuntimeException
206         {
207             System.out.println("N201IMB: constructModel called; Connecting to IMB");
208             this.simulator = (OTSDEVSSimulatorInterface) pSimulator;
209             SimpleAnimator imbAnimator = (SimpleAnimator) pSimulator;
210             try
211             {
212                 CompoundProperty imbSettings = null;
213                 for (Property<?> property : this.modelProperties)
214                 {
215                     if (property.getKey().equals(OTSIMBConnector.PROPERTY_KEY))
216                     {
217                         imbSettings = (CompoundProperty) property;
218                     }
219                 }
220                 System.out.println("link count " + this.network.getLinkMap().size());
221                 Throw.whenNull(imbSettings, "IMB Settings not found in properties");
222                 this.imbConnector = OTSIMBConnector.create(imbSettings, "OTS");
223                 new NetworkTransceiver(this.imbConnector, imbAnimator, this.network);
224                 new NodeTransceiver(this.imbConnector, imbAnimator, this.network);
225                 new LinkGTUTransceiver(this.imbConnector, imbAnimator, this.network);
226                 new LaneGTUTransceiver(this.imbConnector, imbAnimator, this.network);
227                 new GTUTransceiver(this.imbConnector, imbAnimator, this.network);
228                 new SensorGTUTransceiver(this.imbConnector, imbAnimator, this.network);
229                 new SimulatorTransceiver(this.imbConnector, imbAnimator);
230             }
231             catch (IMBException exception)
232             {
233                 throw new SimRuntimeException(exception);
234             }
235 
236             // Stream to allow the xml-file to be retrievable from a JAR file
237             InputStream stream = URLResource.getResourceAsStream("/N201v8.xml");
238             XmlNetworkLaneParser nlp = new XmlNetworkLaneParser(this.simulator);
239             try
240             {
241                 nlp.build(stream, this.network);
242                 // ODMatrixTrips matrix = N201ODfactory.get(network);
243                 // N201ODfactory.makeGeneratorsFromOD(network, matrix, this.simulator);
244             }
245             catch (NetworkException | ParserConfigurationException | SAXException | IOException | NamingException
246                     | GTUException | OTSGeometryException exception)
247             {
248                 exception.printStackTrace();
249             }
250             Query query = N201ODfactory.getQuery(this.network, new RoadSampler(this.simulator));
251             try
252             {
253                 new StatisticsGTULaneTransceiver(this.imbConnector, imbAnimator, this.network.getId(), query, new Duration(30,
254                         TimeUnit.SECOND));
255             }
256             catch (IMBException exception)
257             {
258                 throw new SimRuntimeException(exception);
259             }
260 
261             URL gisURL = URLResource.getResource("/N201/map.xml");
262             System.err.println("GIS-map file: " + gisURL.toString());
263             CoordinateTransform rdto0 = new CoordinateTransformRD(0, 0);
264             new GisRenderable2D(this.simulator, gisURL, rdto0);
265         }
266 
267         /** {@inheritDoc} */
268         @Override
269         public SimulatorInterface<Time, Duration, OTSSimTimeDouble> getSimulator()
270 
271         {
272             return this.simulator;
273         }
274 
275         /** {@inheritDoc} */
276         @Override
277         public final String toString()
278         {
279             return "N201Model [simulator=" + this.simulator + "]";
280         }
281 
282     }
283 
284     /**
285      * Convert coordinates to/from the Dutch RD system.
286      */
287     class CoordinateTransformRD implements CoordinateTransform, Serializable
288     {
289         /** */
290         private static final long serialVersionUID = 20141017L;
291 
292         /** */
293         final double dx;
294 
295         /** */
296         final double dy;
297 
298         /**
299          * @param dx x transform
300          * @param dy y transform
301          */
302         public CoordinateTransformRD(final double dx, final double dy)
303         {
304             this.dx = dx;
305             this.dy = dy;
306         }
307 
308         /** {@inheritDoc} */
309         @Override
310         public float[] floatTransform(double x, double y)
311         {
312             double[] d = doubleTransform(x, y);
313             return new float[] { (float) d[0], (float) d[1] };
314         }
315 
316         /** {@inheritDoc} */
317         @Override
318         public double[] doubleTransform(double x, double y)
319         {
320             try
321             {
322                 Point2D c = TransformWGS84DutchRDNew.fromWGS84(x, y);
323                 return new double[] { c.getX() - this.dx, c.getY() - this.dy };
324             }
325             catch (Exception exception)
326             {
327                 System.err.println(exception.getMessage());
328                 return new double[] { 0, 0 };
329             }
330         }
331 
332         /** {@inheritDoc} */
333         @Override
334         public final String toString()
335         {
336             return "CoordinateTransformRD [dx=" + this.dx + ", dy=" + this.dy + "]";
337         }
338     }
339 
340 }