package loadfromxml; import java.awt.Color; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.rmi.RemoteException; import java.util.ArrayList; import javax.naming.NamingException; import javax.swing.JFileChooser; import javax.swing.JOptionPane; import javax.swing.filechooser.FileFilter; import javax.xml.parsers.ParserConfigurationException; import org.djunits.unit.DurationUnit; import org.djunits.unit.SpeedUnit; import org.djunits.value.ValueException; import org.djunits.value.vdouble.scalar.Acceleration; import org.djunits.value.vdouble.scalar.Duration; import org.djunits.value.vdouble.scalar.Length; import org.djunits.value.vdouble.scalar.Speed; import org.djunits.value.vdouble.scalar.Time; import org.opentrafficsim.base.modelproperties.Property; import org.opentrafficsim.base.modelproperties.PropertyException; import org.opentrafficsim.base.parameters.ParameterException; import org.opentrafficsim.core.dsol.OTSModelInterface; import org.opentrafficsim.core.geometry.OTSGeometryException; import org.opentrafficsim.core.gtu.GTUException; import org.opentrafficsim.core.gtu.GTUType; import org.opentrafficsim.core.gtu.animation.AccelerationGTUColorer; import org.opentrafficsim.core.gtu.animation.GTUColorer; import org.opentrafficsim.core.gtu.animation.IDGTUColorer; import org.opentrafficsim.core.gtu.animation.SpeedGTUColorer; import org.opentrafficsim.core.gtu.animation.SwitchableGTUColorer; import org.opentrafficsim.core.network.NetworkException; import org.opentrafficsim.core.network.OTSLink; import org.opentrafficsim.core.network.OTSNetwork; import org.opentrafficsim.core.network.OTSNode; import org.opentrafficsim.road.animation.AnimationToggles; import org.opentrafficsim.road.gtu.animation.BlockingColorer; import org.opentrafficsim.road.gtu.animation.DesiredSpeedColorer; import org.opentrafficsim.road.gtu.animation.FixedColor; import org.opentrafficsim.road.gtu.animation.GTUTypeColorer; import org.opentrafficsim.road.gtu.animation.SplitColorer; import org.opentrafficsim.road.gtu.lane.plan.operational.LaneOperationalPlanBuilder; import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser; import org.opentrafficsim.road.network.lane.conflict.ConflictBuilder; import org.opentrafficsim.road.network.lane.object.SpeedSign; import org.opentrafficsim.simulationengine.AbstractWrappableAnimation; import org.opentrafficsim.simulationengine.OTSSimulationException; import org.xml.sax.SAXException; import nl.tudelft.simulation.dsol.SimRuntimeException; import nl.tudelft.simulation.dsol.simtime.SimTimeDoubleUnit; import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface; import nl.tudelft.simulation.dsol.simulators.SimulatorInterface; import nl.tudelft.simulation.event.EventProducer; /** * Select a OTS-network XML file, load it and run it. *
* Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License.
*
* @version $Revision$, $LastChangedDate$, by $Author$, initial version Apr 21, 2017
* @author Alexander Verbraeck
* @author Peter Knoppers
* @author Wouter Schakel
*/
public class LoadXML extends AbstractWrappableAnimation
{
/** */
private static final long serialVersionUID = 20170421L;
/** Name of the XML file. */
private String fileName = null;
/** The XML code. */
private String xml = null;
/**
* Load a network from an XML file; program entry point.
* @param args String[]; the command line arguments; optional name of file to load
* @throws IOException when the file could not be read
* @throws PropertyException should never happen
* @throws OTSSimulationException when an error occurs during simulation
* @throws NamingException when a name collision is detected
* @throws SimRuntimeException should never happen
*/
public static void main(final String[] args)
throws IOException, SimRuntimeException, NamingException, OTSSimulationException, PropertyException
{
LaneOperationalPlanBuilder.INSTANT_LANE_CHANGES = true;
LoadXML loadXML = new LoadXML();
if (0 == args.length)
{
JFileChooser fileChooser = new JFileChooser();
fileChooser.addChoosableFileFilter(new FileFilter()
{
@Override
public boolean accept(final File f)
{
String name = f.getName();
int length = name.length();
if (length < 5)
{
return false;
}
String type = name.substring(length - 4);
return type.equalsIgnoreCase(".xml");
}
@Override
public String getDescription()
{
return "XML files";
}
});
fileChooser.removeChoosableFileFilter(fileChooser.getAcceptAllFileFilter());
if (JFileChooser.APPROVE_OPTION != fileChooser.showOpenDialog(null))
{
System.out.println("No file chosen; exiting");
System.exit(0);
}
loadXML.fileName = fileChooser.getSelectedFile().getAbsolutePath();
}
else
{
loadXML.fileName = args[0];
}
loadXML.xml = new String(Files.readAllBytes(Paths.get(loadXML.fileName)));
try
{
loadXML.buildAnimator(Time.ZERO, Duration.ZERO, new Duration(3600, DurationUnit.SI), new ArrayList