AnyLogic
Expand
Font size
All Implemented Interfaces:
com.anylogic.engine.internal.Child, Serializable

public class Statechart<T extends Enum<T> & IStatechartState<?,T>>
extends Object
implements Serializable, com.anylogic.engine.internal.Child
Statechart - the most advanced construct to describe event- and time-driven behavior. Statechart has states and transitions. Transitions may be triggered by timeouts or rates, messages received by the statechart, and conditions. Transition execution may lead to a state change where a new set of transitions becomes active. States in the statechart may be hierarchical, i.e. contain other states and transitions. The actual structure of state diagram is stored in the agent.
There are two ways to send a message to the statechart:
- call receiveMessage(Object) or receiveMessage(int) method, and
- call fireEvent(Object) method.
receiveMessage() assumes no queuing for incoming messages. If the received message cannot immediately cause scheduling of a transition, it is discarded. Therefore, if, for example, there are two transitions: one (from state S0 to S1) triggered by message A, and another (from S1 to S2) triggered by message B, and the statechart receives messages A and B at the same time while in the state S0, only first transition will be taken, and message B will be discarded.
fireEvent() supports queuing for incoming messages. The message added to the queue by fireEvent() can be consumed either immediately or after a number of zero-time steps of the statechart, otherwise it will be discarded. In the example above both transitions will be taken if the messages A and B are received via fireEvent() method.
Using fireEvent() is less efficient than using receiveMessage() both time and memory-wise, so if you do care and are sure that no "chains" of zero-time message-triggered transitions can happen, use receiveMessage().
Memory: sizeof(Object) + 18 bytes + sizeof(array with concurrently active transitions) + sizeof(message queue)
Author:
AnyLogic North America, LLC https://anylogic.com
See Also:
Serialized Form

Constructor Summary

ConstructorDescription
Statechart(Agent ao, short maxat)
Constructs the statechart object.

Method Summary

Modifier and TypeMethodDescription
voidfireEvent(Object msg)
Adds a message to the statechart queue.
AgentgetActiveObject()
Deprecated.
Use getAgent() instead
TgetActiveSimpleState()
Returns the currently active simple state of the statechart
AgentgetAgent()
Returns the agent that owns the statechart.
StringgetFullName()
Returns the name of the statechart prefixed by the full name of its agent.
Set<T>getFullState()
Returns the currently active composite states of the statechart, including the current simple state.
StringgetName()
Returns the name of the statechart as specified by the user
TgetState()
Returns the currently active simple state of the statechart.
Please note that this function doesn't return composite states
booleanisStateActive(IStatechartState state)
Returns true if the statechart is at the specified state, i.e.
voidonChange()
Should be called if the statechart has at least one transition of type Condition or Rate when something changes in the agent and probably rate changes or condition becomes true.
voidonDestroy()
Should be called when the statechart is destroyed, e.g.
booleanreceiveMessage(int msg)
Same as receiveMessage(Object) but with an integer as message.
booleanreceiveMessage(Object msg)
Posts a message to the statechart without queueing: the message is either immediately consumed (if there is a matching transition active) or is discarded.
voidrestoreOwner(Object owner)
Deprecated.
voidsetActiveState_xjal(T st)
This method is shouldn't be called by user (is public due to technical reasons)
voidstart()
Should be called when the agent starts.
StringtoString() 

Methods inherited from class java.lang.Object

equals, getClass, hashCode, notify, notifyAll, wait, wait, wait

Constructor Details

Statechart

public Statechart(Agent ao,
 short maxat)
Constructs the statechart object. Does not start the statechart.
Parameters:
ao - agent where this statechart belongs to
maxat - maximum possible number of concurrently active transitions

Method Details

getName

public String getName()
Returns the name of the statechart as specified by the user
Returns:
The name of the statechart.

getFullName

public String getFullName()
Returns the name of the statechart prefixed by the full name of its agent.
Returns:
The full name of the statechart.

toString

public String toString()
Overrides:
toString in class Object

getAgent

public Agent getAgent()
Returns the agent that owns the statechart.
Returns:
The agent owning this statechart.

getActiveObject

@Deprecated
public Agent getActiveObject()
Deprecated.
Use getAgent() instead

receiveMessage

public boolean receiveMessage(Object msg)
Posts a message to the statechart without queueing: the message is either immediately consumed (if there is a matching transition active) or is discarded. If a message matches two or more transitions, they will be able to execute independently (subject to the statechart structure), thus consuming the same message. If two or more messages arrive simultaneously and both match the trigger of a transition, the second message will be ignored.
Parameters:
msg - the message posted to the statechart
Returns:
true if the message matched at least one trigger

receiveMessage

public boolean receiveMessage(int msg)
Same as receiveMessage(Object) but with an integer as message.
Parameters:
msg - the integer posted to the statechart
Returns:
true if the message matched at least one trigger

isStateActive

public boolean isStateActive(IStatechartState state)
Returns true if the statechart is at the specified state, i.e. exactly in the state for a simple state and in one of its inner states for a composite state.
Parameters:
state - the state
Returns:
true if state is currently active

getState

public T getState()
Returns the currently active simple state of the statechart.
Please note that this function doesn't return composite states
Returns:
the current simple state
Since:
7.3.7
See Also:

getActiveSimpleState

public T getActiveSimpleState()
Returns the currently active simple state of the statechart
Returns:
the current simple state

getFullState

public Set<T> getFullState()
Returns the currently active composite states of the statechart, including the current simple state. The returned set is unordered.
Since:
8.0

fireEvent

public void fireEvent(Object msg)
Adds a message to the statechart queue. It will be consumed either immediately or after a number of zero-time steps of the statechart, otherwise it will be discarded.
Parameters:
msg - the message received by the statechart

start

@AnyLogicInternalCodegenAPI
public void start()
Should be called when the agent starts. Activates the statechart: starts the initially active transitions, performs the entry actions, etc.

setActiveState_xjal

@AnyLogicInternalCodegenAPI
public void setActiveState_xjal(T st)
This method is shouldn't be called by user (is public due to technical reasons)

onChange

public void onChange()
Should be called if the statechart has at least one transition of type Condition or Rate when something changes in the agent and probably rate changes or condition becomes true.

onDestroy

@AnyLogicInternalCodegenAPI
public void onDestroy()
Should be called when the statechart is destroyed, e.g. when the owner agent is destroyed or when statechart comes to final state.
Deletes all events scheduled by the currently active statechart transitions and unsubmits all their conditions from the numeric engine

restoreOwner

@AnyLogicInternalCodegenAPI
@Deprecated
public void restoreOwner(Object owner)
Deprecated.
This method normally should not be called by user
This method restores owner of this object
The method is used in snapshot saving/loading
Specified by:
restoreOwner in interface com.anylogic.engine.internal.Child
Parameters:
owner - owner of this object, usually Agent, Experiment or ShapeGroup