Font size

Checking the agent’s state

Using statechart’s API

To find out which statechart’s state is currently active, use the statechart’s getState() or getActiveSimpleState() function. Both functions do the same — they return the current active simple state of the statechart. For instance, to get the current active state of the agent’s statechart from the action of the agent’s event or some other element, call:


To check whether an agent is currently in some particular state of its statechart, you can use the statechart’s isStateActive() function. The function returns true if the specified state is currently active, and false otherwise.

Example: statechart.isStateActive(Busy);

Using agent’s API

You can also use the inState() function of the agent for the same purpose. For example, in the code of the agent-truck of type Truck (and in actions of any elements defined on its diagram), simply call inState(Moving).

When you call this function from the code of the truck’s owner (e.g., from Main), start the function call with referring to this agent-truck of type Truck and add the agent type prefix to the checked state name (Truck.Moving):



Demo model: HIV Diffusion and Syringe Usage

Study the given demo model to see the inState() function usage. In this model agents of type IDUser have two statecharts. You call the function in different ways depending on the current context — the place where you are calling the function from. If you search through this model for the inState text, you will find the following text occurrences:

  1. In the Main agent, there is the IDUsers agent population. In the Statistics section of its properties, there is the NInfected statistics function defined, which calculates the number of infected people. Here you can see the following code:

    item.inState( IDUser.Infected )

    In this particular place, when you define the Condition of the statistics function for the agent population, you access the agent — item of the agent population as item. As the function argument, you pass the name of the agent’s state prefixed with the name of the agent type: IDUser.Infected.

  2. In the UpdateHistograms event’s Action, you can find the following code:

    for( IDUser user : IDUsers ) {
      if( user.inState( IDUser.Susceptible ) )
        SusceptibleH.add( user.Experience );
        InfectedH.add( user.Experience );

    This event is defined in the Main agent type. The for loop iterates through the agents of the IDUsers agent population, each iterated agent is accessed here as user. So here the function is called with this user prefix: user.inState( IDUser.Susceptible )

  3. One more use case: dynamic expression defined in the Fill color property of the oval — animation shape of the IDUser:

    inState( Infected ) ? red : dodgerBlue

    Here the red color is used if the agent is currently in the Infected state. Please note that here the function is called without a prefix since you type this code for the element that is defined inside the IDUser agent type, whose statechart we analyze. For the same reason, the state name also does not have a prefix, it is accessed just as Infected, not as IDUser.Infected as we saw before.

How can we improve this article?