View Javadoc
1   package org.opentrafficsim.aimsun;
2   
3   import java.awt.Dimension;
4   import java.io.ByteArrayInputStream;
5   import java.io.File;
6   import java.io.IOException;
7   import java.net.URISyntaxException;
8   import java.nio.charset.StandardCharsets;
9   import java.nio.file.Files;
10  import java.nio.file.Paths;
11  import java.util.LinkedHashMap;
12  import java.util.Map;
13  
14  import javax.naming.NamingException;
15  import javax.swing.JFileChooser;
16  import javax.swing.JOptionPane;
17  import javax.swing.filechooser.FileFilter;
18  import javax.xml.bind.JAXBException;
19  import javax.xml.parsers.ParserConfigurationException;
20  
21  import org.djunits.value.vdouble.scalar.Duration;
22  import org.djunits.value.vdouble.scalar.Length;
23  import org.djunits.value.vdouble.scalar.Time;
24  import org.opentrafficsim.core.dsol.AbstractOTSModel;
25  import org.opentrafficsim.core.dsol.OTSLoggingAnimator;
26  import org.opentrafficsim.core.dsol.OTSModelInterface;
27  import org.opentrafficsim.core.dsol.OTSSimulationException;
28  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
29  import org.opentrafficsim.core.geometry.OTSGeometryException;
30  import org.opentrafficsim.core.gtu.GTUDumper;
31  import org.opentrafficsim.core.gtu.GTUException;
32  import org.opentrafficsim.core.gtu.GTUType;
33  import org.opentrafficsim.core.network.NetworkException;
34  import org.opentrafficsim.draw.core.OTSDrawingException;
35  import org.opentrafficsim.road.network.OTSRoadNetwork;
36  import org.opentrafficsim.road.network.factory.xml.XmlParserException;
37  import org.opentrafficsim.road.network.factory.xml.parser.XmlNetworkLaneParser;
38  import org.opentrafficsim.road.network.lane.CrossSectionLink;
39  import org.opentrafficsim.road.network.lane.conflict.ConflictBuilder;
40  import org.opentrafficsim.road.network.lane.conflict.LaneCombinationList;
41  import org.opentrafficsim.swing.gui.OTSAnimationPanel;
42  import org.opentrafficsim.swing.gui.OTSSimulationApplication;
43  import org.xml.sax.SAXException;
44  
45  import nl.tudelft.simulation.dsol.SimRuntimeException;
46  import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterException;
47  import nl.tudelft.simulation.jstats.streams.MersenneTwister;
48  import nl.tudelft.simulation.jstats.streams.StreamInterface;
49  
50  /**
51   * Select a OTS-network XML file, load it and run it.
52   * <p>
53   * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
54   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
55   * <p>
56   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Apr 21, 2017 <br>
57   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
58   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
59   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
60   */
61  public class LoadXML extends OTSSimulationApplication<OTSModelInterface>
62  {
63      /** */
64      private static final long serialVersionUID = 20170421L;
65  
66      /**
67       * @param model OTSModelInterface; the model
68       * @param animationPanel OTSAnimationPanel; the animation panel
69       * @throws OTSDrawingException on drawing error
70       */
71      public LoadXML(final OTSModelInterface model, final OTSAnimationPanel animationPanel) throws OTSDrawingException
72      {
73          super(model, animationPanel);
74      }
75  
76      /**
77       * Load a network from an XML file; program entry point.
78       * @param args String[]; the command line arguments; optional name of file to load
79       * @throws IOException when the file could not be read
80       * @throws InputParameterException should never happen
81       * @throws OTSSimulationException when an error occurs during simulation
82       * @throws NamingException when a name collision is detected
83       * @throws SimRuntimeException should never happen
84       */
85      public static void main(final String[] args)
86              throws IOException, SimRuntimeException, NamingException, OTSSimulationException, InputParameterException
87      {
88          // TODO: LaneOperationalPlanBuilder.INSTANT_LANE_CHANGES = false;
89          String fileName;
90          String xml;
91          if (0 == args.length)
92          {
93              JFileChooser fileChooser = new JFileChooser();
94              fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
95              fileChooser.addChoosableFileFilter(new FileFilter()
96              {
97  
98                  @Override
99                  public boolean accept(final File f)
100                 {
101                     if (f.isDirectory())
102                     {
103                         return true;
104                     }
105                     String name = f.getName();
106                     int length = name.length();
107                     if (length < 5)
108                     {
109                         return false;
110                     }
111                     String type = name.substring(length - 4);
112                     return type.equalsIgnoreCase(".xml");
113                 }
114 
115                 @Override
116                 public String getDescription()
117                 {
118                     return "XML files";
119                 }
120             });
121             fileChooser.removeChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
122             if (JFileChooser.APPROVE_OPTION != fileChooser.showOpenDialog(null))
123             {
124                 System.out.println("No file chosen; exiting");
125                 System.exit(0);
126             }
127             fileName = fileChooser.getSelectedFile().getAbsolutePath();
128         }
129         else
130         {
131             fileName = args[0];
132         }
133         xml = new String(Files.readAllBytes(Paths.get(fileName)));
134         try
135         {
136             OTSLoggingAnimator simulator = new OTSLoggingAnimator("C:/Temp/loadXMLeventlog.txt");
137             XMLModel xmlModel = new XMLModel(simulator, "XML model", "Model built from XML file " + fileName, xml);
138             Map<String, StreamInterface> map = new LinkedHashMap<>();
139             // TODO: This seed is Aimsun specific.
140             map.put("generation", new MersenneTwister(6L));
141             simulator.initialize(Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600.0), xmlModel, map);
142             OTSAnimationPanel animationPanel = new OTSAnimationPanel(xmlModel.getNetwork().getExtent(), new Dimension(800, 600),
143                     simulator, xmlModel, DEFAULT_COLORER, xmlModel.getNetwork());
144             new LoadXML(xmlModel, animationPanel);
145         }
146         catch (SimRuntimeException | OTSDrawingException sre)
147         {
148             JOptionPane.showMessageDialog(null, sre.getMessage(), "Exception occured", JOptionPane.ERROR_MESSAGE);
149             System.exit(1);
150         }
151     }
152 
153     /**
154      * The Model.
155      */
156     static class XMLModel extends AbstractOTSModel
157     {
158         /** */
159         private static final long serialVersionUID = 20170421L;
160 
161         /** The network. */
162         private OTSRoadNetwork network;
163 
164         /** The XML. */
165         private final String xml;
166 
167         /**
168          * @param simulator OTSSimulatorInterface; the simulator
169          * @param shortName String; name of the model
170          * @param description String; description of the model
171          * @param xml String; the XML string
172          */
173         XMLModel(final OTSSimulatorInterface simulator, final String shortName, final String description, final String xml)
174         {
175             super(simulator, shortName, description);
176             this.xml = xml;
177         }
178 
179         /** {@inheritDoc} */
180         @Override
181         public void constructModel() throws SimRuntimeException
182         {
183             this.network = new OTSRoadNetwork(getShortName(), true);
184             try
185             {
186                 XmlNetworkLaneParser.build(new ByteArrayInputStream(this.xml.getBytes(StandardCharsets.UTF_8)), this.network,
187                         getSimulator(), false);
188                 // TODO: These links are Aimsun specific.
189                 LaneCombinationList ignoreList = new LaneCombinationList();
190                 ignoreList.addLinkCombination((CrossSectionLink) this.network.getLink("928_J5"),
191                         (CrossSectionLink) this.network.getLink("928_J6"));
192                 ignoreList.addLinkCombination((CrossSectionLink) this.network.getLink("925_J1"),
193                         (CrossSectionLink) this.network.getLink("925_J2"));
194                 LaneCombinationList permittedList = new LaneCombinationList();
195                 ConflictBuilder.buildConflictsParallel(this.network, this.network.getGtuType(GTUType.DEFAULTS.VEHICLE),
196                         getSimulator(), new ConflictBuilder.FixedWidthGenerator(Length.instantiateSI(2.0)), ignoreList,
197                         permittedList);
198                 new GTUDumper(simulator, Time.ZERO, Duration.instantiateSI(60), network, "C:/Temp/loadxml");
199             }
200             catch (NetworkException | OTSGeometryException | JAXBException | URISyntaxException | XmlParserException
201                     | SAXException | ParserConfigurationException | GTUException exception)
202             {
203                 exception.printStackTrace();
204                 // Abusing the SimRuntimeException to propagate the message to the main method (the problem could actually be a
205                 // parsing problem)
206                 throw new SimRuntimeException(exception.getMessage());
207             }
208         }
209 
210         /** {@inheritDoc} */
211         @Override
212         public OTSRoadNetwork getNetwork()
213         {
214             return this.network;
215         }
216 
217     }
218 
219 }