This tutorial will demonstrate you how to create action charts -
block charts allowing defining
algorithms graphically in the style of
structured programming. Action charts are very helpful since
using them you can
define algorithms even if you are
familiar with syntax of some Java operators. Morever, action chart gives
you one more evident benefit: it visualizes the
implemented algorithm, making it more intuitive to another
To simplify the teaching process we will build an action chart in the existing model - Billiard Balls. In this model several balls move within a 2D rectangular area. The balls elastically rebound from each other and from the area borders.
We will create an action chart defining an algorithm that is implemented in the current model using the checkHit() function. This algorithm checks whether a given billiard ball has collided with some other ball at the moment.
The algorithm loops over all balls present in the model (they are defined as a collection of agents of type Ball) and checks the conditions evidencing the ball collision. Two conditions should be met: the distance between the balls should be less than the ball diameter, and the balls should move towards each other. If the collision will be detected, the action chart will return the ball that collided with the given one.
Prepare the model for the tutorial
Now you have created the simpliest action
chart. From this point on, you
should insert required blocks into their places inside the
chart according to the logic of the algorithm you are
To find collisions of a given ball, we need to iteratively check all balls present in our model. So now we need to add a loop to our algorithm that will iterate through all balls present in the model and check the collision conditions.The best way to loop through a collection of objects is to use the For Loop. There are two forms of a "for loop" - Generic and Collection iterator. As its name says, Collection Iterator allows iterating through a collection. On each iteration you can perform a set of actions with next in turn collection item.
Insert a "for loop" into action chart
Drag the For Loop from the Actionchart palette into our action chart.
While moving the mouse over the graphical editor you will see insertion points of action chart indicated with little blue circles. In this tutorial we will help you to find the correct insertion points by providing you with illustrative figures. We will indicate the required insertion points on these figures with red arrows. Release the mouse button over the indicated insertion point. New "for loop" block will be inserted into this place.
Decision block is the simpliest way to route the algorithm flow. The block has two exit branches - true and false. You can define any sequence of actions for either of the two branches using other action chart blocks. When the control reaches the "decision" block, it decides which branch to take. If the condition defined for this "decision" block is met (i.e. it evaluates to true), true branch is taken. Otherwise, false branch is taken.Insert a "decision" block
Drag the Decision element from the Actionchart palette onto the diagram onto the insertion point inside the "for loop":
New "decision" block will be inserted into this place.
Go to the Properties view and specify the condition in the Condition field. Condition is Java expression that produces a boolean result. Type b==b1 here. You will see the condition shown inside the block on the diagram.
Now we want to skip our procedure of founding the ball collisions in the case we compare the ball with itself. We need just to place one string of code in the branch of the action chart that will be taken if the result of our condition will be true. We add code snippets performing some actions into the action chart using Code blocks. Code can be either a simple Java statement, or a set of statements, each one terminated by a semicolon. This code will be executed when the action chart control will reach the "code" block.Insert a "code" block
Drag the Code element from the Actionchart palette into the insertion point located in the true branch of the "decision" block.
New "code" block will be inserted into this place.
Go to the Properties
view. In the Code
field, type Java code you want to be executed when the control of the
action chart will reach this block. Type here
This code will be shown inside the block on the diagram.
Now our action chart will skip checking collisions of the ball with itself and will continue iterating through the collection of balls.
Now we want to implement the first collision condition - check that the balls are in the collision distance (the distance between the balls is less than the ball diameter).
For that, first we will declare a pair of local
variables to store intermediate calculation results. Variables in
action charts are defined using Local
Variable blocks. Please note
that a local variable is visible only down the action
chart starting from the declaration point.
Drag the Local Variable from the Actionchart palette into the insertion point at the bottom of the "for loop" area:
Declare one more local variable
more "decision" block
Now we need to check the second collision condition - that the balls are moving towards each other at the moment. We will implement this checking in the same way as the first one - first, we will declare some local variables and then add a "decision" block to perform checking the condition itself.Declare two local variables
Insert one more "decision" block
Now we need to specify values that our action chart will return. This can be done using Return blocks. 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.
First, in the case we have found a ball collision, we should return the ball that has collided with the one passed as the action chart argument.Insert a "return" block
Each branch of the action chart should end with the Return block. That is the reason that when we started drawing an action chart, one "return" block was automatically included into the action chart to make the construction complete. The control is passed to this block when the algorithm will be finished and no ball collisions were found, so we need to return null here.Specify the return value of the action chart
Action chart is called in the same way as a
function - you
write the action chart
name followed by parenthesis, specifying the action chart
arguments inside the parenthesis. So, all calls of
in this model will call our action chart now. You can run your model
and check that
it will work as before with the checkHit() function.
But now your algorithm will be more intuitive to other users of your