View Javadoc
1   package loadfromxml;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.File;
5   import java.io.IOException;
6   import java.nio.charset.StandardCharsets;
7   import java.nio.file.Files;
8   import java.nio.file.Paths;
9   import java.rmi.RemoteException;
10  import java.util.ArrayList;
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.parsers.ParserConfigurationException;
17  
18  import org.djunits.unit.DurationUnit;
19  import org.djunits.value.vdouble.scalar.Duration;
20  import org.djunits.value.vdouble.scalar.Time;
21  import org.opentrafficsim.base.modelproperties.Property;
22  import org.opentrafficsim.base.modelproperties.PropertyException;
23  import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface;
24  import org.opentrafficsim.core.dsol.OTSModelInterface;
25  import org.opentrafficsim.core.dsol.OTSSimTimeDouble;
26  import org.opentrafficsim.core.geometry.OTSGeometryException;
27  import org.opentrafficsim.core.gtu.GTUException;
28  import org.opentrafficsim.core.gtu.animation.GTUColorer;
29  import org.opentrafficsim.core.network.NetworkException;
30  import org.opentrafficsim.core.network.OTSNetwork;
31  import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser;
32  import org.opentrafficsim.simulationengine.AbstractWrappableAnimation;
33  import org.opentrafficsim.simulationengine.OTSSimulationException;
34  import org.xml.sax.SAXException;
35  
36  import nl.tudelft.simulation.dsol.SimRuntimeException;
37  import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
38  import nl.tudelft.simulation.event.EventProducer;
39  
40  /**
41   * Select a OTS-network XML file, load it and run it.
42   * <p>
43   * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
44   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
45   * <p>
46   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Apr 21, 2017 <br>
47   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
48   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
49   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
50   */
51  public class LoadXML extends AbstractWrappableAnimation
52  {
53  
54      /** */
55      private static final long serialVersionUID = 20170421L;
56  
57      /** Name of the XML file. */
58      private String fileName = null;
59  
60      /** The XML code. */
61      private String xml = null;
62  
63      /**
64       * Load a network from an XML file; program entry point.
65       * @param args String[]; the command line arguments (currently not used)
66       * @throws IOException when the file could not be read
67       * @throws PropertyException should never happen
68       * @throws OTSSimulationException when an error occurs during simulation
69       * @throws NamingException when a name collision is detected
70       * @throws SimRuntimeException should never happen
71       */
72      public static void main(final String[] args) throws IOException, SimRuntimeException, NamingException,
73              OTSSimulationException, PropertyException
74      {
75          JFileChooser fileChooser = new JFileChooser();
76          fileChooser.addChoosableFileFilter(new FileFilter()
77          {
78  
79              @Override
80              public boolean accept(final File f)
81              {
82                  String name = f.getName();
83                  int length = name.length();
84                  if (length < 5)
85                  {
86                      return false;
87                  }
88                  String type = name.substring(length - 4);
89                  return type.equalsIgnoreCase(".xml");
90              }
91  
92              @Override
93              public String getDescription()
94              {
95                  return "XML files";
96              }
97          });
98          fileChooser.removeChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
99          if (JFileChooser.APPROVE_OPTION != fileChooser.showOpenDialog(null))
100         {
101             System.out.println("No file chosen; exiting");
102             System.exit(0);
103         }
104         LoadXML loadXML = new LoadXML();
105         loadXML.fileName = fileChooser.getSelectedFile().getAbsolutePath();
106         loadXML.xml = new String(Files.readAllBytes(Paths.get(loadXML.fileName)));
107         try
108         {
109             loadXML.buildAnimator(Time.ZERO, Duration.ZERO, new Duration(3600, DurationUnit.SI), new ArrayList<Property<?>>(),
110                     null, true);
111         }
112         catch (SimRuntimeException sre)
113         {
114             JOptionPane.showMessageDialog(null, sre.getMessage(), "Exception occured", JOptionPane.ERROR_MESSAGE);
115             System.exit(1);
116         }
117     }
118 
119     /** {@inheritDoc} */
120     @Override
121     public final String shortName()
122     {
123         return this.fileName;
124     }
125 
126     /** {@inheritDoc} */
127     @Override
128     public final String description()
129     {
130         return "OTS network from " + this.fileName;
131     }
132 
133     /** Currently active XML model. */
134     private XMLModel model = null;
135 
136     /** {@inheritDoc} */
137     @Override
138     protected final OTSModelInterface makeModel(final GTUColorer colorer) throws OTSSimulationException
139     {
140         this.model = new XMLModel();
141         return this.model;
142     }
143 
144     /**
145      * The network.
146      */
147     class XMLModel extends EventProducer implements OTSModelInterface
148     {
149 
150         /** */
151         private static final long serialVersionUID = 20170421L;
152 
153         /** The network. */
154         private OTSNetwork network;
155 
156         /** The simulator. */
157         private SimulatorInterface<Time, Duration, OTSSimTimeDouble> simulator;
158 
159         /** {@inheritDoc} */
160         @SuppressWarnings("synthetic-access")
161         @Override
162         public void constructModel(final SimulatorInterface<Time, Duration, OTSSimTimeDouble> theSimulator)
163                 throws SimRuntimeException, RemoteException
164         {
165             this.simulator = theSimulator;
166             XmlNetworkLaneParser nlp = new XmlNetworkLaneParser((OTSDEVSSimulatorInterface) theSimulator);
167             try
168             {
169                 this.network = nlp.build(new ByteArrayInputStream(LoadXML.this.xml.getBytes(StandardCharsets.UTF_8)));
170             }
171             catch (NetworkException | ParserConfigurationException | SAXException | IOException | NamingException
172                     | GTUException | OTSGeometryException exception)
173             {
174                 exception.printStackTrace();
175                 // Abusing the SimRuntimeException to propagate the message to the main method (the problem could actually be a
176                 // SAXException)
177                 throw new SimRuntimeException(exception.getMessage());
178             }
179         }
180 
181         /** {@inheritDoc} */
182         @Override
183         public SimulatorInterface<Time, Duration, OTSSimTimeDouble> getSimulator() throws RemoteException
184         {
185             return this.simulator;
186         }
187 
188         /** {@inheritDoc} */
189         @Override
190         public OTSNetwork getNetwork()
191         {
192             return this.network;
193         }
194 
195     }
196 }