1   package org.opentrafficsim.ahfe;
2   
3   import java.util.HashMap;
4   import java.util.HashSet;
5   import java.util.Map;
6   import java.util.Set;
7   import java.util.SortedSet;
8   
9   import org.djunits.value.vdouble.scalar.Duration;
10  import org.djunits.value.vdouble.scalar.Length;
11  import org.djunits.value.vdouble.scalar.Time;
12  import org.opentrafficsim.base.TimeStampedObject;
13  import org.opentrafficsim.base.parameters.ParameterException;
14  import org.opentrafficsim.base.parameters.ParameterTypeDuration;
15  import org.opentrafficsim.base.parameters.ParameterTypes;
16  import org.opentrafficsim.base.parameters.Parameters;
17  import org.opentrafficsim.core.gtu.GTUException;
18  import org.opentrafficsim.core.gtu.perception.PerceptionException;
19  import org.opentrafficsim.core.network.LateralDirectionality;
20  import org.opentrafficsim.core.network.NetworkException;
21  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
22  import org.opentrafficsim.road.gtu.lane.perception.LanePerception;
23  import org.opentrafficsim.road.gtu.lane.perception.PerceptionCollectable;
24  import org.opentrafficsim.road.gtu.lane.perception.RelativeLane;
25  import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.DirectNeighborsPerception;
26  import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.HeadwayGtuType;
27  import org.opentrafficsim.road.gtu.lane.perception.categories.neighbors.NeighborsPerception;
28  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
29  
30  import nl.tudelft.simulation.dsol.SimRuntimeException;
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  @Deprecated
43  public abstract class AbstractDelayedNeighborsPerception extends AbstractDelayedPerceptionCategory
44          implements NeighborsPerception
45  {
46  
47      
48      private static final long serialVersionUID = 20170217L;
49  
50      
51      protected static final ParameterTypeDuration TR = ParameterTypes.TR;
52  
53      
54      protected static final ParameterTypeDuration DT = ParameterTypes.DT;
55  
56      
57      private Time initialTime = null;
58  
59      
60      private final DirectNeighborsPerception direct;
61  
62      
63      private Duration reactionTime = null;
64  
65      
66      private Duration plannerTimeStep = null;
67  
68      
69      private Duration remainder = null;
70  
71      
72      public static final String FIRSTLEADERS = "firstLeaders";
73  
74      
75      public static final String FIRSTFOLLOWERS = "firstFollower";
76  
77      
78      public static final String GTUALONGSIDE = "gtuAlongside";
79  
80      
81      public static final String LEADERS = "leaders";
82  
83      
84      public static final String FOLLOWERS = "followers";
85  
86      
87      public static final NeighborsInfoType<SortedSet<RelativeLane>> CROSSSECTION = new NeighborsInfoType<>("cross-section");
88  
89      
90      public static final NeighborsInfoType<Length> ODOMETER = new NeighborsInfoType<>("odometer");
91  
92      
93      private boolean gtuAlongsideLeftOverride = false;
94  
95      
96      private boolean gtuAlongsideRightOverride = false;
97  
98      
99  
100 
101 
102     public AbstractDelayedNeighborsPerception(final LanePerception perception)
103     {
104         super(perception);
105         this.direct = new DirectNeighborsPerception(perception, HeadwayGtuType.COPY);
106     }
107 
108     
109     @Override
110     public final void updateAll() throws GTUException, NetworkException, ParameterException
111     {
112 
113         if (this.remainder == null)
114         {
115             try
116             {
117                 
118                 Parameters params = getPerception().getGtu().getParameters();
119                 this.reactionTime = params.getParameter(TR);
120                 this.plannerTimeStep = params.getParameter(DT);
121                 double rem;
122                 if (this.reactionTime.eq0())
123                 {
124                     rem = 0;
125                 }
126                 else if (this.reactionTime.gt(this.plannerTimeStep))
127                 {
128                     rem = this.reactionTime.si % this.plannerTimeStep.si;
129                 }
130                 else
131                 {
132                     rem = this.plannerTimeStep.si - this.reactionTime.si;
133                 }
134                 this.remainder = Duration.createSI(rem);
135             }
136             catch (ParameterException | GTUException exception)
137             {
138                 throw new RuntimeException("Exception while setting up delayed neighors perception.", exception);
139             }
140         }
141 
142         
143         Time now = getPerception().getGtu().getSimulator().getSimulatorTime();
144         if (this.initialTime == null)
145         {
146             this.initialTime = now;
147         }
148         if (now.minus(this.initialTime).le(this.reactionTime))
149         {
150             updateAllDelayed();
151             return;
152         }
153 
154         if (this.remainder.eq0())
155         {
156             
157             updateAllDelayed();
158         }
159         else
160         {
161             
162             Time scheduledTime = now.plus(this.remainder);
163             try
164             {
165                 getPerception().getGtu().getSimulator().scheduleEventAbs(scheduledTime, this, this, "updateAllDelayed", null);
166             }
167             catch (SimRuntimeException exception)
168             {
169                 throw new RuntimeException("Scheduling perception update in the past.", exception);
170             }
171         }
172 
173         
174 
175 
176 
177 
178 
179 
180         if (getPerception().getLaneStructure().getExtendedCrossSection().contains(RelativeLane.LEFT))
181         {
182             this.gtuAlongsideLeftOverride = newFirstLeaderOrFollower(getFollowers(RelativeLane.LEFT),
183                     this.direct.getFirstFollowers(LateralDirectionality.LEFT))
184                     || newFirstLeaderOrFollower(getLeaders(RelativeLane.LEFT),
185                             this.direct.getFirstLeaders(LateralDirectionality.LEFT))
186                     || this.direct.isGtuAlongside(LateralDirectionality.LEFT);
187         }
188         if (getPerception().getLaneStructure().getExtendedCrossSection().contains(RelativeLane.RIGHT))
189         {
190             this.gtuAlongsideRightOverride = newFirstLeaderOrFollower(getFollowers(RelativeLane.RIGHT),
191                     this.direct.getFirstFollowers(LateralDirectionality.RIGHT))
192                     || newFirstLeaderOrFollower(getLeaders(RelativeLane.RIGHT),
193                             this.direct.getFirstLeaders(LateralDirectionality.RIGHT))
194                     || this.direct.isGtuAlongside(LateralDirectionality.RIGHT);
195         }
196 
197     }
198 
199     
200 
201 
202 
203 
204 
205     private boolean newFirstLeaderOrFollower(final Iterable<? extends HeadwayGTU> delayedSet,
206             final Set<? extends HeadwayGTU> currentSet)
207     {
208         Set<String> set = new HashSet<>();
209         for (HeadwayGTU gtu : delayedSet)
210         {
211             set.add(gtu.getId());
212         }
213         for (HeadwayGTU gtu : currentSet)
214         {
215             if (!set.contains(gtu.getId()) && gtu.getDistance().si < 50)
216             {
217                 return true;
218             }
219         }
220         return false;
221     }
222 
223     
224 
225 
226 
227 
228     public final boolean isGtuAlongsideOverride(final LateralDirectionality lat)
229     {
230         return lat.isLeft() ? this.gtuAlongsideLeftOverride : this.gtuAlongsideRightOverride;
231     }
232 
233     
234 
235 
236 
237 
238 
239     
240     protected void updateAllDelayed() throws GTUException, NetworkException, ParameterException
241     {
242 
243         try
244         {
245             getGtu().getReferencePosition();
246         }
247         catch (GTUException exception)
248         {
249             
250             return;
251         }
252 
253         this.direct.updateAll();
254         
255         setInfo(CROSSSECTION,
256                 new TimeStampedObject<>(getPerception().getLaneStructure().getExtendedCrossSection(), getTimestamp()));
257         setInfo(ODOMETER, new TimeStampedObject<>(getGtu().getOdometer(), getTimestamp()));
258     }
259 
260 
261     
262 
263 
264 
265     public final SortedSet<RelativeLane> getDelayedCrossSection()
266     {
267         try
268         {
269             return getInfo(CROSSSECTION).getObject();
270         }
271         catch (PerceptionException exception)
272         {
273             throw new RuntimeException("Crosssection was not perceived.", exception);
274         }
275     }
276 
277     
278 
279 
280 
281     public static final class NeighborsInfoType<T> extends DelayedInfoType<T>
282     {
283 
284         
285         private static final Map<String, NeighborsInfoType<?>> LANEINFOTYPES = new HashMap<>();
286 
287         
288 
289 
290 
291         public NeighborsInfoType(final String id)
292         {
293             super(id, TR);
294         }
295 
296         
297 
298 
299 
300 
301         @SuppressWarnings("unchecked")
302         public static NeighborsInfoType<SortedSet<HeadwayGTU>> getSortedSetType(final String id)
303         {
304             if (!LANEINFOTYPES.containsKey(id))
305             {
306                 LANEINFOTYPES.put(id, new NeighborsInfoType<SortedSet<HeadwayGTU>>(id));
307             }
308             return (NeighborsInfoType<SortedSet<HeadwayGTU>>) LANEINFOTYPES.get(id);
309         }
310 
311         
312 
313 
314 
315 
316         @SuppressWarnings("unchecked")
317         public static NeighborsInfoType<PerceptionCollectable<HeadwayGTU, LaneBasedGTU>> getIterableType(final String id)
318         {
319             if (!LANEINFOTYPES.containsKey(id))
320             {
321                 LANEINFOTYPES.put(id, new NeighborsInfoType<SortedSet<HeadwayGTU>>(id));
322             }
323             return (NeighborsInfoType<PerceptionCollectable<HeadwayGTU, LaneBasedGTU>>) LANEINFOTYPES.get(id);
324         }
325 
326         
327 
328 
329 
330 
331         @SuppressWarnings("unchecked")
332         public static NeighborsInfoType<Boolean> getBooleanType(final String id)
333         {
334             if (!LANEINFOTYPES.containsKey(id))
335             {
336                 LANEINFOTYPES.put(id, new NeighborsInfoType<SortedSet<HeadwayGTU>>(id));
337             }
338             return (NeighborsInfoType<Boolean>) LANEINFOTYPES.get(id);
339         }
340 
341         
342         @Override
343         public String toString()
344         {
345             return "NeighborsInfoType []";
346         }
347 
348     }
349 
350 }