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://github.com/peter-knoppers">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 factory for tactical planners
51       */
52      public LaneBasedStrategicalRoutePlannerFactory(
53              final LaneBasedTacticalPlannerFactory<? extends LaneBasedTacticalPlanner> tacticalPlannerFactory)
54      {
55          this.tacticalPlannerFactory = tacticalPlannerFactory;
56          this.routeGenerator = RouteGenerator.NULL;
57          this.parameterFactory = new ParameterFactoryDefault();
58      }
59  
60      /**
61       * Constructor with factory for tactical planners.
62       * @param tacticalPlannerFactory factory for tactical planners
63       * @param parametersFactory factory for parameters
64       */
65      public LaneBasedStrategicalRoutePlannerFactory(
66              final LaneBasedTacticalPlannerFactory<? extends LaneBasedTacticalPlanner> tacticalPlannerFactory,
67              final ParameterFactory parametersFactory)
68      {
69          this.tacticalPlannerFactory = tacticalPlannerFactory;
70          this.routeGenerator = RouteGenerator.NULL;
71          this.parameterFactory = parametersFactory;
72      }
73  
74      /**
75       * Constructor with factory for tactical planners.
76       * @param tacticalPlannerFactory factory for tactical planners
77       * @param parametersFactory factory for parameters
78       * @param routeGenerator route supplier
79       */
80      public LaneBasedStrategicalRoutePlannerFactory(
81              final LaneBasedTacticalPlannerFactory<? extends LaneBasedTacticalPlanner> tacticalPlannerFactory,
82              final ParameterFactory parametersFactory, final RouteGenerator routeGenerator)
83      {
84          this.tacticalPlannerFactory = tacticalPlannerFactory;
85          this.parameterFactory = parametersFactory;
86          this.routeGenerator = routeGenerator;
87      }
88  
89      @Override
90      public final Speed peekDesiredSpeed(final GtuType gtuType, final Speed speedLimit, final Speed maxGtuSpeed)
91              throws GtuException
92      {
93          return this.tacticalPlannerFactory.peekDesiredSpeed(gtuType, speedLimit, maxGtuSpeed, peekParameters(gtuType));
94      }
95  
96      @Override
97      public final Length peekDesiredHeadway(final GtuType gtuType, final Speed speed) throws GtuException
98      {
99          return this.tacticalPlannerFactory.peekDesiredHeadway(gtuType, speed, peekParameters(gtuType));
100     }
101 
102     /**
103      * Determine or return the next parameter set.
104      * @param gtuType GTU type to generate parameters for
105      * @return next parameter set
106      * @throws GtuException on parameter exception
107      */
108     private Parameters peekParameters(final GtuType gtuType) throws GtuException
109     {
110         if (this.peekedParameters != null)
111         {
112             return this.peekedParameters;
113         }
114         try
115         {
116             this.peekedParameters = this.tacticalPlannerFactory.getParameters();
117             this.parameterFactory.setValues(this.peekedParameters, gtuType);
118         }
119         catch (ParameterException exception)
120         {
121             throw new GtuException("Parameter was set to illegal value.", exception);
122         }
123         return this.peekedParameters;
124     }
125 
126     @Override
127     public final LaneBasedStrategicalRoutePlanner create(final LaneBasedGtu gtu, final Route route, final Node origin,
128             final Node destination) throws GtuException
129     {
130         LaneBasedStrategicalRoutePlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(
131                 this.tacticalPlannerFactory.create(gtu), route, gtu, origin, destination, this.routeGenerator);
132         gtu.setParameters(nextParameters(gtu.getType()));
133         return strategicalPlanner;
134     }
135 
136     /**
137      * Returns the parameters for the next GTU.
138      * @param gtuType GTU type of GTU to be generated
139      * @return parameters for the next GTU
140      * @throws GtuException on parameter exception
141      */
142     protected final Parameters nextParameters(final GtuType gtuType) throws GtuException
143     {
144         Parameters parameters = peekParameters(gtuType);
145         this.peekedParameters = null;
146         return parameters;
147     }
148 
149     @Override
150     public final String toString()
151     {
152         return "LaneBasedStrategicalRoutePlannerFactory [tacticalPlannerFactory=" + this.tacticalPlannerFactory + "]";
153     }
154 
155 }