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.instantiateSI(1.56), Acceleration.instantiateSI(0.5),
124                     Acceleration.instantiateSI(5.0), 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.instantiateSI(2.09), Acceleration.instantiateSI(1.0), Acceleration.instantiateSI(4.0), true,
128                     true, "%.0f", 2.0));
129             carMap.add(new InputParameterDoubleScalar<LengthUnit, Length>("s0", "Distance headway (m)", "Distance headway (m)",
130                     Length.instantiateSI(3.0), Length.instantiateSI(1.0), Length.instantiateSI(10.0), true, true, "%.0f", 3.0));
131             carMap.add(new InputParameterDoubleScalar<DurationUnit, Duration>("tSafe", "Time headway (s)", "Time headway (s)",
132                     Duration.instantiateSI(1.2), Duration.instantiateSI(1.0), Duration.instantiateSI(4.0), true, true, "%.0f",
133                     4.0));
134         }
135         catch (InputParameterException exception)
136         {
137             exception.printStackTrace();
138         }
139     }
140 
141     /**
142      * Make a map of input parameters for a demo with a truck tabs with parameters.
143      * @param map the map to add the truck input tab to
144      * @param displayPriority the display priority to use for the truck map in the generic map
145      */
146     public static void makeInputParameterMapTruck(final InputParameterMap map, final double displayPriority)
147     {
148         try
149         {
150             InputParameterMap truckMap;
151             if (map.getValue().containsKey("truck"))
152             {
153                 truckMap = (InputParameterMap) map.get("truck");
154             }
155             else
156             {
157                 truckMap = new InputParameterMap("truck", "Truck", "Truck parameters", displayPriority);
158                 map.add(truckMap);
159             }
160 
161             truckMap.add(new InputParameterDoubleScalar<AccelerationUnit, Acceleration>("a", "Maximum acceleration (m/s2)",
162                     "Maximum acceleration (m/s2)", Acceleration.instantiateSI(0.75), Acceleration.instantiateSI(0.5),
163                     Acceleration.instantiateSI(5.0), true, true, "%.0f", 1.0));
164             truckMap.add(new InputParameterDoubleScalar<AccelerationUnit, Acceleration>("b",
165                     "Maximum comfortable deceleration (m/s2)", "Maximum comfortable deceleration (m/s2)",
166                     Acceleration.instantiateSI(1.25), Acceleration.instantiateSI(1.0), Acceleration.instantiateSI(4.0), true,
167                     true, "%.0f", 2.0));
168             truckMap.add(new InputParameterDoubleScalar<LengthUnit, Length>("s0", "Distance headway (m)",
169                     "Distance headway (m)", Length.instantiateSI(3.0), Length.instantiateSI(1.0), Length.instantiateSI(10.0),
170                     true, true, "%.0f", 3.0));
171             truckMap.add(new InputParameterDoubleScalar<DurationUnit, Duration>("tSafe", "Time headway (s)", "Time headway (s)",
172                     Duration.instantiateSI(1.2), Duration.instantiateSI(1.0), Duration.instantiateSI(4.0), true, true, "%.0f",
173                     4.0));
174         }
175         catch (InputParameterException exception)
176         {
177             exception.printStackTrace();
178         }
179     }
180 
181     /**
182      * Get the car parameters as entered.
183      * @param rootMap the root map of the model with a 'car' tab with the parameters
184      * @return the parameters where a, b, s0 and tSafe have been updated with the user's choices
185      * @throws ParameterException when the parameter was given an illegal setting
186      * @throws InputParameterException when the input parameter could not be found
187      */
188     public static Parameters getParametersCar(final InputParameterMap rootMap)
189             throws ParameterException, InputParameterException
190     {
191         Parameters parametersCar = DefaultsFactory.getDefaultParameters();
192         Acceleration aCar = (Acceleration) rootMap.get("car.a").getCalculatedValue();
193         Acceleration bCar = (Acceleration) rootMap.get("car.b").getCalculatedValue();
194         Length s0Car = (Length) rootMap.get("car.s0").getCalculatedValue();
195         Duration tSafeCar = (Duration) rootMap.get("car.tSafe").getCalculatedValue();
196         parametersCar.setParameter(ParameterTypes.A, aCar);
197         parametersCar.setParameter(ParameterTypes.B, bCar);
198         parametersCar.setParameter(ParameterTypes.S0, s0Car);
199         parametersCar.setParameter(ParameterTypes.T, tSafeCar);
200         return parametersCar;
201     }
202 
203     /**
204      * Get the truck parameters as entered.
205      * @param rootMap the root map of the model with a 'truck' tab with the parameters
206      * @return the parameters where a, b, s0 and tSafe have been updated with the user's choices
207      * @throws ParameterException when the parameter was given an illegal setting
208      * @throws InputParameterException when the input parameter could not be found
209      */
210     public static Parameters getParametersTruck(final InputParameterMap rootMap)
211             throws ParameterException, InputParameterException
212     {
213         Parameters parametersTruck = DefaultsFactory.getDefaultParameters();
214         Acceleration aTruck = (Acceleration) rootMap.get("truck.a").getCalculatedValue();
215         Acceleration bTruck = (Acceleration) rootMap.get("truck.b").getCalculatedValue();
216         Length s0Truck = (Length) rootMap.get("truck.s0").getCalculatedValue();
217         Duration tSafeTruck = (Duration) rootMap.get("truck.tSafe").getCalculatedValue();
218         parametersTruck.setParameter(ParameterTypes.A, aTruck);
219         parametersTruck.setParameter(ParameterTypes.B, bTruck);
220         parametersTruck.setParameter(ParameterTypes.S0, s0Truck);
221         parametersTruck.setParameter(ParameterTypes.T, tSafeTruck);
222         return parametersTruck;
223     }
224 
225 }