AnyLogic 9
Expand
Font size

Statechart API

You can use the following functions to programmatically control statecharts and states.

Statechart functions
Function Description
Statechart<T> getStatechart(Agent agent) Returns the statechart from the given agent.
IStatechartState getActiveSimpleState() Returns the currently active simple state of the statechart.
IStatechartState getState() Returns the currently active simple state of the statechart.
This function does not return composite states.
Set<T> getFullState() Returns the unordered set of the currently active composite states from the statechart, including the current simple state.
String getName() Returns the name of the statechart.
String getFullName() Returns the name of the statechart prefixed by the full name of its agent.
boolean isStateActive(IStatechartState state) Returns true if the given state (simple or composite) is active, otherwise returns false.

state — the state
Agent getAgent() Returns the agent this statechart belongs to.
boolean inState(IStatechartState state) Returns true if the agent is in the specified state: the exact state for a simple state or one of the inner states for a composite state.

state — the state
boolean receiveMessage(Object msg) Posts a message to the statechart without queuing: 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 (according 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.
Returns true if the message matched at least one trigger; returns false otherwise.

msg — the message posted to the statechart
boolean receiveMessage(int msg) Same as receiveMessage(Object), but with an integer value as a message.
Returns true if the message matched at least one trigger, false otherwise.

msg — the integer posted to the statechart
fireEvent(Object msg) Adds a message to the statechart’s queue. The message will be consumed either immediately or after a number of zero-time steps of the statechart, otherwise it will be discarded.

msg — the message received by the statechart
void onChange() Should be called when something changes in the agent. If the statechart has at least one transition of the Condition or Rate type, this will allow to detect when the rate changes or a condition becomes true.
State functions
Function Description
IStatechartState getContainerState() Returns the immediate container state of this state, or null if this state is at the topmost level (has no container state).
Set<T> getFullState() Returns the unordered set containing this state and all composite states (if any) containing the current state.
Collection<T> getSimpleStatesDeep() Returns the collection of all simple states inside the current state.
Statechart<T> getStatechart(Agent agent) Returns the statechart for the given agent that owns this state. Returns null if the agent has no statechart with this state.

agent — the agent you want to get the statechart for
Set<T> getStatesInside() Returns an unordered set of all states (both simple and composite) contained inside this state.
boolean inState(Agent agent) Returns true if the current state is active for the specified agent; returns false otherwise.

agent — the agent that owns the current state
boolean containsState(IStatechartState simpleState) Checks if this state of a statechart is composite and contains the simpleState state (deep contents — inside contained composite states — are also checked).
boolean isSimpleState() Returns true if this state is a simple state; false if composite.
String name() Returns the name of this state.
int ordinal() Returns the ordinal number of this state (states are numbered in a deterministic order).
Transition functions
Function Description
boolean isActive() Returns true if the transition firing is scheduled, and false otherwise.
double getRest() Returns the time remaining before the scheduled transition fires, in model time units, or infinity if the firing is not scheduled.
double getRest(TimeUnits units) Returns the time remaining before the scheduled transition firing, in specified time units. For example, getRest(MINUTE) returns the remaining time in minutes.

units — the time units
How can we improve this article?