1 package org.opentrafficsim.road.gtu.strategical;
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.core.parameters.ParameterFactory;
10 import org.opentrafficsim.core.parameters.ParameterFactoryDefault;
11 import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
12 import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedTacticalPlanner;
13 import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedTacticalPlannerFactory;
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public abstract class AbstractLaneBasedStrategicalPlannerFactory<T extends LaneBasedStrategicalPlanner>
30 implements LaneBasedStrategicalPlannerFactory<T>
31 {
32
33
34 private final LaneBasedTacticalPlannerFactory<? extends LaneBasedTacticalPlanner> tacticalPlannerFactory;
35
36
37 private final ParameterFactory parameterFactory;
38
39
40 private Parameters peekedParameters = null;
41
42
43
44
45
46
47 public AbstractLaneBasedStrategicalPlannerFactory(
48 final LaneBasedTacticalPlannerFactory<? extends LaneBasedTacticalPlanner> tacticalPlannerFactory)
49 {
50 this.tacticalPlannerFactory = tacticalPlannerFactory;
51 this.parameterFactory = new ParameterFactoryDefault();
52 }
53
54
55
56
57
58
59
60 public AbstractLaneBasedStrategicalPlannerFactory(
61 final LaneBasedTacticalPlannerFactory<? extends LaneBasedTacticalPlanner> tacticalPlannerFactory,
62 final ParameterFactory parametersFactory)
63 {
64 this.tacticalPlannerFactory = tacticalPlannerFactory;
65 this.parameterFactory = parametersFactory;
66 }
67
68
69 @Override
70 public final Speed peekDesiredSpeed(final GTUType gtuType, final Speed speedLimit, final Speed maxGtuSpeed)
71 throws GTUException
72 {
73 return this.tacticalPlannerFactory.peekDesiredSpeed(gtuType, speedLimit, maxGtuSpeed, peekParameters(gtuType));
74 }
75
76
77 @Override
78 public final Length peekDesiredHeadway(final GTUType gtuType, final Speed speed) throws GTUException
79 {
80 return this.tacticalPlannerFactory.peekDesiredHeadway(gtuType, speed, peekParameters(gtuType));
81 }
82
83
84
85
86
87
88
89 private Parameters peekParameters(final GTUType gtuType) throws GTUException
90 {
91 if (this.peekedParameters != null)
92 {
93 return this.peekedParameters;
94 }
95 try
96 {
97 this.peekedParameters = this.tacticalPlannerFactory.getParameters();
98 Parameters parameters = getParameters();
99 if (parameters != null)
100 {
101 parameters.setAllIn(this.peekedParameters);
102 }
103 this.parameterFactory.setValues(this.peekedParameters, gtuType);
104 }
105 catch (ParameterException exception)
106 {
107 throw new GTUException("Parameter was set to illegal value.", exception);
108 }
109 return this.peekedParameters;
110 }
111
112
113
114
115
116
117
118 protected abstract Parameters getParameters();
119
120
121
122
123
124
125
126 protected final Parameters nextParameters(final GTUType gtuType) throws GTUException
127 {
128 Parameters parameters = peekParameters(gtuType);
129 this.peekedParameters = null;
130 return parameters;
131 }
132
133
134
135
136
137
138
139 protected final LaneBasedTacticalPlanner nextTacticalPlanner(final LaneBasedGTU gtu) throws GTUException
140 {
141 return this.tacticalPlannerFactory.create(gtu);
142 }
143
144
145
146
147
148 protected final LaneBasedTacticalPlannerFactory<? extends LaneBasedTacticalPlanner> getTacticalPlannerFactory()
149 {
150 return this.tacticalPlannerFactory;
151 }
152
153 }