> For the complete documentation index, see [llms.txt](https://docs.rainbird.ai/rainbird/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rainbird.ai/rainbird/knowledge-modelling/modelling-features/rblang-reference/rules-and-expressions.md).

# Rules and expressions

### Rule structure

Rules are relationship instances with nested conditions.

```xml
<relinst type="is eligible" object="true" cf="100" name="Eligible if 18+">
    <condition rel="has age" subject="%S" object="%AGE"/>
    <condition expression="%AGE is greater than or equal to 18"/>
</relinst>
```

Use these rule attributes:

* `type` — the relationship being inferred
* `object` — the inferred value when it is fixed
* `cf` — certainty factor
* `alt` — alternative evidence text shown in the evidence tree
* `name` — optional rule name

Rule names do not need to be unique. Keep them under 170 characters.

### Evidence text

Rule attributes and condition attributes support `alt`.

In Studio, `alt` is called **evidence text**.

Use it to return a clearer explanation in the evidence tree.

That explanation appears alongside the fact.

It can be shown instead of the raw fact text.

Variables can be used in `alt` text, but they must be wrapped in `{{ }}`.

```xml
<relinst type="speaks" cf="100" alt="The evidence shows {{%S}} speaks the language of {{%O}} because">
   <condition rel="lives in" subject="%S" object="%COUNTRY" alt="they live in {{%COUNTRY}}"/>
   <condition rel="has national language" subject="%COUNTRY" object="%O" alt="and they mostly speak {{%O}} in that country."/>
</relinst>
```

### Variables

Variables start with `%`.

* `%S` — the current subject
* `%O` — the output object
* custom names such as `%AGE` or `%RESULT`

Use relationship conditions to bind values.

```xml
<condition rel="has age" subject="%S" object="%AGE"/>
```

Use expression conditions to test or calculate values.

```xml
<condition expression="%AGE is greater than 30"/>
<condition expression="%AGE + 10" value="%O"/>
```

### Critical expression rules

{% hint style="warning" %}
`=` is only for comparison. Never use `=` for assignment.
{% endhint %}

Correct assignment:

```xml
<condition expression="%X * 2" value="%O"/>
<condition expression="%AGE + 10" value="%RESULT"/>
```

Invalid assignment:

```xml
<condition expression="%O = %X * 2"/>
<condition expression="%RESULT = %AGE + 10"/>
```

### String comparisons

Use single quotes around string literals.

```xml
<condition expression="%NAME is equal to 'John Smith'"/>
<condition expression="%STATUS is not equal to 'Premium'"/>
```

### Missing facts

Do not test for `null` directly.

Use `countRelationshipInstances()` instead.

```xml
<condition expression="countRelationshipInstances(%S, 'has purchase date', *) is equal to 0"/>
<condition expression="countRelationshipInstances(%PRODUCT, 'has purchase date', *) is greater than 0"/>
```

### Multi-step calculations

Break complex logic into separate steps.

```xml
<condition expression="%AGE * 2" value="%STEP1"/>
<condition expression="%STEP1 + 10" value="%STEP2"/>
<condition expression="%STEP2 / 3" value="%O"/>
```

### Condition weighting and behaviour

Conditions can influence certainty and success.

```xml
<condition expression="%AGE >= 18" weight="100" behaviour="mandatory"/>
<condition expression="%ACTIVE = true" weight="80" behaviour="optional"/>
```

* `mandatory` — must be satisfied
* `optional` — adds confidence when satisfied

### Authoring guidance

Keep each rule focused on one result.

Write rules for the general case. Do not hard-code output values that only fit one example dataset.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.rainbird.ai/rainbird/knowledge-modelling/modelling-features/rblang-reference/rules-and-expressions.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
