All Comparative Expressions
All Comparative Expressions
Comparative expressions are functions that compare 2 variables or constants, and can be used to compare string or number type concepts. Comparative expression functions are built inside “expressions” when creating a rule, normally with the following formula:
%VARIABLE_1 comparative_expression %VARIABLE_2
There are 6 types of comparative expressions:
- is equal to / is equal / equal / equals
- is not equal to / does not equal
- greater than / is greater than / gt
- less than / is less / lt
- greater than or equal to / gte
- less than or equal to / lte
Any of the forms above can be used when creating the expression (‘greater than‘ , ‘is greater than‘ and ‘gt‘ will all perform the same function).
Comparative expressions are really useful when creating conditions for rules, and can be used to create rules that can trigger outcomes or not, depending on whether the conditions of a rule have been satisfied.
To illustrate how comparative expressions can be used, we will build a simple map which uses the different functions to check whether a child (LaMarcus Thompson) can ride a big and potentially dangerous roller coaster.
Before LaMarcus can ride the coaster, a few things need to be checked (size, weight, etc…). We start by creating 5 concepts (“Size” as number, “Money” as number, “Date of birth” as date, “Medical condition” as string, “Weight” as number), which we link to a “person concept” by 5 relationships. We enable the questions on each of these relationships, and we create 3 concept instances for the “Medical condition” concept (“Fragile heart”, “Weak stomach”, “None of the above”).
Figure 1: Basic model
We’ll then create rules to check whether LaMarcus can ride using all of the comparative expressions:
First of all we check if today is the birthday of the child, as the ride is free for those who have their birthday “today”.
We start by creating a “Free ride” concept and a relationship linked to the “Person” concept. We add into the conditions the date of today, and the date of birth of LaMarcus, the child.
Figure 2: Creating the relationship and variable for the comparison
Now we need to create the comparative expression, which will test if today’s month is the same as the month a person was born in, and if the day of the month is the same day of the month that someone was born on:
Figure 3: Comparative expressions “is equal to”
The rule is object specific to the “true” boolean concept , as the relationship will only return true if today’s date is the same as of the child’s birthday.
LaMarcus can only ride the roller coaster if he has no medical conditions. Hence, if the child’s medical condition is different from “None of the above (medical conditions)” then he will be forbidden from riding the roller coaster. To get Rainbird to filter out people with medical conditions, we will use the comparative expression “is not equal to”:
Figure 4: Building the structure of the rule for forbidding medical conditioned children
Figure 5: Comparative expression “is not equal to”
Note that the expression wouldn’t work if the relationship “has medical condition” was a plural relationship. If a variable is plural, comparative expressions don’t work as Rainbird will only consider the first item of a variable, which could lead to incorrect wrong results.
Rainbird will also need to check if the child is not too heavy to ride the roller coaster, as the ride may not be able to support those above a certain weight. The rules of the roller coaster state that anyone over 100kg cannot ride the rollercoaster as they may damage the ride. However, if they weigh exactly 100kg they can still ride the coaster. To create this rule in Rainbird, we use the same relationship as for the previous test (“is forbidden to ride”), and we create a rule with the ‘greater than‘ comparative expression as shown below:
Figure 6: Building the rule which will forbid people over the maximum weight
Figure 7: Comparative expression “greater than”
Rainbird will also need to check if the child is tall enough to ride the roller coaster. It could be dangerous if LaMarcus doesn’t fit into the seat of the cart. The minimum height for the ride is 150cm, so if someone is 150cm tall he can ride, but anything less than 150cm will forbid him from riding. To create this rule in Rainbird we again use the “is forbidden to ride” relationship, and we create a rule with the “less than” comparative expression.
Figure 8: Building the rule which will forbid people under 150cm tall
Figure 9: Comparative expression “less than”
The “greater than or equal” to expression works the same as the “greater than” expression, butincludes the value we are comparing the variable to. If we used the “greater than or equal to” expression for our weight rule that forbids people over 100kg from riding the coaster (see the greater than section), the rulewould also forbid people that weigh 100kg.
Rainbird will use the “greater than or equal to” expression to check if the child has enough money (£2.50) to pay for the ride. We specifically use the greater than or equal expression because the child can afford the ride if he has exactly £2.50. Here is how we build the rule:
Figure 10: Building the structure to check if the person can afford the ride
Note that we created a rule that triggers true if the person is entitled to a free ride.
Figure 11: Comparative expression “greater than or equal”
The ‘less than or equal to/lte‘ expression works the same as the less than expression, the only difference is that it will include the value we are comparing the variable to. If we had used this comparative expression for our height rule (see the less than section), our rule would also forbid from riding the rollercoaster those that are exactly 150cm.
Rainbird will use the ‘less than or equal to/lte‘ expression to check if the child is ready to wait 20 minutes before the ride as there is an average of 20 minutes of queue for this ride. If he is ready to wait 20 minutes or less, then we advise him to queue for it. Here is how we create this check:
Figure 12: Building the structure to check if the person is patient enough for this ride
Figure 13: Comparative expression “less than or equal”
We can then combine all the relationships we have createdinto one overall relationship, and give a final outcome to LaMarcus Thompson:
Figure 14: Final outcome of the combined rules using different comparative expressions
Query and Results
When using the below RBLang, please run the query on the rule ‘has final advice’ for an example of a query which uses multiple comparatice expressions.
Version 1.02 – Last Update: 22/02/2021