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