Rules --> Advanced Expressions -->

countRelationshipInstance

The countRelationshipInstance expression is used to count concept instances/facts either within the map or entered when querying the map. The countRelationship expression (or countRel for short) can be used with string, number and date instances. The expression can not be directly used with a boolean function. However, by using a comparative expression in combination with the countRel expression, a boolean function can be used to answer a yes or no question.

This article will demonstrate how to build a knowledge map that will use the countRelationshipInstance expression to work out whether a customer is eligible for a discount at a car dealership, by counting the amount of vehicles a customer owns (the customer will be eligible for the discount if they own more than one vehicle).

To start the map off, create the following string concepts: ‘Customer’, ‘Vehicle’ and ‘Discount’

Next, link the concepts with the relationships ‘entitled to’ (where we will place our rule) and ‘own vehicle’ (to count the amount of vehicles), as shown in Figure 1:

Figure 1: Concepts and Relationships

 

Now, create concept instances for the vehicle concept:

Figure 2: Three example concept instances of the Vehicle Concept

The end-user will be able to select up to three vehicles whilst completing the query. The instances could also be entered by the user at run-time.

Now it’s time to create the rule that will use the countRelationshipInstance expression. The rule states that a  if the amount of vehicles a person owns is greater than 1, a multi-car discount will apply (in the example below, we have used the shortcut for greater than: gt).

Open up the “entitled to” relationship, click ‘Add Rule’, and then ‘Add Expression’. As the rule will be object specific, select “multi-car discount” as the object of the rule. Add the following expression to the expression box:

countRelationshipInstances(%S, owns vehicle, *) gt 1

The countRelationship expression, will count the amount of relationship instances that exist between the %SUBJECT (the customer) and the object of the “owns vehicle” relationship (represented as *, as the object(s) of the relationship(s) will be selected by the customer at runtime). Each vehicle object selected (or manually entered) by the customer will create a new relationship instance of the “owns vehicle” relationship; if Rainbird detects that more than 1 relationship instance exists, then the multi-car discount applies.

As the rule is object-specific, the result of the variable does not need to be assigned to %O.

Figure 3: Object Specific (multi-car-discount) is true if the countRel returns true

We also need to create a rule so that Rainbird will return a result if the customer owns exactly one vehicle.

Return to the “entitled to” relationship and click ‘Add Rule’. Next, click ‘Add Expression’ and add the following expression to the rule:

countRelationshipInstances(%S, owns vehicle, *) = 1 

As no concept instance has been defined for “no discount applicable”, we need to create a stringConcatenation for Rainbird to return as a result.

After creating the expression, click ‘add expression’ again, and add “Sorry, you are not entitled to multi-car discount” to the expression box. The variable will need to be assigned to the variable “%O” (the object) in the field ‘Assign to a variable’, so that Rainbird will return the string concatenation as the object of the relationship (the answer) if the countRelationshipInstance(%S, owns vehicle, *) = 1 expression is satisfied: 

Figure 4: Building the second rule

Now, if Rainbird counts only 1 relationship instance for the ‘owns vehicle’ relationship, Rainbird will return “Sorry, you are not entitled to multi-car discount” as the result of the query.

Variations:

The countRelationshipInstance expression used above can be changed, depending on the requirements of the knowledge map. Click on a heading below to find out more about the different variations of the expression:

 

The RBLang below will generate a map that uses the different variations of the countRelationshipInstance expression mentioned in the article. Click on ‘Export .rbird’ to download the knowledge map, or ‘copy RBLang’ and paste the code directly into Rainbird.

Query and Results

To test the different countRelationshipInstance expressions, run the query on the relationship specified at the end of each individual expression variation section above.

Article Feedback form
Did you find this article useful?

Version 1.02 – Last Update: 23/03/2021

Bitnami