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
39
40
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
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
60 @Override
61 public final String shortName() {
62 return "TestXMLModel";
63 }
64
65
66 @Override
67 public final String description() {
68 return "TestXMLModel";
69 }
70
71
72 @Override
73 public final void stopTimersThreads() {
74 super.stopTimersThreads();
75 }
76
77
78 @Override
79 protected final void addTabs(final SimpleSimulatorInterface simulator) {
80 return;
81 }
82
83
84 @Override
85 protected final OTSModelInterface makeModel(final GTUColorer colorer) {
86 return new VissimImport();
87 }
88
89
90 @Override
91 protected final java.awt.geom.Rectangle2D.Double makeAnimationRectangle() {
92
93 return new Rectangle2D.Double(162000, 384500, 2000, 2000);
94 }
95
96
97 @Override
98 public final String toString() {
99 return "TestVissimParser []";
100 }
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 class VissimImport implements OTSModelInterface {
118
119 private static final long serialVersionUID = 20141121L;
120
121
122 private OTSDEVSSimulatorInterface simulator;
123
124
125 private OTSNetwork network = new OTSNetwork("test Vissim network");
126
127
128 @Override
129 public final void constructModel(final SimulatorInterface<Time, Duration, OTSSimTimeDouble> pSimulator)
130 throws SimRuntimeException {
131
132
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
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
176
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
188 @Override
189 public SimulatorInterface<Time, Duration, OTSSimTimeDouble> getSimulator()
190 {
191 return this.simulator;
192 }
193
194
195 @Override
196 public OTSNetwork getNetwork()
197 {
198 return this.network;
199 }
200
201
202 @Override
203 public final String toString() {
204 return "TestVissimParser [simulator=" + this.simulator + "]";
205 }
206
207 }
208
209 }