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