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
19
20
21
22
23
24
25
26
27
28 public class DataSampler implements EventListenerInterface
29 {
30
31 private final List<Lane> lanes;
32
33
34 private final OTSNetwork network;
35
36
37 private final Set<String> gtus = new HashSet<>();
38
39
40
41
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
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
73 }
74 }
75
76
77 @Override
78 public String toString()
79 {
80 return "DataSampler [lanes=" + this.lanes + ", network=" + this.network + ", #gtus=" + this.gtus.size() + "]";
81 }
82
83 }
84 ;