Class GraphAgent<N extends AgentGraphNode,A extends AgentGraphArc>
- Type Parameters:
N- type of values stored in graph nodesA- type of values stored in graph arcs
- Direct Known Subclasses:
LongAgent,LongAgentTail
GraphEnvironment.- Author:
- Andrey Malykhanov
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionGraphAgent(Engine engine) Creates a new instance ofGraphAgentthat is simulated with the specifiedEngine. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddCallbackPosition(GeometricGraphPosition<N, A> position) Adds a callback position that will initiate callingonCallbackPositionReached(GeometricGraphPosition, boolean)method when this agent reaches the specified position.voidafterEnteredArc(Graph<N, A>.Arc arc) Callback method called immediately after entering an arc.voidafterEnteredNode(Graph<N, A>.Node node) Callback method called immediately after entering a node.booleanallowEnterArc(Graph<N, A>.Arc arc) Callback method used to implement advanced functionality like passby or waiting for something specific before entering arcs.voidbeforeExitedArc(Graph<N, A>.Arc arc) Callback method called before exiting entering an arc.voidbeforeExitedNode(Graph<N, A>.Node node) Callback method called before exiting a node.voidStops this agent and forgets its moving path and target.protected voidprotected voidvoidRemoves all callback positions of this agent.protected voidprotected voidengine()Returns the reference to theEnginethat is used to simulate this agent's behaviorprotected voidprotected voidenterNodeInternal(Graph<N, A>.Node node) protected voidenterSegment(Segment segment, double segmentAbsOffset) protected voiddoubleReturns the non-negative absolute offset of position on arc where this agent is currently located, or-1if this agent is not currently located on an arc.doubleReturns the current heading of this agent if this agent is currently on an arc.Returns the currentPointin 2D space pointing to the current location of this agent, or a point with infinite coordinates (new Point(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY)if this agent is not currently in graph.doubleReturns the current absolute offset of this agent's position on its trajectory, or 0 if this agent does not have any trajectory.Returns the arc on which this agent is currently located, ornullif this agent is not currently located on an arcdoubleReturns the non-negative absolute offset of position on arc where this agent is currently located, or-1if this agent is not currently located on an arcReturns the node where this agent is currently located, ornullif this agent is not currently located at a nodeReturns the currentPointwhere this agent is currently located in 2D space.Returns current position of this agent in form ofGeometricGraphPosition, ornullif this agent is not currently in graphdoubleGets the distance this agent traveled since its creation or lastresetDistanceTraveled()call.GraphEnvironment<N, A, ?> Returns aGraphEnvironmentwhich has been set to this agent, ornullif no environment has been set to this agentReturns the current path of this agent in form ofAgentGraphPath, ornullif this agent has currently no pathdoubleReturns the current heading of this agent if this agent is currently on an arc.getName()Returns the name of this agentprotected AgentGraphPath<N, A> getShortestPath(GeometricGraphPosition<N, A> destPosition) protected AgentGraphPath<N, A> protected AgentGraphPath<N, A> getShortestPathToClosest(List<GeometricGraphPosition<N, A>> destPositions) Returns the current remaining trajectory of this agent, i.e.doubleReturns the current velocity of this agent, or zero if this agent is not movingbooleanChecks if this agent has been set to aGraphEnvironment.booleanhasPath()Checks whether this agent currently has a path, i.e.booleanisAtNode()Checks whether this agent is currently located in some node of the graph of its graph environment.booleanChecks whether this agent is in graph, i.e.booleanisMoving()Checks whether this agent is currently moving, i.e.booleanisOnArc()Checks whether this agent is currently located on some arc of the graph of its graph environment.protected booleanisOtherAgentInNodeHavingSameDirection(GraphAgent<?, ?> otherAgent) protected booleanisPossibleToStartMoving(double velocity) Jumps this agent to the specified absolute offset of the specified arc of graph of this agent'sGraphEnvironment.Jumps this agent to the specified node of graph of this agent'sGraphEnvironment.jumpTo(GeometricGraphPosition<N, A> position) Jumps this agent to the specified position.Jumps this agent to the node having the specified value.protected voidjumpToInternal(Graph<N, A>.Arc arc, double arcAbsOffset) protected voidjumpToInternal(Graph<N, A>.Node node) protected voidprotected voidprotected voidmoveAlongPath(AgentGraphPath<N, A> path, double 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<N, A> 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.Starts moving this agent to the node in the graph having the specified value with the specified velocity along the shortest path from agent's current position.moveToClosest(List<GeometricGraphPosition<N, A>> 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.protected voidmoveToClosestInternal(List<GeometricGraphPosition<N, A>> destPositions, double velocity) protected voidprotected voidmoveToInternal(GeometricGraphPosition<N, A> destPosition, double velocity) voidonCallbackPositionReached(GeometricGraphPosition<N, A> postition, boolean isAgentCallback) Callback method called when this agent reaches a callback position.voidonDestinationReached(GeometricGraphPosition<N, A> destPosition) Callback method called after this agent reaches its destination, i.e.voidonOtherAgentReached(GraphAgent<N, A> otherAgent, boolean isMovingInSameDirection) Callback method called when this agent reaches another agent, i.e.voidonPathNotFound(List<GeometricGraphPosition<N, A>> possibleDestinationPositions) Callback method called when no path can be found from this agent's position to the destination specified in one of the overloads ofmoveToormoveToClosestmethod.protected voidvoidremoveCallbackPosition(GeometricGraphPosition<N, A> position) Removes a callback position previously added byaddCallbackPosition(GeometricGraphPosition)method.voidRemoves this agent from theGraphEnvironmentwhich it has been set to.protected voidvoidResets the built-in distance traveled counter to zero.protected voidsetCurrentPathInternal(AgentGraphPath<N, A> path) voidsetGraphEnvironment(GraphEnvironment<N, A, ?> graphEnvironment) Sets theGraphEnvironmentto this agent.voidsetVelocity(double newVelocity) Sets the new velocity to this agent.protected voidsetVelocityInternal(double newVelocity) protected voidstartMovingAlongPath(double velocity) doubletime()Returns the current simulation timeprotected voidtryExecuteCommand(Runnable runnable, Command.Type type) protected voidprotected voidMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, 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
-
lastTrajectoryNodeIndex
protected int lastTrajectoryNodeIndex -
lastTrajectoryPointIndex
protected int lastTrajectoryPointIndex -
basis
protected int basis -
engine
-
-
Constructor Details
-
GraphAgent
Creates a new instance ofGraphAgentthat is simulated with the specifiedEngine.- Parameters:
engine- specified engine
-
-
Method Details
-
time
public double time()Description copied from interface:AgentReturns the current simulation time -
getCurrentNode
Returns the node where this agent is currently located, ornullif this agent is not currently located at a node- Returns:
- node where this agent is currently located, or
null
-
getCurrentArc
Returns the arc on which this agent is currently located, ornullif this agent is not currently located on an arc- Returns:
- arc on which this agent is currently located, or
null - See Also:
-
getCurrentArcAbsOffset
public double getCurrentArcAbsOffset()Returns the non-negative absolute offset of position on arc where this agent is currently located, or-1if this agent is not currently located on an arc- Returns:
- absolute offset of position on arc where this agent is currently
located, or
-1 - See Also:
-
getCurrentAnimationPoint
Returns the currentPointin 2D space pointing to the current location of this agent, or a point with infinite coordinates (new Point(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY)if this agent is not currently in graph.This method must be called from animation when it is needed to determine where to show this agent. Calling
getCurrentPoint()from animation might result in non-reproducibility of the model.- Returns:
- current
Pointin 2D space pointing to the current location of this agent, or a point with infinite coordinates
-
getCurrentPosition
Returns current position of this agent in form ofGeometricGraphPosition, ornullif this agent is not currently in graph- Returns:
GeometricGraphPositionpointing to current position of this agent, ornull- See Also:
-
getGraphPath
Returns the current path of this agent in form ofAgentGraphPath, ornullif this agent has currently no path- Returns:
- current path of this agent in form of
AgentGraphPath, ornull
-
getTrajectory
Returns the current remaining trajectory of this agent, i.e. thePolylineahead of this agent that it will travel according to its path. Returned polyline includes the currentSegmentand all segments ahead of this agent. Returnsnullif this agent has currently no path- Returns:
Polylinerepresenting the remaining trajectory of this agent, ornull
-
setVelocity
public void setVelocity(double newVelocity) Sets the new velocity to this agent. If this agent is moving and the specified new velocity does not equal to agent's current velocity, this agent's velocity gets changed immediately.Positive velocity can only be set if this agent has its path (i.e.
). Otherwise, ahasPath()== trueRuntimeExceptionis thrownNegative and infinite velocities are not allowed. If such value is passed, a
RuntimeExceptionis thrown- Parameters:
newVelocity- - specified velocity- See Also:
-
getVelocity
public double getVelocity()Returns the current velocity of this agent, or zero if this agent is not moving- Returns:
- current velocity of this agent
-
isMoving
public boolean isMoving()Checks whether this agent is currently moving, i.e. its velocity is greater than zero ()getVelocity()>0- Returns:
trueis this agent is moving,falseotherwise- See Also:
-
cancelMoving
public void cancelMoving()Stops this agent and forgets its moving path and target. More specifically:- If this agent is moving, stops moving
- If this agent has a path (even if this agent is not moving), erases this path, sets the path to
null- If this agent does not have a path, does nothing
- See Also:
-
moveTo
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
setVelocity(double)is called with a positive value.If no path is found from agent's current position to the specified position,
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.- Parameters:
destPosition- specified destination position in the graph of this agent'sGraphEnvironmentvelocity- specified velocity. Must be finite and non-negative- See Also:
-
moveToClosest
public CompletionStage<Void> moveToClosest(List<GeometricGraphPosition<N, A>> 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.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
setVelocity(double)is called with a positive value.If no path is found from agent's current position to either of the specified positions,
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.- 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:
-
jumpTo
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 placed
-
jumpTo
Jumps this agent to the specified node 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:
node- node of the graph of this agent'sGraphEnvironmentwhere this agent will be placed
-
jumpTo
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 placed
-
tryUpdatePosition
protected void tryUpdatePosition() -
enterSegment
-
onSegmentExited
protected void onSegmentExited() -
beforeExitedArc
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 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
- Parameters:
arc- arc being exited- See Also:
-
afterEnteredNode
Callback method called immediately after entering a node. To be overridden in subclasses. This agent is already in the specified node when this method is called- Parameters:
node- node being entered- See Also:
-
beforeExitedNode
Callback method called before exiting a node. To be overridden in subclasses. This agent is still in the specified node when this method is called- Parameters:
node- node being exited- See Also:
-
afterEnteredArc
Callback method called immediately after entering an arc. To be overridden in subclasses. This agent is already on the specified arc when this method is calledEvent of entering an arc can happen in 3 cases:
- in process of moving
- after jumping to a position on the specified arc
- after turning to opposite arc
- Parameters:
arc- arc being entered- See Also:
-
onDestinationReached
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 ofmoveToormoveToClosestmethod.Note that
jumpTomethods do not initiate movements and thus do not lead to calling this callback.- Parameters:
destPosition- the position that has been reached- See Also:
-
jumpToPathDestPosition
protected void jumpToPathDestPosition() -
startMovingAlongPath
protected void startMovingAlongPath(double velocity) -
isPossibleToStartMoving
protected boolean isPossibleToStartMoving(double velocity) -
addCallbackPosition
Adds a callback position that will initiate callingonCallbackPositionReached(GeometricGraphPosition, boolean)method when this agent reaches the specified position.If callback positions are disabled in this agent's
GraphEnvironment, throws aRuntimeException. Callback positions can be enabled or disabled in constructor ofGraphEnvironment(boolean, boolean, boolean, boolean, boolean)The methodGraphEnvironment.isCallbackPositionsEnabled()checks whether the callback positions are enabled- Parameters:
position- specified position- See Also:
-
removeCallbackPosition
Removes a callback position previously added byaddCallbackPosition(GeometricGraphPosition)method. Does nothing if no such callback position has previously been added.- Parameters:
position- callback position to be removed- See Also:
-
onCallbackPositionReached
public void onCallbackPositionReached(GeometricGraphPosition<N, A> postition, boolean isAgentCallback) Callback method called when this agent reaches a callback position. To be overridden in subclasses.- Parameters:
postition- callback position that have been reachedisAgentCallback-trueif the callback position is this agent's callback position added byaddCallbackPosition(GeometricGraphPosition)method,falseotherwise
-
onOtherAgentReached
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.- Parameters:
otherAgent- another agentisMovingInSameDirection-trueif both agents are moving in the same direction, falseotherwise
-
clearCallbackPositions
public void clearCallbackPositions()Removes all callback positions of this agent. -
checkEvents
protected void checkEvents() -
setGraphEnvironment
Sets theGraphEnvironmentto this agent. This agent starts to live in the specified graph environment after this call. If this agent has been assigned to some other graph environment, removes it from that graph environment before placing into the specified one.- Parameters:
graphEnvironment- graph environment to be set to this agent
-
isInGraph
public boolean isInGraph()Checks whether this agent is in graph, i.e. ithasGraphEnvironment()and it has been jumped to some position of the graph environment's graph.This method returns
trueif and only ifgetCurrentPosition()!= null- Returns:
trueif this agent is in graph,falseotherwise- See Also:
-
isAtNode
public boolean isAtNode()Checks whether this agent is currently located in some node of the graph of its graph environment.The result of this method is equivalent to result of the following expression:
isInGraph() && getCurrentNode() != null- Returns:
trueif this agent is in a node,falseotherwise- See Also:
-
isOnArc
public boolean isOnArc()Checks whether this agent is currently located on some arc of the graph of its graph environment.The result of this method is equivalent to the result of the following expression:
isInGraph()&&getCurrentArc()!= null- Returns:
trueif this agent is on an arc,falseotherwise
-
hasPath
public boolean hasPath()Checks whether this agent currently has a path, i.e. it has some way and destination to reach. The path can be retrieved bygetGraphPath()method.- Returns:
trueif this agent currently has a path,falseotherwise- See Also:
-
hasGraphEnvironment
public boolean hasGraphEnvironment()Checks if this agent has been set to aGraphEnvironment. This can be done withsetGraphEnvironment(GraphEnvironment)method- Returns:
trueif this agent has been set to aGraphEnvironment,falseotherwise
-
onPathNotFound
Callback method called when no path can be found from this agent's position to the destination specified in one of the overloads ofmoveToormoveToClosestmethod.- See Also:
-
setCurrentPathInternal
-
eraseCurrentPathInternal
protected void eraseCurrentPathInternal() -
leaveCurrentNodeInternal
protected void leaveCurrentNodeInternal() -
leaveCurrentArcInternal
protected void leaveCurrentArcInternal() -
enterNodeInternal
-
enterArcInternal
-
jumpTo
Jumps this agent to the node having the specified value. If this agent is moving or has a path, cancels its movement and forgets its path before jumping.- Parameters:
nodeValue- value of the node in the graph of this agent'sGraphEnvironmentwhere this agent will be placed
-
getGraphEnvironment
Returns aGraphEnvironmentwhich has been set to this agent, ornullif no environment has been set to this agent- Returns:
GraphEnvironmentwhich has been set to this agent, ornull- See Also:
-
moveTo
Starts moving this agent to the node in the graph having the specified value 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
setVelocity(double)is called with a positive value.If no path is found from agent's current position to the specified node,
onPathNotFound(List<GeometricGraphPosition<N, A>>)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.- Parameters:
nodeValue- value of the node in the graph of this agent'sGraphEnvironmentwhich this agent will be sent tovelocity- specified velocity. Must be finite and non-negative- See Also:
-
enableCommandsExecution
protected void enableCommandsExecution() -
cancelMovingInternal
protected void cancelMovingInternal() -
moveToInternal
-
moveToClosestInternal
protected void moveToClosestInternal(List<GeometricGraphPosition<N, A>> destPositions, double velocity) -
getShortestPathToClosest
protected AgentGraphPath<N,A> getShortestPathToClosest(List<GeometricGraphPosition<N, A>> destPositions) -
getShortestPath
-
getShortestPath
-
jumpToInternal
-
jumpToInternal
-
disableCommandsExecution
protected void disableCommandsExecution() -
setVelocityInternal
protected void setVelocityInternal(double newVelocity) -
getHeading
public double getHeading()Returns the current heading of this agent if this agent is currently on an arc. Current heading is the heading of theSegmentwhich this agent is currently on. If this agent is at a node, returns zero.The heading is calculated according to the rules described for
Segment.getHeading()method.- Returns:
- current heading of this agent, in radians, or 0 if this agent is at a node
-
getCurrentAnimationHeading
public double getCurrentAnimationHeading()Returns the current heading of this agent if this agent is currently on an arc. Current heading is the heading of theSegmentwhich this agent is currently on. If this agent is at a node, returns zero.This method must be called from animation when it is needed to determine where to show this agent. Calling
getHeading()from animation might result in non-reproducibility of the model.- Returns:
- current heading of this agent, in radians, or 0 if this agent is at a node
-
isOtherAgentInNodeHavingSameDirection
-
moveTo
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.
- Parameters:
indicator- thePredicatewhich must returntrueto indicate that a node is a valid destination
-
moveToIndicatorInternal
-
getCurrentPoint
Returns the currentPointwhere this agent is currently located in 2D space. If this agent is not in a graph, returns a point with infinite coordinates (new Point(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY)).- Returns:
- current
Pointwhere this agent is currently located in 2D space
-
getCurrentAnimationArcAbsOffset
public double getCurrentAnimationArcAbsOffset()Returns the non-negative absolute offset of position on arc where this agent is currently located, or-1if this agent is not currently located on an arc.This method must be called from animation when it is needed to determine where to show this agent. Calling
getCurrentArcAbsOffset()from animation might result in non-reproducibility of the model.- Returns:
- absolute offset of position on arc where this agent is currently
located, or
-1 - See Also:
-
resetDistanceTraveled
public void resetDistanceTraveled()Resets the built-in distance traveled counter to zero.- See Also:
-
getDistanceTraveled
public double getDistanceTraveled()Gets the distance this agent traveled since its creation or lastresetDistanceTraveled()call.- Returns:
- distance this agent traveled since its creation or last
resetDistanceTraveled()call
-
tryUpdateDistanceTraveled
protected void tryUpdateDistanceTraveled() -
allowEnterArc
Callback method used to implement advanced functionality like passby or waiting for something specific before entering arcs. The basic implementation is always to returntrue. To be overridden in subclasses.- Parameters:
arc- arc that this agent attempts to enter- Returns:
trueif this agent should be allowed to enter the specified arc,falseotherwise
-
moveTo
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
setVelocity(double)is called with a positive value.If no path is found from agent's current position to the specified node,
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.- 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:
-
getCurrentAnimationTrajectoryAbsoluteOffset
public double getCurrentAnimationTrajectoryAbsoluteOffset()Returns the current absolute offset of this agent's position on its trajectory, or 0 if this agent does not have any trajectory.- Returns:
- current absolute offset of this agent's position on its trajectory, or 0
-
tryExecuteCommand
-
moveAlongPath
-
removeFromGraphInternal
protected void removeFromGraphInternal() -
removeFromGraphEnvironment
public void removeFromGraphEnvironment()Removes this agent from theGraphEnvironmentwhich it has been set to. -
getName
Description copied from interface:AgentReturns the name of this agent -
engine
Description copied from interface:AgentReturns the reference to theEnginethat is used to simulate this agent's behavior
-