Cognitive Tutor Authoring Tools 2.6 > Using the Tools > Generalizing an example-tracing tutor with formulas > Writing a formula

5.3. Writing a formula

5.3.1. Referring to other links and widgets
5.3.2. Operators
5.3.3. Return types
5.3.4. Using the Formula Builder
5.3.5. Syntax-checking

To get started writing a formula, view and edit the student input matching on a step (link) of your choice.

To display the Formula Matcher dialog for a link:

  1. Open the Edit Student Input Matching... dialog by clicking the link for which you'd like to specify a formula, and selecting Edit Student Input Matching....

  2. Switch the matcher type from its current selection ("Exact Match" by default) to "Formula Match".

You can now enter a formula in the input text area, or select Fx to open the Formula Builder, which allows you to browse and select the available Java and CTAT functions and add arguments to the functions.

Your formula will have access to variables, operators, and functions, which are described in the following sections.

Next, test your formula to see if it matches (or doesn't match) some student input by switching CTAT's Author Mode to Test Tutor and interacting with the tutor. Find out if the step you performed matched the formula (and if didn't match, why not) by opening the Edit Student Input Matching dialog again, and clicking the Last Evaluation.

See Section 5.4, “Testing a formula” for more help testing and debugging your formula.

5.3.1. Referring to other links and widgets

CTAT provides the following predefined variables for use in formulas.

[Tip]Tip

Link numbers are shown in link labels as the first number, followed by a period. You will need to reference link numbers if you intend to reference other steps in your formula.

Referring to author-specified values

These variables are given their values whenever a link is created or modified. You can see these values in the Edit Student Input Matching dialog of the various links in the graph.

  • linkN.authorSelection: the selection specified for link N.

  • linkN.authorAction: the action specified for link N.

  • linkN.authorInput: the input specified for link N.

  • linkN.authorActor: the actor who should perform the step (either Student, Tutor, or Any), specified for link N.

Referring to student-entered values

These variables are given their values whenever a student performs a step which is modeled in the graph.

  • selection: the selection of the widget the student has just used (ie, the current selection)

  • action: the action the student has just performed (ie, the current action)

  • input: the input the student has just entered (ie, the current input)

  • linkN.selection: the selection the student interacted with that was traced in link N.

  • linkN.action: the action the student performed that was traced in link N.

  • linkN.input: the input the student entered that was matched by link N.

  • linkN.actor: the actor who performed the action (either student or tutor) that was matched by link N.

Referring to student interface widgets

You can refer to the content of most CTAT user interface widgets by specifying the instance name of the widget in a formula.

For example, to refer to the content of two DorminTextFields, one called givenDenom1 and the other called givenDenom2, refer to their instance names in the formula:

givenDenom1*givenDenom2

These widget variables are updated under a few conditions. If the widget is configured in "Set Start State" mode, then its variable value is updated whenever the start state is loaded; otherwise, the widget's variable value is updated when the content of the widget is changed or the widget is used (by either the student or the tutor).

For some widgets, it may not be obvious what value they will return when they are referenced. For example:

  • dorminLabel: returns the value of the label

  • dorminButton: returns the constant "-1"

To determine the format of a particular widget's values:

  1. Demonstrate a step with the widget. A new link should appear in the behavior graph.

  2. Open the Edit Student Input Matching... dialog for the new link by clicking the link and selecting Edit Student Input Matching....

  3. Examine the value in the "Input" field. The format of the value is the same as if you referenced it in a formula.

    See also Section 5.4, “Testing a formula”, which explains how to compare expected and observed values.

5.3.2. Operators

In a formula, you have access to the following simple operators:

  • +

  • -

  • *

  • /

You can use these operators in your formulas to perform calculations involving numbers.

[Note]Note

The order of operations in formulas is the same as in the Java programming language and basic math: multiplication and division take precedence over (happen before) addition and subtraction. If two operators have the same precedence, they are evaluated left to right. You can override these rules by using parentheses.

A simple example using numbers and operators:

4*3/6-2+1

(Evaluates as 1.)

A more complex example: the quadratic formula could be expressed in a formula as follows:

 sqrt(link1.input * link1.input - 4 * link3.input)) / -2

5.3.3. Return types

Each formula you write (and any functions that compose it) will return a value of a specific type (number or boolean) when it is evaluated. Set the expected return type to the left of the formula field.

Figure 2.19. Return type mismatch: the dialog specifies a numerical equality test, but the formula returns a boolean value (true or false)

Return type mismatch: the dialog specifies a numerical equality test, but the formula returns a boolean value (true or false)

Your formula's return type should match the type set in the formula matcher. For example, if your formula returns a boolean (true or false), the formula matcher should be set to compare "boolean"; otherwise, the evaluation will not happen. In the screenshot above, the formula is set to evaluate with equals ("="), which is a numerical comparison, but the formula and its function equals will return a boolean value (true or false). To fix this, set the return type to "boolean", or press Check, which will set the return type for you.

5.3.4. Using the Formula Builder

[Note]Note

To use the Formula Builder, you must first install its documentation as described in Section 5.2, “Installing the Formula Builder”.

The Formula Builder is both a tool for constructing formulas with valid syntax and for exploring the available functions. Shown below, the Formula Builder allows you to enter values for the arguments of any function; it then constructs the function syntax for you in the Output area.

Figure 2.20. Using the "chooseRandomly" function with the Formula Builder

Using the "chooseRandomly" function with the Formula Builder

Category. Explore available functions by selecting a function category. After you select a category, the list of functions in that category updates. Two Java categories, String and Math, are available in addition to CTAT-specific functions.

Function. Explore functions by selecting one from this list. After you select a function, the function's description will be shown below.

Arguments. Here you can add arguments to the function. Double-click a cell in the right-hand column to enter an argument value. Add more arguments (for functions that accept multiple arguments) by clicking Add argument.

Description. A synopsis of the selected function.

Output. The function call as it will appear when you press OK. While you can edit this text directly, it is best to modify the output by modifying the function arguments above.

5.3.5. Syntax-checking

When you've written a formula that you want to try, first check that it's well-formed by clicking Check which will tell you 1) if the formula is valid, and 2) whether or not it can match the input that was demonstrated (shown to the left). If the formula is syntactically valid, you'll see a message like "OK". If the formula's syntax is invalid, you'll see a more specific error message.

[Tip]Tip

If you see an error message, look for the ^ character, which should point to the first piece of the formula that is invalid.