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.road.network.factory.xml.XmlNetworkLaneParser;
50  import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
51  import org.opentrafficsim.simulationengine.OTSSimulationException;
52  import org.opentrafficsim.simulationengine.SimpleAnimator;
53  import org.opentrafficsim.simulationengine.SimpleSimulatorInterface;
54  import org.xml.sax.SAXException;
55  
56  /**
57   * <p>
58   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
59   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
60   * <p>
61   * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
62   * initial version Oct 17, 2014 <br>
63   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
64   */
65  public class A58IMB extends AbstractWrappableAnimation
66  {
67      /** */
68      private static final long serialVersionUID = 20161007L;
69  
70      /** The model. */
71      private A58Model model;
72  
73      /**
74       * Main program.
75       * @param args String[]; the command line arguments (not used)
76       * @throws SimRuntimeException should never happen
77       */
78      public static void main(final String[] args) throws SimRuntimeException
79      {
80          SwingUtilities.invokeLater(new Runnable()
81          {
82              @Override
83              public void run()
84              {
85                  try
86                  {
87                      A58IMB n201Model = new A58IMB();
88                      // 1 hour simulation run for testing
89                      n201Model.buildAnimator(new Time(0.0, TimeUnit.SECOND), new Duration(0.0, TimeUnit.SECOND),
90                              new Duration(10.0, TimeUnit.HOUR), new ArrayList<Property<?>>(), null, true);
91                  }
92                  catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
93                  {
94                      exception.printStackTrace();
95                  }
96              }
97          });
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     public final String shortName()
103     {
104         return "Model A58";
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     public final String description()
110     {
111         return "Model A58 - IMB";
112     }
113 
114     /** {@inheritDoc} */
115     @Override
116     public final void stopTimersThreads()
117     {
118         super.stopTimersThreads();
119     }
120 
121     /** {@inheritDoc} */
122     @Override
123     protected final JPanel makeCharts(final SimpleSimulatorInterface simulator)
124     {
125         return null;
126     }
127 
128     /** {@inheritDoc} */
129     @Override
130     protected final OTSModelInterface makeModel(final GTUColorer colorer)
131     {
132         System.out.println("A58IMB.makeModel called");
133         this.model = new A58Model(getSavedUserModifiedProperties(), colorer, new OTSNetwork("A58 network"));
134         return this.model;
135     }
136 
137     /**
138      * @return the saved user properties for a next run
139      */
140     private List<Property<?>> getSavedUserModifiedProperties()
141     {
142         return this.savedUserModifiedProperties;
143     }
144 
145     /** {@inheritDoc} */
146     @Override
147     protected final Double makeAnimationRectangle()
148     {
149         return new Rectangle2D.Double(150000, 385000, 5500, 5000);
150     }
151 
152     /** {@inheritDoc} */
153     @Override
154     public final String toString()
155     {
156         return "A58 network - IMB []";
157     }
158 
159     /**
160      * Model to test the XML parser.
161      * <p>
162      * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
163      * All rights reserved. BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim
164      * License</a>.
165      * <p>
166      * $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $,
167      * initial version un 27, 2015 <br>
168      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
169      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
170      */
171     class A58Model implements OTSModelInterface
172     {
173         /** */
174         private static final long serialVersionUID = 20141121L;
175 
176         /** The simulator. */
177         private OTSDEVSSimulatorInterface simulator;
178 
179         /** User settable properties. */
180         private List<Property<?>> modelProperties = null;
181 
182         /** the network as created by the AbstractWrappableIMBAnimation. */
183         private final OTSNetwork network;
184 
185         /** Connector to the IMB hub. */
186         OTSIMBConnector imbConnector;
187 
188         /**
189          * @param modelProperties ArrayList&lt;AbstractProperty&lt;?&gt;&gt;; the properties
190          * @param gtuColorer the default and initial GTUColorer, e.g. a DefaultSwitchableTUColorer.
191          * @param network Network; the network
192          */
193         A58Model(final List<Property<?>> modelProperties, final GTUColorer gtuColorer, final OTSNetwork network)
194         {
195             this.modelProperties = modelProperties;
196             this.network = network;
197         }
198 
199         /** {@inheritDoc} */
200         @Override
201         public final void constructModel(
202                 final SimulatorInterface<Time, Duration, OTSSimTimeDouble> pSimulator)
203                 throws SimRuntimeException
204         {
205             System.out.println("A58IMB: constructModel called; Connecting to IMB");
206             this.simulator = (OTSDEVSSimulatorInterface) pSimulator;
207             SimpleAnimator imbAnimator = (SimpleAnimator) pSimulator;
208             try
209             {
210                 CompoundProperty imbSettings = null;
211                 for (Property<?> property : this.modelProperties)
212                 {
213                     if (property.getKey().equals(OTSIMBConnector.PROPERTY_KEY))
214                     {
215                         imbSettings = (CompoundProperty) property;
216                     }
217                 }
218                 System.out.println("link count " + this.network.getLinkMap().size());
219                 Throw.whenNull(imbSettings, "IMB Settings not found in properties");
220                 this.imbConnector = OTSIMBConnector.create(imbSettings, "OTS");
221                 new NetworkTransceiver(this.imbConnector, imbAnimator, this.network);
222                 new NodeTransceiver(this.imbConnector, imbAnimator, this.network);
223                 new LinkGTUTransceiver(this.imbConnector, imbAnimator, this.network);
224                 new LaneGTUTransceiver(this.imbConnector, imbAnimator, this.network);
225                 new GTUTransceiver(this.imbConnector, imbAnimator, this.network);
226                 new SensorGTUTransceiver(this.imbConnector, imbAnimator, this.network);
227                 new SimulatorTransceiver(this.imbConnector, imbAnimator);
228             }
229             catch (IMBException exception)
230             {
231                 throw new SimRuntimeException(exception);
232             }
233 
234             // Stream to allow the xml-file to be retrievable from a JAR file
235             InputStream stream = URLResource.getResourceAsStream("/A58v1.xml");
236             XmlNetworkLaneParser nlp = new XmlNetworkLaneParser(this.simulator);
237             try
238             {
239                 nlp.build(stream, this.network);
240                 // ODMatrixTrips matrix = A58ODfactory.get(network);
241                 // A58ODfactory.makeGeneratorsFromOD(network, matrix, this.simulator);
242             }
243             catch (NetworkException | ParserConfigurationException | SAXException | IOException | NamingException | GTUException
244                     | OTSGeometryException exception)
245             {
246                 exception.printStackTrace();
247             }
248             // TODO
249             // Query query = N201ODfactory.getQuery(this.network, new Sampler(this.simulator));
250             // try
251             // {
252             // new StatisticsGTULaneTransceiver(this.imbConnector, imbAnimator, this.network, query,
253             // new Duration(30, TimeUnit.SECOND));
254             // }
255             // catch (IMBException exception)
256             // {
257             // throw new SimRuntimeException(exception);
258             // }
259 
260             // TODO
261             URL gisURL = URLResource.getResource("/A58/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 "A58Model [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 }