Class Train

All Implemented Interfaces:
Agent, Timeable

public class Train extends LongAgent<RailNode,RailArc>
Represents a train consisting a sequence of rail cars.

This class provides functionality for managing train movement along a rail network, handling reservations of tracks, coupling and splitting trains, and calculating routes while considering other trains, booked tracks, and prohibited arcs.

Key features of this class include:

  • Managing the train's cars and calculating the total length.
  • Accessing the train's environment (TrainsEnvironment) and booked tracks.
  • Calculating and booking movement along a TrainRoute.
  • Coupling with another train to form a combined train or splitting into two trains.
  • Determining route availability based on other trains and reserved tracks.
  • Moving along routes with proper handling of direction and path reversals.

Trains must not be moved while splitting. All operations that affect track occupation or movement are managed in coordination with TrainsEnvironment.

Author:
Andrey Malykhanov, Andrey Korotin
  • Field Details

    • cars

      protected List<? extends RailCar> cars
    • trainsEnvironment

      protected TrainsEnvironment trainsEnvironment
    • route

      protected TrainRoute route
    • currentPathIndex

      protected int currentPathIndex
    • length

      protected double length
    • highlightedRoute

      public List<Polyline> highlightedRoute
  • Constructor Details

    • Train

      public Train(Engine engine)
      Creates a new Train with the specified Engine.

      The train is initialized without any rail cars. Movement from the tail is disabled by default.

      Parameters:
      engine - the Engine for this train
    • Train

      public Train(Engine engine, List<? extends RailCar> cars)
      Creates a new Train with the specified Engine and a list of rail cars.

      The cars are assigned in sequence, and movement from the tail is disabled by default.

      Parameters:
      engine - the Engine for this train
      cars - the list of RailCars that make up this train
  • Method Details

    • getLength

      public double getLength()
      Returns the total length of this train.
      Overrides:
      getLength in class LongAgent<RailNode,RailArc>
      Returns:
      the length of the train
    • setGraphEnvironment

      public void setGraphEnvironment(TrainsEnvironment trainsEnvironment)
      Sets the TrainsEnvironment for this train.

      This environment contains information about the rail network, other trains, and booking states. It also sets up the train in the graph environment with its current length.

      Parameters:
      trainsEnvironment - the TrainsEnvironment to associate with this train
    • moveAlongRoute

      public void moveAlongRoute(TrainRoute route, double velocity)
      Starts moving the train along the specified TrainRoute with the given velocity.

      The train follows the consecutive paths of the route. If the train's current tail position matches the destination position of the route, onDestinationReached(com.amalgamasimulation.graphagent.GeometricGraphPosition<com.amalgamasimulation.trains.RailNode, com.amalgamasimulation.trains.RailArc>) is called. If the train needs to move in the reverse direction, it first turns around.

      Parameters:
      route - the TrainRoute along which the train should move
      velocity - the velocity at which the train moves along the route
    • moveAlongPathInternal

      protected CompletionStage<Void> moveAlongPathInternal(AgentGraphPath<RailNode,RailArc> path, double velocity)
    • onDestinationReached

      public void onDestinationReached(GeometricGraphPosition<RailNode,RailArc> destPosition)
      Description copied from class: GraphAgent
      Callback method called after this agent reaches its destination, i.e. the final position of its path. The destination is determined as final position of any movement initiated by any overload of moveTo or moveToClosest method.

      Note that jumpTo methods do not initiate movements and thus do not lead to calling this callback.

      Overrides:
      onDestinationReached in class LongAgent<RailNode,RailArc>
      Parameters:
      destPosition - the position that has been reached
      See Also:
    • beforeTailExitedArc

      public void beforeTailExitedArc(Graph<RailNode,RailArc>.Arc arc)
      Description copied from class: LongAgent
      Callback method called before the tail of this long agent exits an arc. To be overridden in subclasses. The tail of this long agent is still in the specified arc when this method is called.
      Overrides:
      beforeTailExitedArc in class LongAgent<RailNode,RailArc>
      Parameters:
      arc - arc being exited by the tail of this long agent
    • afterHeadEnteredArc

      public void afterHeadEnteredArc(Graph<RailNode,RailArc>.Arc arc)
      Description copied from class: LongAgent
      Callback method called immediately after the head of this long agent enters an arc. To be overridden in subclasses. The head of this long agent is already on the specified arc when this method is called.
      Overrides:
      afterHeadEnteredArc in class LongAgent<RailNode,RailArc>
      Parameters:
      arc - arc being entered by the head of this long agent
    • removeFromGraphEnvironment

      public void removeFromGraphEnvironment()
      Description copied from class: GraphAgent
      Removes this agent from the GraphEnvironment which it has been set to.
      Overrides:
      removeFromGraphEnvironment in class GraphAgent<RailNode,RailArc>
    • split

      public Pair<Train,Train> split(int carsFromHead)
      Splits this train into two separate trains at the specified number of cars from the head.

      The train must not be moving when this method is called. The first new train will consist of the first carsFromHead cars, and the second train will consist of the remaining cars. Both new trains inherit the positions and occupied arcs of the original train, with proper adjustment to reflect the split.

      Parameters:
      carsFromHead - the number of cars from the head that should form the first train; must be in the range [1, cars count - 1]
      Returns:
      a Pair where the first element is the train containing the first cars and the second element is the train containing the remaining cars
      Throws:
      IllegalArgumentException - if carsFromHead is out of valid range
      RuntimeException - if the train is currently moving
    • couple

      public Train couple(Train otherTrain)
      Couples this train with another train to form a new combined train.

      The occupied arcs from both trains are also combined and passed to the new train to maintain track reservations. After coupling, the original trains are removed from the environment.

      The new train inherits the position of the appropriate head or tail based on alignment.

      Parameters:
      otherTrain - the Train to couple with this train
      Returns:
      a new Train consisting of the combined cars of both trains
      Throws:
      RuntimeException - if the trains are not aligned at their heads or tails
    • getCarsCount

      public int getCarsCount()
      Returns the number of cars in this train.
      Returns:
      the number of RailCars
    • getCars

      public List<? extends RailCar> getCars()
      Returns an unmodifiable list of cars in this train.

      The returned list cannot be modified to ensure the internal car sequence remains consistent.

      Returns:
      an unmodifiable list of RailCars
    • onRouteEndReached

      public void onRouteEndReached(TrainRoute route)
      Called when the train reaches the final destination of the route.
      Parameters:
      route - route that was used by the train to arrive to the destination
      See Also:
    • getRoute

      public TrainRoute getRoute()
      Returns the route which this train is currently moving along or null if there is no such route.
      Returns:
      route or null
    • turnAround

      public void turnAround()
      Turns the train around, i.e. switches its logical head and tail.
      The list of rail cars is reversed to keep the physical order of the cars the same.
      Overrides:
      turnAround in class LongAgent<RailNode,RailArc>
    • updateCarsSequence

      protected void updateCarsSequence()
    • afterEnteredTrack

      public void afterEnteredTrack(RailTrack track)
      Called immediately after the head of the train enters the specified track.
      Can be overridden in subclasses.
      Parameters:
      track - track that is being entered by the train
    • beforeExitedTrack

      public void beforeExitedTrack(RailTrack track)
      Called right before the tail of the train exits the specified track.
      Can be overridden in subclasses.
      Parameters:
      track - track that is being exited by the train
    • getCurrentTrack

      public RailTrack getCurrentTrack()
      Returns the track where the head of the train is currently located.
      Returns:
      track where the head of the train is currently located
    • calculateRouteTo

      public TrainRoute calculateRouteTo(RailDestination railDestination, boolean considerBookedTracks, boolean considerTrainsPredicate, double velocity)
      Calculates a route for this train from a head or a tail to destination position.

      The route calculation considers booked tracks. Arcs that are blocked or overlap with non-moving trains.

      The initial direction of the train for movement equal HEAD_OR_TAIL

      Parameters:
      railDestination - RailDestination as end movement destination
      considerBookedTracks - whether booked tracks should be considered as obstacles
      considerTrainsPredicate - whether non-moving trains should be considered as obstacles
      velocity - the velocity of the train, used for timing along the route
      Returns:
      a TrainRoute representing the calculated path from source to destination
    • calculateRouteTo

      public TrainRoute calculateRouteTo(RailDestination railDestination, boolean considerBookedTracks, boolean considerTrainsPredicate, List<RailNode> visitNodes, double velocity)
      Calculates a route for this train from a head or a tail to destination position.

      The route calculation considers booked tracks, and necessary nodes to visit. Arcs that are blocked or overlap with non-moving trains.

      The initial direction of the train for movement equal HEAD_OR_TAIL

      Parameters:
      railDestination - RailDestination as end movement destination
      considerBookedTracks - whether booked tracks should be considered as obstacles
      considerTrainsPredicate - whether non-moving trains should be considered as obstacles
      visitNodes - a list of RailNodes that the route must visit, in the specified order
      velocity - the velocity of the train, used for timing along the route
      Returns:
      a TrainRoute representing the calculated path from source to destination
    • calculateRouteTo

      public TrainRoute calculateRouteTo(RailDestination railDestination, boolean considerBookedTracks, boolean considerTrainsPredicate, List<RailNode> visitNodes, Set<RailArc> prohibitedArcs, double velocity)
      Calculates a route for this train from a head or a tail to destination position.

      The route calculation considers booked tracks, prohibited arcs and necessary nodes to visit. Arcs that are blocked or overlap with non-moving trains.

      The initial direction of the train for movement equal HEAD_OR_TAIL

      Parameters:
      railDestination - RailDestination as end movement destination
      considerBookedTracks - whether booked tracks should be considered as obstacles
      considerTrainsPredicate - whether non-moving trains should be considered as obstacles
      visitNodes - a list of RailNodes that the route must visit, in the specified order
      prohibitedArcs - a set of RailArcs that are forbidden for the moving
      velocity - the velocity of the train, used for timing along the route
      Returns:
      a TrainRoute representing the calculated path from source to destination
    • calculateRouteTo

      public TrainRoute calculateRouteTo(TrainsEnvironment.TrainDirection movementStartTrainDirection, RailDestination railDestination, boolean considerBookedTracks, boolean considerTrainsPredicate, double velocity)
      Calculates a route for this train from a head or a tail to destination position.

      The route calculation considers booked tracks. Arcs that are blocked or overlap with non-moving trains.

      Parameters:
      movementStartTrainDirection - the initial direction of the train for movement
      railDestination - RailDestination as end movement destination
      considerBookedTracks - whether booked tracks should be considered as obstacles
      considerTrainsPredicate - whether non-moving trains should be considered as obstacles
      velocity - the velocity of the train, used for timing along the route
      Returns:
      a TrainRoute representing the calculated path from source to destination
    • calculateRouteTo

      public TrainRoute calculateRouteTo(TrainsEnvironment.TrainDirection movementStartTrainDirection, RailDestination railDestination, boolean considerBookedTracks, boolean considerTrainsPredicate, List<RailNode> visitNodes, double velocity)
      Calculates a route for this train from a head or a tail to destination position.

      The route calculation considers booked tracks and necessary nodes to visit. Arcs that are blocked or overlap with non-moving trains.

      Parameters:
      movementStartTrainDirection - the initial direction of the train for movement
      railDestination - RailDestination as end movement destination
      considerBookedTracks - whether booked tracks should be considered as obstacles
      considerTrainsPredicate - whether non-moving trains should be considered as obstacles
      visitNodes - a list of RailNodes that the route must visit, in the specified order
      velocity - the velocity of the train, used for timing along the route
      Returns:
      a TrainRoute representing the calculated path from source to destination
    • calculateRouteTo

      public TrainRoute calculateRouteTo(TrainsEnvironment.TrainDirection movementStartTrainDirection, RailDestination railDestination, boolean considerBookedTracks, boolean considerTrainsPredicate, List<RailNode> visitNodes, Set<RailArc> prohibitedArcs, double velocity)
      Calculates a route for this train from a head or a tail to destination position.

      The route calculation considers booked tracks, prohibited arcs, and necessary nodes to visit. Arcs that are blocked or overlap with non-moving trains.

      Parameters:
      movementStartTrainDirection - the initial direction of the train for movement
      railDestination - RailDestination as end movement destination
      considerBookedTracks - whether booked tracks should be considered as obstacles
      considerTrainsPredicate - whether non-moving trains should be considered as obstacles
      visitNodes - a list of RailNodes that the route must visit, in the specified order
      prohibitedArcs - a set of RailArcs that are forbidden for the moving
      velocity - the velocity of the train, used for timing along the route
      Returns:
      a TrainRoute representing the calculated path from source to destination
    • calculateRouteTo

      public TrainRoute calculateRouteTo(GeometricGraphPosition<RailNode,RailArc> destPosition, boolean considerBookedTracks, boolean considerTrainsPredicate, double velocity)
      Calculates a route for this train from a head or a tail to destination position.

      The route calculation considers booked tracks. Arcs that are blocked or overlap with non-moving trains.

      The initial direction of the train for movement equal HEAD_OR_TAIL

      Parameters:
      destPosition - GeometricGraphPosition as end movement destination
      considerBookedTracks - whether booked tracks should be considered as obstacles
      considerTrainsPredicate - whether non-moving trains should be considered as obstacles
      velocity - the velocity of the train, used for timing along the route
      Returns:
      a TrainRoute representing the calculated path from source to destination
    • calculateRouteTo

      public TrainRoute calculateRouteTo(GeometricGraphPosition<RailNode,RailArc> destPosition, boolean considerBookedTracks, boolean considerTrainsPredicate, List<RailNode> visitNodes, double velocity)
      Calculates a route for this train from a head or a tail to destination position.

      The route calculation considers booked tracks, and necessary nodes to visit. Arcs that are blocked or overlap with non-moving trains.

      The initial direction of the train for movement equal HEAD_OR_TAIL

      Parameters:
      destPosition - GeometricGraphPosition as end movement destination
      considerBookedTracks - whether booked tracks should be considered as obstacles
      considerTrainsPredicate - whether non-moving trains should be considered as obstacles
      visitNodes - a list of RailNodes that the route must visit, in the specified order
      velocity - the velocity of the train, used for timing along the route
      Returns:
      a TrainRoute representing the calculated path from source to destination
    • calculateRouteTo

      public TrainRoute calculateRouteTo(TrainsEnvironment.TrainDirection movementStartTrainDirection, GeometricGraphPosition<RailNode,RailArc> destPosition, boolean considerBookedTracks, boolean considerTrainsPredicate, double velocity)
      Calculates a route for this train from a head or a tail to destination position.

      The route calculation considers booked tracks. Arcs that are blocked or overlap with non-moving trains.

      Parameters:
      movementStartTrainDirection - the initial direction of the train for movement
      destPosition - GeometricGraphPosition as end movement destination
      considerBookedTracks - whether booked tracks should be considered as obstacles
      considerTrainsPredicate - whether non-moving trains should be considered as obstacles
      velocity - the velocity of the train, used for timing along the route
      Returns:
      a TrainRoute representing the calculated path from source to destination
    • calculateRouteTo

      public TrainRoute calculateRouteTo(TrainsEnvironment.TrainDirection movementStartTrainDirection, GeometricGraphPosition<RailNode,RailArc> destPosition, boolean considerBookedTracks, boolean considerTrainsPredicate, List<RailNode> visitNodes, double velocity)
      Calculates a route for this train from a head or a tail to destination position.

      The route calculation considers booked tracks, and necessary nodes to visit. Arcs that are blocked or overlap with non-moving trains.

      Parameters:
      movementStartTrainDirection - the initial direction of the train for movement
      destPosition - GeometricGraphPosition as end movement destination
      considerBookedTracks - whether booked tracks should be considered as obstacles
      considerTrainsPredicate - whether non-moving trains should be considered as obstacles
      visitNodes - a list of RailNodes that the route must visit, in the specified order
      velocity - the velocity of the train, used for timing along the route
      Returns:
      a TrainRoute representing the calculated path from source to destination
    • calculateRouteTo

      public TrainRoute calculateRouteTo(TrainsEnvironment.TrainDirection movementStartTrainDirection, GeometricGraphPosition<RailNode,RailArc> destPosition, boolean considerBookedTracks, boolean considerTrainsPredicate, List<RailNode> visitNodes, Set<RailArc> prohibitedArcs, double velocity)
      Calculates a route for this train from a head or a tail to destination position.

      The route calculation considers booked tracks, prohibited arcs, and necessary nodes to visit. Arcs that are blocked or overlap with non-moving trains.

      Parameters:
      movementStartTrainDirection - the initial direction of the train for movement
      destPosition - GeometricGraphPosition as end movement destination
      considerBookedTracks - whether booked tracks should be considered as obstacles
      considerTrainsPredicate - whether non-moving trains should be considered as obstacles
      visitNodes - a list of RailNodes that the route must visit, in the specified order
      prohibitedArcs - a set of RailArcs that are forbidden for the moving
      velocity - the velocity of the train, used for timing along the route
      Returns:
      a TrainRoute representing the calculated path from source to destination
    • calculateRouteTo

      public TrainRoute calculateRouteTo(TrainsEnvironment.TrainDirection movementStartTrainDirection, GeometricGraphPosition<RailNode,RailArc> destPosition, BiPredicate<RailTrack,Object> considerBookedTracks, Predicate<Train> considerTrainsPredicate, List<RailNode> visitNodes, Set<RailArc> prohibitedArcs, double beginTime, double velocity)
      Calculates a route for this train from a head or a tail to destination position.

      The route calculation considers booked tracks, prohibited arcs, and necessary nodes to visit. Arcs that are blocked or overlap with non-moving trains.

      Parameters:
      movementStartTrainDirection - the initial direction of the train for movement
      destPosition - GeometricGraphPosition as end movement destination
      considerBookedTracks - a BiPredicate to determine which booked tracks should be treated as occupied; the first argument is a RailTrack, the second is the booker object
      considerTrainsPredicate - a Predicate that determines which non-moving trains should be considered as obstacles; only trains for which this predicate returns true are considered
      visitNodes - a list of RailNodes that the route must visit, in the specified order
      prohibitedArcs - a set of RailArcs that are forbidden for the moving
      beginTime - the starting time for the route calculation
      velocity - the velocity of the train, used for timing along the route
      Returns:
      a TrainRoute representing the calculated path from source to destination
    • tryMoveTo

      public Pair<TrainRoute,Interval> tryMoveTo(TrainsEnvironment.TrainDirection movementStartTrainDirection, List<RailDestination> railDestinations, BiPredicate<RailTrack,Object> considerBookedTracks, Predicate<Train> considerTrainsPredicate, List<RailNode> visitNodes, Set<RailArc> prohibitedArcs, double beginTime, double velocity)
      Attempts to move the train to one of the specified rail destinations along the best available route.

      The method evaluates all potential routes to the given destinations, considering booked tracks, non-moving trains, mandatory visit nodes. The best route is selected based on minimal end movement time and then minimal route length.

      If a valid route is found, it books the corresponding time intervals for this train, schedules movement along the route via the engine, and returns the route along with the movement interval. If no route is available, the method returns a route of null or an interval with Double.POSITIVE_INFINITY as both start and end times.

      Parameters:
      movementStartTrainDirection - the initial direction of the train for movement
      railDestinations - the list of RailDestinations to reach
      considerBookedTracks - a BiPredicate to determine which booked tracks should be treated as occupied; the first argument is a RailTrack, the second is the booker object
      considerTrainsPredicate - a Predicate that determines which non-moving trains should be considered as obstacles; only trains for which this predicate returns true are considered
      visitNodes - a list of RailNodes that the route must visit, in the specified order
      prohibitedArcs - a set of RailArcs that are forbidden for the moving
      beginTime - the starting time for the route calculation
      velocity - the velocity of the train, used for timing along the route
      Returns:
      a Pair containing:
      • the selected TrainRoute, or null if no route is available
      • an Interval representing the start and end times of the movement along the route; if movement is impossible, Interval.EMPTY
    • tryMoveTo

      public Pair<TrainRoute,Interval> tryMoveTo(TrainsEnvironment.TrainDirection movementStartTrainDirection, List<RailDestination> railDestinations, boolean considerBookedTracks, boolean considerTrainsPredicate, double velocity)
      Attempts to move the train to one of the specified rail destinations along the best available route.

      The method evaluates all potential routes to the given destinations, considering booked tracks, non-moving trains. The best route is selected based on minimal end movement time and then minimal route length.

      If a valid route is found, it books the corresponding time intervals for this train, schedules movement along the route via the engine, and returns the route along with the movement interval. If no route is available, the method returns a route of null or an interval with Double.POSITIVE_INFINITY as both start and end times.

      Parameters:
      movementStartTrainDirection - the initial direction of the train for movement
      railDestinations - the list of RailDestinations to reach
      considerBookedTracks - whether booked tracks should be considered as obstacles
      considerTrainsPredicate - whether non-moving trains should be considered as obstacles
      velocity - the velocity of the train, used for timing along the route
      Returns:
      a Pair containing:
      • the selected TrainRoute, or null if no route is available
      • an Interval representing the start and end times of the movement along the route; if movement is impossible, Interval.EMPTY
    • tryMoveTo

      public Pair<TrainRoute,Interval> tryMoveTo(TrainsEnvironment.TrainDirection movementStartTrainDirection, List<RailDestination> railDestinations, double velocity)
      Attempts to move the train to one of the specified rail destinations along the best available route.

      The method evaluates all potential routes to the given destinations, considering booked tracks, non-moving trains. The best route is selected based on minimal end movement time and then minimal route length.

      If a valid route is found, it books the corresponding time intervals for this train, schedules movement along the route via the engine, and returns the route along with the movement interval. If no route is available, the method returns a route of null or an interval with Double.POSITIVE_INFINITY as both start and end times. This method calculates the route, taking into account other non-moving trains and booked tracks.

      Parameters:
      movementStartTrainDirection - the initial direction of the train for movement
      railDestinations - the list of RailDestinations to reach
      velocity - the velocity of the train, used for timing along the route
      Returns:
      a Pair containing:
      • the selected TrainRoute, or null if no route is available
      • an Interval representing the start and end times of the movement along the route; if movement is impossible, Interval.EMPTY
    • getReverseCars

      public List<? extends RailCar> getReverseCars()
      Returns a list of this train's cars in reversed order.

      The original car sequence is not modified; a new reversed list is returned.

      Returns:
      a list of RailCars in reverse order
    • getCurrentTracks

      public List<RailTrack> getCurrentTracks()
      Returns the track where the head of the train is currently located.
      Returns:
      track where the head of the train is currently located
    • onOtherAgentReached

      public void onOtherAgentReached(GraphAgent<RailNode,RailArc> otherAgent, boolean isMovingInSameDirection)
      Description copied from class: GraphAgent
      Callback method called when this agent reaches another agent, i.e. graph positions of this agent and the other agent become either equal or reverse. To be overridden in subclasses.
      Overrides:
      onOtherAgentReached in class LongAgent<RailNode,RailArc>
      Parameters:
      otherAgent - another agent
      isMovingInSameDirection - true if both agents are moving in the same direction, false otherwise
    • beforeExitedArc

      public void beforeExitedArc(Graph<RailNode,RailArc>.Arc arc)
      Description copied from class: GraphAgent
      Callback method called before exiting entering an arc. To be overridden in subclasses. This agent is still on the specified arc when this method is called

      Event of exiting an arc can happen in 3 cases:

    • in process of moving
    • before jumping from current position on an arc
    • before turning to opposite arc
    • Overrides:
      beforeExitedArc in class LongAgent<RailNode,RailArc>
      Parameters:
      arc - arc being exited
      See Also:
    • getTrackBooker

      public Object getTrackBooker()
      Returns the object that should be considered as the booker for tracks reserved by this train.
      Returns:
      the object representing the booker of the tracks
    • setPossibleStartMoveFromTail

      public void setPossibleStartMoveFromTail(boolean isPossibleStartMoveFromTail)
      Description copied from class: LongAgent
      Set the possibility of finding the shortest path from the tail of the agent.
      Overrides:
      setPossibleStartMoveFromTail in class LongAgent<RailNode,RailArc>
      Parameters:
      isPossibleStartMoveFromTail - possibility of finding the shortest path from the tail of the agent (default true)
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • checkArcsBeforeJumping

      protected void checkArcsBeforeJumping(Graph<RailNode,RailArc>.Arc arc, double arcAbsOffset, List<Graph<RailNode,RailArc>.Arc> previousArcs)
      Overrides:
      checkArcsBeforeJumping in class LongAgent<RailNode,RailArc>
    • moveTo

      public CompletionStage<Void> moveTo(GeometricGraphPosition<RailNode,RailArc> destPosition, double velocity)
      Description copied from class: GraphAgent
      Starts moving this agent to the specified destination position in the graph with the specified velocity along the shortest path from agent's current position.

      The shortest path is calculated with respect to weights of arcs and nodes set in agent's GraphEnvironment.

      If this agent is currently moving somewhere else, it forgets its current destination and is assigned a new path.

      If zero velocity is specified, the path is set to this agent, but it just does not start moving. It can start moving when GraphAgent.setVelocity(double) is called with a positive value.

      If no path is found from agent's current position to the specified position, GraphAgent.onPathNotFound(List) method is called. The default implementation of this method throws a RuntimeException, but it can be overridden in subclasses.

      If this agent is not in graph or has not been assigned to a GraphEnvironment, a RuntimeException is thrown.

      Overrides:
      moveTo in class LongAgent<RailNode,RailArc>
      Parameters:
      destPosition - specified destination position in the graph of this agent's GraphEnvironment
      velocity - specified velocity. Must be finite and non-negative
      See Also:
    • moveTo

      public CompletionStage<Void> moveTo(Graph<RailNode,RailArc>.Node node, double velocity)
      Description copied from class: GraphAgent
      Starts moving this agent to the specified node in the graph with the specified velocity along the shortest path from agent's current position.

      The shortest path is calculated with respect to weights of arcs and nodes set in agent's GraphEnvironment.

      If this agent is currently moving somewhere else, it forgets its current destination and is assigned a new path.

      If zero velocity is specified, the path is set to this agent, but it just does not start moving. It can start moving when GraphAgent.setVelocity(double) is called with a positive value.

      If no path is found from agent's current position to the specified node, GraphAgent.onPathNotFound(List) method is called. The default implementation of this method throws a RuntimeException, but it can be overridden in subclasses.

      If this agent is not in graph or has not been assigned to a GraphEnvironment, a RuntimeException is thrown.

      Overrides:
      moveTo in class LongAgent<RailNode,RailArc>
      Parameters:
      node - node in the graph of this agent's GraphEnvironment which this agent will be sent to
      velocity - specified velocity. Must be finite and non-negative
      See Also:
    • moveToClosest

      public CompletionStage<Void> moveToClosest(List<GeometricGraphPosition<RailNode,RailArc>> destPositions, double velocity)
      Description copied from class: GraphAgent
      Starts moving this agent to the closest one of the specified destination positions in the graph with the specified velocity along the shortest path from this agent's current position.

      Closeness and shortest path are determined with respect to weights of arcs and nodes set in agent's GraphEnvironment.

      If this agent is currently moving somewhere else, it forgets its current destination and is assigned a new path.

      If zero velocity is specified, the path is set to this agent, but it just does not start moving. It can start moving when GraphAgent.setVelocity(double) is called with a positive value.

      If no path is found from agent's current position to either of the specified positions, GraphAgent.onPathNotFound(List) method is called. The default implementation of this method throws a RuntimeException, but it can be overridden in subclasses.

      If this agent is not in graph or has not been assigned to a GraphEnvironment, a RuntimeException is thrown.

      Overrides:
      moveToClosest in class LongAgent<RailNode,RailArc>
      Parameters:
      destPositions - list of destination positions in the graph of this agent's GraphEnvironment, from which the closest one is selected
      velocity - specified velocity. Must be finite and non-negative
      See Also:
    • moveTo

      public CompletionStage<Void> moveTo(Predicate<Graph<RailNode,RailArc>.Node> indicator, double velocity)
      Description copied from class: GraphAgent
      Starts movement of the agent from the current position (node or arc) to the specified graph node along the shortest path.


      If no path exists between agent's current position (node or arc) to the specified node, function does nothing.
      If agent already is in the specified node, function does nothing.

      Overrides:
      moveTo in class LongAgent<RailNode,RailArc>
      Parameters:
      indicator - the Predicate which must return true to indicate that a node is a valid destination
    • moveAlongPath

      public CompletionStage<Void> moveAlongPath(AgentGraphPath<RailNode,RailArc> path, double velocity)
      Overrides:
      moveAlongPath in class LongAgent<RailNode,RailArc>
    • getHeadRailDestination

      public RailDestination getHeadRailDestination()
      Returns a RailDestination where the head of this long agent is currently located which indicates the location for movement to the head of the train, or null if this agent is not in a graph.
      Returns:
      RailDestination where the head of this long agent is currently located which indicates the location for movement to the head of the train, or null if this agent is not in a graph.
      See Also:
    • getTailRailDestination

      public RailDestination getTailRailDestination()
      Returns a RailDestination where the tail of this long agent is currently located which indicates the location for movement to the tail of the train, or null if this agent is not in a graph.
      Returns:
      RailDestination where the tail of this long agent is currently located which indicates the location for movement to the tail of the train, or null if this agent is not in a graph.
      See Also:
    • getTrainsEnvironment

      public TrainsEnvironment getTrainsEnvironment()
      Returns the TrainsEnvironment associated with this train.
      Returns:
      the TrainsEnvironment for this train
    • jumpTo

      public CompletionStage<Void> jumpTo(Graph<RailNode,RailArc>.Arc arc, double arcAbsOffset, boolean shouldBookArcs)
      Jumps this agent to the specified absolute offset of the specified arc of graph of this agent's GraphEnvironment. If this agent is moving or has a path, cancels its movement and forgets its path before jumping.
      Parameters:
      arc - arc of the graph of this agent's GraphEnvironment where this agent will be placed
      arcAbsOffset - absolute offset of the specified arc where this agent will be placed
      shouldBookArcs - if true, all occupied arcs will be booked as occupied from the current time to Double.POSITIVE_INFINITY
    • jumpTo

      public CompletionStage<Void> jumpTo(GeometricGraphPosition<RailNode,RailArc> position, boolean shouldBookArcs)
      Jumps this agent to the specified position. If this agent is moving or has a path, cancels its movement and forgets its path before jumping.
      Parameters:
      position - position in the graph of this agent's GraphEnvironment where this agent will be placed
      shouldBookArcs - if true, all occupied arcs will be booked as occupied from the current time to Double.POSITIVE_INFINITY
    • jumpTo

      public void jumpTo(Graph<RailNode,RailArc>.Arc arc, double arcAbsOffset, List<Graph<RailNode,RailArc>.Arc> previousArcs, boolean shouldBookArcs)
      Jumps this agent to the specified arc at the specified absolute offset and places it on the specified list of previous arcs. If this agent is moving or has a path, cancels its movement and forgets its path before jumping.
      Parameters:
      arc - specified arc
      arcAbsOffset - absolute offset on the specified arc
      previousArcs - list of arcs where to place this long agent
      shouldBookArcs - if true, all occupied arcs will be booked as occupied from the current time to Double.POSITIVE_INFINITY