View Javadoc
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   * InputParameterHelper.java.
23   * <p>
24   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
25   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
26   * </p>
27   * @author <a href="https://github.com/averbraeck" target="_blank">Alexander Verbraeck</a>
28   */
29  public final class InputParameterHelper implements ParameterFactory
30  {
31  
32      /** Input parameter map. */
33      private final InputParameterMap rootMap;
34  
35      /**
36       * Constructor.
37       * @param rootMap input parameter map
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       * Make a map of input parameters for a demo with a car/truck ratio and car/truck tabs with parameters.
70       * @param map the map to add the car/truck input parameters to
71       * @param probabilityDisplayPriority the display priority to use for the car probability in the generic map
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      * Make a map of input parameters for a demo with a car tabs with parameters.
104      * @param map the map to add the car input tab to
105      * @param displayPriority the display priority to use for the car tab in the generic map
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      * Make a map of input parameters for a demo with a truck tabs with parameters.
141      * @param map the map to add the truck input tab to
142      * @param displayPriority the display priority to use for the truck map in the generic map
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      * Get the car parameters as entered.
178      * @param rootMap the root map of the model with a 'car' tab with the parameters
179      * @return the parameters where a, b, s0 and tSafe have been updated with the user's choices
180      * @throws ParameterException when the parameter was given an illegal setting
181      * @throws InputParameterException when the input parameter could not be found
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      * Get the truck parameters as entered.
200      * @param rootMap the root map of the model with a 'truck' tab with the parameters
201      * @return the parameters where a, b, s0 and tSafe have been updated with the user's choices
202      * @throws ParameterException when the parameter was given an illegal setting
203      * @throws InputParameterException when the input parameter could not be found
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 }