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