Walkthrough 2: Decision Trees

Input

A decision tree - nodes in the tree represent questions, edges - labeled as yes / no - represent answers to the questions.  

Notes:

- N = number of questions.

- The end points of the decision tree are decision nodes - labeled as di - each decision node has a description text.

 

Sample Input

Question 1

- no, Goto Question 2

- yes, Goto Question 3

 

Question 2

- no, end with text "TEXT FOR ENDPOINT 2"

- yes, Goto Question 4

 

Question 3

- no, end with text "TEXT FOR ENDPOINT 2"

- yes, Goto Question 5

 

Question 4

- no, end with text "TEXT FOR ENDPOINT 2"

- yes, end with text "TEXT FOR ENDPOINT 1"

 

Question 5

- no, Goto Question 6

- yes, end with text "TEXT FOR ENDPOINT 2"

 

Question 6

- no, end with text "TEXT FOR ENDPOINT 2"

- yes, end with text "TEXT FOR ENDPOINT 3"

Designing Worksheet to Mimic Supplied Decision Tree

1. Set up the worksheet's UI

Open up the Worksheet Editor, and type in the N questions separated by newlines.

- At the end of the each question, add two radio buttons (- Insert > Form Element > Radio Button from the Worksheet menu) labeled Yes and No.  These represent the answers to the question.

- Add an paragraph at the end. The purpose of this paragraph is to contain the decision. 

- Add two buttons (- Insert > Form Element > Input Button from the Worksheet menu) with the labels 'Back' and 'Continue' (- button label is denoted as the property 'Value' in the dialog). 

 

Sample UI

Question 1 No  Yes

Question 2 No  Yes

Question 3 No  ​Yes

Question 4 No  ​Yes

Question 5 No  ​Yes

Question 6 No  ​Yes

 

    

 

2. Convert the answers i.e. the radio buttons, and the containers i.e. the paragraphs containing question and the answers, the two buttons, and the end paragraph into worksheet cells.

 

- For the ith question, set the Id of paragraph as node(qi) e.g. node(q3) for the 3rd question, and the Ids of the radio buttons as qi.

- Set the 'value' property of the No and Yes answers to 'no' and 'yes' respectively (minus the quotes), and check the option 'Refresh worksheet on change' for each radio button.  

- Set the Id of the 'Back' and 'Continue' buttons as 'back' and 'continue' respectively (minus the quotes), and check the option 'Refresh worksheet on change'.

- Set the Id of the last paragraph, which is supposed to contain the decision, as 'end'. 

 

3. Specify the rules controlling the behavior of the worksheet. To do so, open up the Rule Editor (- View > Rule Editor from the Worksheet menu).

Add the following facts and rules.

  1. For every node i (1 ≤ i  ≤ N) in the decision tree, add the fact - question(qi) - e.g. question(q1)
  2. For every branch in the decision tree that is directed from questioni to questionj based on answer a (yes / no), add the fact - branch(qi, a, qj)
    e.g. branch(q1, yes, q3) 
  3. For every branch in the decision tree from questioni to a decision node, say d, based on answer a add the fact: branch(qi, a, end(T)) where T is the textual description of the decision (enclosed within double quotes) e.g. branch(q6, yes, end("Exempt from paying Tax"))
  4. Add the following:
      pos(active(NEXT)) :- active(CURRENT) & branch(CURRENT,ANSWER,NEXT) & 
                           value(CURRENT,ANSWER) & 
                           pluss(value(continue,true))
      neg(active(CURRENT)) :- active(CURRENT) & value(CURRENT,ANSWER) & 
                           pluss(value(continue,true))
      innerhtml(end, X) :- active(end(X))
      innerhtml(end, "") :- countofall(X,active(end(X)),0)
      style(node(Q), display, none) :- question(Q) & ~active(Q)
      style(node(Q), display, block) :- question(Q) & active(Q)
      style(end, display, none) :- countofall(X,active(end(X)),0)
      style(end, display, block) :- active(end(X))
      style(continue, display, none) :- active(CURRENT) & 
                                        countofall(ANSWER, value(CURRENT,ANSWER), 0)
      style(continue, display, inline) :- active(CURRENT) & value(CURRENT,ANSWER)
      attribute(back, disabled, true) :- active(q1)
      attribute(back, disabled, true) :- active(end(X))
      attribute(back, disabled, false) :- ~attribute(back, disabled, true)
      active(q1)
      

Explanation of the set of rules (4)

Note that at each step of the decision tree, exactly one question is presented to the user, or the final decision. This is modeled using the relation active(X) where X denotes the identifier of the question / decision that is currently presented. The dynamic rules pos(...) :- ... and neg(..) :- ... update the active relation, based on the branches specified, when the user clicks on the button with Id 'continue'

The rules matching the format innerhtml(end, X) :- ... set up the content of the paragraph 'end' depending on the final decision X.

The styling rules determine when the 'continue' button should be visible, and when the final decision should be shown. 

The attribute / property rules determine when the 'back' button should be disabled i.e. at the start and the end.

The fact active(q1) denotes that question 1 i.e. q1 is active at the start. 

 

Note that the set of facts and rules (4) is the same no matter which decision tree you have to model. For different decision trees, you need only configure the facts in (1), (2), and (3).

Save the rules and preview the worksheet.

Output

Worksheet that models the decision tree.

Sample Output



 

Question 1 No  Yes

Question 2 No  Yes

Question 3 No  ​Yes

Question 4 No  ​Yes

Question 5 No  ​Yes

Question 6 No  ​Yes