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 InputParameterMap; input parameter map
38       */
39      public InputParameterHelper(final InputParameterMap rootMap)
40      {
41          this.rootMap = rootMap;
42      }
43  
44      /** {@inheritDoc} */
45      @Override
46      public void setValues(final Parameters parameters, final GtuType gtuType) throws ParameterException
47      {
48          try
49          {
50              if (gtuType.isOfType(DefaultsNl.CAR))
51              {
52                  getParametersCar(this.rootMap).setAllIn(parameters);
53              }
54              else if (gtuType.isOfType(DefaultsNl.TRUCK))
55              {
56                  getParametersTruck(this.rootMap).setAllIn(parameters);
57              }
58              else
59              {
60                  throw new ParameterException("GtuType " + gtuType + " not supported in demo parameter factory.");
61              }
62          }
63          catch (InputParameterException exception)
64          {
65              throw new ParameterException(exception);
66          }
67      }
68  
69      /**
70       * Make a map of input parameters for a demo with a car/truck ratio and car/truck tabs with parameters.
71       * @param map InputParameterMap; the map to add the car/truck input parameters to
72       * @param probabilityDisplayPriority double; the display priority to use for the car probability in the generic map
73       */
74      public static void makeInputParameterMapCarTruck(final InputParameterMap map, final double probabilityDisplayPriority)
75      {
76          try
77          {
78              InputParameterMap genericMap;
79              if (map.getValue().containsKey("generic"))
80              {
81                  genericMap = (InputParameterMap) map.get("generic");
82              }
83              else
84              {
85                  genericMap = new InputParameterMap("generic", "Generic", "Generic parameters", 1.0);
86                  map.add(genericMap);
87              }
88              genericMap.add(new InputParameterDouble("carProbability", "Car probability",
89                      "Probability that the next generated GTU is a passenger car", 0.8, 0.0, 1.0, true, true, "%.00f",
90                      probabilityDisplayPriority));
91  
92              makeInputParameterMapCar(map, 2.0);
93  
94              makeInputParameterMapTruck(map, 3.0);
95  
96          }
97          catch (InputParameterException exception)
98          {
99              exception.printStackTrace();
100         }
101     }
102 
103     /**
104      * Make a map of input parameters for a demo with a car tabs with parameters.
105      * @param map InputParameterMap; the map to add the car input tab to
106      * @param displayPriority double; the display priority to use for the car tab in the generic map
107      */
108     public static void makeInputParameterMapCar(final InputParameterMap map, final double displayPriority)
109     {
110         try
111         {
112             InputParameterMap carMap;
113             if (map.getValue().containsKey("car"))
114             {
115                 carMap = (InputParameterMap) map.get("car");
116             }
117             else
118             {
119                 carMap = new InputParameterMap("car", "Car", "Car parameters", displayPriority);
120                 map.add(carMap);
121             }
122 
123             carMap.add(new InputParameterDoubleScalar<AccelerationUnit, Acceleration>("a", "Maximum acceleration (m/s2)",
124                     "Maximum acceleration (m/s2)", Acceleration.instantiateSI(1.56), Acceleration.instantiateSI(0.5),
125                     Acceleration.instantiateSI(5.0), true, true, "%.0f", 1.0));
126             carMap.add(new InputParameterDoubleScalar<AccelerationUnit, Acceleration>("b",
127                     "Maximum comfortable deceleration (m/s2)", "Maximum comfortable deceleration (m/s2)",
128                     Acceleration.instantiateSI(2.09), Acceleration.instantiateSI(1.0), Acceleration.instantiateSI(4.0), true,
129                     true, "%.0f", 2.0));
130             carMap.add(new InputParameterDoubleScalar<LengthUnit, Length>("s0", "Distance headway (m)", "Distance headway (m)",
131                     Length.instantiateSI(3.0), Length.instantiateSI(1.0), Length.instantiateSI(10.0), true, true, "%.0f", 3.0));
132             carMap.add(new InputParameterDoubleScalar<DurationUnit, Duration>("tSafe", "Time headway (s)", "Time headway (s)",
133                     Duration.instantiateSI(1.2), Duration.instantiateSI(1.0), Duration.instantiateSI(4.0), true, true, "%.0f",
134                     4.0));
135         }
136         catch (InputParameterException exception)
137         {
138             exception.printStackTrace();
139         }
140     }
141 
142     /**
143      * Make a map of input parameters for a demo with a truck tabs with parameters.
144      * @param map InputParameterMap; the map to add the truck input tab to
145      * @param displayPriority double; the display priority to use for the truck map in the generic map
146      */
147     public static void makeInputParameterMapTruck(final InputParameterMap map, final double displayPriority)
148     {
149         try
150         {
151             InputParameterMap truckMap;
152             if (map.getValue().containsKey("truck"))
153             {
154                 truckMap = (InputParameterMap) map.get("truck");
155             }
156             else
157             {
158                 truckMap = new InputParameterMap("truck", "Truck", "Truck parameters", displayPriority);
159                 map.add(truckMap);
160             }
161 
162             truckMap.add(new InputParameterDoubleScalar<AccelerationUnit, Acceleration>("a", "Maximum acceleration (m/s2)",
163                     "Maximum acceleration (m/s2)", Acceleration.instantiateSI(0.75), Acceleration.instantiateSI(0.5),
164                     Acceleration.instantiateSI(5.0), true, true, "%.0f", 1.0));
165             truckMap.add(new InputParameterDoubleScalar<AccelerationUnit, Acceleration>("b",
166                     "Maximum comfortable deceleration (m/s2)", "Maximum comfortable deceleration (m/s2)",
167                     Acceleration.instantiateSI(1.25), Acceleration.instantiateSI(1.0), Acceleration.instantiateSI(4.0), true,
168                     true, "%.0f", 2.0));
169             truckMap.add(new InputParameterDoubleScalar<LengthUnit, Length>("s0", "Distance headway (m)",
170                     "Distance headway (m)", Length.instantiateSI(3.0), Length.instantiateSI(1.0), Length.instantiateSI(10.0),
171                     true, true, "%.0f", 3.0));
172             truckMap.add(new InputParameterDoubleScalar<DurationUnit, Duration>("tSafe", "Time headway (s)", "Time headway (s)",
173                     Duration.instantiateSI(1.2), Duration.instantiateSI(1.0), Duration.instantiateSI(4.0), true, true, "%.0f",
174                     4.0));
175         }
176         catch (InputParameterException exception)
177         {
178             exception.printStackTrace();
179         }
180     }
181 
182     /**
183      * Get the car parameters as entered.
184      * @param rootMap InputParameterMap; the root map of the model with a 'car' tab with the parameters
185      * @return the parameters where a, b, s0 and tSafe have been updated with the user's choices
186      * @throws ParameterException when the parameter was given an illegal setting
187      * @throws InputParameterException when the input parameter could not be found
188      */
189     public static Parameters getParametersCar(final InputParameterMap rootMap)
190             throws ParameterException, InputParameterException
191     {
192         Parameters parametersCar = DefaultsFactory.getDefaultParameters();
193         Acceleration aCar = (Acceleration) rootMap.get("car.a").getCalculatedValue();
194         Acceleration bCar = (Acceleration) rootMap.get("car.b").getCalculatedValue();
195         Length s0Car = (Length) rootMap.get("car.s0").getCalculatedValue();
196         Duration tSafeCar = (Duration) rootMap.get("car.tSafe").getCalculatedValue();
197         parametersCar.setParameter(ParameterTypes.A, aCar);
198         parametersCar.setParameter(ParameterTypes.B, bCar);
199         parametersCar.setParameter(ParameterTypes.S0, s0Car);
200         parametersCar.setParameter(ParameterTypes.T, tSafeCar);
201         return parametersCar;
202     }
203 
204     /**
205      * Get the truck parameters as entered.
206      * @param rootMap InputParameterMap; the root map of the model with a 'truck' tab with the parameters
207      * @return the parameters where a, b, s0 and tSafe have been updated with the user's choices
208      * @throws ParameterException when the parameter was given an illegal setting
209      * @throws InputParameterException when the input parameter could not be found
210      */
211     public static Parameters getParametersTruck(final InputParameterMap rootMap)
212             throws ParameterException, InputParameterException
213     {
214         Parameters parametersTruck = DefaultsFactory.getDefaultParameters();
215         Acceleration aTruck = (Acceleration) rootMap.get("truck.a").getCalculatedValue();
216         Acceleration bTruck = (Acceleration) rootMap.get("truck.b").getCalculatedValue();
217         Length s0Truck = (Length) rootMap.get("truck.s0").getCalculatedValue();
218         Duration tSafeTruck = (Duration) rootMap.get("truck.tSafe").getCalculatedValue();
219         parametersTruck.setParameter(ParameterTypes.A, aTruck);
220         parametersTruck.setParameter(ParameterTypes.B, bTruck);
221         parametersTruck.setParameter(ParameterTypes.S0, s0Truck);
222         parametersTruck.setParameter(ParameterTypes.T, tSafeTruck);
223         return parametersTruck;
224     }
225 
226 }