1 package org.opentrafficsim.road.gtu.lane.tactical;
2
3 import org.djunits.value.vdouble.scalar.Length;
4 import org.djunits.value.vdouble.scalar.Speed;
5 import org.opentrafficsim.base.parameters.ParameterException;
6 import org.opentrafficsim.base.parameters.Parameters;
7 import org.opentrafficsim.core.gtu.GTUException;
8 import org.opentrafficsim.core.gtu.GTUType;
9 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
10 import org.opentrafficsim.road.gtu.lane.perception.PerceptionFactory;
11 import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
12 import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModelFactory;
13 import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
14 import org.opentrafficsim.road.network.speed.SpeedLimitTypes;
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 public abstract class AbstractLaneBasedTacticalPlannerFactory<T extends LaneBasedTacticalPlanner>
33 implements LaneBasedTacticalPlannerFactory<T>
34 {
35
36
37 private final CarFollowingModelFactory<? extends CarFollowingModel> carFollowingModelFactory;
38
39
40 private CarFollowingModel peekedCarFollowingModel = null;
41
42
43 private final PerceptionFactory perceptionFactory;
44
45
46
47
48
49
50 public AbstractLaneBasedTacticalPlannerFactory(
51 final CarFollowingModelFactory<? extends CarFollowingModel> carFollowingModelFactory, final PerceptionFactory perceptionFactory)
52 {
53 this.carFollowingModelFactory = carFollowingModelFactory;
54 this.perceptionFactory = perceptionFactory;
55 }
56
57
58
59
60
61
62 private CarFollowingModel peekCarFollowingModel()
63 {
64 if (this.peekedCarFollowingModel != null)
65 {
66 return this.peekedCarFollowingModel;
67 }
68 this.peekedCarFollowingModel = this.carFollowingModelFactory.generateCarFollowingModel();
69 return this.peekedCarFollowingModel;
70 }
71
72
73
74
75
76
77 protected final CarFollowingModel nextCarFollowingModel(final LaneBasedGTU gtu)
78 {
79 CarFollowingModel model = peekCarFollowingModel();
80 model.init(gtu);
81 this.peekedCarFollowingModel = null;
82 return model;
83 }
84
85
86
87
88
89
90
91 protected final Parameters getCarFollowingParameters() throws ParameterException
92 {
93 return this.carFollowingModelFactory.getParameters();
94 }
95
96
97
98
99
100
101 protected final String getCarFollowingModelFactoryString()
102 {
103 return this.carFollowingModelFactory.toString();
104 }
105
106
107 @Override
108 public final Speed peekDesiredSpeed(final GTUType gtuType, final Speed speedLimit, final Speed maxGtuSpeed,
109 final Parameters parameters) throws GTUException
110 {
111 try
112 {
113 SpeedLimitInfo sli = new SpeedLimitInfo();
114 sli.addSpeedInfo(SpeedLimitTypes.MAX_VEHICLE_SPEED, maxGtuSpeed);
115 sli.addSpeedInfo(SpeedLimitTypes.FIXED_SIGN, speedLimit);
116 return peekCarFollowingModel().desiredSpeed(parameters, sli);
117 }
118 catch (ParameterException exception)
119 {
120 throw new GTUException(exception);
121 }
122 }
123
124
125 @Override
126 public final Length peekDesiredHeadway(final GTUType gtuType, final Speed speed, final Parameters parameters)
127 throws GTUException
128 {
129 try
130 {
131 return peekCarFollowingModel().desiredHeadway(parameters, speed);
132 }
133 catch (ParameterException exception)
134 {
135 throw new GTUException(exception);
136 }
137 }
138
139
140
141
142
143 public PerceptionFactory getPerceptionFactory()
144 {
145 return this.perceptionFactory;
146 }
147
148 }