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>(), null, true); } catch (SimRuntimeException sre) { JOptionPane.showMessageDialog(null, sre.getMessage(), "Exception occured", JOptionPane.ERROR_MESSAGE); System.exit(1); } } /** {@inheritDoc} */ @Override public final String shortName() { return this.fileName; } /** {@inheritDoc} */ @Override public final String description() { return "OTS network from " + this.fileName; } /** Currently active XML model. */ private XMLModel model = null; /** GTU colorer. */ private GTUColorer colorer = SwitchableGTUColorer.builder().addActiveColorer(new FixedColor(Color.BLUE, "Blue")) .addColorer(GTUTypeColorer.DEFAULT).addColorer(new IDGTUColorer()) .addColorer(new SpeedGTUColorer(new Speed(150, SpeedUnit.KM_PER_HOUR))) .addColorer(new DesiredSpeedColorer(new Speed(50, SpeedUnit.KM_PER_HOUR), new Speed(150, SpeedUnit.KM_PER_HOUR))) .addColorer(new AccelerationGTUColorer(Acceleration.createSI(-6.0), Acceleration.createSI(2))) .addColorer(new SplitColorer()).addColorer(new BlockingColorer()).build(); /** {@inheritDoc} */ @Override protected final OTSModelInterface makeModel() throws OTSSimulationException { this.model = new XMLModel(); return this.model; } /** {@inheritDoc} */ @Override protected final void addAnimationToggles() { AnimationToggles.setIconAnimationTogglesFull(this); toggleAnimationClass(OTSLink.class); toggleAnimationClass(OTSNode.class); showAnimationClass(SpeedSign.class); } /** * The network. */ class XMLModel extends EventProducer implements OTSModelInterface { /** */ private static final long serialVersionUID = 20170421L; /** The network. */ private OTSNetwork network; /** The simulator. */ private SimulatorInterface simulator; /** {@inheritDoc} */ @SuppressWarnings("synthetic-access") @Override public void constructModel(final SimulatorInterface theSimulator) throws SimRuntimeException { this.simulator = theSimulator; XmlNetworkLaneParser nlp = new XmlNetworkLaneParser((DEVSSimulatorInterface.TimeDoubleUnit) theSimulator, getColorer()); try { this.network = nlp.build(new ByteArrayInputStream(LoadXML.this.xml.getBytes(StandardCharsets.UTF_8)), false); ConflictBuilder.buildConflicts(this.network, GTUType.VEHICLE, (DEVSSimulatorInterface.TimeDoubleUnit) theSimulator, new ConflictBuilder.FixedWidthGenerator(Length.createSI(2.0))); } catch (NetworkException | ParserConfigurationException | SAXException | IOException | NamingException | GTUException | OTSGeometryException | ValueException | ParameterException exception) { exception.printStackTrace(); // Abusing the SimRuntimeException to propagate the message to the main method (the problem could actually be a // SAXException) throw new SimRuntimeException(exception.getMessage()); } } /** {@inheritDoc} */ @Override public SimulatorInterface getSimulator() { return this.simulator; } /** {@inheritDoc} */ @Override public OTSNetwork getNetwork() { return this.network; } } /** {@inheritDoc} */ @Override public GTUColorer getColorer() { return this.colorer; } }