Calypso Module 9: Indented Rules and the Fourth Law
Doing Two Things If a WHEN Part is TrueIf 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 NecessarySuppose 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 ParentNow 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 CountingNormally 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 CalypsoThe 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:
Actions That FailActions 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.
Overridden ActionsAnother 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 CompleteActions come in three flavors:
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 DiscoveryDo the Module 9 Review and Discovery activity to review what you've learned.
Next ModuleIn 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.