📖
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
  • Task: Create the lookup table
  • How does it work?
  • Task: Simplify the AgeSurcharge function
  • Recap
  1. Tutorial

Using Lookup Tables

Introduction

In this tutorial we will revisit Constant tables and use one of its very useful functionalities: lookup.

Lookup tables allow you to implement something called Decision tables. This should help simplify the logic we've written in the previous tutorial where we calculated the age surcharge.

We'll be rewriting code in this tutorial, but the overall logic should not change. Our Testcase should still succeed after our changes!

Task: Create the lookup table

  1. Open the Travel insurance calculator from the previous tutorials.

  2. Let's first delete these three constants:

    • ExtraCostPerDay18_30

    • ExtraCostPerDay31_50

    • ExtraCostPerDay51AndUp

    Note: you can do this by clicking the (...) icon of the constant in the menu and clicking Delete OR opening the constant and clicking "Delete" in the bottom right corner of the screen.

  3. Create a new constant named ExtraCostsPerDay and set its Constant Type to Table

You should be somewhat familiar with Constant tables if you've followed the tutorials in order so far. Let's start filling the table with data.

  1. First, click the + COLUMN button to add a new column

  2. Rename the value column to age

  3. Rename the value2 column to result

Keys are not as important in lookup tables, we will start with with 1 and let it automatically increment.

  1. Click on the first cell (key)

  2. Enter 1 in this cell

  3. Press Tab to move to the age column

  4. Enter < 18 and press Tab

  5. Leave the result cell 0 and press Enter

This should create a new row with a key that is 2.

Enter the following rows to complete the table:

Alternatively, copy and paste the following CSV while you have the ExtraCostsPerDay constant open.

key,age,result
1,< 18,0
2,>= 18 AND <= 30,0.15
3,> 30 AND <= 50,0.1
4,> 50,0.05

Note: Make sure you are not in a text field when you paste, or the contents of the CSV will be pasted in that instead.

How does it work?

You may have noticed that the cells under the age column are expressions and not plain values. These expressions are used when the lookup() method is used.

For example you could try this in a new rule named LookupTest:

ExtraCostsPerDay.lookup(20)

This will look up the value 20 in the table in the first column (the Key column is not counted) and return the value in the last column. This will result in 0.15, because the second row's expression (>= 18 AND <= 30) satisfies for the given input of 20.

Task: Simplify the AgeSurcharge function

Next we're going to simplify the AgeSurcharge function. Its Function body currently should look something like this:

if (age >= 18 && age < 31) {
    return numberOfDays * ExtraCostPerDay18_30;
}
if (age >= 31 && age < 51) {
    return numberOfDays * ExtraCostPerDay31_50;
}
if (age >= 51) {
    return numberOfDays * ExtraCostPerDay51AndUp;
}
return 0;

We're going to actually replace this with just a single line of code!

  1. Delete the contents of AgeSurcharge's Function body code

  2. Add the following code:

    return numberOfDays * ExtraCostsPerDay.lookup(age);
  3. Save the ruleset

If everything went well, the ruleset should work as before. If you followed the previous tutorial on adding Testcases, the Testcase we've created should still succeed, as we did not change the overall logic.

Recap

Complex code that deals with calculations with tiered values can be rewritten using Constant tables and their lookup() method.

This reduces code in your ruleset and allows for easy management of business logic.

PreviousAdding TestcasesNextUsing Entities