Calypso Module 9: Indented Rules and the Fourth Law
Learning Goal: In this module you will learn how to make
Cozmo take two actions when a WHEN condition is true, using
the "Do Two Things" idiom. The trick is to make the second rule
dependent on the first by indenting it. You will also learn a
special case of Do Two Things called "Count Actions". Finally
we'll examine the Fourth Law of Calypso, which explains exactly
how indented rules work.
Doing Two Things If a WHEN Part is True
If we want Cozmo to do two things when something is true, we can use
an indented rule beneath the main rule. This is called the Do Two Things idiom:
How to Indent a Rule
(1) Put the pencil on the WHEN tile.
(2) "Pick up" the rule by pressing the A button on the game controller
or hitting the Enter key. The rule turns yellow when you pick it up.
(3) Slide the rule to the right using either the left stick or the
right arrow key. A line will appear connecting the rule to its parent:
(4) Finally, put the rule down by pressing the A button or hitting the
Enter key again, and it will turn white again:
It's also possible to indent a rule by clicking on the WHEN tile with
the mouse and then clicking and dragging the rule.
Whenever a rule is indented, it has a "parent" which it remains
attached to. Moving the parent will move its children along with it.
Moving an indented rule can change its parent. A rule's connection to
its parent is always visible in the display.
Trying "Do Two Things"
Start a new Calypso program and enter the rules from the example above:
The "lift" tile, when it appears alone, tells Cozmo to move his lift
up or down. If the lift is down he will raise it; if it's up he will
lower it.
Run the program and tap on a cube. Notice that both rules are
highlighted when you tap, and both actions are performed.
Why Indentation Is Necessary
Suppose we un-indent the second rule, resulting in this program:
Make a prediction: What will happen when you run this program?
Try running the program and see if your prediction was correct.
Inheriting "It" From A Parent
Now consider this program, which you can think of as a "Do Three
Things" pattern:
In response to a cube tap, the program moves the lift, plays a sound,
and also makes the cube change color. The glow rule uses the "it"
tile to refer to the cube that was tapped. In this program, the
meaning of "it" is determined by the parent rule's "WHEN feel". Try
running the program.
Now consider what happens if we un-indent the glow rule:
Indented rules that have an empty WHEN, such as the glow rule, can
refer to their parent's value of "it", but an un-indented rule has no
parent. That is why Calypso marks the "it" as illegal (orange
shading) in the display above. If you run the program, this rule will
be ignored until the problem is corrected.
Keeping Score: the "Count Actions" Idiom
"Count Actions" is a special case of the "Do Two Things" idiom that
makes use of Calypso's score-keeping feature. The flashcard shows how
we can use "Count Actions" whenever we move the lift so that we also
count the number of times this happens.
The available score actions are "+score" to add to a score, "-score"
to subtract from a score, and "set-score" to set the value of a score.
All of these tiles are located under "game" in the menu of DO actions.
Try running this program and seeing how quickly you can run up the
score by tapping on cubes.
Make a prediction: What will happen if you remove the
indentation from the "+score" rule?
Run the program and see if your prediction was correct.
Continuous Counting
Normally we want to count events that are only momentarily true, such as a "tap".
Suppose we instead chose to score a point whenever Cozmo sees a cube in the camera frame.
We'll also have him move his lift up and down:
Try running this program. Show Cozmo a cube, and then hide it from him.
Puzzle question: If you only want to score a point when a cube
first becomes visible in the camera frame, not for every frame the
cube is visible, what tile should you add to the program?
The Fourth Law of Calypso
The Fourth Law of Calypso says: "An indented rule can run only when
its parent's action succeeds." Let's examine what this means.
Every indented rule is connected to a parent rule above it. The
parent rule can only run when its WHEN part is true. If the parent's
WHEN part is false, its action won't even be attempted, so the
indented rule can't possibly run. This is illustrated in the leftmost
scene in the Fourth Law graphic below. Since Cozmo doesn't see a green
cube, the parent rule cannot run (and is shown as dimmed). The indented rule isn't even
considered, so it's dimmed as well:
If the parent rule can run and its action succeeds, then the indented
rule can be considered. In the middle scene in the Fourth Law
graphic, the parent rule is running and Cozmo is moving toward a green
cube, but the yellow score is zero. Therefore the indented rule's
WHEN part is false and the indented rule still cannot run.
If the parent rule can run and its action succeeds, and the indented
rule can also run, then the indented rule's action is taken. This is
the case in the rightmost scene in the Fourth Law graphic. Cozmo is
moving toward the green cube because the parent rule ran, and
simultaneously playing a sound because the indented rule also ran. He
is "doing two things".
This is the first example we've seen where both rules have non-empty
WHEN parts. For idioms such as Do Two Things or Count Actions, the
parent rule has a non-empty WHEN but the indented rule typically has
an empty WHEN. An empty WHEN is considered to always be true.
Actions That Fail
Actions can fail for a variety of reasons. A common example is trying
to grab a cube but missing it. If we're trying to count the number of
cubes Cozmo has grabbed, we don't want to count the misses. This is
why the Fourth Law makes an indented rule depend on the success
of the parent rule's action, not the truth of the parent rule's WHEN
part. Consider the example below:
If the grab action fails, the indented rule will not be considered.
Experiment: Try running the program above.
- Tilt Cozmo's head down so he can see a cube when it's close to him.
- Move a cube close to him so that he's "bumping it".
- Let him pick up the cube and watch him score a point when he succeeds.
- Then press the green A button to make him drop the cube.
- Move the cube in front of him to prompt him to grab it a second
time, but when he moves toward it, snatch the cube away. This
will cause the grab to fail.
- Observe the result. Did the red score change this time?
Overridden Actions
Another way an action might not succeed is if it is overridden by an
earlier action in accordance with the Third Law, which says that when
actions conflict, the earliest wins. Consider this program:
For as long as a red cube is visible, Cozmo will glow green, therefore
he cannot glow blue in response to a button press. Since the glow
blue rule's action is overridden (Third Law) by the glow green rule
that precedes it, the indented rule will not be considered, and no
points will be scored. If you hide the red cube so that the glow
green rule cannot run, pressing the A button will glow Cozmo blue and
score a point.
Try out the program and see which rules are highlighted when you press
the A button with a red cube present or absent.
Actions That Take Time to Complete
Actions come in three flavors:
- Actions that always succeed (unless overridden), and
complete immediately. Examples include "glow" (which can be
overridden as we saw in the previous exercise) and "+score"
(which cannot). Because these actions complete immediately, they
can be taken on every rule cycle. This is what makes it possible
to run up a score very quickly.
- Actions that always succeed (unless overridden or blocked),
but take time to complete. Examples include "say", "play",
"lift", and "look". Because these actions take time to complete,
repeating the action will be blocked if the previous one hasn't
finished. For example, Cozmo can only say one thing at a time.
If we ask him to say "Hello" over and over again, a bunch of
attempted hellos will be blocked while he finishes the first
"Hello". Once he's finished, the next attempt at saying "Hello"
will succeed, and subsequent attempts will again be blocked while
he's speaking.
- Actions that take time and can fail. Actions such as
"grab" might not be successful. It takes time for Cozmo to
attempt the action and determine whether it succeeded. Therefore,
this type of action temporarily suspends program execution: no
other rules will be considered until we know whether the action
has succeeded or failed.
Let's take a closer look at actions that take a while to complete by
comparing two similar programs. Here is the first one:
In the above program the parent rule's action completes immediately,
so the red score will be incremented on every rule cycle and runs up
very quickly. The indented rule can also run on every ryle cycle, but
its action takes a while to complete, so on most rule cycles the
action is blocked because Cozmo is already speaking. But every so
often the action succeeds.
Now let's look at the second program, where the parent/child
relationship is switched:
In the second program, the parent rule's say action takes time to
complete, and can be blocked if Cozmo is already speaking. Since the
parent rule's action does not succeed on every rule cycle, the
indented rule is not considered on most rule cycles; only when the
parent's action succeeds. Therefore the red score runs up much more
slowly than before. Now it tracks the number of "Hellos" that have
been said. Because actions like "say" that cannot fail are considered
to succeed immediately, even though they don't complete for a while,
the red score is incremented at the start of the utterance, not at the
end.
Review and Discovery
Do the Module 9 Review and Discovery
activity to review what you've learned.
Next Module
In the next module you'll learn to write programs
with more than one page of rules.
Back to Calypso Curriculum overview.
Copyright © 2017 Visionary Machines LLC.
|