package org.opentrafficsim.imb.demo; import java.awt.geom.Point2D; import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D.Double; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; import java.net.URL; import java.util.ArrayList; import java.util.List; import javax.naming.NamingException; import javax.swing.SwingUtilities; import javax.xml.parsers.ParserConfigurationException; import org.djunits.unit.DurationUnit; import org.djunits.value.vdouble.scalar.Duration; import org.djunits.value.vdouble.scalar.Time; import org.opentrafficsim.base.modelproperties.CompoundProperty; import org.opentrafficsim.base.modelproperties.Property; import org.opentrafficsim.base.modelproperties.PropertyException; import org.opentrafficsim.core.dsol.OTSDEVSSimulatorInterface; import org.opentrafficsim.core.dsol.OTSModelInterface; import org.opentrafficsim.core.dsol.OTSSimTimeDouble; import org.opentrafficsim.core.geometry.OTSGeometryException; import org.opentrafficsim.core.gis.TransformWGS84DutchRDNew; import org.opentrafficsim.core.gtu.GTUException; import org.opentrafficsim.core.gtu.animation.GTUColorer; import org.opentrafficsim.core.network.NetworkException; import org.opentrafficsim.core.network.OTSNetwork; import org.opentrafficsim.imb.IMBException; import org.opentrafficsim.imb.connector.OTSIMBConnector; import org.opentrafficsim.imb.transceiver.urbanstrategy.GTUTransceiver; import org.opentrafficsim.imb.transceiver.urbanstrategy.LaneGTUTransceiver; import org.opentrafficsim.imb.transceiver.urbanstrategy.LinkGTUTransceiver; import org.opentrafficsim.imb.transceiver.urbanstrategy.NetworkTransceiver; import org.opentrafficsim.imb.transceiver.urbanstrategy.NodeTransceiver; import org.opentrafficsim.imb.transceiver.urbanstrategy.SensorGTUTransceiver; import org.opentrafficsim.imb.transceiver.urbanstrategy.SimulatorTransceiver; import org.opentrafficsim.imb.transceiver.urbanstrategy.StatisticsGTULaneTransceiver; import org.opentrafficsim.kpi.sampling.Query; import org.opentrafficsim.road.animation.AnimationToggles; import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser; import org.opentrafficsim.road.network.sampling.RoadSampler; import org.opentrafficsim.simulationengine.AbstractWrappableAnimation; import org.opentrafficsim.simulationengine.OTSSimulationException; import org.opentrafficsim.simulationengine.SimpleAnimator; import org.xml.sax.SAXException; import nl.javel.gisbeans.io.esri.CoordinateTransform; import nl.tudelft.simulation.dsol.SimRuntimeException; import nl.tudelft.simulation.dsol.animation.D2.GisRenderable2D; import nl.tudelft.simulation.dsol.simulators.SimulatorInterface; import nl.tudelft.simulation.language.Throw; import nl.tudelft.simulation.language.io.URLResource; /** *

* Copyright (c) 2013-2017 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 Oct 17, 2014
* @author Alexander Verbraeck */ public class N201IMB extends AbstractWrappableAnimation { /** */ private static final long serialVersionUID = 20161007L; /** The model. */ private N201Model model; /** * Main program. * @param args String[]; the command line arguments (not used) * @throws SimRuntimeException should never happen */ public static void main(final String[] args) throws SimRuntimeException { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { try { N201IMB n201Model = new N201IMB(); // 1 hour simulation run for testing n201Model.buildAnimator(Time.ZERO, Duration.ZERO, new Duration(10.0, DurationUnit.HOUR), new ArrayList>(), null, true); } catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception) { exception.printStackTrace(); } } }); } /** {@inheritDoc} */ @Override public final String shortName() { return "Model N201"; } /** {@inheritDoc} */ @Override public final String description() { return "Model N201 - IMB"; } /** {@inheritDoc} */ @Override public final void stopTimersThreads() { super.stopTimersThreads(); } /** {@inheritDoc} */ @Override protected final OTSModelInterface makeModel(final GTUColorer colorer) { System.out.println("N201IMB.makeModel called"); this.model = new N201Model(getSavedUserModifiedProperties(), colorer, new OTSNetwork("N201 network")); return this.model; } /** * @return the saved user properties for a next run */ private List> getSavedUserModifiedProperties() { return this.savedUserModifiedProperties; } /** {@inheritDoc} */ @Override protected void addAnimationToggles() { AnimationToggles.setTextAnimationTogglesStandard(this); this.addToggleGISButtonText(" GIS Layers:", this.model.getGisMap(), "Turn GIS map layer on or off"); } /** {@inheritDoc} */ @Override protected final Double makeAnimationRectangle() { return new Rectangle2D.Double(103000, 478000, 5500, 5000); } /** {@inheritDoc} */ @Override public final String toString() { return "N201 network - IMB []"; } /** * Model to test the XML parser. *

* Copyright (c) 2013-2017 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 */ class N201Model implements OTSModelInterface { /** */ private static final long serialVersionUID = 20141121L; /** The simulator. */ private OTSDEVSSimulatorInterface simulator; /** User settable properties. */ private List> modelProperties = null; /** the network as created by the AbstractWrappableIMBAnimation. */ private final OTSNetwork network; /** Connector to the IMB hub. */ OTSIMBConnector imbConnector; /** the GIS map. */ private GisRenderable2D gisMap; /** * @param modelProperties ArrayList<AbstractProperty<?>>; the properties * @param gtuColorer the default and initial GTUColorer, e.g. a DefaultSwitchableTUColorer. * @param network Network; the network */ N201Model(final List> modelProperties, final GTUColorer gtuColorer, final OTSNetwork network) { this.modelProperties = modelProperties; this.network = network; } /** {@inheritDoc} */ @Override public final void constructModel(final SimulatorInterface pSimulator) throws SimRuntimeException { System.out.println("N201IMB: constructModel called; Connecting to IMB"); this.simulator = (OTSDEVSSimulatorInterface) pSimulator; SimpleAnimator imbAnimator = (SimpleAnimator) pSimulator; try { CompoundProperty imbSettings = null; for (Property property : this.modelProperties) { if (property.getKey().equals(OTSIMBConnector.PROPERTY_KEY)) { imbSettings = (CompoundProperty) property; } } System.out.println("link count " + this.network.getLinkMap().size()); Throw.whenNull(imbSettings, "IMB Settings not found in properties"); this.imbConnector = OTSIMBConnector.create(imbSettings, "OTS"); new NetworkTransceiver(this.imbConnector, imbAnimator, this.network); new NodeTransceiver(this.imbConnector, imbAnimator, this.network); new LinkGTUTransceiver(this.imbConnector, imbAnimator, this.network); new LaneGTUTransceiver(this.imbConnector, imbAnimator, this.network); new GTUTransceiver(this.imbConnector, imbAnimator, this.network); new SensorGTUTransceiver(this.imbConnector, imbAnimator, this.network); new SimulatorTransceiver(this.imbConnector, imbAnimator); } catch (IMBException exception) { throw new SimRuntimeException(exception); } // Stream to allow the xml-file to be retrievable from a JAR file InputStream stream = URLResource.getResourceAsStream("/N201v8.xml"); XmlNetworkLaneParser nlp = new XmlNetworkLaneParser(this.simulator); try { nlp.build(stream, this.network); // ODMatrixTrips matrix = N201ODfactory.get(network); // N201ODfactory.makeGeneratorsFromOD(network, matrix, this.simulator); } catch (NetworkException | ParserConfigurationException | SAXException | IOException | NamingException | GTUException | OTSGeometryException exception) { exception.printStackTrace(); } Query query = N201ODfactory.getQuery(this.network, new RoadSampler(this.simulator)); try { new StatisticsGTULaneTransceiver(this.imbConnector, imbAnimator, this.network.getId(), query, new Duration(30, DurationUnit.SECOND)); } catch (IMBException exception) { throw new SimRuntimeException(exception); } URL gisURL = URLResource.getResource("/N201/map.xml"); System.err.println("GIS-map file: " + gisURL.toString()); CoordinateTransform rdto0 = new CoordinateTransformRD(0, 0); this.gisMap = new GisRenderable2D(this.simulator, gisURL, rdto0); } /** * @return gisMap */ public final GisRenderable2D getGisMap() { return this.gisMap; } /** {@inheritDoc} */ @Override public SimulatorInterface getSimulator() { return this.simulator; } /** {@inheritDoc} */ @Override public final OTSNetwork getNetwork() { return this.network; } /** {@inheritDoc} */ @Override public final String toString() { return "N201Model [simulator=" + this.simulator + "]"; } } /** * Convert coordinates to/from the Dutch RD system. */ class CoordinateTransformRD implements CoordinateTransform, Serializable { /** */ private static final long serialVersionUID = 20141017L; /** */ final double dx; /** */ final double dy; /** * @param dx x transform * @param dy y transform */ public CoordinateTransformRD(final double dx, final double dy) { this.dx = dx; this.dy = dy; } /** {@inheritDoc} */ @Override public float[] floatTransform(double x, double y) { double[] d = doubleTransform(x, y); return new float[] { (float) d[0], (float) d[1] }; } /** {@inheritDoc} */ @Override public double[] doubleTransform(double x, double y) { try { Point2D c = TransformWGS84DutchRDNew.fromWGS84(x, y); return new double[] { c.getX() - this.dx, c.getY() - this.dy }; } catch (Exception exception) { System.err.println(exception.getMessage()); return new double[] { 0, 0 }; } } /** {@inheritDoc} */ @Override public final String toString() { return "CoordinateTransformRD [dx=" + this.dx + ", dy=" + this.dy + "]"; } } }