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 ofGraphAgent
that is simulated with the specifiedEngine
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addCallbackPosition
(GeometricGraphPosition<N, A> position) Adds a callback position that will initiate callingonCallbackPositionReached(GeometricGraphPosition, boolean)
method when this agent reaches the specified position.void
afterEnteredArc
(Graph<N, A>.Arc arc) Callback method called immediately after entering an arc.void
afterEnteredNode
(Graph<N, A>.Node node) Callback method called immediately after entering a node.boolean
allowEnterArc
(Graph<N, A>.Arc arc) Callback method used to implement advanced functionality like passby or waiting for something specific before entering arcs.void
beforeExitedArc
(Graph<N, A>.Arc arc) Callback method called before exiting entering an arc.void
beforeExitedNode
(Graph<N, A>.Node node) Callback method called before exiting a node.void
Stops this agent and forgets its moving path and target.protected void
protected void
void
Removes all callback positions of this agent.protected void
protected void
engine()
Returns the reference to theEngine
that is used to simulate this agent's behaviorprotected void
protected void
enterNodeInternal
(Graph<N, A>.Node node) protected void
enterSegment
(Segment segment, double segmentAbsOffset) protected void
double
Returns the non-negative absolute offset of position on arc where this agent is currently located, or-1
if this agent is not currently located on an arc.double
Returns the current heading of this agent if this agent is currently on an arc.Returns the currentPoint
in 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.double
Returns 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, ornull
if this agent is not currently located on an arcdouble
Returns the non-negative absolute offset of position on arc where this agent is currently located, or-1
if this agent is not currently located on an arcReturns the node where this agent is currently located, ornull
if this agent is not currently located at a nodeReturns the currentPoint
where this agent is currently located in 2D space.Returns current position of this agent in form ofGeometricGraphPosition
, ornull
if this agent is not currently in graphdouble
Gets the distance this agent traveled since its creation or lastresetDistanceTraveled()
call.GraphEnvironment
<N, A, ?> Returns aGraphEnvironment
which has been set to this agent, ornull
if no environment has been set to this agentReturns the current path of this agent in form ofAgentGraphPath
, ornull
if this agent has currently no pathdouble
Returns 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.double
Returns the current velocity of this agent, or zero if this agent is not movingboolean
Checks if this agent has been set to aGraphEnvironment
.boolean
hasPath()
Checks whether this agent currently has a path, i.e.boolean
isAtNode()
Checks whether this agent is currently located in some node of the graph of its graph environment.boolean
Checks whether this agent is in graph, i.e.boolean
isMoving()
Checks whether this agent is currently moving, i.e.boolean
isOnArc()
Checks whether this agent is currently located on some arc of the graph of its graph environment.protected boolean
isOtherAgentInNodeHavingSameDirection
(GraphAgent<?, ?> otherAgent) protected boolean
isPossibleToStartMoving
(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 void
jumpToInternal
(Graph<N, A>.Arc arc, double arcAbsOffset) protected void
jumpToInternal
(Graph<N, A>.Node node) protected void
protected void
protected void
moveAlongPath
(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 void
moveToClosestInternal
(List<GeometricGraphPosition<N, A>> destPositions, double velocity) protected void
protected void
moveToInternal
(GeometricGraphPosition<N, A> destPosition, double velocity) void
onCallbackPositionReached
(GeometricGraphPosition<N, A> postition, boolean isAgentCallback) Callback method called when this agent reaches a callback position.void
onDestinationReached
(GeometricGraphPosition<N, A> destPosition) Callback method called after this agent reaches its destination, i.e.void
onOtherAgentReached
(GraphAgent<N, A> otherAgent, boolean isMovingInSameDirection) Callback method called when this agent reaches another agent, i.e.void
onPathNotFound
(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 ofmoveTo
ormoveToClosest
method.protected void
void
removeCallbackPosition
(GeometricGraphPosition<N, A> position) Removes a callback position previously added byaddCallbackPosition(GeometricGraphPosition)
method.void
Removes this agent from theGraphEnvironment
which it has been set to.protected void
void
Resets the built-in distance traveled counter to zero.protected void
setCurrentPathInternal
(AgentGraphPath<N, A> path) void
setGraphEnvironment
(GraphEnvironment<N, A, ?> graphEnvironment) Sets theGraphEnvironment
to this agent.void
setVelocity
(double newVelocity) Sets the new velocity to this agent.protected void
setVelocityInternal
(double newVelocity) protected void
startMovingAlongPath
(double velocity) double
time()
Returns the current simulation timeprotected void
tryExecuteCommand
(Runnable runnable, Command.Type type) protected void
protected void
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods 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 ofGraphAgent
that is simulated with the specifiedEngine
.- Parameters:
engine
- specified engine
-
-
Method Details
-
time
public double time()Description copied from interface:Agent
Returns the current simulation time -
getCurrentNode
Returns the node where this agent is currently located, ornull
if 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, ornull
if 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-1
if 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 currentPoint
in 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
Point
in 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
, ornull
if this agent is not currently in graph- Returns:
GeometricGraphPosition
pointing to current position of this agent, ornull
- See Also:
-
getGraphPath
Returns the current path of this agent in form ofAgentGraphPath
, ornull
if 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. thePolyline
ahead of this agent that it will travel according to its path. Returned polyline includes the currentSegment
and all segments ahead of this agent. Returnsnull
if this agent has currently no path- Returns:
Polyline
representing 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()
== trueRuntimeException
is thrownNegative and infinite velocities are not allowed. If such value is passed, a
RuntimeException
is 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:
true
is this agent is moving,false
otherwise- 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
, aRuntimeException
is thrown.- Parameters:
destPosition
- specified destination position in the graph of this agent'sGraphEnvironment
velocity
- 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
, aRuntimeException
is 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'sGraphEnvironment
where 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'sGraphEnvironment
where 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'sGraphEnvironment
where 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 ofmoveTo
ormoveToClosest
method.Note that
jumpTo
methods 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
-true
if the callback position is this agent's callback position added byaddCallbackPosition(GeometricGraphPosition)
method,false
otherwise
-
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
-true
if both agents are moving in the same direction, false
otherwise
-
clearCallbackPositions
public void clearCallbackPositions()Removes all callback positions of this agent. -
checkEvents
protected void checkEvents() -
setGraphEnvironment
Sets theGraphEnvironment
to 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
true
if and only ifgetCurrentPosition()
!= null- Returns:
true
if this agent is in graph,false
otherwise- 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:
true
if this agent is in a node,false
otherwise- 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:
true
if this agent is on an arc,false
otherwise
-
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:
true
if this agent currently has a path,false
otherwise- See Also:
-
hasGraphEnvironment
public boolean hasGraphEnvironment()Checks if this agent has been set to aGraphEnvironment
. This can be done withsetGraphEnvironment(GraphEnvironment)
method- Returns:
true
if this agent has been set to aGraphEnvironment
,false
otherwise
-
onPathNotFound
Callback method called when no path can be found from this agent's position to the destination specified in one of the overloads ofmoveTo
ormoveToClosest
method.- 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'sGraphEnvironment
where this agent will be placed
-
getGraphEnvironment
Returns aGraphEnvironment
which has been set to this agent, ornull
if no environment has been set to this agent- Returns:
GraphEnvironment
which 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
, aRuntimeException
is thrown.- Parameters:
nodeValue
- value of the node in the graph of this agent'sGraphEnvironment
which 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 theSegment
which 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 theSegment
which 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
- thePredicate
which must returntrue
to indicate that a node is a valid destination
-
moveToIndicatorInternal
-
getCurrentPoint
Returns the currentPoint
where 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
Point
where 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-1
if 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:
true
if this agent should be allowed to enter the specified arc,false
otherwise
-
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
, aRuntimeException
is thrown.- Parameters:
node
- node in the graph of this agent'sGraphEnvironment
which 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 theGraphEnvironment
which it has been set to. -
getName
Description copied from interface:Agent
Returns the name of this agent -
engine
Description copied from interface:Agent
Returns the reference to theEngine
that is used to simulate this agent's behavior
-