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.TimeUnit;
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       * 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          SwingUtilities.invokeLater(new Runnable() {
44              @Override
45              public void run() {
46                  try {
47                      TestVissimParser xmlModel = new TestVissimParser();
48                      // 1 hour simulation run for testing
49                      xmlModel.buildAnimator(new Time(0.0, TimeUnit.SECOND), new Duration(0.0, TimeUnit.SECOND), new Duration(
50                          60.0, TimeUnit.MINUTE), new ArrayList<org.opentrafficsim.base.modelproperties.Property<?>>(), null,
51                          true);
52                  } catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception) {
53                      exception.printStackTrace();
54                  }
55              }
56          });
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      public final String shortName() {
62          return "TestXMLModel";
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      public final String description() {
68          return "TestXMLModel";
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      public final void stopTimersThreads() {
74          super.stopTimersThreads();
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      protected final void addTabs(final SimpleSimulatorInterface simulator) {
80          return;
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      protected final OTSModelInterface makeModel(final GTUColorer colorer) {
86          return new VissimImport();
87      }
88  
89      /** {@inheritDoc} */
90      @Override
91      protected final java.awt.geom.Rectangle2D.Double makeAnimationRectangle() {
92          // return new Rectangle2D.Double(-1000, -1000, 2000, 2000);
93          return new Rectangle2D.Double(162000, 384500, 2000, 2000);
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public final String toString() {
99          return "TestVissimParser []";
100     }
101 
102     /**
103      * Model to test the Vissim File Format parser.
104      * <p>
105      * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. <br>
106      * All rights reserved. BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim
107      * License</a>.
108      * <p>
109      * $LastChangedDate: 2016-12-11 14:07:37 +0100 (Sun, 11 Dec 2016) $, @version $Revision: 2838 $, by $Author: averbraeck $,
110      * initial version un 27, 2015 <br>
111      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
112      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
113      */
114     /**
115      * @author P070518
116      */
117     class VissimImport implements OTSModelInterface {
118         /** */
119         private static final long serialVersionUID = 20141121L;
120 
121         /** The simulator. */
122         private OTSDEVSSimulatorInterface simulator;
123 
124         /** The network. */
125         private OTSNetwork network = new OTSNetwork("test Vissim network");
126 
127         /** {@inheritDoc} */
128         @Override
129         public final void constructModel(final SimulatorInterface<Time, Duration, OTSSimTimeDouble> pSimulator)
130             throws SimRuntimeException {
131 
132             // OTS network or SmartTraffic??
133             boolean OpenTrafficSim = false;
134             String sinkKillClassName;
135             String sensorClassName;
136             String trafficLightName;
137             if (OpenTrafficSim) {
138                 sinkKillClassName = "org.opentrafficsim.road.network.lane.object.sensor.SinkSensor";
139                 sensorClassName = "org.opentrafficsim.road.network.lane.object.sensor.SimpleReportingSensor";
140                 trafficLightName = "org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight";
141             } else {
142                 sinkKillClassName = "nl.grontmij.smarttraffic.model.KillSensor";
143                 sensorClassName = "nl.grontmij.smarttraffic.model.CheckSensor";
144                 trafficLightName = "org.opentrafficsim.road.network.lane.object.trafficlight.SimpleTrafficLight";
145             }
146             this.simulator = (OTSDEVSSimulatorInterface) pSimulator;
147             ClassLoader classLoader = getClass().getClassLoader();
148             URL inputUrl = null;
149             try {
150                 inputUrl = new URL(classLoader.getResource("ehv_eisen1_VA.inpx").toString());
151             } catch (MalformedURLException e1) {
152                 // TODO Auto-generated catch block
153                 e1.printStackTrace();
154             }
155             String path = classLoader.getResource("").getPath().toString();
156             File outputFile = new File(path, "/testEindhoven.xml");
157             try {
158                 outputFile.createNewFile();
159             } catch (IOException e1) {
160                 e1.printStackTrace();
161             }
162             VissimNetworkLaneParser nlp = new VissimNetworkLaneParser(this.simulator);
163 
164             try {
165                 this.network = nlp.build(inputUrl, outputFile, network, sinkKillClassName, sensorClassName,
166                     trafficLightName);
167             } catch (NetworkException | ParserConfigurationException | SAXException | IOException | NamingException
168                 | GTUException | OTSGeometryException exception) {
169                 exception.printStackTrace();
170             }
171 
172         }
173 
174         /**
175          * @param property
176          * @return a double
177          */
178         private Double parseDouble(Property property) {
179             if (property.getValue() != null) {
180                 if (property.getValue().toString() != null) {
181                     return Double.parseDouble(property.getValue().toString());
182                 }
183             }
184             return Double.NaN;
185         }
186 
187         /** {@inheritDoc} */
188         @Override
189         public SimulatorInterface<Time, Duration, OTSSimTimeDouble> getSimulator()
190         {
191             return this.simulator;
192         }
193 
194         /** {@inheritDoc} */
195         @Override
196         public OTSNetwork getNetwork()
197         {
198             return this.network;
199         }
200 
201         /** {@inheritDoc} */
202         @Override
203         public final String toString() {
204             return "TestVissimParser [simulator=" + this.simulator + "]";
205         }
206 
207     }
208 
209 }