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  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  public abstract class AbstractLaneBasedTacticalPlannerFactory<T extends LaneBasedTacticalPlanner>
33          implements LaneBasedTacticalPlannerFactory<T>
34  {
35  
36      
37      private final CarFollowingModelFactory<? extends CarFollowingModel> carFollowingModelFactory;
38  
39      
40      private CarFollowingModel peekedCarFollowingModel = null;
41  
42      
43      private final PerceptionFactory perceptionFactory;
44  
45      
46  
47  
48  
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  
60  
61  
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  
75  
76  
77  
78      protected final CarFollowingModel nextCarFollowingModel(final LaneBasedGTU gtu)
79      {
80          CarFollowingModel model = peekCarFollowingModel();
81          model.init(gtu);
82          this.peekedCarFollowingModel = null; 
83          return model;
84      }
85  
86      
87  
88  
89  
90  
91  
92      protected final Parameters getCarFollowingParameters() throws ParameterException
93      {
94          return this.carFollowingModelFactory.getParameters();
95      }
96  
97      
98  
99  
100 
101 
102     protected final String getCarFollowingModelFactoryString()
103     {
104         return this.carFollowingModelFactory.toString();
105     }
106 
107     
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     
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 
142 
143 
144     public PerceptionFactory getPerceptionFactory()
145     {
146         return this.perceptionFactory;
147     }
148 
149 }