package org.opentrafficsim.imb.demo; import static org.opentrafficsim.road.gtu.lane.RoadGTUTypes.CAR; import static org.opentrafficsim.road.gtu.lane.RoadGTUTypes.TRUCK; 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.lang.reflect.InvocationTargetException; import java.net.URL; import java.util.HashSet; import java.util.List; import java.util.Set; import javax.naming.NamingException; import javax.swing.JFrame; import javax.swing.SwingUtilities; import javax.xml.parsers.ParserConfigurationException; import org.djunits.unit.DurationUnit; import org.djunits.unit.FrequencyUnit; import org.djunits.unit.TimeUnit; import org.djunits.value.vdouble.scalar.Duration; import org.djunits.value.vdouble.scalar.Frequency; import org.djunits.value.vdouble.scalar.Length; import org.djunits.value.vdouble.scalar.Time; import org.opentrafficsim.base.modelproperties.ContinuousProperty; 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.GTUType; 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.interfaces.GtuTypeDataInterface; import org.opentrafficsim.kpi.sampling.KpiGtuDirectionality; import org.opentrafficsim.kpi.sampling.Query; import org.opentrafficsim.kpi.sampling.Sampler; import org.opentrafficsim.kpi.sampling.meta.MetaDataGtuType; import org.opentrafficsim.kpi.sampling.meta.MetaDataSet; import org.opentrafficsim.road.animation.AnimationToggles; import org.opentrafficsim.road.network.factory.xml.XmlNetworkLaneParser; import org.opentrafficsim.road.network.lane.CrossSectionLink; import org.opentrafficsim.road.network.sampling.GtuTypeData; import org.opentrafficsim.road.network.sampling.LinkData; import org.opentrafficsim.road.network.sampling.RoadSampler; import org.opentrafficsim.road.network.sampling.data.ReferenceSpeed; 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.tno.imb.TConnection; import nl.tno.imb.mc.ModelParameters; import nl.tno.imb.mc.ModelStarter; import nl.tno.imb.mc.ModelState; import nl.tno.imb.mc.Parameter; import nl.tudelft.simulation.dsol.SimRuntimeException; import nl.tudelft.simulation.dsol.animation.D2.GisRenderable2D; import nl.tudelft.simulation.dsol.simulators.Simulator; import nl.tudelft.simulation.dsol.simulators.SimulatorInterface; 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.
*
* @version $Revision$, $LastChangedDate$, by $Author$, initial version 18 nov. 2016
* @author Alexander Verbraeck
* @author Peter Knoppers
* @author Wouter Schakel
*/
public class ModelControlA58 extends ModelStarter
{
/** Amount of ACC. */
double penetrationRate;
/** Model. */
A58Model model;
/** Animation. */
A58Animation a58Animation;
/** Thread for statistics. */
Thread statisticsThread;
/** Thread that start model. */
Thread startThread;
/**
* @param args the command line args
* @param providedModelName the model name
* @param providedModelId the model id
* @throws IMBException when IMB connection fails
*/
public ModelControlA58(String[] args, String providedModelName, int providedModelId) throws IMBException
{
super(args, providedModelName, providedModelId);
}
/**
* Tester.
* @param args empty
* @throws IMBException shen IMB connection fails
* @throws InterruptedException not used right now
* @throws InvocationTargetException when running the model control fails
*/
public static void main(String[] args) throws IMBException, InvocationTargetException, InterruptedException
{
if (args.length == 0)
{
ModelControlA58 modelControlA58 = new ModelControlA58(new String[0], "A58 model", 1248);
modelControlA58.startModel(null, modelControlA58.connection); // null will use default penetration rate
}
else
{
SwingUtilities.invokeAndWait(new Runnable()
{
@Override
public void run()
{
try
{
new ModelControlA58(args, "A58 model", 1248);
}
catch (IMBException exception)
{
exception.printStackTrace();
}
}
});
}
// try
// {
// Thread.sleep(2000);
// }
// catch (InterruptedException exception)
// {
// exception.printStackTrace();
// }
// modelControlA58.doStopModel();
}
/** {@inheritDoc} */
@Override
public void startModel(ModelParameters parameters, TConnection imbConnection)
{
this.startThread = new Thread(new Runnable()
{
public void run()
{
ModelControlA58.this.penetrationRate = 0.0;
if (parameters != null && parameters.parameterExists("penetration"))
{
try
{
ModelControlA58.this.penetrationRate = ((double) parameters.getParameterValue("penetration")) / 100;
}
catch (IMBException exception)
{
// should not happen, we check for parameter existence
exception.printStackTrace();
}
}
else
{
System.out.println(
"No penetration parameter found, using default value of " + ModelControlA58.this.penetrationRate);
}
ModelControlA58.this.a58Animation = new A58Animation(imbConnection);
try
{
ModelControlA58.this.a58Animation.buildAnimator(Time.ZERO, Duration.ZERO,
new Duration(3600.000001, DurationUnit.SI), null, null, true);
}
catch (SimRuntimeException | NamingException | OTSSimulationException | PropertyException exception)
{
exception.printStackTrace();
}
try
{
signalModelState(ModelState.READY);
}
catch (IMBException exception)
{
throw new RuntimeException("Exception while setting READY state on A58 model.", exception);
}
}
});
this.startThread.start();
}
/** {@inheritDoc} */
@Override
public void stopModel()
{
System.out.println("stopModel called on ModelControlA58");
if (this.statisticsThread != null)
{
this.statisticsThread.interrupt();
this.startThread.interrupt();
try
{
System.out.println("ModelControlA58 joining with statisticsThread");
this.statisticsThread.join();
this.startThread.join();
}
catch (InterruptedException exception)
{
exception.printStackTrace();
}
}
if (null != this.model)
{
((Simulator