package org.opentrafficsim.demo.network.xml; import java.awt.Dimension; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.Serializable; import java.net.URISyntaxException; import java.nio.charset.StandardCharsets; import java.rmi.RemoteException; import javax.naming.NamingException; import javax.swing.SwingUtilities; import javax.xml.bind.JAXBException; import javax.xml.parsers.ParserConfigurationException; import org.djunits.value.vdouble.scalar.Duration; import org.djunits.value.vdouble.scalar.Time; import org.opentrafficsim.core.dsol.AbstractOTSModel; import org.opentrafficsim.core.dsol.OTSAnimator; import org.opentrafficsim.core.dsol.OTSReplication; import org.opentrafficsim.core.dsol.OTSSimulatorInterface; import org.opentrafficsim.core.gtu.GtuException; import org.opentrafficsim.core.network.NetworkException; import org.opentrafficsim.demo.network.xml.Grid10X10.TestXMLModelGrid; import org.opentrafficsim.draw.core.OTSDrawingException; import org.opentrafficsim.draw.factory.DefaultAnimationFactory; import org.opentrafficsim.road.network.RoadNetwork; import org.opentrafficsim.road.network.defaults.RoadNetworkDefaults; import org.opentrafficsim.road.network.factory.xml.XmlParserException; import org.opentrafficsim.road.network.factory.xml.parser.XmlNetworkLaneParser; import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLight; import org.opentrafficsim.road.network.lane.object.trafficlight.TrafficLightColor; import org.opentrafficsim.swing.gui.OTSAnimationPanel; import org.opentrafficsim.swing.gui.OTSSimulationApplication; import org.opentrafficsim.trafficcontrol.TrafficControlException; import org.xml.sax.SAXException; import nl.tudelft.simulation.dsol.SimRuntimeException; import nl.tudelft.simulation.language.DSOLException; /** * Grid10X10.java. *

* Copyright (c) 2019-2021 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See DJUNITS License. *

* @author Alexander Verbraeck */ @SuppressWarnings({"checkstyle:operatorwrap", "checkstyle:linelength"}) public class Grid10X10 extends OTSSimulationApplication { /** */ private static final long serialVersionUID = 1L; //@formatter:off /** xml heading. */ private static final String XML_HEADER = "\r\n" + "\r\n" + "\r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + "\r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + "\r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + "\r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + "\r\n" + " \r\n" + " \r\n" + " \r\n" + "\r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + "\r\n" + " \r\n" + " \r\n" + "\r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + "\r\n" + " \r\n" + " \r\n" + " \r\n" + " 2m\r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " 3.3m\r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " -6.2m\r\n" + " 3.3m\r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " 3.3m\r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n"; /** xml footer. */ private static final String XML_FOOTER = " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " 3600s\r\n" + " 1\r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + " \r\n" + "\r\n" + "\r\n"; //@formatter:on /** number of x/y. */ private static final int SIZE = 100; /** spacing. */ private static final int SPACING = 50; /** * @param model TestXMLModelGrid; model * @param panel OTSAnimationPanel; panel * @throws OTSDrawingException on error */ public Grid10X10(final TestXMLModelGrid model, final OTSAnimationPanel panel) throws OTSDrawingException { super(model, panel); // System.out.println("ANIMATEMAP.SIZE = " + this.defaultAnimationFactory.animatedObjects.size()); } /** */ private DefaultAnimationFactory defaultAnimationFactory; /** * Creates the animation objects. This method is overridable. The default uses {@code DefaultAnimationFactory}. * @throws OTSDrawingException on animation error */ @Override protected void animateNetwork() throws OTSDrawingException { this.defaultAnimationFactory = DefaultAnimationFactory.animateNetwork(getModel().getNetwork(), getModel().getSimulator(), getAnimationPanel().getGTUColorer()); } /** * Program entry point. * @param args String[]; args */ public static void main(final String[] args) { StringBuilder xml = new StringBuilder(); xml.append(XML_HEADER); xml.append("\n"); for (int x = 0; x < SIZE; x++) { xml.append(String.format(" \n", x, SPACING * x + SPACING / 2, 0)); xml.append(String.format(" \n", x, SPACING * x + SPACING / 2, SIZE * SPACING)); } for (int y = 0; y < SIZE; y++) { xml.append(String.format(" \n", y, 0, SPACING * y + SPACING / 2)); xml.append(String.format(" \n", y, SIZE * SPACING, SPACING * y + SPACING / 2)); } //@formatter:off final String link = " \r\n" + " \r\n" + " r1\r\n" + " \r\n"; //@formatter:on for (int x = 0; x < SIZE; x++) { xml.append(String.format(link, "NS" + x, "N" + x, "S" + x)); } for (int y = 0; y < SIZE; y++) { xml.append(String.format(link, "WE" + y, "W" + y, "E" + y)); } xml.append("\n\n"); xml.append(XML_FOOTER); System.out.println(xml.toString()); SwingUtilities.invokeLater(new Runnable() { @Override public void run() { try { OTSAnimator simulator = new OTSAnimator("Grid10X10"); TestXMLModelGrid xmlModel = new TestXMLModelGrid(simulator, xml.toString()); OTSReplication replication = new OTSReplication("rep", Time.ZERO, Duration.ZERO, Duration.instantiateSI(3600)); simulator.initialize(xmlModel, replication); OTSAnimationPanel animationPanel = new OTSAnimationPanel(xmlModel.getNetwork().getExtent(), new Dimension(800, 600), simulator, xmlModel, DEFAULT_COLORER, xmlModel.getNetwork()); new Grid10X10(xmlModel, animationPanel); animationPanel.enableSimulationControlButtons(); } catch (SimRuntimeException | NamingException | RemoteException | OTSDrawingException | DSOLException exception) { exception.printStackTrace(); } } }); } /** {@inheritDoc} */ @Override public final String toString() { return "TestXMLParserGrid []"; } /** * Model to test the XML parser. *

* Copyright (c) 2013-2021 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands.
* All rights reserved. BSD-style license. See OpenTrafficSim * License. *

* $LastChangedDate: 2015-09-14 01:33:02 +0200 (Mon, 14 Sep 2015) $, @version $Revision: 1401 $, by $Author: averbraeck $, * initial version un 27, 2015
* @author Alexander Verbraeck * @author Peter Knoppers */ static class TestXMLModelGrid extends AbstractOTSModel { /** */ private static final long serialVersionUID = 20141121L; /** the network. */ private RoadNetwork network; /** the xml string. */ private final String xml; /** * @param simulator OTSSimulatorInterface; the simulator * @param xml String; xml string */ TestXMLModelGrid(final OTSSimulatorInterface simulator, final String xml) { super(simulator); this.xml = xml; } /** {@inheritDoc} */ @Override public final void constructModel() throws SimRuntimeException { this.network = new RoadNetwork("Grid network", getSimulator()); RoadNetworkDefaults.registerDefaults(this.network); try { ByteArrayInputStream bos = new ByteArrayInputStream(this.xml.getBytes(StandardCharsets.UTF_8)); XmlNetworkLaneParser.build(bos, this.network, true); } catch (NetworkException | ParserConfigurationException | SAXException | JAXBException | URISyntaxException | XmlParserException | GtuException | IOException | TrafficControlException exception) { exception.printStackTrace(); } for (TrafficLight tl : this.network.getObjectMap(TrafficLight.class).values()) { tl.setTrafficLightColor(TrafficLightColor.GREEN); } } /** {@inheritDoc} */ @Override public RoadNetwork getNetwork() { return this.network; } /** {@inheritDoc} */ @Override public final String toString() { return "TestXMLModelGrid [simulator=" + this.simulator + "]"; } /** {@inheritDoc} */ @Override public Serializable getSourceId() { return "TestXMLModelGrid"; } } }