## Conditions

Conditions are used in flows in three different places:

- In [flow guards](/content/docs/reference/primitives/starting-flows/#flow-guards/index.html) to determine whether a flow can be started.
- In the [next](/content/docs/reference/primitives/flows/#steps/index.html) field of a flow step to choose between branches of your business logic.
- In the `rejections` field of a `collect` step to [validate slot values](/content/docs/reference/primitives/flow-steps/#slot-validation/index.html).

### Syntax

Conditions in flows are written using natural language that can include logical operators, conditional operators and other constructs. They are evaluated with the [pypred](https://github.com/armon/pypred) library.

These conditions support the following operators,

- `not`: Negates a condition.
- `and`: Combines two conditions with logical AND.
- `or`: Combines two conditions with logical OR.
- `>`: Greater than.
- `>=`: Greater than or equal to.
- `<`: Less than.
- `<=`: Less than or equal to.
- `=`: Equal to.
- `!=`: Not equal to.
- `is`: Checks for identity.
- `is not`: Checks for non-identity.
- `contains`: Subset operator that checks if a set contains a value
- `matches`: Uses regular expressions to match strings.

#### Parentheses

Use parentheses to group expressions and control the order of evaluation. For example:

```yaml
- collect: age
  next:
    - if: slots.age < 18
      then: under_18_step
    - if: (slots.age > 18 and (consent = "yes" or consent = "y"))
      then: consent_accept
    - else: consent_decline
```

#### Subset Operator

Subset operator `contains` can be used in the format `SET contains VALUE` where `SET` is the set of possible values and `VALUE` being the name of slot. For example:

```yaml
- collect: emergency
  next:
    - if: "{'WARN' 'ERR' 'CRIT'} contains slots.error_level"
      then: handoff
    - else: everything_okay
```

`contains` operator can also be used to identify substrings when used as `slots.product contains "Rasa"` however this check is case-sensitive.

#### Constants

- String literals: Enclose in single or double quotes (`'example'` or `"example").
- Numeric literals: Numbers without quotes (`42`).
- Constants: `true`, `false`, `undefined`, `null`, `empty`

#### Regular Expressions

When using the `matches` operator, you can include regular expression modifiers. The regex modifier should be enclosed in quotes. For example, this flow step checks if the `zipcode` slot contains a United States zip code:

```yaml
- collect: zipcode
  description: ask zipcode and check if its a zip code
  next:
    - if: slots.zipcode matches "\d{5}(-\d{4})?"
      then: ask_payment
    - else: wrong_zipcode
```

A case-insensitive substring comparison can be made with the condition `product matches "(?i).*rasa.*"` which checks for the substring `rasa` within the variable `product`.

#### Empty Values

If you want to check if a boolean slot is not set, you need to use the syntax `<boolean-slot> is null`.
To check if a text slot is not set or empty, use the syntax `not <text-slot>`.

#### Examples

Here are some examples of conditions that demonstrate the use of different constructs.

```yaml
# Simple conditions
age > 18
name is "Alice"
name is empty
status = "active"
status is not null

# Combining Conditions
age > 21 and gender = "female"
category = "electronics" or category = "computers"
status = "active" and (priority = 1 or priority = 2)
status = empty or status is null
description matches "/error \d{3}/i" and (severity = "high" or source contains "server")
```

## Namespaces

Namespaces are used to access different types of data in predicates used in [branching conditions](/content/docs/reference/primitives/flow-steps/#next-property/index.html), [slot validation](/content/docs/reference/primitives/flow-steps/#slot-validation/index.html), and [flow guards](/content/docs/reference/primitives/starting-flows/#flow-guards/index.html). There are two available namespaces: `slots` and `context`. The `slots` namespace is used to access slot values, while the `context` namespace is used to access the current dialogue frame properties.

### Slots

The `slots` namespace is used to access slot values. The slot name must be prefixed with `slots.` to be accessible in the condition. For example:

```yaml
- id: some_question
  collect: age
  next:
    - if: slots.age < 18
      then: under_18_step
    - else: over_18_step
```

Make sure to have the slot defined in the [domain](/content/docs/reference/config/domain/index.html).
If the slot is not defined in the domain or the slot is not prefixed with `slots.` namespace, the validation that runs during training will fail with an appropriate error.

### Context

The `context` namespace is used to access the properties of the current [dialogue frame](/content/docs/reference/primitives/conditions/#dialogue-frames/index.html). The property must be prefixed with `context.` to be accessible in the predicate. For example:

```yaml
  pattern_completed:
    description:  a flow has been completed and there is nothing else to be done
    steps:
      - noop: true
        next:
          - if: context.previous_flow_name != "greeting"
            then:
              - action: utter_what_can_help_with
                next: END
          - else: stop
      - id: stop
        action: action_stop
```

You can also use jinja templating to access the context namespace. For example:

```yaml
  pattern_completed:
    description:  a flow has been completed and there is nothing else to be done
    steps:
      - noop: true
        next:
          - if: "{{context.previous_flow_name}}" != "greeting"
            then:
              - action: utter_what_can_help_with
                next: END
          - else: stop
      - id: stop
        action: action_stop
```

#### Dialogue Frames

The dialogue manager organizes the advancement of flows (both user-defined and built-in) in a dialogue frame stack. The dialogue frame stack represents a LIFO (Last-In-First-Out) stack of dialogue frames. Different types of dialogue frames are mapped to built-in conversational patterns that enable [conversation repair](/content/docs/learn/concepts/conversation-patterns/index.html).

Each dialogue frame has a `flow_id` and `step_id` property. The `flow_id` is the id of the current flow and the `step_id` is the id of the current step in the flow.

The following dialogue frames types are available:

01. [cancel](/content/docs/reference/primitives/conditions/#cancel/index.html): handles flow cancellation
02. [chitchat](/content/docs/reference/primitives/conditions/#chitchat/index.html): handles chitchat
03. [clarify](/content/docs/reference/primitives/conditions/#clarify/index.html): handles clarification
04. [collect information](/content/docs/reference/primitives/conditions/#collect-information/index.html): handles information collection
05. [completion](/content/docs/reference/primitives/conditions/#completion/index.html): handles flow completion
06. [continue interrupted](/content/docs/reference/primitives/conditions/#continue-interrupted/index.html): handles continuation of interrupted flows
07. [correction](/content/docs/reference/primitives/conditions/#correction/index.html): handles correction
08. [internal error](/content/docs/reference/primitives/conditions/#internal-error/index.html): handles internal errors
09. [search](/content/docs/reference/primitives/conditions/#knowledge-search/index.html): handles knowledge search
10. [skip question](/content/docs/reference/primitives/conditions/#skip-question/index.html): handles skipping of information collection
11. [code change](/content/docs/reference/primitives/conditions/#code-change/index.html): cleans the stack after an assistant update
12. [can not handle](/content/docs/reference/primitives/conditions/#can-not-handle/index.html): handles situations where the assistant cannot handle
13. [human handoff](/content/docs/reference/primitives/conditions/#human-handoff/index.html): handles handoff to human
14. [validate slot](/content/docs/reference/primitives/conditions/#validate-slot/index.html): handles real-time slot validations that are strictly independent of business logic
15. [customer satisfaction](/content/docs/reference/primitives/conditions/#customer-satisfaction/index.html): handles customer satisfaction feedback collection at the end of a conversation
