Action Charts. Defining Algorithms Visually

Complex simulation modeling cannot go without algorithms that usually perform some data processing or calculations. AnyLogic supports action charts - structured block charts allowing defining algorithms graphically in the style of structured programming. We use widely known extension of an approach suggested by Dijkstra that splits algorithms into sub-sections with a single point of entry. It states that three ways of combining programs—sequencing, selection, and iteration—are sufficient to express any computable algorithm. The style reduces understanding an algorithm to understanding each structure on its own. 

Action charts are very helpful since using them you can define algorithms even if you are not familiar with syntax of Java operators.

In fact, action chart visually defines a function and you can use ordinary AnyLogic functions as before. However, using action charts gives you one more evident benefit: it visualizes the implemented algorithm, making it more intuitive to other users.

Action chart is constructed from the following blocks located on the Actionchart page of the Palette view:

Action Chart Action Chart creates the basic action chart consisting of a starting point and "return" block. Defines the general action chart properties.
Code
Code 
block allows inserting a code snippet performing some action into your action chart. 
Decision
Decision
block is the simpliest way to route the algorithm flow depending on the condition. The block has two exit branches - true and false.  When the control reaches the "decision" block, it decides which branch to take. If the condition defined for this block is met, true branch is taken. Otherwise, false branch is taken.
Local Variable
Is used to declare local variable into an action chart. The local variable is visible only down the action chart starting from the declaration point.
While Loop
Iteration loop. Loop actions are executed if the condition defined for this loop evaluates to true. The condition is evaluated once at the beginning of the loop and again before each further iteration of the action.
Do While Loop
Iteration loop. Loop actions are executed if the condition defined for this loop evaluates to true. The condition is evaluated once at the beginning of the loop and again before each further iteration of the action.
For Loop

Iteration loop. There are two forms of a "for loop".

Collection Iterator allows iterating through a collection. On each iteration you can perform a set of actions with next in turn collection item.

Generic "for loop" executes a set of actions several times, until the specified condition is met.
Return
Return
block plays two roles: it specifies the value the action chart will return (if it's return type is not void) and also causes that value to be returned immediately.
Break
Break
block controls the flow of the loop. It stops the current iteration of the loop (and optionally quits the loop without executing the rest of the iterations).

To draw an action chart, you first place  Action Chart onto the graphical diagram. This creates the basic construction consisting of starting point and "return" block drawn. From this point on, you insert required blocks into their place inside the created action chart according to the logic of the implemented algorithm. 

Action charts are called in the same way as functions - you write the action chart name followed by parentheses. In the case action chart has some arguments, you should pass argument values separated by commas inside the parentheses. The values should be provided in the order they are defined in the Action Chart.


Related topics

 Creating an action chart. Tutorial