1 package org.opentrafficsim.road.gtu.lane.tactical.util.lmrs;
2
3 import java.util.HashSet;
4 import java.util.Set;
5
6 import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
7
8 /**
9 * Keeps data for LMRS for a specific GTU.
10 * <p>
11 * Copyright (c) 2013-2017 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
12 * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
13 * <p>
14 * @version $Revision$, $LastChangedDate$, by $Author$, initial version 8 nov. 2016 <br>
15 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
16 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
17 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
18 */
19 public final class LmrsData
20 {
21
22 /** Form of synchronization. */
23 private final Synchronization synchronization;
24
25 /** Most recent leaders. */
26 private final Set<String> leaders = new HashSet<>();
27
28 /** Current leaders. */
29 private final Set<String> tempLeaders = new HashSet<>();
30
31 /** Vehicle that is being synchronized to. */
32 private String syncVehicle;
33
34 /**
35 * @param synchronization synchronization
36 */
37 public LmrsData(final Synchronization synchronization)
38 {
39 this.synchronization = synchronization;
40 }
41
42 /**
43 * Checks if the given leader is a new leader.
44 * @param gtu gtu to check
45 * @return whether the gtu is a new leader
46 */
47 boolean isNewLeader(final HeadwayGTU gtu)
48 {
49 this.tempLeaders.add(gtu.getId());
50 return !this.leaders.contains(gtu.getId());
51 }
52
53 /**
54 * Remembers the leaders of the current time step (those forwarded to isNewLeader()) for the next time step.
55 */
56 void finalizeStep()
57 {
58 this.leaders.clear();
59 this.leaders.addAll(this.tempLeaders);
60 this.tempLeaders.clear();
61 }
62
63 /**
64 * Remembers the gtu that is synchronized to.
65 * @param gtu gtu that is synchronized to
66 */
67 void setSyncVehicle(final HeadwayGTU gtu)
68 {
69 this.syncVehicle = gtu == null ? null : gtu.getId();
70 }
71
72 /**
73 * Returns whether the provided gtu is the gtu that is synchronized to.
74 * @param gtu gtu to inquiry
75 * @return whether the provided gtu is the gtu that is synchronized to
76 */
77 boolean isSyncVehicle(final HeadwayGTU gtu)
78 {
79 return this.syncVehicle == null ? false : gtu.getId().equals(this.syncVehicle);
80 }
81
82 /**
83 * Returns the gtu from the set that is the current sync vehicle, or {@code null} of there is no sync vehicle or it is not
84 * in the set.
85 * @param adjLeaders leaders in adjacent lane
86 * @return gtu from the set that is the current sync vehicle
87 */
88 HeadwayGTU getSyncVehicle(final Set<HeadwayGTU> adjLeaders)
89 {
90 if (this.syncVehicle == null)
91 {
92 return null;
93 }
94 for (HeadwayGTU leader : adjLeaders)
95 {
96 if (leader.getId().equals(this.syncVehicle))
97 {
98 return leader;
99 }
100 }
101 return null;
102 }
103
104 /**
105 * Returns the synchronization.
106 * @return synchronization
107 */
108 Synchronization getSynchronization()
109 {
110 return this.synchronization;
111 }
112
113 }