View Javadoc
1   package org.opentrafficsim.demo;
2   
3   import java.util.List;
4   
5   import org.djunits.value.vdouble.scalar.Acceleration;
6   import org.djunits.value.vdouble.scalar.Duration;
7   import org.djunits.value.vdouble.scalar.Length;
8   import org.opentrafficsim.base.modelproperties.CompoundProperty;
9   import org.opentrafficsim.base.modelproperties.Property;
10  import org.opentrafficsim.base.modelproperties.PropertyException;
11  import org.opentrafficsim.base.modelproperties.SelectionProperty;
12  import org.opentrafficsim.core.gtu.GTUException;
13  import org.opentrafficsim.core.gtu.behavioralcharacteristics.BehavioralCharacteristics;
14  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedCFLCTacticalPlannerFactory;
15  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedGTUFollowingDirectedChangeTacticalPlannerFactory;
16  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedGTUFollowingTacticalPlannerFactory;
17  import org.opentrafficsim.road.gtu.lane.tactical.following.AbstractIDM;
18  import org.opentrafficsim.road.gtu.lane.tactical.following.GTUFollowingModelOld;
19  import org.opentrafficsim.road.gtu.lane.tactical.following.IDMOld;
20  import org.opentrafficsim.road.gtu.lane.tactical.following.IDMPlusFactory;
21  import org.opentrafficsim.road.gtu.lane.tactical.following.IDMPlusOld;
22  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Altruistic;
23  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.Egoistic;
24  import org.opentrafficsim.road.gtu.lane.tactical.lanechangemobil.LaneChangeModel;
25  import org.opentrafficsim.road.gtu.lane.tactical.lmrs.LMRSFactory;
26  import org.opentrafficsim.road.gtu.lane.tactical.toledo.ToledoFactory;
27  import org.opentrafficsim.road.gtu.lane.tactical.util.TrafficLightUtil;
28  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
29  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
30  import org.opentrafficsim.road.gtu.strategical.route.LaneBasedStrategicalRoutePlannerFactory;
31  import org.opentrafficsim.road.modelproperties.IDMPropertySet;
32  
33  /**
34   * Utilities for demos, e.g. parsing.
35   * <p>
36   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
37   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
38   * </p>
39   * $LastChangedDate: 2015-07-24 02:58:59 +0200 (Fri, 24 Jul 2015) $, @version $Revision: 1147 $, by $Author: averbraeck $,
40   * initial version Nov 7, 2016 <br>
41   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
42   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
43   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
44   */
45  public final class PropertiesParser
46  {
47      /** */
48      private PropertiesParser()
49      {
50          // private constructor; do not instantiate class
51      }
52  
53      /**
54       * Get the car following model for a GTU type, e.g. "Car" or "Truck" from the properties.
55       * @param properties the properties to parse
56       * @param gtuType the type of GTU, e.g. "Car" or "Truck"
57       * @return GTUFollowingModelOld; the car following model
58       * @throws PropertyException in case parsing fails
59       */
60      public static GTUFollowingModelOld parseGTUFollowingModelOld(final List<Property<?>> properties, final String gtuType)
61              throws PropertyException
62      {
63          // Get car-following model name
64          String carFollowingModelName = null;
65          CompoundProperty propertyContainer = new CompoundProperty("", "", "", properties, false, 0);
66          Property<?> cfmp = propertyContainer.findByKey("CarFollowingModel");
67          if (null == cfmp)
68          {
69              throw new PropertyException("Cannot find \"Car following model\" property");
70          }
71          if (cfmp instanceof SelectionProperty)
72          {
73              carFollowingModelName = ((SelectionProperty) cfmp).getValue();
74          }
75          else
76          {
77              throw new Error("\"Car following model\" property has wrong type");
78          }
79  
80          // Get car-following model parameter
81          for (Property<?> ap : new CompoundProperty("", "", "", properties, false, 0))
82          {
83              if (ap instanceof CompoundProperty)
84              {
85                  CompoundProperty cp = (CompoundProperty) ap;
86                  if (ap.getKey().contains("IDM"))
87                  {
88                      Acceleration a = IDMPropertySet.getA(cp);
89                      Acceleration b = IDMPropertySet.getB(cp);
90                      Length s0 = IDMPropertySet.getS0(cp);
91                      Duration tSafe = IDMPropertySet.getTSafe(cp);
92                      GTUFollowingModelOld gtuFollowingModel = null;
93                      if (carFollowingModelName.equals("IDM"))
94                      {
95                          gtuFollowingModel = new IDMOld(a, b, s0, tSafe, 1.0);
96                      }
97                      else if (carFollowingModelName.equals("IDM+"))
98                      {
99                          gtuFollowingModel = new IDMPlusOld(a, b, s0, tSafe, 1.0);
100                     }
101                     else
102                     {
103                         throw new PropertyException("Unknown gtu following model: " + carFollowingModelName);
104                     }
105                     if (ap.getKey().contains(gtuType))
106                     {
107                         return gtuFollowingModel;
108                     }
109                 }
110             }
111         }
112         throw new PropertyException("Cannot determine GTU following model for GTU type " + gtuType);
113     }
114 
115     /**
116      * Get the lane change model from the properties.
117      * @param properties the properties to parse
118      * @return LaneChangeModel; the lane change model
119      * @throws PropertyException in case parsing fails
120      */
121     public static LaneChangeModel parseLaneChangeModel(final List<Property<?>> properties) throws PropertyException
122     {
123         CompoundProperty propertyContainer = new CompoundProperty("", "", "", properties, false, 0);
124         Property<?> cfmp = propertyContainer.findByKey("LaneChanging");
125         if (null == cfmp)
126         {
127             throw new PropertyException("Cannot find \"Lane changing\" property");
128         }
129         if (cfmp instanceof SelectionProperty)
130         {
131             String laneChangeModelName = ((SelectionProperty) cfmp).getValue();
132             if ("Egoistic".equals(laneChangeModelName))
133             {
134                 return new Egoistic();
135             }
136             else if ("Altruistic".equals(laneChangeModelName))
137             {
138                 return new Altruistic();
139             }
140             throw new PropertyException("Lane changing " + laneChangeModelName + " not implemented");
141         }
142         throw new PropertyException("\"Lane changing\" property has wrong type");
143     }
144 
145     /**
146      * Get the strategical planner factory from the properties.
147      * @param properties the properties to parse
148      * @param gtuFollowingModel the car following model in case it is needed
149      * @param laneChangeModel the lane change model in case it is needed
150      * @return LaneBasedStrategicalPlannerFactory; the tactical planner factory
151      * @throws PropertyException in case parsing fails
152      * @throws GTUException in case LMRS Factory cannot be created
153      */
154     public static LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner> parseStrategicalPlannerFactory(
155             final List<Property<?>> properties, final GTUFollowingModelOld gtuFollowingModel,
156             final LaneChangeModel laneChangeModel) throws PropertyException, GTUException
157     {
158         for (Property<?> ap : new CompoundProperty("", "", "", properties, false, 0))
159         {
160             if (ap instanceof SelectionProperty)
161             {
162                 SelectionProperty sp = (SelectionProperty) ap;
163                 if ("TacticalPlanner".equals(sp.getKey()))
164                 {
165                     String tacticalPlannerName = sp.getValue();
166                     if ("IDM".equals(tacticalPlannerName))
167                     {
168                         return new LaneBasedStrategicalRoutePlannerFactory(
169                                 new LaneBasedGTUFollowingTacticalPlannerFactory(gtuFollowingModel));
170                     }
171                     else if ("MOBIL/IDM".equals(tacticalPlannerName))
172                     {
173                         return new LaneBasedStrategicalRoutePlannerFactory(
174                                 new LaneBasedCFLCTacticalPlannerFactory(gtuFollowingModel, laneChangeModel));
175                     }
176                     else if ("DIRECTED/IDM".equals(tacticalPlannerName))
177                     {
178                         return new LaneBasedStrategicalRoutePlannerFactory(
179                                 new LaneBasedGTUFollowingDirectedChangeTacticalPlannerFactory(gtuFollowingModel));
180                     }
181                     else if ("LMRS".equals(tacticalPlannerName))
182                     {
183                         // provide default parameters with the car-following model
184                         BehavioralCharacteristics defaultBehavioralCFCharacteristics = new BehavioralCharacteristics();
185                         defaultBehavioralCFCharacteristics.setDefaultParameters(AbstractIDM.class);
186                         defaultBehavioralCFCharacteristics.setDefaultParameters(TrafficLightUtil.class);
187                         return new LaneBasedStrategicalRoutePlannerFactory(
188                                 new LMRSFactory(new IDMPlusFactory(), defaultBehavioralCFCharacteristics));
189                     }
190                     else if ("Toledo".equals(tacticalPlannerName))
191                     {
192                         return new LaneBasedStrategicalRoutePlannerFactory(new ToledoFactory());
193                     }
194                     else
195                     {
196                         throw new PropertyException("Don't know how to create a " + tacticalPlannerName + " tactical planner");
197                     }
198                 }
199             }
200         }
201         throw new PropertyException("No TacticalPlanner key found in the properties");
202     }
203 
204 }