Conditional Actions

A conditional action checks the value of any parameter in the session’s memory to see if it matches an expected value (the condition). A conditional action may specify several conditions along with the actions that should be taken when the condition is satisfied. Diatheke will evaulate the conditions in the order they are listed in the conditional action until it finds one that is satisfied. If it reaches the end of the list without matching a condition, it will use the default actions if they are specified. If there are no default actions, Diatheke will log an error and end the current story.

The syntax to specify the condition is fairly simple. The only type of value comparison Diatheke can do is to check if string values are equal or not. To do this, the parameter name is surrounded by ${} (e.g., ${fruit}), which references the current value of the parameter. Then the specific check is followed by .is() or .not(). For example, if the model wants to check if the parameter “fruit” is an “apple” or an “orange” it would use this syntax:

conditions:
  - condition: ${fruit}.is(apple)
    actions:
      # Do something because it's an apple

  - condition: ${fruit}.is(orange)
    actions:
      # Do something because it's an orange

  - condition: ${fruit}.not(apple) AND ${fruit}.not(orange)
    actions:
      # Do something because the fruit is not an apple
      # and it is not an orange.

Everything within the parentheses (including spaces) is considered to be part of the expected string value. It is also possible to compare multiple parameters as part of the same condition using the keywords AND (the comparisons on both sides of the AND must be true) and OR (only one of the comparisons on both sides of the OR needs to be true). To avoid ambiguity, the AND/OR conditions may be surrounded by parentheses. The conditions inside the parentheses will be evaluated first. For example,

# The OR condition is evaluated first, and its result is
# used in the AND condition (with ${veggie}).
condition: ( ${fruit}.is(apple) OR ${fruit}.is(orange) ) AND ${veggie}.is(carrot)

For comparison purposes, a parameter that is not defined in memory is the same as one that has an empty string (and vice versa - a parameter with an empty string is considered to be undefined). To check if a parameter is defined or not, the syntax is:

conditions:
  # Check if fruit is defined (i.e., it is not
  # the empty string).
  - condition: ${fruit}.defined
    actions:

  # Check if fruit is undefined (i.e., it is
  # the empty string).
  - condition: ${fruit}.undefined
    actions:

Default conditions are executed if none of the preceding conditions are true. For example

conditions:
  # Check if fruit is undefined (i.e., it is
  # the empty string).
  - condition: ${fruit}.undefined
    actions:
  - condition: ${fruit}.is(apple)
    actions:
default_actions:
  - type: reply
    id: default_reply