View Javadoc
1   package org.opentrafficsim.graphs;
2   
3   import java.rmi.RemoteException;
4   import java.util.HashSet;
5   import java.util.List;
6   import java.util.Set;
7   
8   import org.opentrafficsim.core.gtu.GTU;
9   import org.opentrafficsim.core.network.Network;
10  import org.opentrafficsim.core.network.OTSNetwork;
11  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
12  import org.opentrafficsim.road.network.lane.Lane;
13  
14  import nl.tudelft.simulation.event.EventInterface;
15  import nl.tudelft.simulation.event.EventListenerInterface;
16  
17  /**
18   * <p>
19   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
20   * BSD-style license. See <a href="http://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
21   * </p>
22   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
23   * initial version Aug 26, 2016 <br>
24   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
25   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
26   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
27   */
28  public class DataSampler implements EventListenerInterface
29  {
30      /** The (ordered) set of observed lanes. */
31      private final List<Lane> lanes;
32      
33      /** The OTS Network that knows about all the GTUs and Lanes. */
34      private final OTSNetwork network;
35      
36      /** The GTUs currently in the network. */
37      private final Set<String> gtus = new HashSet<>();
38  
39      /**
40       * @param network the network
41       * @param lanes lanes to sample on
42       * 
43       */
44      public DataSampler(final OTSNetwork network, final List<Lane> lanes)
45      {
46          this.lanes = lanes;
47          this.network = network;
48          network.addListener(this, Network.GTU_ADD_EVENT);
49          network.addListener(this, Network.GTU_REMOVE_EVENT);
50      }
51      
52      /** {@inheritDoc} */
53      @Override
54      public final void notify(final EventInterface event) throws RemoteException
55      {
56          if (event.getType().equals(Network.GTU_ADD_EVENT))
57          {
58              Object[] addInfo = (Object[]) event.getContent();
59              this.gtus.add(addInfo[0].toString());
60              GTU gtu = this.network.getGTU(addInfo[0].toString());
61              gtu.addListener(this, LaneBasedGTU.LANEBASED_MOVE_EVENT);
62          }
63          else if (event.getType().equals(Network.GTU_REMOVE_EVENT))
64          {
65              Object[] removeInfo = (Object[]) event.getContent();
66              this.gtus.remove(removeInfo[0].toString());
67              GTU gtu = this.network.getGTU(removeInfo[0].toString());
68              gtu.removeListener(this, LaneBasedGTU.LANEBASED_MOVE_EVENT);
69          }
70          else if (event.getType().equals(LaneBasedGTU.LANEBASED_MOVE_EVENT))
71          {
72              // Ignored.
73          }
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      public String toString()
79      {
80          return "DataSampler [lanes=" + this.lanes + ", network=" + this.network + ", #gtus=" + this.gtus.size() + "]";
81      }
82  
83  }
84  ;