View Javadoc
1   package org.opentrafficsim.road.gtu.strategical.route;
2   
3   import java.io.Serializable;
4   
5   import org.opentrafficsim.core.distributions.ProbabilityException;
6   import org.opentrafficsim.core.gtu.GTUException;
7   import org.opentrafficsim.core.gtu.behavioralcharacteristics.BehavioralCharacteristics;
8   import org.opentrafficsim.core.gtu.behavioralcharacteristics.BehavioralCharacteristicsFactory;
9   import org.opentrafficsim.core.gtu.behavioralcharacteristics.BehavioralCharacteristicsFactoryDefault;
10  import org.opentrafficsim.core.network.route.Route;
11  import org.opentrafficsim.core.network.route.RouteGenerator;
12  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
13  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedTacticalPlanner;
14  import org.opentrafficsim.road.gtu.lane.tactical.LaneBasedTacticalPlannerFactory;
15  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlanner;
16  import org.opentrafficsim.road.gtu.strategical.LaneBasedStrategicalPlannerFactory;
17  
18  /**
19   * Factory for creating {@code LaneBasedStrategicalRoutePlanner} using any {@code LaneBasedTacticalPlannerFactory}.
20   * <p>
21   * Copyright (c) 2013-2016 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
22   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
23   * <p>
24   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Aug 2, 2016 <br>
25   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
26   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
27   * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
28   */
29  
30  public class LaneBasedStrategicalRoutePlannerFactory
31          implements LaneBasedStrategicalPlannerFactory<LaneBasedStrategicalPlanner>, Serializable
32  {
33  
34      /** */
35      private static final long serialVersionUID = 20160811L;
36  
37      /** Route generator for the next strategical planner. */
38      private final RouteGenerator routeGenerator;
39  
40      /** Behavioral characteristics for the next strategical planner. */
41      private BehavioralCharacteristics behavioralCharacteristics;
42  
43      /** Factory for tactical planners. */
44      private final LaneBasedTacticalPlannerFactory<? extends LaneBasedTacticalPlanner> tacticalPlannerFactory;
45  
46      /** Characteristics factory. */
47      private final BehavioralCharacteristicsFactory behavioralCharacteristicsFactory;
48  
49      /**
50       * Constructor with factory for tactical planners.
51       * @param tacticalPlannerFactory factory for tactical planners
52       */
53      public LaneBasedStrategicalRoutePlannerFactory(
54              final LaneBasedTacticalPlannerFactory<? extends LaneBasedTacticalPlanner> tacticalPlannerFactory)
55      {
56          this.tacticalPlannerFactory = tacticalPlannerFactory;
57          this.routeGenerator = null;
58          this.behavioralCharacteristicsFactory = new BehavioralCharacteristicsFactoryDefault();
59      }
60  
61      /**
62       * Constructor with factory for tactical planners.
63       * @param tacticalPlannerFactory factory for tactical planners
64       * @param behavioralCharacteristicsFactory factory for behavioral characteristics
65       */
66      public LaneBasedStrategicalRoutePlannerFactory(
67              final LaneBasedTacticalPlannerFactory<? extends LaneBasedTacticalPlanner> tacticalPlannerFactory,
68              final BehavioralCharacteristicsFactory behavioralCharacteristicsFactory)
69      {
70          this.tacticalPlannerFactory = tacticalPlannerFactory;
71          this.routeGenerator = null;
72          this.behavioralCharacteristicsFactory = behavioralCharacteristicsFactory;
73      }
74  
75      /**
76       * Constructor with factory for tactical planners and route generator.
77       * @param tacticalPlannerFactory factory for tactical planners
78       * @param routeGenerator route generator
79       */
80      public LaneBasedStrategicalRoutePlannerFactory(
81              final LaneBasedTacticalPlannerFactory<? extends LaneBasedTacticalPlanner> tacticalPlannerFactory,
82              final RouteGenerator routeGenerator)
83      {
84          this.tacticalPlannerFactory = tacticalPlannerFactory;
85          this.routeGenerator = routeGenerator;
86          this.behavioralCharacteristicsFactory = new BehavioralCharacteristicsFactoryDefault();
87      }
88  
89      /**
90       * Constructor with factory for tactical planners and route generator.
91       * @param tacticalPlannerFactory factory for tactical planners
92       * @param routeGenerator route generator
93       * @param behavioralCharacteristicsFactory factory for behavioral characteristics
94       */
95      public LaneBasedStrategicalRoutePlannerFactory(
96              final LaneBasedTacticalPlannerFactory<? extends LaneBasedTacticalPlanner> tacticalPlannerFactory,
97              final RouteGenerator routeGenerator, final BehavioralCharacteristicsFactory behavioralCharacteristicsFactory)
98      {
99          this.tacticalPlannerFactory = tacticalPlannerFactory;
100         this.routeGenerator = routeGenerator;
101         this.behavioralCharacteristicsFactory = behavioralCharacteristicsFactory;
102     }
103 
104     /** {@inheritDoc} */
105     @Override
106     public final BehavioralCharacteristics getDefaultBehavioralCharacteristics()
107     {
108         return this.tacticalPlannerFactory.getDefaultBehavioralCharacteristics();
109     }
110 
111     /** {@inheritDoc} */
112     @Override
113     public final void setBehavioralCharacteristics(final BehavioralCharacteristics behavioralCharacteristics)
114     {
115         this.behavioralCharacteristics = behavioralCharacteristics;
116     }
117 
118     /** {@inheritDoc} */
119     @Override
120     public final LaneBasedStrategicalPlanner create(final LaneBasedGTU gtu) throws GTUException
121     {
122         if (this.behavioralCharacteristics == null)
123         {
124             this.behavioralCharacteristics = this.getDefaultBehavioralCharacteristics();
125             this.behavioralCharacteristicsFactory.setValues(this.behavioralCharacteristics, gtu.getGTUType());
126         }
127         Route route = null;
128         if (this.routeGenerator != null)
129         {
130             try
131             {
132                 route = this.routeGenerator.draw();
133             }
134             catch (ProbabilityException exception)
135             {
136                 throw new GTUException(exception);
137             }
138         }
139         LaneBasedStrategicalRoutePlanner strategicalPlanner = new LaneBasedStrategicalRoutePlanner(
140                 this.behavioralCharacteristics, this.tacticalPlannerFactory.create(gtu), route, gtu);
141 
142         this.behavioralCharacteristics = null;
143         return strategicalPlanner;
144     }
145 
146     /** {@inheritDoc} */
147     @Override
148     public final String toString()
149     {
150         return "LaneBasedStrategicalRoutePlannerFactory [tacticalPlannerFactory=" + this.tacticalPlannerFactory + "]";
151     }
152 
153 }