Expertsystemen

 

Getting started with formulas

This section will introduce you to an important aspect of the Studio: formulas.

Formulas transform one or more values into another (single) value.

The transforms range from primary school math to complex calculations and transformations of text, dates, and datasets, using functions one may also find in a spreadsheet program.

The values created by a formula can in their turn be used in other formulas and text output.

Adding a formula

To add a formula, go to the Action menu and select (formula icon) formula.

adding a formula
adding a formula

A new window will pop up:

formula window
formula window

Here, you can enter a name, condition and the formula itself. For now, we will only discuss the formula. For the name, pick anything that describes the formula. Conditions are explained more extensively in Conditions. To show the possibilities of formulas, we will provide a few examples.

Formula example

In a new decision tree, add two (question icon) questions to the node start: one called number1 and the other number2. Use Number as input type for the questions. These will be the numbers that will be used for the calculations.

Adding a question with number input
Adding a question with number input

In the same node, you can define the formulas. Add a formula by going to Actions in the menu and click Formula. You can name the formula addition, and leave the condition empty. To add the two numbers, enter the following in the formula field:

number1 + number2

Click [OK] and the formula should be added to your Actions panel.

It is important to be careful what name you attach to a variable. You can imagine that if we would have called our number1 variable enter_a_number1 our syntax would have become quite long. Always use short names that indicate what the variable stands for.

Now, in the same node start, we can add more formulas that subtract, divide or multiply number1 and number2. To do so, we use the minus sign (-) for subtraction, an asterisk (*) for multiplication and a slash (/) for dividing.

After adding these formulas, the ‘Actions’ panel of the ‘start’ node should look similar to the picture below:

actions panel after adding formula
actions panel after adding formula

Showing formula results

Now that we have the decision tree model do some calculations for us, it would be nice if we could show the results of these calculations.

We’ll add a (add node icon) new node that we’ll name result. This will be the node in which we will show the answers of the calculations done. In this node, add we’ll add a (text icon) Text (see Text, titles and questions ). In this text we’ll include the formula we just made.

Now, we’re still inside graph main, but inside another node (result) than the one containing the addition (that was start—remember?).

So we cannot simply use addition to refer to our result as we did earlier. We’ll have to tell the system we are looking for the variable addition in node start. Such a node-qualified name (<node>.<variable>) reads like this:

start.addition

But we’re not done yet: When you place a variable in a text, we have to indicate that it’s not just a piece of text, but that it ought to be replaced with the thing it references. This is done with a caret sign (^) or by enclosing the reference inside a caret and accolades (^{_}). Thus, the syntax will be:

^start.addition or ^{start.addition}

In the example, a bit more text is added. Subtraction, division and multiplication have been added as well, like this:

Number 1: ^{start.number1}, number 2: ^{start.number2}

- Addition: ^{start.addition}
- Subtraction: ^{start.subtraction}
- Multiplication: ^{start.multiplication}
- Division: ^{start.division}

If everything went well, we can save the decision tree and run it (see running your model). In the first step, we are asked to fill in two numbers, after which the decision tree model calculates the formulas you defined and shows us the answers in the second step.

When run with numbers 6 and 4, we’ll get the following output (on the web):

Formulas in a text
Formulas in a text

More on notation

A word about variable notation. References are resolved according to scope, of which there are three:

  • node local: from the same node a simple <variable>, e.g. number1, suffices.
  • graph local / from another node in the same graph: you’ll have to (at least) use <node>.<variable>, e.g. start.number1
  • global (from anywhere): a full <graph>.<node>.<variable> id is necessary, e.g. main.start.number1

Note also it is not an error to use a higher scope than necessary, it’s just more verbose.

For more information on the structure of a model, see Model flow.