Class AbstractPerceptionCategory<G extends Gtu,P extends Perception<G>>
- Type Parameters:
G- GTU typeP- perception type
- All Implemented Interfaces:
Type<AbstractPerceptionCategory<G,,P>> PerceptionCategory<G,P>
- Direct Known Subclasses:
DirectEgoPerception
PerceptionCategory and allows sub-classes to easily implement lazy evaluation through the
computeIfAbsent() methods.
Copyright (c) 2013-2026 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.
- Author:
- Alexander Verbraeck, Peter Knoppers, Wouter Schakel
-
Constructor Summary
ConstructorsConstructorDescriptionAbstractPerceptionCategory(P perception) Constructor setting the perception. -
Method Summary
Modifier and TypeMethodDescriptionprotected StringReturns a string representation of the cache.protected <T> TcomputeIfAbsent(Object key, Supplier<T> supplier) Returns the cached value for the given key, or computes it if it's absent or not from the current simulation time.protected <T> TcomputeIfAbsent(Object key, Supplier<T> supplier, Object context) Returns the cached value for the given key, or computes it if it's absent or not from the current simulation time.protected <T> TcomputeIfAbsent(Object key, Supplier<T> supplier, Object... context) Returns the cached value for the given key, or computes it if it's absent or not from the current simulation time.getGtu()Returns the connected GTU.Returns the connected perception.final org.djunits.value.vdouble.scalar.DurationReturns the current time.
-
Constructor Details
-
AbstractPerceptionCategory
Constructor setting the perception.- Parameters:
perception- perception
-
-
Method Details
-
getPerception
Returns the connected perception.- Returns:
- connected perception
-
getGtu
Returns the connected GTU.- Returns:
- connected GTU
-
getTimestamp
public final org.djunits.value.vdouble.scalar.Duration getTimestamp()Returns the current time.- Returns:
- current time
-
computeIfAbsent
Returns the cached value for the given key, or computes it if it's absent or not from the current simulation time. The key represents a type of information, e.g. 'ego speed'.This method will compute the information if required. A simple manner to define a
Supplier<T>is to create a method and invoke it using a lambda expression. For example, suppose we have the methodpublic Speed getEgoSpeed()for the tactical planner to use, andprivate Speed computeEgoSpeed()for internal use, then we could use the following line insidegetEgoSpeed():return computeIfAbsent("speedLimit", () -> computeEgoSpeed());- Type Parameters:
T- value type- Parameters:
key- key defining which information is requestedsupplier- supplier- Returns:
- cached or computed value
-
computeIfAbsent
Returns the cached value for the given key, or computes it if it's absent or not from the current simulation time. The key represents a type of information, e.g. 'leading GTUs'. This information is context specific, namely the lane for which 'leading GTUs' are requested.This method will compute the information if required. A simple manner to define a
Supplier<T>is to create a method and invoke it using a lambda expression. For example, suppose we have the methodpublic List<HeadwayGtu> getLeaders(Lane)for the tactical planner to use, andprivate List<HeadwayGtu> computeLeaders(Lane)for internal use, then we could use the following line insidegetLeaders(Lane):return computeIfAbsent("leaders", () -> computeLeaders(lane))- Type Parameters:
T- value type- Parameters:
key- key defining which information is requested, it may be contextual if it's context dependentsupplier- suppliercontext- object defining the context, e.g. the lane for which the information is requested- Returns:
- cached or computed value
-
computeIfAbsent
Returns the cached value for the given key, or computes it if it's absent or not from the current simulation time. The key represents a type of information, e.g. 'neighboring GTUs'. This information is context specific, namely the lane for which 'neighboring GTUs' are requested, as well as the longitudinal direction.It is not advised to use this method at high frequency as it contains slight overhead. Instead, consider defining keys directly for various contexts. For example:
private final Object leftLaneLeadersKey = new Object(); private final Object leftLaneFollowersKey = new Object(); private final Object righttLaneLeadersKey = new Object(); private final Object rightLaneFollowersKey = new Object();
This method will compute the information if required. A simple manner to define aSupplier<T>is to create a method and invoke it using a lambda expression. For example, suppose we have the methodpublic List<HeadwayGtu> getNeighbors(Lane, LongiudinalDirection)for the tactical planner to use, andprivate List<HeadwayGtu> computeNeighbors(Lane, LongiudinalDirection)for internal use, then we could use the following line insidegetNeighbors(Lane, LongiudinalDirection):return computeIfAbsent("neighbors", () -> computeNeighbors(lane, longDir))- Type Parameters:
T- value type- Parameters:
key- key defining which information is requested, it may be contextual if it's context dependentsupplier- suppliercontext- objects defining the context, e.g. the lane and direction for which the information is requested- Returns:
- cached or computed value
-
cacheAsString
Returns a string representation of the cache. This is in the form:[speed=12.3m/s, acceleration=2.1m/s2]and can be used in {code toString()} methods of sub-classes.- Returns:
- string representation of the cache.
-