View Javadoc
1   package org.opentrafficsim.road.network.factory.vissim;
2   
3   import java.awt.geom.Rectangle2D;
4   import java.io.File;
5   import java.io.IOException;
6   import java.net.MalformedURLException;
7   import java.net.URL;
8   import java.util.ArrayList;
9   
10  import javax.naming.NamingException;
11  import javax.swing.SwingUtilities;
12  import javax.xml.parsers.ParserConfigurationException;
13  
14  import org.djunits.unit.DurationUnit;
15  import org.djunits.value.vdouble.scalar.Duration;
16  import org.djunits.value.vdouble.scalar.Time;
17  import org.opengis.feature.Property;
18  import org.opentrafficsim.base.modelproperties.PropertyException;
19  import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
20  import org.opentrafficsim.core.dsol.OTSModelInterface;
21  import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
22  import org.opentrafficsim.core.geometry.OTSGeometryException;
23  import org.opentrafficsim.core.gtu.GTUException;
24  import org.opentrafficsim.core.gtu.animation.GTUColorer;
25  import org.opentrafficsim.core.network.NetworkException;
26  import org.opentrafficsim.core.network.OTSNetwork;
27  import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
28  import org.opentrafficsim.simulationengine.OTSSimulationException;
29  import org.opentrafficsim.simulationengine.SimpleSimulatorInterface;
30  import org.xml.sax.SAXException;
31  
32  import nl.tudelft.simulation.dsol.SimRuntimeException;
33  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
34  
35  public class TestVissimParser extends AbstractWrappableAnimation
36  {
37  
38      /**
39       * Main program.
40       * @param args String[]; the command line arguments (not used)
41       * @throws SimRuntimeException should never happen
42       */
43      public static void main(final String[] args) throws SimRuntimeException
44      {
45          SwingUtilities.invokeLater(new Runnable()
46          {
47              @Override
48              public void run()
49              {
50                  try
51                  {
52                      TestVissimParser xmlModel = new TestVissimParser();
53                      // 1 hour simulation run for testing
54                      xmlModel.buildAnimator(Time.ZERO, Duration.ZERO, new Duration(60.0, DurationUnit.MINUTE),
55                              new ArrayList<org.opentrafficsim.base.modelproperties.Property<?>>(), null, true);
56                  }
57                  catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
58                  {
59                      exception.printStackTrace();
60                  }
61              }
62          });
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      public final String shortName()
68      {
69          return "TestXMLModel";
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public final String description()
75      {
76          return "TestXMLModel";
77      }
78  
79      /** {@inheritDoc} */
80      @Override
81      public final void stopTimersThreads()
82      {
83          super.stopTimersThreads();
84      }
85  
86      /** {@inheritDoc} */
87      @Override
88      protected final void addTabs(final SimpleSimulatorInterface simulator)
89      {
90          return;
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      protected final OTSModelInterface makeModel(final GTUColorer colorer)
96      {
97          return new VissimImport();
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     protected final java.awt.geom.Rectangle2D.Double makeAnimationRectangle()
103     {
104         // return new Rectangle2D.Double(-1000, -1000, 2000, 2000);
105         return new Rectangle2D.Double(162000, 384500, 2000, 2000);
106     }
107 
108     /** {@inheritDoc} */
109     @Override
110     public final String toString()
111     {
112         return "TestVissimParser []";
113     }
114 
115     /**
116      * Model to test the Vissim File Format parser.
117      * <p>
118      * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
119      * All rights reserved. BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim
120      * License</a>.
121      * <p>
122      * $LastChangedDate: 2017-04-29 13:29:16 +0200 (Sat, 29 Apr 2017) $, @version $Revision: 3573 $, by $Author: averbraeck $,
123      * initial version un 27, 2015 <br>
124      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
125      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
126      */
127     /**
128      * @author P070518
129      */
130     class VissimImport implements OTSModelInterface
131     {
132         /** */
133         private static final long serialVersionUID = 20141121L;
134 
135         /** The simulator. */
136         private OTSDEVSSimulatorInterface simulator;
137 
138         /** The network. */
139         private OTSNetwork network = new OTSNetwork("test Vissim network");
140 
141         /** {@inheritDoc} */
142         @Override
143         public final void constructModel(final SimulatorInterface<Time, Duration, OTSSimTimeDouble> pSimulator)
144                 throws SimRuntimeException
145         {
146 
147             // OTS network or SmartTraffic??
148             boolean OpenTrafficSim = false;
149             String sinkKillClassName;
150             String sensorClassName;
151             String trafficLightName;
152             if (OpenTrafficSim)
153             {
154                 sinkKillClassName = "org.opentrafficsim.road.network.lane.object.sensor.SinkSensor";
155                 sensorClassName = "org.opentrafficsim.road.network.lane.object.sensor.SimpleReportingSensor";
156                 trafficLightName = "org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight";
157             }
158             else
159             {
160                 sinkKillClassName = "nl.grontmij.smarttraffic.model.KillSensor";
161                 sensorClassName = "nl.grontmij.smarttraffic.model.CheckSensor";
162                 trafficLightName = "org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight";
163             }
164             this.simulator = (OTSDEVSSimulatorInterface) pSimulator;
165             ClassLoader classLoader = getClass().getClassLoader();
166             URL inputUrl = null;
167             try
168             {
169                 inputUrl = new URL(classLoader.getResource("ehv_eisen1_VA.inpx").toString());
170             }
171             catch (MalformedURLException e1)
172             {
173                 // TODO Auto-generated catch block
174                 e1.printStackTrace();
175             }
176             String path = classLoader.getResource("").getPath().toString();
177             File outputFile = new File(path, "/testEindhoven.xml");
178             try
179             {
180                 outputFile.createNewFile();
181             }
182             catch (IOException e1)
183             {
184                 e1.printStackTrace();
185             }
186             VissimNetworkLaneParser nlp = new VissimNetworkLaneParser(this.simulator);
187 
188             try
189             {
190                 this.network = nlp.build(inputUrl, outputFile, network, sinkKillClassName, sensorClassName, trafficLightName);
191             }
192             catch (NetworkException | ParserConfigurationException | SAXException | IOException | NamingException | GTUException
193                     | OTSGeometryException exception)
194             {
195                 exception.printStackTrace();
196             }
197 
198         }
199 
200         /**
201          * @param property
202          * @return a double
203          */
204         private Double parseDouble(Property property)
205         {
206             if (property.getValue() != null)
207             {
208                 if (property.getValue().toString() != null)
209                 {
210                     return Double.parseDouble(property.getValue().toString());
211                 }
212             }
213             return Double.NaN;
214         }
215 
216         /** {@inheritDoc} */
217         @Override
218         public SimulatorInterface<Time, Duration, OTSSimTimeDouble> getSimulator()
219         {
220             return this.simulator;
221         }
222 
223         /** {@inheritDoc} */
224         @Override
225         public OTSNetwork getNetwork()
226         {
227             return this.network;
228         }
229 
230         /** {@inheritDoc} */
231         @Override
232         public final String toString()
233         {
234             return "TestVissimParser [simulator=" + this.simulator + "]";
235         }
236 
237     }
238 
239 }