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