Example 04 - Decision Tree

In this chapter we will walk through build decision tree using Visual Lambda.

Decision Tree - "Action Group Conditional"

Visual Lambda module body can be one of the following expressions:

 

Difference Between an "Action Group Conditional" and an "Action Group of Case Expressions"

An action group of case expressions can be expressed as a pseudo code as following:

            if (Condition1)
                Perform Action1
            if (Condition2)
                Perform Action2
            if (Condition3)
                Perform Action3
              ...  ...
            

Note: In an action group of case expressions, even if "Condition1" is true (Action1 will be performed), "Condition2", "Condition3", etc are still evaluated. In another words, all conditions are evaluated, and more than one action1 may be performed if the corresponding conditions are evaluated to be true.

An action group conditional expressions can be expressed as a pseudo code as following:

            if (Condition1)
                Perform Action1
            else if (Condition2)
                Perform Action2
            else if (Condition3)
                Perform Action3
              ...  ...
            else
                Perform OtherwiseAction
            

Note: In an action group conditional expression, at most one action will be performed, if the evaluation of any condition returns true, the corresponding action will be performed, and the other conditions are ignored. If none of the condition evaluations is true, the "Otherwise" action will be performed. Note the "Otherwise" action is optional.

 

Example of "Action Group Conditional"

The following example demonstrates a Visual Lambda module "Grade". It takes an input of integer parameter called "Score", and returns a character value of grade based on the following rule:

Score Grade
>= 90 A
90 ~ 80 B
80 ~ 70 C
70 ~ 60 D
< 60 E

1. First create a module named "Grade". Set the return type as "char".

 

2. Right click on node "Parameters" to add a new parameter "Score".

 

3. Right click on the root node "Module" to add a "Action Group Conditional".

 

4. The action node "ConditionalActionGroup" is created. Right click on that node to add a conditional action "Case".

 

5. The "case" node is created. Right click on it. Add a ">= GreaterEqual [int]" expression as the "Condition" operand.

 

6. A ">=" expression node is created. Right click on it. Add the "Score" parameter as the first operand "Oprand 0" (the left operand of the comparison expression).

 

7. Right click on the ">= GreaterEqual [int]" expression node again. Add the second operand "Oprand 1" as a "Value" expression.

 

8. A value dialog will pop up. Specify the value "90". Then click OK.

 

9. The condition expression is finished. Now right click on node "Case" again. Add a "Return" action.

 

10. Specify a constant "Value" expression as return value.

 

11. Specify the value 'A' in the return value dialog. Now the first conditional action is completed. Right click the "Action Group COnditional" node to add a second conditional action "Condition 1". Note the first conditional action has a name of "Condition 0". An choice "Set Condition 0" is added to the pop up menu so that user can modify the first conditional action just added.

 

12. Repeat the steps from 5 to 11 to add more conditional actions. As shown below, the conditional actions to return 'A', 'B', "C', 'D' are added. Now we add the last one as an "Otherwise" action, by right clicking the "Action GroupConditional" -> "Add Condition 4" -> "Otherwise".

 

13. In "Otherwise" expression, we add a "Return" action to simply return value 'E'. Below is a screen shot of the final module in Visual Lambda editor. Try to expand and collapse the "Condition" nodes and "Return" nodes to see the difference in presentation in the tree view of Visual Lambda editor.