View Javadoc
1   package org.opentrafficsim.road.gtu.generator;
2   
3   import java.io.Serializable;
4   import java.rmi.RemoteException;
5   import java.util.Collections;
6   import java.util.LinkedHashMap;
7   import java.util.LinkedHashSet;
8   import java.util.LinkedList;
9   import java.util.Map;
10  import java.util.Queue;
11  import java.util.Set;
12  import java.util.SortedSet;
13  import java.util.TreeSet;
14  
15  import javax.naming.NamingException;
16  
17  import org.djunits.unit.DurationUnit;
18  import org.djunits.value.vdouble.scalar.Duration;
19  import org.djunits.value.vdouble.scalar.Length;
20  import org.djunits.value.vdouble.scalar.Speed;
21  import org.djunits.value.vdouble.scalar.Time;
22  import org.djutils.event.EventProducer;
23  import org.djutils.event.EventType;
24  import org.djutils.exceptions.Throw;
25  import org.djutils.immutablecollections.ImmutableMap;
26  import org.opentrafficsim.base.Identifiable;
27  import org.opentrafficsim.base.TimeStampedObject;
28  import org.opentrafficsim.base.parameters.ParameterException;
29  import org.opentrafficsim.core.distributions.Generator;
30  import org.opentrafficsim.core.distributions.ProbabilityException;
31  import org.opentrafficsim.core.dsol.OTSSimulatorInterface;
32  import org.opentrafficsim.core.geometry.Bounds;
33  import org.opentrafficsim.core.geometry.DirectedPoint;
34  import org.opentrafficsim.core.geometry.OTSGeometryException;
35  import org.opentrafficsim.core.gtu.GTUDirectionality;
36  import org.opentrafficsim.core.gtu.GTUErrorHandler;
37  import org.opentrafficsim.core.gtu.GTUException;
38  import org.opentrafficsim.core.gtu.GTUType;
39  import org.opentrafficsim.core.gtu.RelativePosition;
40  import org.opentrafficsim.core.idgenerator.IdGenerator;
41  import org.opentrafficsim.core.network.NetworkException;
42  import org.opentrafficsim.road.gtu.generator.GeneratorPositions.GeneratorLanePosition;
43  import org.opentrafficsim.road.gtu.generator.GeneratorPositions.GeneratorLinkPosition;
44  import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGTUCharacteristics;
45  import org.opentrafficsim.road.gtu.generator.characteristics.LaneBasedGTUCharacteristicsGenerator;
46  import org.opentrafficsim.road.gtu.lane.LaneBasedGTU;
47  import org.opentrafficsim.road.gtu.lane.LaneBasedIndividualGTU;
48  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTU;
49  import org.opentrafficsim.road.gtu.lane.perception.headway.HeadwayGTUReal;
50  import org.opentrafficsim.road.network.OTSRoadNetwork;
51  import org.opentrafficsim.road.network.lane.CrossSectionLink;
52  import org.opentrafficsim.road.network.lane.DirectedLanePosition;
53  import org.opentrafficsim.road.network.lane.Lane;
54  import org.opentrafficsim.road.network.lane.LaneDirection;
55  
56  import nl.tudelft.simulation.dsol.SimRuntimeException;
57  
58  /**
59   * Lane based GTU generator. This generator generates lane based GTUs using a LaneBasedTemplateGTUType. The template is used to
60   * generate a set of GTU characteristics at the times implied by the headway generator. These sets are queued until there is
61   * sufficient room to construct a GTU at the specified lane locations. The speed of a construction GTU may be reduced to ensure
62   * it does not run into its immediate leader GTU.
63   * <p>
64   * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
65   * BSD-style license. See <a href="http://opentrafficsim.org/docs/current/license.html">OpenTrafficSim License</a>.
66   * <p>
67   * @version $Revision$, $LastChangedDate$, by $Author$, initial version Mar 8, 2016 <br>
68   * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
69   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
70   */
71  public class LaneBasedGTUGenerator extends EventProducer implements Serializable, Identifiable, GtuGeneratorQueue
72  {
73      /** */
74      private static final long serialVersionUID = 20160000L;
75  
76      /**
77       * Event of a generated GTU. Payload: LaneBasedIndividualGTU
78       */
79      public static final EventType GTU_GENERATED_EVENT = new EventType("GENERATOR.GTU_GENERATED");
80  
81      /** FIFO for templates that have not been generated yet due to insufficient room/headway, per position, and per link. */
82      private final Map<CrossSectionLink,
83              Map<GeneratorLanePosition, Queue<TimeStampedObject<LaneBasedGTUCharacteristics>>>> unplacedTemplates =
84                      new LinkedHashMap<>();
85  
86      /** Name of the GTU generator. */
87      private final String id;
88  
89      /** Time distribution that determines the interval times between GTUs. */
90      private final Generator<Duration> interarrivelTimeGenerator;
91  
92      /** Generates most properties of the GTUs. */
93      private final LaneBasedGTUCharacteristicsGenerator laneBasedGTUCharacteristicsGenerator;
94  
95      /** Total number of GTUs generated so far. */
96      private long generatedGTUs = 0;
97  
98      /** Retry interval for checking if a GTU can be placed. */
99      private Duration reTryInterval = new Duration(0.1, DurationUnit.SI);
100 
101     /** Location and initial direction provider for all generated GTUs. */
102     private final GeneratorPositions generatorPositions;
103 
104     /** Network. */
105     private final OTSRoadNetwork network;
106 
107     /** Simulator. */
108     private final OTSSimulatorInterface simulator;
109 
110     /** The way that this generator checks if it is safe to construct and place the next lane based GTU. */
111     private final RoomChecker roomChecker;
112 
113     /** ID generator. */
114     private final IdGenerator idGenerator;
115 
116     /** Initial distance over which lane changes shouldn't be performed. */
117     private Length noLaneChangeDistance = null;
118 
119     /** Whether GTUs change lane instantaneously. */
120     private boolean instantaneousLaneChange = false;
121 
122     /** GTU error handler. */
123     private GTUErrorHandler errorHandler = GTUErrorHandler.THROW;
124 
125     /** Vehicle generation is ignored on these lanes. */
126     private Set<LaneDirection> disabled = new LinkedHashSet<>();
127 
128     /**
129      * Construct a new lane base GTU generator.
130      * @param id String; name of the new GTU generator
131      * @param interarrivelTimeGenerator Generator&lt;Duration&gt;; generator for the interval times between GTUs
132      * @param laneBasedGTUCharacteristicsGenerator LaneBasedGTUCharacteristicsGenerator; generator of the characteristics of
133      *            each GTU
134      * @param generatorPositions GeneratorPositions; location and initial direction provider for all generated GTUs
135      * @param network OTSRoadNetwork; the OTS network that owns the generated GTUs
136      * @param simulator OTSSimulatorInterface; simulator
137      * @param roomChecker RoomChecker; the way that this generator checks that there is sufficient room to place a new GTU
138      * @param idGenerator IdGenerator; id generator
139      * @throws SimRuntimeException when <cite>startTime</cite> lies before the current simulation time
140      * @throws ProbabilityException pe
141      * @throws ParameterException if drawing from the interarrival generator fails
142      */
143     @SuppressWarnings("parameternumber")
144     public LaneBasedGTUGenerator(final String id, final Generator<Duration> interarrivelTimeGenerator,
145             final LaneBasedGTUCharacteristicsGenerator laneBasedGTUCharacteristicsGenerator,
146             final GeneratorPositions generatorPositions, final OTSRoadNetwork network, final OTSSimulatorInterface simulator,
147             final RoomChecker roomChecker, final IdGenerator idGenerator)
148             throws SimRuntimeException, ProbabilityException, ParameterException
149     {
150         this.id = id;
151         this.interarrivelTimeGenerator = interarrivelTimeGenerator;
152         this.laneBasedGTUCharacteristicsGenerator = laneBasedGTUCharacteristicsGenerator;
153         this.generatorPositions = generatorPositions;
154         this.network = network;
155         this.simulator = simulator;
156         this.roomChecker = roomChecker;
157         this.idGenerator = idGenerator;
158         Duration headway = this.interarrivelTimeGenerator.draw();
159         if (headway != null) // otherwise no demand at all
160         {
161             simulator.scheduleEventRel(headway, this, this, "generateCharacteristics", new Object[] {});
162         }
163     }
164 
165     /**
166      * Sets the initial distance over which lane changes shouldn't be performed.
167      * @param noLaneChangeDistance Length; initial distance over which lane changes shouldn't be performed
168      */
169     public void setNoLaneChangeDistance(final Length noLaneChangeDistance)
170     {
171         this.noLaneChangeDistance = noLaneChangeDistance;
172     }
173 
174     /**
175      * Sets whether GTUs will change lane instantaneously.
176      * @param instantaneous boolean; whether GTUs will change lane instantaneously
177      */
178     public void setInstantaneousLaneChange(final boolean instantaneous)
179     {
180         this.instantaneousLaneChange = instantaneous;
181     }
182 
183     /**
184      * Sets the GTU error handler.
185      * @param gtuErrorHandler GTUErrorHandler; GTU error handler
186      */
187     public void setErrorHandler(final GTUErrorHandler gtuErrorHandler)
188     {
189         this.errorHandler = gtuErrorHandler;
190     }
191 
192     /**
193      * Generate the characteristics of the next GTU.
194      * @throws ProbabilityException when something is wrongly defined in the LaneBasedTemplateGTUType
195      * @throws SimRuntimeException when this method fails to re-schedule itself or the call to the method that tries to place a
196      *             GTU on the road
197      * @throws ParameterException in case of a parameter problem
198      * @throws GTUException if strategical planner cannot generate a plan
199      */
200     @SuppressWarnings("unused")
201     private void generateCharacteristics() throws ProbabilityException, SimRuntimeException, ParameterException, GTUException
202     {
203         synchronized (this.unplacedTemplates)
204         {
205             LaneBasedGTUCharacteristics characteristics = this.laneBasedGTUCharacteristicsGenerator.draw();
206             GTUType gtuType = characteristics.getGTUType();
207             // gather information on number of unplaced templates per lane, and per link, for the drawing of a new position
208             Map<CrossSectionLink, Map<Integer, Integer>> unplaced = new LinkedHashMap<>();
209             for (CrossSectionLink link : this.unplacedTemplates.keySet())
210             {
211                 Map<Integer, Integer> linkMap = new LinkedHashMap<>();
212                 Map<GeneratorLanePosition, Queue<TimeStampedObject<LaneBasedGTUCharacteristics>>> linkTemplates =
213                         this.unplacedTemplates.get(link);
214                 for (GeneratorLanePosition lanePosition : linkTemplates.keySet())
215                 {
216                     linkMap.put(lanePosition.getLaneNumber(), linkTemplates.get(lanePosition).size());
217                 }
218                 unplaced.put(link, linkMap);
219             }
220             // position draw
221             GeneratorLinkPosition linkPosition =
222                     this.generatorPositions.draw(gtuType, characteristics.getDestination(), characteristics.getRoute());
223             Speed desiredSpeed = characteristics.getStrategicalPlannerFactory().peekDesiredSpeed(gtuType,
224                     linkPosition.speedLimit(gtuType), characteristics.getMaximumSpeed());
225             GeneratorLanePosition lanePosition = linkPosition.draw(gtuType, unplaced.get(linkPosition.getLink()), desiredSpeed);
226 
227             // skip if disabled at this lane-direction
228             Set<LaneDirection> lanes = new LinkedHashSet<>();
229             for (DirectedLanePosition pos : lanePosition.getPosition())
230             {
231                 lanes.add(pos.getLaneDirection());
232             }
233             if (Collections.disjoint(this.disabled, lanes))
234             {
235                 queueGtu(lanePosition, characteristics);
236             }
237 
238         }
239         Duration headway = this.interarrivelTimeGenerator.draw();
240         if (headway != null)
241         {
242             this.simulator.scheduleEventRel(headway, this, this, "generateCharacteristics", new Object[] {});
243         }
244     }
245 
246     /**
247      * Check if the queue is non-empty and, if it is, try to place the GTUs in the queue on the road.
248      * @param position GeneratorLanePosition; position
249      * @throws SimRuntimeException should never happen
250      * @throws GTUException when something wrong in the definition of the GTU
251      * @throws OTSGeometryException when something is wrong in the definition of the GTU
252      * @throws NetworkException when something is wrong with the initial location of the GTU
253      * @throws NamingException ???
254      * @throws ProbabilityException pe
255      */
256     @SuppressWarnings("unused")
257     private void tryToPlaceGTU(final GeneratorLanePosition position) throws SimRuntimeException, GTUException, NamingException,
258             NetworkException, OTSGeometryException, ProbabilityException
259     {
260         TimeStampedObject<LaneBasedGTUCharacteristics> timedCharacteristics;
261         Queue<TimeStampedObject<LaneBasedGTUCharacteristics>> queue =
262                 this.unplacedTemplates.get(position.getLink()).get(position);
263 
264         synchronized (queue)
265         {
266             timedCharacteristics = queue.peek();
267         }
268         if (null == timedCharacteristics)
269         {
270             return; // Do not re-schedule this method
271         }
272 
273         LaneBasedGTUCharacteristics characteristics = timedCharacteristics.getObject();
274         SortedSet<HeadwayGTU> leaders = new TreeSet<>();
275         for (DirectedLanePosition dirPos : position.getPosition())
276         {
277             getFirstLeaders(dirPos.getLaneDirection(), dirPos.getPosition().neg().minus(characteristics.getFront()),
278                     dirPos.getPosition(), leaders);
279         }
280         Duration since = this.simulator.getSimulatorAbsTime().minus(timedCharacteristics.getTimestamp());
281         Placement placement = this.roomChecker.canPlace(leaders, characteristics, since, position.getPosition());
282         if (placement.canPlace())
283         {
284             // There is enough room; remove the template from the queue and construct the new GTU
285             synchronized (queue)
286             {
287                 queue.remove();
288             }
289             placeGtu(characteristics, placement.getPosition(), placement.getSpeed());
290             if (queue.size() > 0)
291             {
292                 this.simulator.scheduleEventNow(this, this, "tryToPlaceGTU", new Object[] {position});
293             }
294         }
295         else if (queue.size() > 0)
296         {
297             this.simulator.scheduleEventRel(this.reTryInterval, this, this, "tryToPlaceGTU", new Object[] {position});
298         }
299     }
300 
301     /**
302      * Adds a GTU to the generation queue. This method ignores whether vehicle generation is enabled at the location. This
303      * allows an external party to govern (over some time) what vehicles are generated.
304      * @param characteristics LaneBasedGTUCharacteristics; characteristics of GTU to add to the queue
305      * @param position Set&lt;LaneDirection&gt;; position to generate the GTU at
306      */
307     public final void queueGtu(final LaneBasedGTUCharacteristics characteristics, final Set<LaneDirection> position)
308     {
309         // first find the correct GeneratorLanePosition
310         GeneratorLanePosition genPosition = null;
311         Set<LaneDirection> genSet = new LinkedHashSet<>();
312         for (GeneratorLanePosition lanePosition : this.generatorPositions.getAllPositions())
313         {
314             for (DirectedLanePosition dirPos : lanePosition.getPosition())
315             {
316                 genSet.add(dirPos.getLaneDirection());
317             }
318             if (genSet.equals(position))
319             {
320                 genPosition = lanePosition;
321                 break;
322             }
323             genSet.clear();
324         }
325         Throw.when(genPosition == null, IllegalStateException.class, "Position %s is not part of the generation.", position);
326         try
327         {
328             queueGtu(genPosition, characteristics);
329         }
330         catch (SimRuntimeException exception)
331         {
332             throw new RuntimeException("Unexpected exception while scheduling tryToPlace event.", exception);
333         }
334     }
335 
336     /**
337      * Places the characteristics in the queue pertaining to the position, and schedules a call to {@code tryToPlace} now if the
338      * queue length is 1.
339      * @param lanePosition GeneratorLanePosition; position to generate the GTU at
340      * @param characteristics LaneBasedGTUCharacteristics; characteristics of GTU to add to the queue
341      * @throws SimRuntimeException when an event is scheduled in the past
342      */
343     private void queueGtu(final GeneratorLanePosition lanePosition, final LaneBasedGTUCharacteristics characteristics)
344             throws SimRuntimeException
345     {
346         if (!this.unplacedTemplates.containsKey(lanePosition.getLink()))
347         {
348             this.unplacedTemplates.put(lanePosition.getLink(), new LinkedHashMap<>());
349         }
350         Map<GeneratorLanePosition, Queue<TimeStampedObject<LaneBasedGTUCharacteristics>>> linkMap =
351                 this.unplacedTemplates.get(lanePosition.getLink());
352         if (!linkMap.containsKey(lanePosition))
353         {
354             linkMap.put(lanePosition, new LinkedList<>());
355         }
356         Queue<TimeStampedObject<LaneBasedGTUCharacteristics>> queue = linkMap.get(lanePosition);
357         queue.add(new TimeStampedObject<>(characteristics, this.simulator.getSimulatorAbsTime()));
358         if (queue.size() == 1)
359         {
360             this.simulator.scheduleEventNow(this, this, "tryToPlaceGTU", new Object[] {lanePosition});
361         }
362     }
363 
364     /**
365      * Places a GTU, regardless of whether it has room. The user of this method should verify this is the case.
366      * @param characteristics LaneBasedGTUCharacteristics; characteristics
367      * @param position Set&lt;DirectedLanePosition&gt;; position
368      * @param speed Speed; speed
369      * @throws NamingException on exception
370      * @throws GTUException on exception
371      * @throws NetworkException on exception
372      * @throws SimRuntimeException on exception
373      * @throws OTSGeometryException on exception
374      */
375     public final void placeGtu(final LaneBasedGTUCharacteristics characteristics, final Set<DirectedLanePosition> position,
376             final Speed speed) throws NamingException, GTUException, NetworkException, SimRuntimeException, OTSGeometryException
377     {
378         String gtuId = this.idGenerator.nextId();
379         LaneBasedIndividualGTU gtu = new LaneBasedIndividualGTU(gtuId, characteristics.getGTUType(),
380                 characteristics.getLength(), characteristics.getWidth(), characteristics.getMaximumSpeed(),
381                 characteristics.getFront(), this.simulator, this.network);
382         gtu.setMaximumAcceleration(characteristics.getMaximumAcceleration());
383         gtu.setMaximumDeceleration(characteristics.getMaximumDeceleration());
384         gtu.setVehicleModel(characteristics.getVehicleModel());
385         gtu.setNoLaneChangeDistance(this.noLaneChangeDistance);
386         gtu.setInstantaneousLaneChange(this.instantaneousLaneChange);
387         gtu.setErrorHandler(this.errorHandler);
388         gtu.init(characteristics.getStrategicalPlannerFactory().create(gtu, characteristics.getRoute(),
389                 characteristics.getOrigin(), characteristics.getDestination()), position, speed);
390         this.generatedGTUs++;
391         fireEvent(GTU_GENERATED_EVENT, gtu);
392     }
393 
394     /**
395      * Adds the first GTU on the lane to the set, or any number or leaders on downstream lane(s) if there is no GTU on the lane.
396      * @param lane LaneDirection; lane to search on
397      * @param startDistance Length; distance from generator location (nose) to start of the lane
398      * @param beyond Length; location to search downstream of which is the generator position, or the start for downstream lanes
399      * @param set Set&lt;HeadwayGTU&gt;; set to add the GTU's to
400      * @throws GTUException if a GTU is incorrectly positioned on a lane
401      */
402     private void getFirstLeaders(final LaneDirection lane, final Length startDistance, final Length beyond,
403             final Set<HeadwayGTU> set) throws GTUException
404     {
405         LaneBasedGTU next = lane.getLane().getGtuAhead(beyond, lane.getDirection(), RelativePosition.FRONT,
406                 this.simulator.getSimulatorAbsTime());
407         if (next != null)
408         {
409             Length headway;
410             if (lane.getDirection().isPlus())
411             {
412                 headway = startDistance.plus(next.position(lane.getLane(), next.getRear()));
413             }
414             else
415             {
416                 headway = startDistance.plus(lane.getLane().getLength().minus(next.position(lane.getLane(), next.getRear())));
417             }
418             if (headway.si < 300)
419             {
420                 set.add(new HeadwayGTUReal(next, headway, true));
421             }
422             return;
423         }
424         ImmutableMap<Lane, GTUDirectionality> downstreamLanes =
425                 lane.getLane().downstreamLanes(lane.getDirection(), this.network.getGtuType(GTUType.DEFAULTS.VEHICLE));
426         for (Lane downstreamLane : downstreamLanes.keySet())
427         {
428             Length startDistanceDownstream = startDistance.plus(lane.getLane().getLength());
429             if (startDistanceDownstream.si > 300)
430             {
431                 return;
432             }
433             GTUDirectionality dir = downstreamLanes.get(downstreamLane);
434             Length beyondDownstream = dir.isPlus() ? Length.ZERO : downstreamLane.getLength();
435             getFirstLeaders(new LaneDirection(downstreamLane, dir), startDistanceDownstream, beyondDownstream, set);
436         }
437     }
438 
439     /** {@inheritDoc} */
440     @Override
441     public final String toString()
442     {
443         return "LaneBasedGTUGenerator " + this.id + " on " + this.generatorPositions;
444     }
445 
446     /**
447      * @return generatedGTUs.
448      */
449     public final long getGeneratedGTUs()
450     {
451         return this.generatedGTUs;
452     }
453 
454     /**
455      * Retrieve the id of this LaneBasedGTUGenerator.
456      * @return String; the id of this LaneBasedGTUGenerator
457      */
458     @Override
459     public final String getId()
460     {
461         return this.id;
462     }
463 
464     /**
465      * Disable the vehicle generator during the specific time. Underlying processes such as drawing characteristics and headways
466      * are continued, but simply will not result in the queuing of the GTU.
467      * @param start Time; start time
468      * @param end Time; end time
469      * @param laneDirections Set&lt;LaneDirection&gt;; lanes to disable generation on
470      * @throws SimRuntimeException if time is incorrect
471      */
472     public void disable(final Time start, final Time end, final Set<LaneDirection> laneDirections) throws SimRuntimeException
473     {
474         Throw.when(end.lt(start), SimRuntimeException.class, "End time %s is before start time %s.", end, start);
475         this.simulator.scheduleEventAbsTime(start, this, this, "disable", new Object[] {laneDirections});
476         this.simulator.scheduleEventAbsTime(end, this, this, "enable", new Object[0]);
477     }
478 
479     /**
480      * Disables the generator.
481      * @param laneDirections Set&lt;LaneDirection&gt;; lanes to disable generation on
482      */
483     @SuppressWarnings("unused")
484     private void disable(final Set<LaneDirection> laneDirections)
485     {
486         Throw.when(this.disabled != null && !this.disabled.isEmpty(), IllegalStateException.class,
487                 "Disabling a generator that is already disabled is not allowed.");
488         this.disabled = laneDirections;
489     }
490 
491     /**
492      * Enables the generator.
493      */
494     @SuppressWarnings("unused")
495     private void enable()
496     {
497         this.disabled = new LinkedHashSet<>();
498     }
499 
500     /**
501      * Interface for class that checks that there is sufficient room for a proposed new GTU and returns the maximum safe speed
502      * and position for the proposed new GTU.
503      */
504     public interface RoomChecker
505     {
506         /**
507          * Return the maximum safe speed and position for a new GTU with the specified characteristics. Returns
508          * {@code Placement.NO} if there is no safe speed and position. This method might be called with an empty leader set
509          * such that the desired speed can be implemented.
510          * @param leaders SortedSet&lt;HeadwayGTU&gt;; leaders, usually 1, possibly more after a branch
511          * @param characteristics LaneBasedGTUCharacteristics; characteristics of the proposed new GTU
512          * @param since Duration; time since the GTU wanted to arrive
513          * @param initialPosition Set&lt;DirectedLanePosition&gt;; initial position
514          * @return Speed; maximum safe speed, or null if a GTU with the specified characteristics cannot be placed at the
515          *         current time
516          * @throws NetworkException this method may throw a NetworkException if it encounters an error in the network structure
517          * @throws GTUException on parameter exception
518          */
519         Placement canPlace(SortedSet<HeadwayGTU> leaders, LaneBasedGTUCharacteristics characteristics, Duration since,
520                 Set<DirectedLanePosition> initialPosition) throws NetworkException, GTUException;
521     }
522 
523     /**
524      * Placement contains the information that a {@code RoomChecker} returns.
525      * <p>
526      * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
527      * <br>
528      * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>.
529      * <p>
530      * @version $Revision$, $LastChangedDate$, by $Author$, initial version 12 jan. 2018 <br>
531      * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a>
532      * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
533      * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a>
534      */
535     public static final class Placement
536     {
537 
538         /** Value if the GTU cannot be placed. */
539         public static final Placement NO = new Placement();
540 
541         /** Speed. */
542         private final Speed speed;
543 
544         /** Position. */
545         private final Set<DirectedLanePosition> position;
546 
547         /**
548          * Constructor for NO.
549          */
550         private Placement()
551         {
552             this.speed = null;
553             this.position = null;
554         }
555 
556         /**
557          * Constructor.
558          * @param speed Speed; speed
559          * @param position Set&lt;DirectedLanePosition&gt;; position
560          */
561         public Placement(final Speed speed, final Set<DirectedLanePosition> position)
562         {
563             Throw.whenNull(speed, "Speed may not be null. Use Placement.NO if the GTU cannot be placed.");
564             Throw.whenNull(position, "Position may not be null. Use Placement.NO if the GTU cannot be placed.");
565             this.speed = speed;
566             this.position = position;
567         }
568 
569         /**
570          * Returns whether the GTU can be placed.
571          * @return whether the GTU can be placed
572          */
573         public boolean canPlace()
574         {
575             return this.speed != null && this.position != null;
576         }
577 
578         /**
579          * Returns the speed.
580          * @return Speed; speed
581          */
582         public Speed getSpeed()
583         {
584             return this.speed;
585         }
586 
587         /**
588          * Returns the position.
589          * @return Set&lt;DirectedLanePosition&gt;; position
590          */
591         public Set<DirectedLanePosition> getPosition()
592         {
593             return this.position;
594         }
595 
596         /** {@inheritDoc} */
597         @Override
598         public String toString()
599         {
600             return "Placement [speed=" + this.speed + ", position=" + this.position + "]";
601         }
602 
603     }
604 
605     /** {@inheritDoc} */
606     @Override
607     public DirectedPoint getLocation()
608     {
609         return this.generatorPositions.getLocation();
610     }
611 
612     /** {@inheritDoc} */
613     @Override
614     public Bounds getBounds() throws RemoteException
615     {
616         return this.generatorPositions.getBounds();
617     }
618 
619     /** {@inheritDoc} */
620     @Override
621     public Map<DirectedPoint, Integer> getQueueLengths()
622     {
623         Map<DirectedPoint, Integer> result = new LinkedHashMap<>();
624         for (CrossSectionLink link : this.unplacedTemplates.keySet())
625         {
626             for (GeneratorLanePosition lanePosition : this.unplacedTemplates.get(link).keySet())
627             {
628                 result.put(lanePosition.getPosition().iterator().next().getLocation(),
629                         this.unplacedTemplates.get(link).get(lanePosition).size());
630             }
631         }
632         for (GeneratorLanePosition lanePosition : this.generatorPositions.getAllPositions())
633         {
634             DirectedPoint p = lanePosition.getPosition().iterator().next().getLocation();
635             if (!result.containsKey(p))
636             {
637                 result.put(p, 0);
638             }
639         }
640         return result;
641     }
642 
643     /** {@inheritDoc} */
644     @Override
645     public Serializable getSourceId()
646     {
647         return this.id;
648     }
649 
650 }