AnyLogic
Expand
Font size
  • java.lang.Object
    • com.anylogic.engine.Statechart<T>
All Implemented Interfaces:
com.anylogic.engine.internal.Child, java.io.Serializable

public class Statechart<T extends java.lang.Enum<T> & IStatechartState<?,​T>>
extends java.lang.Object
implements java.io.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

Constructors 
Statechart​(Agent ao, short maxat)
Constructs the statechart object.
Constructor Description

Method Summary

All Methods Instance Methods Concrete Methods Deprecated Methods 
void fireEvent​(java.lang.Object msg)
Adds a message to the statechart queue.
Agent getActiveObject()
Deprecated.
Use getAgent() instead
T getActiveSimpleState()
Returns the currently active simple state of the statechart
Agent getAgent()
Returns the agent that owns the statechart.
java.lang.String getFullName()
Returns the name of the statechart prefixed by the full name of its agent.
java.util.Set<T> getFullState()
Returns the currently active composite states of the statechart, including the current simple state.
java.lang.String getName()
Returns the name of the statechart as specified by the user
T getState()
Returns the currently active simple state of the statechart.
Please note that this function doesn't return composite states
boolean isStateActive​(IStatechartState state)
Returns true if the statechart is at the specified state, i.e.
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.
void onDestroy()
Should be called when the statechart is destroyed, e.g.
boolean receiveMessage​(int msg)
Same as receiveMessage(Object) but with an integer as message.
boolean receiveMessage​(java.lang.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.
void restoreOwner​(java.lang.Object owner)
Deprecated.
void setActiveState_xjal​(T st)
This method is shouldn't be called by user (is public due to technical reasons)
void start()
Should be called when the agent starts.
java.lang.String toString()  
Modifier and Type Method Description

Methods inherited from class java.lang.Object

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

Constructor Detail

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 Detail

getName

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

getFullName

public java.lang.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 java.lang.String toString()
Overrides:
toString in class java.lang.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​(java.lang.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:
Agent.inState(IStatechartState), getFullState(), isStateActive(IStatechartState)

getActiveSimpleState

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

getFullState

public java.util.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​(java.lang.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​(java.lang.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
How can we improve this article?