Create and Run Your First Ruleset

Introduction

Now you're going to create and run a ruleset, which will be based on the example from the previous tutorial—the decision of whether a loan applicant is eligible for a discount based on age. After creating the ruleset, you're going to run it by trying it out from within Rulecube.

By the end of this tutorial, you'll have a ruleset that determines if a loan applicant is eligible for a discount and returns the result as a number value. Zero if the applicant isn't eligible and 1.5 if they are.

Furthermore, you'll know how to:

  • Create a ruleset.

  • Set the input.

  • Define a rule.

  • Set the output.

  • Try out a ruleset.

Before you begin

  • You must be logged in to Rulecube to create a ruleset

Step-by-step, you're going to create a ruleset and then run it in the Sandbox to see what it does.

Create a ruleset

Start off with creating a new ruleset:

  1. Click + Ruleset and select Blank ruleset to create a ruleset. ↳ The Add new ruleset pop-up opens.

  2. In the Ruleset name field, enter Loan discount.

  3. Click OK.

Rulecube creates the ruleset and shows the General settings page for the ruleset.

Notice the Input, Rules, and Output sections in the menu on the left, which correspond to the core components discussed in the previous tutorial.

When you haven't created any ruleset yet, Rulecube shows a page where you can click Start to create your first ruleset.

Task: Set the input

Your ruleset is now nothing more than a skeleton, so add an input to work with:

  1. Click the + sign behind Input from the left menu. ↳ The Input details page opens:

  2. In the Name field, enter Age and press Enter. ↳ Rulecube changes the input's name to Age and shows this alert:

Task: Define a rule and set the output

Okay, now it's time to put your ruleset to work by adding a rule:

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

  2. In the Name field, enter DiscountPercentage.

  3. Leave the Type to number to set the output type.

  4. In the Expression field, enter this expression:

    Age < 35 ? 1.5 : 0

    This is a so-called ternary expression with these parts:

    1. The condition followed by a question mark. In this case: Age < 35.

    2. The result when the condition is true, followed by a colon. In this case: 1.5.

    3. The result when the condition is false. In this case: 0.

    Another, longer way to write this expression would be:

    if (Age < 35) {
      return 1.5;
    }
    else {
      return 0;
    }

    IMPORTANT: note that we use the return statement here. When writing rules that are more complex than single-line expressions, you need to add the keyword return to the value you want the rule to have.

  5. Click Save. ↳ The Save pop-up opens.

  6. Select the Do not ask again checkbox and click OK.

Try out the ruleset

Let's try out your ruleset and see if it works as expected with these steps:

  1. Click Try it out! ↳ The Sandbox page opens.

  2. In the Age field, enter 34 and click Execute. ↳ Your rule is run and shows the expected value of 1.5 for the DiscountPercentage output:

  3. Now test the ruleset by entering 35 for age. ↳ DiscountPercentage should be 0, as expected.

  4. Try zero and a negative value for age. ↳ DiscountPercentage returns 1.5, which is technically correct as the provided ages are less than 35. But it would be better if your ruleset signals such ages as incorrect, and you'll deal with that later. For now, you can suffice by modifying the expression so that ages of 0 and less result in a DiscountPercentage of 0, which is neater and safer:

    1. Figure out how the condition should be to catch 0 and negative ages and modify the expression accordingly.

    2. Try zero and a negative value for age again to test your ruleset.

    3. Also, test correct values such as 34 and 35 to ensure that these are still processed correctly.

A solution to the modified condition to catch 0 and negative ages
Age>0 && Age<35 ? 1.5 : 0

Recap

You've created a ruleset with its core components and tried and tested it successfully from Rulecube.

What's next?

Now let's call the ruleset via its API as you would in practice in the next tutorial.

Last updated