Class Train
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 Summary
FieldsModifier and TypeFieldDescriptionprotected intprotected doubleprotected TrainRouteprotected TrainsEnvironmentFields inherited from class com.amalgamasimulation.graphagent.LongAgent
adjacentAgents, aheadAgent, aheadAgentNotBlockingPath, animatedSelfPath, animationPolyline, behindAgent, isPossibleStartMoveFromTail, longAgentsEnvironment, longAgentWeightKey, occupiedArcs, occupiedNodes, ownVelocity, selfPathLastUpdateTime, shouldTurnAroundBeforeMovement, tailFields inherited from class com.amalgamasimulation.graphagent.GraphAgent
basis, engine, lastTrajectoryNodeIndex, lastTrajectoryPointIndex -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidafterEnteredTrack(RailTrack track) Called immediately after the head of the train enters the specified track.
Can be overridden in subclasses.voidCallback method called immediately after the head of this long agent enters an arc.voidbeforeExitedArc(Graph<RailNode, RailArc>.Arc arc) Callback method called before exiting entering an arc.voidbeforeExitedTrack(RailTrack track) Called right before the tail of the train exits the specified track.
Can be overridden in subclasses.voidCallback method called before the tail of this long agent exits an arc.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.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.calculateRouteTo(RailDestination railDestination, boolean considerBookedTracks, boolean considerTrainsPredicate, double velocity) Calculates a route for this train from a head or a tail to destination position.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.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.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.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.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.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.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.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.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.protected voidcheckArcsBeforeJumping(Graph<RailNode, RailArc>.Arc arc, double arcAbsOffset, List<Graph<RailNode, RailArc>.Arc> previousArcs) Couples this train with another train to form a new combined train.getCars()Returns an unmodifiable list of cars in this train.intReturns the number of cars in this train.Returns the track where the head of the train is currently located.Returns the track where the head of the train is currently located.Returns aRailDestinationwhere the head of this long agent is currently located which indicates the location for movement to the head of the train, ornullif this agent is not in a graph.doubleReturns the total length of this train.Returns a list of this train's cars in reversed order.getRoute()Returns the route which this train is currently moving along ornullif there is no such route.Returns aRailDestinationwhere the tail of this long agent is currently located which indicates the location for movement to the tail of the train, ornullif this agent is not in a graph.Returns the object that should be considered as the booker for tracks reserved by this train.Returns theTrainsEnvironmentassociated with this train.Jumps this agent to the specified absolute offset of the specified arc of graph of this agent'sGraphEnvironment.voidjumpTo(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.jumpTo(GeometricGraphPosition<RailNode, RailArc> position, boolean shouldBookArcs) Jumps this agent to the specified position.moveAlongPath(AgentGraphPath<RailNode, RailArc> path, double velocity) protected CompletionStage<Void> moveAlongPathInternal(AgentGraphPath<RailNode, RailArc> path, double velocity) voidmoveAlongRoute(TrainRoute route, double velocity) Starts moving the train along the specifiedTrainRoutewith the given velocity.Starts moving this agent to the specified node in the graph with the specified velocity along the shortest path from agent's current position.moveTo(GeometricGraphPosition<RailNode, RailArc> destPosition, double velocity) 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.Starts movement of the agent from the current position (node or arc) to the specified graph node along the shortest path.moveToClosest(List<GeometricGraphPosition<RailNode, RailArc>> destPositions, double velocity) 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.voidonDestinationReached(GeometricGraphPosition<RailNode, RailArc> destPosition) Callback method called after this agent reaches its destination, i.e.voidonOtherAgentReached(GraphAgent<RailNode, RailArc> otherAgent, boolean isMovingInSameDirection) Callback method called when this agent reaches another agent, i.e.voidonRouteEndReached(TrainRoute route) Called when the train reaches the final destination of the route.voidRemoves this agent from theGraphEnvironmentwhich it has been set to.voidsetGraphEnvironment(TrainsEnvironment trainsEnvironment) Sets theTrainsEnvironmentfor this train.voidsetPossibleStartMoveFromTail(boolean isPossibleStartMoveFromTail) Set the possibility of finding the shortest path from the tail of the agent.split(int carsFromHead) Splits this train into two separate trains at the specified number of cars from the head.toString()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.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.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.voidTurns the train around, i.e.protected voidMethods inherited from class com.amalgamasimulation.graphagent.LongAgent
addAdjacentAgent, afterEnteredArc, afterEnteredNode, afterHeadEnteredNode, allowEnterArc, beforeExitedNode, beforeTailExitedNode, cancelMoving, checkArcPositionForOccupancy, checkArcsOrder, checkNodeForOccupancy, checkPositionsInsideAgent, checkReverseArcs, checkSelfOverlay, clearInternalCollections, comparePathsLessThan, createPathForTailAgent, enterArcFollowingAgent, enterNodeInternal, eraseCurrentPathInternal, fillOccupiedCollectionAfterSeveralArcsJumping, getAdjacentAgents, getAheadAgent, getAnimationSelfPath, getBehindAgent, getCurrentAnimationPolyline, getCurrentAnimationTailArcAbsOffset, getCurrentHeadAnimationPoint, getCurrentHeadPosition, getCurrentTailAnimationPoint, getCurrentTailPosition, getLongAgentWeightKey, getNextPathArc, getOccupiedArcs, getOccupiedNodes, getPositionInsideAgent, getSelfPath, getShortestHeadTailPath, getShortestPath, getShortestPath, getShortestPathToClosest, isAheadAgentBlockingMovement, isAheadAgentNotBlockingPath, isOtherAgentInNodeHavingSameDirection, isPossibleStartMoveFromTail, isPossibleToStartMoving, jumpTo, jumpTo, jumpTo, jumpTo, jumpTo, jumpTo, jumpTo, jumpToPathDestPosition, jumpToSeveralArcs, moveTo, onAheadAgentVelocityChanged, onCollidedWithOtherAgent, recalculateCollisions, removeAdjacentAgent, removeFromGraphInternal, removeNodeAdjacentAgents, setAheadAgent, setBehindAgent, setCurrentPathInternal, setGraphEnvironment, setGraphEnvironment, setLongAgentWeightKey, setVelocity, setVelocityInternal, startMovingAlongPath, turnAroundInternalMethods inherited from class com.amalgamasimulation.graphagent.GraphAgent
addCallbackPosition, cancelMovingInternal, checkEvents, clearCallbackPositions, disableCommandsExecution, enableCommandsExecution, engine, enterArcInternal, enterSegment, getCurrentAnimationArcAbsOffset, getCurrentAnimationHeading, getCurrentAnimationPoint, getCurrentAnimationTrajectoryAbsoluteOffset, getCurrentArc, getCurrentArcAbsOffset, getCurrentNode, getCurrentPoint, getCurrentPosition, getDistanceTraveled, getGraphEnvironment, getGraphPath, getHeading, getName, getTrajectory, getVelocity, hasGraphEnvironment, hasPath, isAtNode, isInGraph, isMoving, isOnArc, jumpToInternal, jumpToInternal, leaveCurrentArcInternal, leaveCurrentNodeInternal, moveToClosestInternal, moveToIndicatorInternal, moveToInternal, onCallbackPositionReached, onPathNotFound, onSegmentExited, removeCallbackPosition, resetDistanceTraveled, time, tryExecuteCommand, tryUpdateDistanceTraveled, tryUpdatePositionMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface com.amalgamasimulation.utils.time.Timeable
dateToTime, dateToTime, day, duration, hour, minute, second, timeToDate, week, wholeDays, wholeDays, wholeHours, wholeHours, wholeUnits
-
Field Details
-
cars
-
trainsEnvironment
-
route
-
currentPathIndex
protected int currentPathIndex -
length
protected double length -
highlightedRoute
-
-
Constructor Details
-
Method Details
-
getLength
public double getLength()Returns the total length of this train. -
setGraphEnvironment
Sets theTrainsEnvironmentfor 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- theTrainsEnvironmentto associate with this train
-
moveAlongRoute
Starts moving the train along the specifiedTrainRoutewith 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- theTrainRoutealong which the train should movevelocity- the velocity at which the train moves along the route
-
moveAlongPathInternal
protected CompletionStage<Void> moveAlongPathInternal(AgentGraphPath<RailNode, RailArc> path, double velocity) -
onDestinationReached
Description copied from class:GraphAgentCallback 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 ofmoveToormoveToClosestmethod.Note that
jumpTomethods do not initiate movements and thus do not lead to calling this callback.- Overrides:
onDestinationReachedin classLongAgent<RailNode,RailArc> - Parameters:
destPosition- the position that has been reached- See Also:
-
beforeTailExitedArc
Description copied from class:LongAgentCallback 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:
beforeTailExitedArcin classLongAgent<RailNode,RailArc> - Parameters:
arc- arc being exited by the tail of this long agent
-
afterHeadEnteredArc
Description copied from class:LongAgentCallback 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:
afterHeadEnteredArcin classLongAgent<RailNode,RailArc> - Parameters:
arc- arc being entered by the head of this long agent
-
removeFromGraphEnvironment
public void removeFromGraphEnvironment()Description copied from class:GraphAgentRemoves this agent from theGraphEnvironmentwhich it has been set to.- Overrides:
removeFromGraphEnvironmentin classGraphAgent<RailNode,RailArc>
-
split
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
carsFromHeadcars, 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
Pairwhere the first element is the train containing the first cars and the second element is the train containing the remaining cars - Throws:
IllegalArgumentException- ifcarsFromHeadis out of valid rangeRuntimeException- if the train is currently moving
-
couple
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- theTrainto couple with this train- Returns:
- a new
Trainconsisting 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
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
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
Returns the route which this train is currently moving along ornullif 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:
turnAroundin classLongAgent<RailNode,RailArc>
-
updateCarsSequence
protected void updateCarsSequence() -
afterEnteredTrack
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
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
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-RailDestinationas end movement destinationconsiderBookedTracks- whether booked tracks should be considered as obstaclesconsiderTrainsPredicate- whether non-moving trains should be considered as obstaclesvelocity- the velocity of the train, used for timing along the route- Returns:
- a
TrainRouterepresenting 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-RailDestinationas end movement destinationconsiderBookedTracks- whether booked tracks should be considered as obstaclesconsiderTrainsPredicate- whether non-moving trains should be considered as obstaclesvisitNodes- a list ofRailNodes that the route must visit, in the specified ordervelocity- the velocity of the train, used for timing along the route- Returns:
- a
TrainRouterepresenting 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-RailDestinationas end movement destinationconsiderBookedTracks- whether booked tracks should be considered as obstaclesconsiderTrainsPredicate- whether non-moving trains should be considered as obstaclesvisitNodes- a list ofRailNodes that the route must visit, in the specified orderprohibitedArcs- a set ofRailArcs that are forbidden for the movingvelocity- the velocity of the train, used for timing along the route- Returns:
- a
TrainRouterepresenting 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 movementrailDestination-RailDestinationas end movement destinationconsiderBookedTracks- whether booked tracks should be considered as obstaclesconsiderTrainsPredicate- whether non-moving trains should be considered as obstaclesvelocity- the velocity of the train, used for timing along the route- Returns:
- a
TrainRouterepresenting 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 movementrailDestination-RailDestinationas end movement destinationconsiderBookedTracks- whether booked tracks should be considered as obstaclesconsiderTrainsPredicate- whether non-moving trains should be considered as obstaclesvisitNodes- a list ofRailNodes that the route must visit, in the specified ordervelocity- the velocity of the train, used for timing along the route- Returns:
- a
TrainRouterepresenting 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 movementrailDestination-RailDestinationas end movement destinationconsiderBookedTracks- whether booked tracks should be considered as obstaclesconsiderTrainsPredicate- whether non-moving trains should be considered as obstaclesvisitNodes- a list ofRailNodes that the route must visit, in the specified orderprohibitedArcs- a set ofRailArcs that are forbidden for the movingvelocity- the velocity of the train, used for timing along the route- Returns:
- a
TrainRouterepresenting 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-GeometricGraphPositionas end movement destinationconsiderBookedTracks- whether booked tracks should be considered as obstaclesconsiderTrainsPredicate- whether non-moving trains should be considered as obstaclesvelocity- the velocity of the train, used for timing along the route- Returns:
- a
TrainRouterepresenting 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-GeometricGraphPositionas end movement destinationconsiderBookedTracks- whether booked tracks should be considered as obstaclesconsiderTrainsPredicate- whether non-moving trains should be considered as obstaclesvisitNodes- a list ofRailNodes that the route must visit, in the specified ordervelocity- the velocity of the train, used for timing along the route- Returns:
- a
TrainRouterepresenting 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 movementdestPosition-GeometricGraphPositionas end movement destinationconsiderBookedTracks- whether booked tracks should be considered as obstaclesconsiderTrainsPredicate- whether non-moving trains should be considered as obstaclesvelocity- the velocity of the train, used for timing along the route- Returns:
- a
TrainRouterepresenting 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 movementdestPosition-GeometricGraphPositionas end movement destinationconsiderBookedTracks- whether booked tracks should be considered as obstaclesconsiderTrainsPredicate- whether non-moving trains should be considered as obstaclesvisitNodes- a list ofRailNodes that the route must visit, in the specified ordervelocity- the velocity of the train, used for timing along the route- Returns:
- a
TrainRouterepresenting 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 movementdestPosition-GeometricGraphPositionas end movement destinationconsiderBookedTracks- whether booked tracks should be considered as obstaclesconsiderTrainsPredicate- whether non-moving trains should be considered as obstaclesvisitNodes- a list ofRailNodes that the route must visit, in the specified orderprohibitedArcs- a set ofRailArcs that are forbidden for the movingvelocity- the velocity of the train, used for timing along the route- Returns:
- a
TrainRouterepresenting 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 movementdestPosition-GeometricGraphPositionas end movement destinationconsiderBookedTracks- aBiPredicateto determine which booked tracks should be treated as occupied; the first argument is aRailTrack, the second is the booker objectconsiderTrainsPredicate- aPredicatethat determines which non-moving trains should be considered as obstacles; only trains for which this predicate returnstrueare consideredvisitNodes- a list ofRailNodes that the route must visit, in the specified orderprohibitedArcs- a set ofRailArcs that are forbidden for the movingbeginTime- the starting time for the route calculationvelocity- the velocity of the train, used for timing along the route- Returns:
- a
TrainRouterepresenting 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_INFINITYas both start and end times.- Parameters:
movementStartTrainDirection- the initial direction of the train for movementrailDestinations- the list ofRailDestinations to reachconsiderBookedTracks- aBiPredicateto determine which booked tracks should be treated as occupied; the first argument is aRailTrack, the second is the booker objectconsiderTrainsPredicate- aPredicatethat determines which non-moving trains should be considered as obstacles; only trains for which this predicate returnstrueare consideredvisitNodes- a list ofRailNodes that the route must visit, in the specified orderprohibitedArcs- a set ofRailArcs that are forbidden for the movingbeginTime- the starting time for the route calculationvelocity- the velocity of the train, used for timing along the route- Returns:
- a
Paircontaining:- the selected
TrainRoute, or null if no route is available - an
Intervalrepresenting the start and end times of the movement along the route; if movement is impossible,Interval.EMPTY
- the selected
-
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_INFINITYas both start and end times.- Parameters:
movementStartTrainDirection- the initial direction of the train for movementrailDestinations- the list ofRailDestinations to reachconsiderBookedTracks- whether booked tracks should be considered as obstaclesconsiderTrainsPredicate- whether non-moving trains should be considered as obstaclesvelocity- the velocity of the train, used for timing along the route- Returns:
- a
Paircontaining:- the selected
TrainRoute, or null if no route is available - an
Intervalrepresenting the start and end times of the movement along the route; if movement is impossible,Interval.EMPTY
- the selected
-
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_INFINITYas 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 movementrailDestinations- the list ofRailDestinations to reachvelocity- the velocity of the train, used for timing along the route- Returns:
- a
Paircontaining:- the selected
TrainRoute, or null if no route is available - an
Intervalrepresenting the start and end times of the movement along the route; if movement is impossible,Interval.EMPTY
- the selected
-
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
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:GraphAgentCallback 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:
onOtherAgentReachedin classLongAgent<RailNode,RailArc> - Parameters:
otherAgent- another agentisMovingInSameDirection-trueif both agents are moving in the same direction, falseotherwise
-
beforeExitedArc
Description copied from class:GraphAgentCallback method called before exiting entering an arc. To be overridden in subclasses. This agent is still on the specified arc when this method is calledEvent 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:
beforeExitedArcin classLongAgent<RailNode,RailArc> - Parameters:
arc- arc being exited- See Also:
-
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:LongAgentSet the possibility of finding the shortest path from the tail of the agent.- Overrides:
setPossibleStartMoveFromTailin classLongAgent<RailNode,RailArc> - Parameters:
isPossibleStartMoveFromTail- possibility of finding the shortest path from the tail of the agent (defaulttrue)
-
toString
-
checkArcsBeforeJumping
protected void checkArcsBeforeJumping(Graph<RailNode, RailArc>.Arc arc, double arcAbsOffset, List<Graph<RailNode, RailArc>.Arc> previousArcs) - Overrides:
checkArcsBeforeJumpingin classLongAgent<RailNode,RailArc>
-
moveTo
public CompletionStage<Void> moveTo(GeometricGraphPosition<RailNode, RailArc> destPosition, double velocity) Description copied from class:GraphAgentStarts 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 aRuntimeException, but it can be overridden in subclasses.If this agent is not in graph or has not been assigned to a
GraphEnvironment, aRuntimeExceptionis thrown.- Overrides:
moveToin classLongAgent<RailNode,RailArc> - Parameters:
destPosition- specified destination position in the graph of this agent'sGraphEnvironmentvelocity- specified velocity. Must be finite and non-negative- See Also:
-
moveTo
Description copied from class:GraphAgentStarts 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 aRuntimeException, but it can be overridden in subclasses.If this agent is not in graph or has not been assigned to a
GraphEnvironment, aRuntimeExceptionis thrown.- Overrides:
moveToin classLongAgent<RailNode,RailArc> - Parameters:
node- node in the graph of this agent'sGraphEnvironmentwhich this agent will be sent tovelocity- 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:GraphAgentStarts 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 aRuntimeException, but it can be overridden in subclasses.If this agent is not in graph or has not been assigned to a
GraphEnvironment, aRuntimeExceptionis thrown.- Overrides:
moveToClosestin classLongAgent<RailNode,RailArc> - Parameters:
destPositions- list of destination positions in the graph of this agent'sGraphEnvironment, from which the closest one is selectedvelocity- 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:GraphAgentStarts 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.
-
moveAlongPath
- Overrides:
moveAlongPathin classLongAgent<RailNode,RailArc>
-
getHeadRailDestination
Returns aRailDestinationwhere the head of this long agent is currently located which indicates the location for movement to the head of the train, ornullif this agent is not in a graph.- Returns:
RailDestinationwhere the head of this long agent is currently located which indicates the location for movement to the head of the train, ornullif this agent is not in a graph.- See Also:
-
getTailRailDestination
Returns aRailDestinationwhere the tail of this long agent is currently located which indicates the location for movement to the tail of the train, ornullif this agent is not in a graph.- Returns:
RailDestinationwhere the tail of this long agent is currently located which indicates the location for movement to the tail of the train, ornullif this agent is not in a graph.- See Also:
-
getTrainsEnvironment
Returns theTrainsEnvironmentassociated with this train.- Returns:
- the
TrainsEnvironmentfor 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'sGraphEnvironment. 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'sGraphEnvironmentwhere this agent will be placedarcAbsOffset- absolute offset of the specified arc where this agent will be placedshouldBookArcs- 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'sGraphEnvironmentwhere this agent will be placedshouldBookArcs- 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 arcarcAbsOffset- absolute offset on the specified arcpreviousArcs- list of arcs where to place this long agentshouldBookArcs- if true, all occupied arcs will be booked as occupied from the current time to Double.POSITIVE_INFINITY
-