> For the complete documentation index, see [llms.txt](https://docs.rulecube.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rulecube.com/v2.5/how-to-guides/ruleset-development/create-a-rule.md).

# Create a Rule

## Introduction

This guide explains how to create rules for a ruleset.&#x20;

**Rules** are the main engine of rulesets and the logical implementation of a business rule. For example, suppose you implement a business rule for determining the maximum loan amount given a certain income as input. In that case, the rule is the implementation of the business rule that calculates the actual maximum loan amount:

<table><thead><tr><th width="147.33333333333331">Input</th><th>Rule</th><th>Output</th></tr></thead><tbody><tr><td>Income</td><td>Calculate maximum loan amount</td><td>Maximum loan amount</td></tr></tbody></table>

{% hint style="info" %}
To distinguish rules at the entity and ruleset level, Rulecube speaks of **entity rules** and **global rules**. This guide is about global rules.
{% endhint %}

### The result of rules as output

In general, a rule performs a calculation or makes a decision and returns the result as the output of the ruleset. To facilitate different (business) rule scenarios, Rulecube supports different output types, such as `string` and `date`. For instance, the aforementioned maximum loan amount output is of the `number` type.

<details>

<summary>List of output types and additional rule/output settings</summary>

**Output types**

* `any`\
  As its name implies, an `any` input can contain any type of input.&#x20;
* `number`\
  This type includes negative values and numbers with decimals.
* `boolean`\
  This type expects the values `true` or `false`.
* `string`\
  This type can contain any text, including numbers.
* `date`\
  This type expects a date in `mm/dd/yyyy` format.
* `enum`\
  For this type, you can set a list of possible values. For example, `good`, `bad`, `ok`.&#x20;
* `array`\
  This type contains a one-dimensional array of values.&#x20;

**Additional rule/output settings**

* If you select this type, Rulecube adds the **Possible values** field to the **Rule details** page, where you can enter the corresponding values.
* If you select this type, Rulecube adds the **Sub type** field to the **Rule details** page, where you can set the type for the array elements.

</details>

## Procedure

Let's walk through the steps for creating a rule that calculates the maximum loan amount as 25 percent of a person's annual income.

1. Create a ruleset named `MaxLoan`.

2. Create an input named `AnnualIncome` with **Type** =  `number` and **Label** = `The annual income`.

3. Create a constant named `LoanPercentage` with **Type** =  `simple` and **Value** = `25`.

4. Click the **+** sign behind **Rules** from the left menu.\
   ↳ The **Rule details** page opens.

5. In the **Name** field, enter the name for the rule. For example, `LoanCalculation`.

6. In the **Output name** field, enter `MaximumLoanAmount`.

7. In the **Type** list, select **number**.

8. In the **Expression** field, enter the following code to calculate the maximum loan amount:<br>

   ```javascript
   AnnualIncome * (LoanPercentage / 100)
   ```

9. Click **Save**.

10. Try the ruleset out.

#### Revert changes to an expression

As an extra, let's look at the **Revert** feature for changes to expressions in Rulecube:

9. Move back to the rule again.
10. Change the code for the **Expression** to:<br>

    ```javascript
    AnnualIncome * LoanPercentage / 100
    ```

Now, let's assume you are unsatisfied with this change and want the previous code back. You could retype the original code if you remember, but Rulecube enables you to revert the changes much easier:

11. Click <img src="/files/0tQKYzhumd65bGk2eGCV" alt="" data-size="line"> above the expression.\
    ↳ Rulecube reverts your changes and shows your original code again.
12. Press `Ctrl+S` to save your work.

## Additional resources

* How-to Guide: [Try Out a Rule](/v2.5/how-to-guides/ruleset-development/try-out-a-ruleset.md).
* How-to Guide: [Call a Rule from Your Software](/v2.5/how-to-guides/ruleset-development/call-a-ruleset-from-your-software.md).


---

# 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:

```
GET https://docs.rulecube.com/v2.5/how-to-guides/ruleset-development/create-a-rule.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
