May 2 / Justin Rawlings

A tiered interaction approach to AnyLogic animation

My preceding tips in the "Build your own level-up button" and "Dynamic animation tweaks" posts are useful on their own, but really shine when you use them to to define a consistent Interaction system in your model. If we impose that each Agent reacts the same way to a mouse click we can start using all tools that Object Oriented Programming gives us to reduce or eliminate repeated code.

Consistency across different agent types means the user experience becomes intuitive and informative. Note that when I say 'user' here I also mean you as a developer. Visually debugging a model is orders of magnitude quicker than parsing through log files or running the debugger. If you expect a line to be green and instead its red, something has clearly gone wrong.
To that end I suggest the following tips for dealing with a crowded and/or large Main presentation:

  • Keep it free of clutter
  • Use simple icons
  • Use color to reflect state changes
  • Be sparing with Text
  • Use a sequence of mouse clicks to display different levels of information

A simple yet effective way to handle clicks on an agent is the following:

  • The first click shows an Agents ID/Name and renders any LinkToAgent
  • Clicking on the ID/Name takes you inside that Agent
  • Clicking on a separate button displays detailed diagnostics

What we are really describing is that an Agent needs an 'Interaction' state. The sole driver of which is the mouse clicks on various parts of the presentation. As always the easiest way to model this is a Statechart.
As you can see the driver of this chart are all messages, these messages represent that various clicks that have occurred on the presentation. When dealing with state-chart transitions on message I have found it very helpful to define those messages as Java enum where appropriate.

For the Person agent the following messages suffice:
(you can define enumerations in the ‘Additional Code’ section of the Agent properties window)
By hooking into the 'onClick' options of individual presentation objects we can start driving the statechart:
A look at the Statechart transition that handles Click.ON_NAME shows that the response is to 'jump in' to the agent.
Take a look at the model for more details.

Although this is just a very simple simple statechart driven by a few mouse clicks, it serves as the ideal platform to drive dynamic styling of presentation shapes. Using the in-built AnyLogic functions for dealing with statecharts ( inState, isStateActive, getActiveSimpleState ) along with Java's ternary operators you can concisely and precisely control presentation visuals using the dynamic code parameter.

A rectangle that looks very different when highlighted

In the demo model you will see we use this strategy for controlling what is visible in each state:

The group containing the LineChart is only visible when inspecting

The final interaction sequence of our Person is now:
1: Default presentation
2: user clicks on icon
3: user clicks on box above icon
4: User clicks on different agents with different display states


Nothing here is particularly complex or earth-shattering, but you can see that with a few basic tools it is quite simple to build up a comprehensive interaction system in AnyLogic models.

By giving the user interaction of an Agent its own statechart and methods, you also get the
added bonus of removing that responsibility from other parts of the model. Ideally the only place you would see code pertaining to presentation would be in either the interaction statechart, or that presentation shapes dynamic parameters. Let agent behaviour statecharts and process flow blocks take care of core model logic, leave tweaking rectangle colors to someone else.
Hopefully you have picked up something that may be useful. As always the best way to learn is to explore yourself, so download ‘interaction_and_navigation.alp’ here:

Created with