View Javadoc
1   package org.opentrafficsim.road.gtu.lane.tactical;
2   
3   import org.djunits.value.vdouble.scalar.Length;
4   import org.djunits.value.vdouble.scalar.Speed;
5   import org.opentrafficsim.base.parameters.ParameterException;
6   import org.opentrafficsim.base.parameters.Parameters;
7   import org.opentrafficsim.core.gtu.GTUException;
8   import org.opentrafficsim.core.gtu.GTUType;
9   import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
10  import org.opentrafficsim.road.gtu.lane.perception.PerceptionFactory;
11  import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModel;
12  import org.opentrafficsim.road.gtu.lane.tactical.following.CarFollowingModelFactory;
13  import org.opentrafficsim.road.network.speed.SpeedLimitInfo;
14  import org.opentrafficsim.road.network.speed.SpeedLimitTypes;
15  
16  /**
17   * Abstract tactical planner factory which uses a car-following model factory for supplying peeked desired speed and headway. To
18   * this end the next car-following model is created and used throughout all peek invocations until an implementation of this
19   * class calls {@code nextCarFollowingModel()} to generate a new tactical planner. Implementations should also use
20   * {@code getCarFollowingParameters()} in the {@code getParameters()} method to include the parameters a car-following model
21   * requires.
22   * <p>
23   * Copyright (c) 2013-2018 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
24   * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
25   * <p>
26   * @version $Revision$, $LastChangedDate$, by $Author$, initial version 16 jan. 2018 <br>
27   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
28   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
29   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
30   * @param <T> class of the tactical planner generated
31   */
32  public abstract class AbstractLaneBasedTacticalPlannerFactory<T extends LaneBasedTacticalPlanner>
33          implements LaneBasedTacticalPlannerFactory<T>
34  {
35  
36      /** Constructor for the car-following model. */
37      private final CarFollowingModelFactory<? extends CarFollowingModel> carFollowingModelFactory;
38  
39      /** Peeked car following model. */
40      private CarFollowingModel peekedCarFollowingModel = null;
41      
42      /** Perception factory. */
43      private final PerceptionFactory perceptionFactory;
44  
45      /**
46       * Constructor.
47       * @param carFollowingModelFactory CarFollowingModelFactory&lt;? extends CarFollowingModel&gt;; car-following model factory
48       * @param perceptionFactory PerceptionFactory; perception factory
49       */
50      public AbstractLaneBasedTacticalPlannerFactory(
51              final CarFollowingModelFactory<? extends CarFollowingModel> carFollowingModelFactory, final PerceptionFactory perceptionFactory)
52      {
53          this.carFollowingModelFactory = carFollowingModelFactory;
54          this.perceptionFactory = perceptionFactory;
55      }
56  
57      /**
58       * Returns the next car following model, which will be a fixed peeked instance until {@code nextCarFollowingModel()} is
59       * called.
60       * @return CarFollowingModel; next car following model
61       */
62      private CarFollowingModel peekCarFollowingModel()
63      {
64          if (this.peekedCarFollowingModel != null)
65          {
66              return this.peekedCarFollowingModel;
67          }
68          this.peekedCarFollowingModel = this.carFollowingModelFactory.generateCarFollowingModel();
69          return this.peekedCarFollowingModel;
70      }
71  
72      /**
73       * Returns the next car following model.
74       * @param gtu LaneBasedGTU; gtu
75       * @return CarFollowingModel; next car following model
76       */
77      protected final CarFollowingModel nextCarFollowingModel(final LaneBasedGTU gtu)
78      {
79          CarFollowingModel model = peekCarFollowingModel();
80          model.init(gtu);
81          this.peekedCarFollowingModel = null; // peek will create a new one
82          return model;
83      }
84  
85      /**
86       * Returns the parameters for the car-following model using the factory. This method should be used in the
87       * {@code getParameters()} method of implementing sub-classes.
88       * @return Parameters; parameters for the car-following model using the factory
89       * @throws ParameterException on illegal parameter value
90       */
91      protected final Parameters getCarFollowingParameters() throws ParameterException
92      {
93          return this.carFollowingModelFactory.getParameters();
94      }
95  
96      /**
97       * Returns a {@code String} representation of the car-following model factory. This method may be used in the
98       * {@code toString()} method of implementing sub-classes.
99       * @return String; representation of the car-following model factory
100      */
101     protected final String getCarFollowingModelFactoryString()
102     {
103         return this.carFollowingModelFactory.toString();
104     }
105 
106     /** {@inheritDoc} */
107     @Override
108     public final Speed peekDesiredSpeed(final GTUType gtuType, final Speed speedLimit, final Speed maxGtuSpeed,
109             final Parameters parameters) throws GTUException
110     {
111         try
112         {
113             SpeedLimitInfo sli = new SpeedLimitInfo();
114             sli.addSpeedInfo(SpeedLimitTypes.MAX_VEHICLE_SPEED, maxGtuSpeed);
115             sli.addSpeedInfo(SpeedLimitTypes.FIXED_SIGN, speedLimit);
116             return peekCarFollowingModel().desiredSpeed(parameters, sli);
117         }
118         catch (ParameterException exception)
119         {
120             throw new GTUException(exception);
121         }
122     }
123 
124     /** {@inheritDoc} */
125     @Override
126     public final Length peekDesiredHeadway(final GTUType gtuType, final Speed speed, final Parameters parameters)
127             throws GTUException
128     {
129         try
130         {
131             return peekCarFollowingModel().desiredHeadway(parameters, speed);
132         }
133         catch (ParameterException exception)
134         {
135             throw new GTUException(exception);
136         }
137     }
138     
139     /**
140      * Returns the perception factory.
141      * @return PerceptionFactory; perception factory
142      */
143     public PerceptionFactory getPerceptionFactory()
144     {
145         return this.perceptionFactory;
146     }
147 
148 }