View Javadoc
1   package org.opentrafficsim.road.gtu.strategical;
2   
3   import java.io.Serializable;
4   
5   import org.djunits.value.vdouble.scalar.Length;
6   import org.djunits.value.vdouble.scalar.Speed;
7   import org.opentrafficsim.base.parameters.ParameterException;
8   import org.opentrafficsim.base.parameters.Parameters;
9   import org.opentrafficsim.core.gtu.GtuException;
10  import org.opentrafficsim.core.gtu.GtuType;
11  import org.opentrafficsim.core.network.Node;
12  import org.opentrafficsim.core.network.route.Route;
13  import org.opentrafficsim.core.parameters.ParameterFactory;
14  import org.opentrafficsim.core.parameters.ParameterFactoryDefault;
15  import org.opentrafficsim.road.gtu.lane.LaneBasedGtu;
16  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedTacticalPlanner;
17  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedTacticalPlannerFactory;
18  
19  /**
20   * Factory for creating {@code LaneBasedStrategicalRoutePlanner} using any {@code LaneBasedTacticalPlannerFactory}.
21   * <p>
22   * Copyright (c) 2013-2024 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
23   * BSD-style license. See <a href="https://opentrafficsim.org/docs/license.html">OpenTrafficSim License</a>.
24   * </p>
25   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
26   * @author <a href="https://tudelft.nl/staff/p.knoppers-1">Peter Knoppers</a>
27   * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
28   */
29  public class LaneBasedStrategicalRoutePlannerFactory
30          implements LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalRoutePlanner>, Serializable
31  {
32  
33      /** */
34      private static final long serialVersionUID = 20160811L;
35  
36      /** Factory for tactical planners. */
37      private final LaneBasedTacticalPlannerFactory<? extends LaneBasedTacticalPlanner> tacticalPlannerFactory;
38  
39      /** Route supplier. */
40      private final RouteGenerator routeGenerator;
41  
42      /** Parameter factory. */
43      private final ParameterFactory parameterFactory;
44  
45      /** Peeked parameters. */
46      private Parameters peekedParameters = null;
47  
48      /**
49       * Constructor with factory for tactical planners.
50       * @param tacticalPlannerFactory LaneBasedTacticalPlannerFactory&lt;? extends LaneBasedTacticalPlanner&gt;; factory for
51       *            tactical planners
52       */
53      public LaneBasedStrategicalRoutePlannerFactory(
54              final LaneBasedTacticalPlannerFactory<? extends LaneBasedTacticalPlanner> tacticalPlannerFactory)
55      {
56          this.tacticalPlannerFactory = tacticalPlannerFactory;
57          this.routeGenerator = RouteGenerator.NULL;
58          this.parameterFactory = new ParameterFactoryDefault();
59      }
60  
61      /**
62       * Constructor with factory for tactical planners.
63       * @param tacticalPlannerFactory LaneBasedTacticalPlannerFactory&lt;? extends LaneBasedTacticalPlanner&gt;; factory for
64       *            tactical planners
65       * @param parametersFactory ParameterFactory; factory for parameters
66       */
67      public LaneBasedStrategicalRoutePlannerFactory(
68              final LaneBasedTacticalPlannerFactory<? extends LaneBasedTacticalPlanner> tacticalPlannerFactory,
69              final ParameterFactory parametersFactory)
70      {
71          this.tacticalPlannerFactory = tacticalPlannerFactory;
72          this.routeGenerator = RouteGenerator.NULL;
73          this.parameterFactory = parametersFactory;
74      }
75  
76      /**
77       * Constructor with factory for tactical planners.
78       * @param tacticalPlannerFactory LaneBasedTacticalPlannerFactory&lt;? extends LaneBasedTacticalPlanner&gt;; factory for
79       *            tactical planners
80       * @param parametersFactory ParameterFactory; factory for parameters
81       * @param routeGenerator RouteGenerator; route supplier
82       */
83      public LaneBasedStrategicalRoutePlannerFactory(
84              final LaneBasedTacticalPlannerFactory<? extends LaneBasedTacticalPlanner> tacticalPlannerFactory,
85              final ParameterFactory parametersFactory, final RouteGenerator routeGenerator)
86      {
87          this.tacticalPlannerFactory = tacticalPlannerFactory;
88          this.parameterFactory = parametersFactory;
89          this.routeGenerator = routeGenerator;
90      }
91  
92      /** {@inheritDoc} */
93      @Override
94      public final Speed peekDesiredSpeed(final GtuType gtuType, final Speed speedLimit, final Speed maxGtuSpeed)
95              throws GtuException
96      {
97          return this.tacticalPlannerFactory.peekDesiredSpeed(gtuType, speedLimit, maxGtuSpeed, peekParameters(gtuType));
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     public final Length peekDesiredHeadway(final GtuType gtuType, final Speed speed) throws GtuException
103     {
104         return this.tacticalPlannerFactory.peekDesiredHeadway(gtuType, speed, peekParameters(gtuType));
105     }
106 
107     /**
108      * Determine or return the next parameter set.
109      * @param gtuType GtuType; GTU type to generate parameters for
110      * @return Parameters; next parameter set
111      * @throws GtuException on parameter exception
112      */
113     private Parameters peekParameters(final GtuType gtuType) throws GtuException
114     {
115         if (this.peekedParameters != null)
116         {
117             return this.peekedParameters;
118         }
119         try
120         {
121             this.peekedParameters = this.tacticalPlannerFactory.getParameters();
122             this.parameterFactory.setValues(this.peekedParameters, gtuType);
123         }
124         catch (ParameterException exception)
125         {
126             throw new GtuException("Parameter was set to illegal value.", exception);
127         }
128         return this.peekedParameters;
129     }
130 
131     /** {@inheritDoc} */
132     @Override
133     public final LaneBasedStrategicalRoutePlanner create(final LaneBasedGtu gtu, final Route route, final Node origin,
134             final Node destination) throws GtuException
135     {
136         LaneBasedStrategicalRoutePlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(
137                 this.tacticalPlannerFactory.create(gtu), route, gtu, origin, destination, this.routeGenerator);
138         gtu.setParameters(nextParameters(gtu.getType()));
139         return strategicalPlanner;
140     }
141 
142     /**
143      * Returns the parameters for the next GTU.
144      * @param gtuType GtuType; GTU type of GTU to be generated
145      * @return Parameters; parameters for the next GTU
146      * @throws GtuException on parameter exception
147      */
148     protected final Parameters nextParameters(final GtuType gtuType) throws GtuException
149     {
150         Parameters parameters = peekParameters(gtuType);
151         this.peekedParameters = null;
152         return parameters;
153     }
154 
155     /** {@inheritDoc} */
156     @Override
157     public final String toString()
158     {
159         return "LaneBasedStrategicalRoutePlannerFactory [tacticalPlannerFactory=" + this.tacticalPlannerFactory + "]";
160     }
161 
162 }