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-2019 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,
52              final PerceptionFactory perceptionFactory)
53      {
54          this.carFollowingModelFactory = carFollowingModelFactory;
55          this.perceptionFactory = perceptionFactory;
56      }
57  
58      /**
59       * Returns the next car following model, which will be a fixed peeked instance until {@code nextCarFollowingModel()} is
60       * called.
61       * @return CarFollowingModel; next car following model
62       */
63      private CarFollowingModel peekCarFollowingModel()
64      {
65          if (this.peekedCarFollowingModel != null)
66          {
67              return this.peekedCarFollowingModel;
68          }
69          this.peekedCarFollowingModel = this.carFollowingModelFactory.generateCarFollowingModel();
70          return this.peekedCarFollowingModel;
71      }
72  
73      /**
74       * Returns the next car following model.
75       * @param gtu LaneBasedGTU; gtu
76       * @return CarFollowingModel; next car following model
77       */
78      protected final CarFollowingModel nextCarFollowingModel(final LaneBasedGTU gtu)
79      {
80          CarFollowingModel model = peekCarFollowingModel();
81          model.init(gtu);
82          this.peekedCarFollowingModel = null; // peek will create a new one
83          return model;
84      }
85  
86      /**
87       * Returns the parameters for the car-following model using the factory. This method should be used in the
88       * {@code getParameters()} method of implementing sub-classes.
89       * @return Parameters; parameters for the car-following model using the factory
90       * @throws ParameterException on illegal parameter value
91       */
92      protected final Parameters getCarFollowingParameters() throws ParameterException
93      {
94          return this.carFollowingModelFactory.getParameters();
95      }
96  
97      /**
98       * Returns a {@code String} representation of the car-following model factory. This method may be used in the
99       * {@code toString()} method of implementing sub-classes.
100      * @return String; representation of the car-following model factory
101      */
102     protected final String getCarFollowingModelFactoryString()
103     {
104         return this.carFollowingModelFactory.toString();
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     public final Speed peekDesiredSpeed(final GTUType gtuType, final Speed speedLimit, final Speed maxGtuSpeed,
110             final Parameters parameters) throws GTUException
111     {
112         try
113         {
114             SpeedLimitInfo sli = new SpeedLimitInfo();
115             sli.addSpeedInfo(SpeedLimitTypes.MAX_VEHICLE_SPEED, maxGtuSpeed);
116             sli.addSpeedInfo(SpeedLimitTypes.FIXED_SIGN, speedLimit);
117             return peekCarFollowingModel().desiredSpeed(parameters, sli);
118         }
119         catch (ParameterException exception)
120         {
121             throw new GTUException(exception);
122         }
123     }
124 
125     /** {@inheritDoc} */
126     @Override
127     public final Length peekDesiredHeadway(final GTUType gtuType, final Speed speed, final Parameters parameters)
128             throws GTUException
129     {
130         try
131         {
132             return peekCarFollowingModel().desiredHeadway(parameters, speed);
133         }
134         catch (ParameterException exception)
135         {
136             throw new GTUException(exception);
137         }
138     }
139 
140     /**
141      * Returns the perception factory.
142      * @return PerceptionFactory; perception factory
143      */
144     public PerceptionFactory getPerceptionFactory()
145     {
146         return this.perceptionFactory;
147     }
148 
149 }