1 package org.opentrafficsim.road.gtu.strategical;
2
3 import java.io.Serializable;
4
5 import org.djunits.value.vdouble.scalar.Length;
6 import org.djunits.value.vdouble.scalar.Speed;
7 import org.opentrafficsim.base.parameters.ParameterException;
8 import org.opentrafficsim.base.parameters.Parameters;
9 import org.opentrafficsim.core.gtu.GtuException;
10 import org.opentrafficsim.core.gtu.GtuType;
11 import org.opentrafficsim.core.network.Node;
12 import org.opentrafficsim.core.network.route.Route;
13 import org.opentrafficsim.core.parameters.ParameterFactory;
14 import org.opentrafficsim.core.parameters.ParameterFactoryDefault;
15 import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
16 import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedTacticalPlanner;
17 import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedTacticalPlannerFactory;
18
19
20
21
22
23
24
25
26
27
28
29 public class LaneBasedStrategicalRoutePlannerFactory
30 implements LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalRoutePlanner>, Serializable
31 {
32
33
34 private static final long serialVersionUID = 20160811L;
35
36
37 private final LaneBasedTacticalPlannerFactory<? extends LaneBasedTacticalPlanner> tacticalPlannerFactory;
38
39
40 private final RouteGenerator routeGenerator;
41
42
43 private final ParameterFactory parameterFactory;
44
45
46 private Parameters peekedParameters = null;
47
48
49
50
51
52
53 public LaneBasedStrategicalRoutePlannerFactory(
54 final LaneBasedTacticalPlannerFactory<? extends LaneBasedTacticalPlanner> tacticalPlannerFactory)
55 {
56 this.tacticalPlannerFactory = tacticalPlannerFactory;
57 this.routeGenerator = RouteGenerator.NULL;
58 this.parameterFactory = new ParameterFactoryDefault();
59 }
60
61
62
63
64
65
66
67 public LaneBasedStrategicalRoutePlannerFactory(
68 final LaneBasedTacticalPlannerFactory<? extends LaneBasedTacticalPlanner> tacticalPlannerFactory,
69 final ParameterFactory parametersFactory)
70 {
71 this.tacticalPlannerFactory = tacticalPlannerFactory;
72 this.routeGenerator = RouteGenerator.NULL;
73 this.parameterFactory = parametersFactory;
74 }
75
76
77
78
79
80
81
82
83 public LaneBasedStrategicalRoutePlannerFactory(
84 final LaneBasedTacticalPlannerFactory<? extends LaneBasedTacticalPlanner> tacticalPlannerFactory,
85 final ParameterFactory parametersFactory, final RouteGenerator routeGenerator)
86 {
87 this.tacticalPlannerFactory = tacticalPlannerFactory;
88 this.parameterFactory = parametersFactory;
89 this.routeGenerator = routeGenerator;
90 }
91
92
93 @Override
94 public final Speed peekDesiredSpeed(final GtuType gtuType, final Speed speedLimit, final Speed maxGtuSpeed)
95 throws GtuException
96 {
97 return this.tacticalPlannerFactory.peekDesiredSpeed(gtuType, speedLimit, maxGtuSpeed, peekParameters(gtuType));
98 }
99
100
101 @Override
102 public final Length peekDesiredHeadway(final GtuType gtuType, final Speed speed) throws GtuException
103 {
104 return this.tacticalPlannerFactory.peekDesiredHeadway(gtuType, speed, peekParameters(gtuType));
105 }
106
107
108
109
110
111
112
113 private Parameters peekParameters(final GtuType gtuType) throws GtuException
114 {
115 if (this.peekedParameters != null)
116 {
117 return this.peekedParameters;
118 }
119 try
120 {
121 this.peekedParameters = this.tacticalPlannerFactory.getParameters();
122 this.parameterFactory.setValues(this.peekedParameters, gtuType);
123 }
124 catch (ParameterException exception)
125 {
126 throw new GtuException("Parameter was set to illegal value.", exception);
127 }
128 return this.peekedParameters;
129 }
130
131
132 @Override
133 public final LaneBasedStrategicalRoutePlanner create(final LaneBasedGtu gtu, final Route route, final Node origin,
134 final Node destination) throws GtuException
135 {
136 LaneBasedStrategicalRoutePlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(
137 this.tacticalPlannerFactory.create(gtu), route, gtu, origin, destination, this.routeGenerator);
138 gtu.setParameters(nextParameters(gtu.getType()));
139 return strategicalPlanner;
140 }
141
142
143
144
145
146
147
148 protected final Parameters nextParameters(final GtuType gtuType) throws GtuException
149 {
150 Parameters parameters = peekParameters(gtuType);
151 this.peekedParameters = null;
152 return parameters;
153 }
154
155
156 @Override
157 public final String toString()
158 {
159 return "LaneBasedStrategicalRoutePlannerFactory [tacticalPlannerFactory=" + this.tacticalPlannerFactory + "]";
160 }
161
162 }