package org.opentrafficsim.graphs;
import java.rmi.RemoteException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.opentrafficsim.core.gtu.GTU;
import org.opentrafficsim.core.network.Network;
import org.opentrafficsim.core.network.OTSNetwork;
import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
import org.opentrafficsim.road.network.lane.Lane;
import nl.tudelft.simulation.event.EventInterface;
import nl.tudelft.simulation.event.EventListenerInterface;
/**
*
* Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
* BSD-style license. See OpenTrafficSim License.
*
* $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
* initial version Aug 26, 2016
* @author Alexander Verbraeck
* @author Peter Knoppers
* @author Wouter Schakel
*/
public class DataSampler implements EventListenerInterface
{
/** The (ordered) set of observed lanes. */
private final List lanes;
/** The OTS Network that knows about all the GTUs and Lanes. */
private final OTSNetwork network;
/** The GTUs currently in the network. */
private final Set gtus = new HashSet<>();
/**
* @param network the network
* @param lanes lanes to sample on
*
*/
public DataSampler(final OTSNetwork network, final List lanes)
{
this.lanes = lanes;
this.network = network;
network.addListener(this, Network.GTU_ADD_EVENT);
network.addListener(this, Network.GTU_REMOVE_EVENT);
}
/** {@inheritDoc} */
@Override
public final void notify(final EventInterface event) throws RemoteException
{
if (event.getType().equals(Network.GTU_ADD_EVENT))
{
Object[] addInfo = (Object[]) event.getContent();
this.gtus.add(addInfo[0].toString());
GTU gtu = this.network.getGTU(addInfo[0].toString());
gtu.addListener(this, LaneBasedGTU.LANEBASED_MOVE_EVENT);
}
else if (event.getType().equals(Network.GTU_REMOVE_EVENT))
{
Object[] removeInfo = (Object[]) event.getContent();
this.gtus.remove(removeInfo[0].toString());
GTU gtu = this.network.getGTU(removeInfo[0].toString());
gtu.removeListener(this, LaneBasedGTU.LANEBASED_MOVE_EVENT);
}
else if (event.getType().equals(LaneBasedGTU.LANEBASED_MOVE_EVENT))
{
// Ignored.
}
}
/** {@inheritDoc} */
@Override
public String toString()
{
return "DataSampler [lanes=" + this.lanes + ", network=" + this.network + ", #gtus=" + this.gtus.size() + "]";
}
}
;