1 package org.opentrafficsim.web.test;
2
3 import org.djunits.unit.AccelerationUnit;
4 import org.djunits.unit.DurationUnit;
5 import org.djunits.unit.LengthUnit;
6 import org.djunits.value.vdouble.scalar.Acceleration;
7 import org.djunits.value.vdouble.scalar.Duration;
8 import org.djunits.value.vdouble.scalar.Length;
9 import org.opentrafficsim.base.parameters.ParameterException;
10 import org.opentrafficsim.base.parameters.ParameterTypes;
11 import org.opentrafficsim.base.parameters.Parameters;
12 import org.opentrafficsim.core.definitions.DefaultsNl;
13 import org.opentrafficsim.core.gtu.GtuType;
14 import org.opentrafficsim.core.parameters.ParameterFactory;
15
16 import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterDouble;
17 import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterDoubleScalar;
18 import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterException;
19 import nl.tudelft.simulation.dsol.model.inputparameters.InputParameterMap;
20
21
22
23
24
25
26
27
28
29 public final class InputParameterHelper implements ParameterFactory
30 {
31
32
33 private final InputParameterMap rootMap;
34
35
36
37
38
39 public InputParameterHelper(final InputParameterMap rootMap)
40 {
41 this.rootMap = rootMap;
42 }
43
44 @Override
45 public void setValues(final Parameters parameters, final GtuType gtuType) throws ParameterException
46 {
47 try
48 {
49 if (gtuType.isOfType(DefaultsNl.CAR))
50 {
51 getParametersCar(this.rootMap).setAllIn(parameters);
52 }
53 else if (gtuType.isOfType(DefaultsNl.TRUCK))
54 {
55 getParametersTruck(this.rootMap).setAllIn(parameters);
56 }
57 else
58 {
59 throw new ParameterException("GtuType " + gtuType + " not supported in demo parameter factory.");
60 }
61 }
62 catch (InputParameterException exception)
63 {
64 throw new ParameterException(exception);
65 }
66 }
67
68
69
70
71
72
73 public static void makeInputParameterMapCarTruck(final InputParameterMap map, final double probabilityDisplayPriority)
74 {
75 try
76 {
77 InputParameterMap genericMap;
78 if (map.getValue().containsKey("generic"))
79 {
80 genericMap = (InputParameterMap) map.get("generic");
81 }
82 else
83 {
84 genericMap = new InputParameterMap("generic", "Generic", "Generic parameters", 1.0);
85 map.add(genericMap);
86 }
87 genericMap.add(new InputParameterDouble("carProbability", "Car probability",
88 "Probability that the next generated GTU is a passenger car", 0.8, 0.0, 1.0, true, true, "%.00f",
89 probabilityDisplayPriority));
90
91 makeInputParameterMapCar(map, 2.0);
92
93 makeInputParameterMapTruck(map, 3.0);
94
95 }
96 catch (InputParameterException exception)
97 {
98 exception.printStackTrace();
99 }
100 }
101
102
103
104
105
106
107 public static void makeInputParameterMapCar(final InputParameterMap map, final double displayPriority)
108 {
109 try
110 {
111 InputParameterMap carMap;
112 if (map.getValue().containsKey("car"))
113 {
114 carMap = (InputParameterMap) map.get("car");
115 }
116 else
117 {
118 carMap = new InputParameterMap("car", "Car", "Car parameters", displayPriority);
119 map.add(carMap);
120 }
121
122 carMap.add(new InputParameterDoubleScalar<AccelerationUnit, Acceleration>("a", "Maximum acceleration (m/s2)",
123 "Maximum acceleration (m/s2)", Acceleration.ofSI(1.56), Acceleration.ofSI(0.5), Acceleration.ofSI(5.0),
124 true, true, "%.0f", 1.0));
125 carMap.add(new InputParameterDoubleScalar<AccelerationUnit, Acceleration>("b",
126 "Maximum comfortable deceleration (m/s2)", "Maximum comfortable deceleration (m/s2)",
127 Acceleration.ofSI(2.09), Acceleration.ofSI(1.0), Acceleration.ofSI(4.0), true, true, "%.0f", 2.0));
128 carMap.add(new InputParameterDoubleScalar<LengthUnit, Length>("s0", "Distance headway (m)", "Distance headway (m)",
129 Length.ofSI(3.0), Length.ofSI(1.0), Length.ofSI(10.0), true, true, "%.0f", 3.0));
130 carMap.add(new InputParameterDoubleScalar<DurationUnit, Duration>("tSafe", "Time headway (s)", "Time headway (s)",
131 Duration.ofSI(1.2), Duration.ofSI(1.0), Duration.ofSI(4.0), true, true, "%.0f", 4.0));
132 }
133 catch (InputParameterException exception)
134 {
135 exception.printStackTrace();
136 }
137 }
138
139
140
141
142
143
144 public static void makeInputParameterMapTruck(final InputParameterMap map, final double displayPriority)
145 {
146 try
147 {
148 InputParameterMap truckMap;
149 if (map.getValue().containsKey("truck"))
150 {
151 truckMap = (InputParameterMap) map.get("truck");
152 }
153 else
154 {
155 truckMap = new InputParameterMap("truck", "Truck", "Truck parameters", displayPriority);
156 map.add(truckMap);
157 }
158
159 truckMap.add(new InputParameterDoubleScalar<AccelerationUnit, Acceleration>("a", "Maximum acceleration (m/s2)",
160 "Maximum acceleration (m/s2)", Acceleration.ofSI(0.75), Acceleration.ofSI(0.5), Acceleration.ofSI(5.0),
161 true, true, "%.0f", 1.0));
162 truckMap.add(new InputParameterDoubleScalar<AccelerationUnit, Acceleration>("b",
163 "Maximum comfortable deceleration (m/s2)", "Maximum comfortable deceleration (m/s2)",
164 Acceleration.ofSI(1.25), Acceleration.ofSI(1.0), Acceleration.ofSI(4.0), true, true, "%.0f", 2.0));
165 truckMap.add(new InputParameterDoubleScalar<LengthUnit, Length>("s0", "Distance headway (m)",
166 "Distance headway (m)", Length.ofSI(3.0), Length.ofSI(1.0), Length.ofSI(10.0), true, true, "%.0f", 3.0));
167 truckMap.add(new InputParameterDoubleScalar<DurationUnit, Duration>("tSafe", "Time headway (s)", "Time headway (s)",
168 Duration.ofSI(1.2), Duration.ofSI(1.0), Duration.ofSI(4.0), true, true, "%.0f", 4.0));
169 }
170 catch (InputParameterException exception)
171 {
172 exception.printStackTrace();
173 }
174 }
175
176
177
178
179
180
181
182
183 public static Parameters getParametersCar(final InputParameterMap rootMap)
184 throws ParameterException, InputParameterException
185 {
186 Parameters parametersCar = DefaultsFactory.getDefaultParameters();
187 Acceleration aCar = (Acceleration) rootMap.get("car.a").getCalculatedValue();
188 Acceleration bCar = (Acceleration) rootMap.get("car.b").getCalculatedValue();
189 Length s0Car = (Length) rootMap.get("car.s0").getCalculatedValue();
190 Duration tSafeCar = (Duration) rootMap.get("car.tSafe").getCalculatedValue();
191 parametersCar.setParameter(ParameterTypes.A, aCar);
192 parametersCar.setParameter(ParameterTypes.B, bCar);
193 parametersCar.setParameter(ParameterTypes.S0, s0Car);
194 parametersCar.setParameter(ParameterTypes.T, tSafeCar);
195 return parametersCar;
196 }
197
198
199
200
201
202
203
204
205 public static Parameters getParametersTruck(final InputParameterMap rootMap)
206 throws ParameterException, InputParameterException
207 {
208 Parameters parametersTruck = DefaultsFactory.getDefaultParameters();
209 Acceleration aTruck = (Acceleration) rootMap.get("truck.a").getCalculatedValue();
210 Acceleration bTruck = (Acceleration) rootMap.get("truck.b").getCalculatedValue();
211 Length s0Truck = (Length) rootMap.get("truck.s0").getCalculatedValue();
212 Duration tSafeTruck = (Duration) rootMap.get("truck.tSafe").getCalculatedValue();
213 parametersTruck.setParameter(ParameterTypes.A, aTruck);
214 parametersTruck.setParameter(ParameterTypes.B, bTruck);
215 parametersTruck.setParameter(ParameterTypes.S0, s0Truck);
216 parametersTruck.setParameter(ParameterTypes.T, tSafeTruck);
217 return parametersTruck;
218 }
219
220 }