1 package org.opentrafficsim.road.gtu.lane.tactical.following;
2
3 import org.djunits.unit.AccelerationUnit;
4 import org.djunits.value.vdouble.scalar.Acceleration;
5 import org.djunits.value.vdouble.scalar.Length;
6 import org.djunits.value.vdouble.scalar.Speed;
7 import org.djutils.exceptions.Throw;
8 import org.opentrafficsim.base.parameters.ParameterException;
9 import org.opentrafficsim.base.parameters.Parameters;
10 import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
11 import org.opentrafficsim.road.gtu.lane.perception.PerceptionIterable;
12 import org.opentrafficsim.road.gtu.lane.perception.headway.Headway;
13 import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
14
15
16
17
18
19
20
21
22
23 public abstract class AbstractCarFollowingModel implements CarFollowingModel
24 {
25
26
27 private DesiredHeadwayModel desiredHeadwayModel;
28
29
30 private DesiredSpeedModel desiredSpeedModel;
31
32
33
34
35
36 public AbstractCarFollowingModel(final DesiredHeadwayModel desiredHeadwayModel, final DesiredSpeedModel desiredSpeedModel)
37 {
38 this.desiredHeadwayModel = desiredHeadwayModel;
39 this.desiredSpeedModel = desiredSpeedModel;
40 }
41
42
43 @Override
44 public final Length desiredHeadway(final Parameters parameters, final Speed speed) throws ParameterException
45 {
46 Throw.whenNull(parameters, "Parameters may not be null.");
47 Throw.whenNull(speed, "Speed may not be null.");
48 return this.desiredHeadwayModel.desiredHeadway(parameters, speed);
49 }
50
51
52 @Override
53 public final Speed desiredSpeed(final Parameters parameters, final SpeedLimitInfo speedInfo) throws ParameterException
54 {
55 Throw.whenNull(parameters, "Parameters may not be null.");
56 Throw.whenNull(speedInfo, "Speed limit info may not be null.");
57 return this.desiredSpeedModel.desiredSpeed(parameters, speedInfo);
58 }
59
60
61 @Override
62 public final Acceleration followingAcceleration(final Parameters parameters, final Speed speed,
63 final SpeedLimitInfo speedLimitInfo, final PerceptionIterable<? extends Headway> leaders) throws ParameterException
64 {
65 Throw.whenNull(parameters, "Parameters may not be null.");
66 Throw.whenNull(speed, "Speed may not be null.");
67 Throw.whenNull(speedLimitInfo, "Speed limit info may not be null.");
68 Throw.whenNull(leaders, "Leaders may not be null.");
69
70 if (!leaders.isEmpty() && leaders.first().getDistance().si <= 0)
71 {
72 return new Acceleration(Double.NEGATIVE_INFINITY, AccelerationUnit.SI);
73 }
74
75 Acceleration acc = followingAcceleration(parameters, speed, desiredSpeed(parameters, speedLimitInfo),
76 desiredHeadway(parameters, speed), leaders);
77 return acc;
78 }
79
80
81
82
83
84
85
86
87
88
89
90
91 protected abstract Acceleration followingAcceleration(Parameters parameters, Speed speed, Speed desiredSpeed,
92 Length desiredHeadway, PerceptionIterable<? extends Headway> leaders) throws ParameterException;
93
94
95 @SuppressWarnings("checkstyle:designforextension")
96 @Override
97 public String toString()
98 {
99 return getLongName();
100 }
101
102
103 @Override
104 public final void init(final LaneBasedGtu gtu)
105 {
106 if (this.desiredHeadwayModel instanceof Initialisable)
107 {
108 ((Initialisable) this.desiredHeadwayModel).init(gtu);
109 }
110 if (this.desiredSpeedModel instanceof Initialisable)
111 {
112 ((Initialisable) this.desiredSpeedModel).init(gtu);
113 }
114 }
115
116 }