📖
Rulecube documentation
v2.4
v2.4
  • Overview
  • Quick Start
  • Tutorial
    • Getting started
    • Create and Run Your First Ruleset
    • Use Constants and Methods
    • Create and Call Functions
    • Adding Testcases
    • Using Lookup Tables
    • Using Entities
    • Using Forms
  • How-to Guides
    • Logging in and Authorization
      • Activate Your Account From an Invitation
      • Log in to Rulecube
      • Change Your Password
      • Recover Your Password
      • Two-factor authentication (2FA)
      • Invite Other Users to Rulecube
      • Edit a User's Role
      • Delete a User
    • Ruleset Development
      • Create a Ruleset
      • Set the Input for a Ruleset
      • Create a Rule
      • Generate Alerts
      • Try out a Ruleset
      • Call a Ruleset from Your Software
      • Entities
        • Create an Entity
        • Drag and Drop a JSON Schema or XSD to create entities
        • Entity instantiation
        • Persisted Entities
      • Constants
        • Constant Tables
      • Functions
      • Built-in Functions
      • Create and Run a Testcase
      • Delete a Ruleset or Components
      • Debugging your Ruleset
    • Create a Workflow
      • Workflow step types
      • Working with documents in a workflow
    • Work with (Environment) Variables
    • Call a Ruleset via Its API From Postman
    • Creating input from JSON Schema
    • Use a Ruleset from Your Software
    • Ruleset Productivity Tips
    • Create an Ockto workflow
    • Alert aggregation
    • Forms
      • Introduction and overview
      • Create a Data table
  • Language Reference
    • Global
    • Array
    • Compression
    • Crypto
    • Date
    • Encryption (deprecated)
    • Finance
    • Http
    • Mail
    • MongoDB
    • Ockto
    • PDF
    • SQL
    • Statistics
    • System
    • UserStore
    • Workflow
Powered by GitBook
On this page
  • Introduction
  • Elementary and compound entities
  • Procedure
  • Step 1: Create the ProductItem entity
  • Step 2: Create the InvoiceItem entity
  • Step 3: Create the Invoice entity
  • Step 4: Test your ruleset
  • Additional resources
  1. How-to Guides
  2. Ruleset Development
  3. Entities

Create an Entity

Introduction

This guide explains how to create entities.

Entities are containers into which (input) data and rules acting on that data can be integrated, as in a JavaScript class. Using entities is the most natural/logical way to implement a business rule in a ruleset, and therefore, they improve the manageability of your rulesets.

Within an entity, data become regular properties and rules calculated properties of the entity, like in this Person entity:

  • Name (regular property)

  • Address (regular property)

  • DateOfBirth (regular property)

  • Age (calculated property – rule)

Elementary and compound entities

The Person entity is self-contained and therefore a so-called elementary entity. Elementary entities cover many real-world business rule scenarios, but Rulecube goes even further with compound entities that are composed of two or more connected entities to cover all possible business scenarios.

For example, a compound Invoice entity consists of its own properties and properties based on the InvoiceItem entity, where one invoice can have one or more items. Furthermore, the InvoiceItem entity is connected to the ProductItem entity that holds product details:

Invoice (entity): InvoiceDate TotalAmount InvoiceItems -> InvoiceItem (entity): Count Amount Product -> ProductItem (entity): ProductID Description Price

This way, you can cover each data structure you need in your rulesets.

Procedure

Let's create a compound entity because if you master that, you'll also know how to create elementary entities. You'll create the compound Invoice entity as shown above, including the calculation of Amount at the InvoiceItem level and the TotalAmount at the Invoice level.

For compound entities, it's necessary to work bottom-up because parent entities require child entities to be available. So, first, create ProductItem, then InvoiceItem, and finally, Invoice:

Step 1: Create the ProductItem entity

  1. Create a ruleset named TotalInvoiceAmount.

  2. Create an entity named entProductItem.

  3. Click Add property. ↳ The Input/Property page opens.

  4. In The Name field, enter ProductID. And in the Type list, select string.

  5. Save the property.

  6. Repeat steps 3 to 5 to create the Description property as a string and the Price property as a number. ↳ Finally, your entity should look like this:

Step 2: Create the InvoiceItem entity

  1. Create an entity named entInvoiceItem.

  2. Add a property named Count (Type=number).

  3. Add a property named Product. And in the Type list, select entProductItem. ↳ Now, entInvoiceItem and entProductItem are connected, and entInvoiceItem has become a compound entity. Rulecube shows this with a little cube icon in front of the entInvoiceItem entity:

  4. Add a calculated property for Amount (Type=number) and set its Expression to:

    Count * Product.Price

    Explanation Because entInvoiceItem and entProductItem are connected, you can use the Product.Price to calculate the amount for invoice items.

  5. Save the entity. ↳ The content of entInvoiceItem should look like this:

Step 3: Create the Invoice entity

  1. Create an entity named entInvoice.

  2. Add a property named InvoiceItems and set its Type to array with Sub type = entInvoiceItem. ↳ By defining this property as an array, an invoice can have multiple items. Furthermore, entInvoice and entInvoiceItem are connected now. And because entInvoiceItem is connected to entProductItem, entInvoice is also connected to entProductItem.

  3. Add a calculated property for TotalAmount (Type=number) and set its Expression to:

    InvoiceItems.sum(item => item.Amount)
  4. Save the entity. ↳ Your compound entity structure now should look like this:

Step 4: Test your ruleset

{
  "Invoice": {
    "InvoiceItems": [
      {
        "Product": {
          "ProductID": "PK-207-U",
          "Description": "Rubber bands",
          "Price": 11.75
        },
        "Count": 8
      },
      {
        "Product": {
          "ProductID": "KD-883-J",
          "Description": "6mm tube - 1mtr",
          "Price": 5.15
        },
        "Count": 5
      }      
    ]
  }
}

After executing, you should get a result like this:

{
  "InvoiceItems": [
    {
      "Product": {
        "ProductID": "PK-207-U",
        "Description": "Rubber bands",
        "Price": 11.75
      },
      "Count": 8,
      "Amount": 94
    },
    {
      "Product": {
        "ProductID": "KD-883-J",
        "Description": "6mm tube - 1mtr",
        "Price": 5.15
      },
      "Count": 5,
      "Amount": 25.75
    }
  ],
  "TotalAmount": 119.75
}

With the amount calculated per item and the total amount calculated for the invoice.

Additional resources

PreviousEntitiesNextDrag and Drop a JSON Schema or XSD to create entities

Notice that Rulecube places the of input in front of regular properties and the of rule in front of calculated properties.

Explanation This is an that summarizes all the item amounts into the TotalAmount.

and enter input like this:

If you have a JSON schema or an XSD for your entity structure, Rulecube has a much easier and quicker way to create entities as described in the guide.

arrow function
Try out the ruleset
Drag and Drop