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      private final List<Lane> lanes;
31      
32      private final OTSNetwork network;
33      
34      private final Set<String> gtus = new HashSet<>();
35  
36      /**
37       * @param network the network
38       * @param lanes lanes to sample on
39       * 
40       */
41      public DataSampler(final OTSNetwork network, final List<Lane> lanes)
42      {
43          this.lanes = lanes;
44          this.network = network;
45          network.addListener(this, Network.GTU_ADD_EVENT);
46          network.addListener(this, Network.GTU_REMOVE_EVENT);
47      }
48      
49      /** {@inheritDoc} */
50      @Override
51      public final void notify(final EventInterface event) throws RemoteException
52      {
53          if (event.getType().equals(Network.GTU_ADD_EVENT))
54          {
55              Object[] addInfo = (Object[]) event.getContent();
56              this.gtus.add(addInfo[0].toString());
57              GTU gtu = this.network.getGTU(addInfo[0].toString());
58              gtu.addListener(this, LaneBasedGTU.MOVE_EVENT);
59          }
60          else if (event.getType().equals(Network.GTU_REMOVE_EVENT))
61          {
62              Object[] removeInfo = (Object[]) event.getContent();
63              this.gtus.remove(removeInfo[0].toString());
64              GTU gtu = this.network.getGTU(removeInfo[0].toString());
65              gtu.removeListener(this, LaneBasedGTU.MOVE_EVENT);
66          }
67          else if (event.getType().equals(LaneBasedGTU.MOVE_EVENT))
68          {
69              
70          }
71      }
72  
73  }
74  ;