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).
Build Concepts and Relationships
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
Build Instances
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.
Build Rule
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:
STRUCTURE: (subjectConceptVariable, countRel, *)
Example: countRelationshipInstance(%S, owns vehicle, *)
The expression above can be used to count any instance between the subject concept and the object concept in order to make further calculations or comparisons. For example, if Rainbird was counting the different varieties of cakes a bakery sells (brownies, cookies, and sponge cakes), each type of cake would be a different concept instance, therefore 3 relationship instances on a hypothetical “sells cakes” relationship between a “shop” and “cake concept” would exist, so Rainbird’s outcome would be 3.
In our example map, this type of countRelationshipInstance expression counts the “owns vehicle” relationship instances/facts between the subject concept variable (Customer) and any instance/facts (*) of the Object (Vehicle):
Figure 5: Concept Customer owns vehicle Instances of Vehicle
In the example map, the countRelationshipInstance expression is used to count how many cars a customer owns. The expression is combined with two more comparative expressions so that Rainbird displays a string concatenation if a discount is not available (figure 6) or available (figure 8)
Building the rule (condition NOT fulfilled):
- The countRelationshipInstance counts and compares the amount of cars owned between a subject (%S) and ANY Object.
- If the answer is equal to 1 Rainbird will display a string concatenation as a result
Figure 6: Building the rule which will tell Rainbird if a discount should not be applied
Now, the query will return the following:
Figure 7: Query Result
We also need to create an object specific rule so that Rainbird knows when a multi-car discount applies.
Rule step by step (condition fulfilled):
- First we select the Object (Outcome) for this condition to be ‘multi-car-discount’
- The condition will be met IF countRelationshipInstance counts and compares the number of cars owned between a subject (%S) and ANY Object and if the comparison expression returns a number greater than 1
Figure 8: Building an object-specific rule for the “multi-car discount” object
Query Result:
Figure 9: Comparative expression “equal to”
To test the “subjectConceptVariable, countRel, *” example, in the knowledge map generated by the RBLang in the bottom of the article, please run the query on the relationship ‘entitled to’.
STRUCTURE (subject_specific_instance, countRel, *)
example: e.g. countRelationshipInstance(‘Pete’, owns vehicle, *)
This version of the countRelationshipInstance expression counts the amount of relationship instances (e.g. “owns vehicle” relationship instances) between a specific concept instance/fact (e.g a Customer: Pete) and any Object instance (*) (e.g. Pete owns 16 cars).
Figure 10: Pete will be entitled to a discount if he owns a vehicle
You may want to use the subject specific countRelationshipInstance expression if you want to create a rule or condition that only applies to specific subject concept instances. Returning to the car discount map, you may, for example, want a rule just for the manager of the dealership, Pete. Pete will get.a manager’s discount on any car he wants to buy.
Building the rule
- The countRelationshipInstance counts and compares ‘owns cars’ relationship instances against the specific subject ‘Pete’ and ANY Object.
- If Pete has ANY cars the condition is true and Rainbird will display a string concatenation
Figure 11: Pete received a discount for any car
Rainbird will return the following result:
Figure 12: Query results
To test the outcome in the map generated by the RBLang below, run the query ‘Pete’s case’ and input ‘Pete’ as the Customer (%S). The question about vehicles can be skipped.
STRUCTURE: (subject_specific_instance, countRel, objectConceptVariable)
example: e.g. countRelationshipInstance(‘Pete’, owns vehicle, %VEHICLE)
The above variant of the countRelationshipInstance expression is used to display all relationship instances between a specific subject instance (in our example, Pete), and a specific object concept (in our example, %VEHICLE).
Figure 12: Pete (Customer) owns multiple vehicles
This function could also be used to display the items on a specific person’s shopping list or the goods in a specific container.
In our example Rainbird will look at a specific Customer, Pete , and list all the concept instances/facts of owns vehicle (in our case these are a Ford Fiesta and a VW Tiguan) for Pete:
Figure 13: Facts between Pete and the ‘owns vehicle’ relationship
Building the Rule
- The first Condition %S owns vehicle %VEHICLE is required to define the Object
- The following countRelationshipInstance expression looks at the specific instance ‘Pete’ to list all owned %VEHICLES. This expression will create a new variable %CARS as a result
- %CARS can then be displayed as a string concatenation “Pete owns (amount of) %CARS fact (Vehicle)
- The concatenation is assigned to %O so Rainbird will display the string concatenation as the result of the query
Figure 14: Building the rule
The outcome of the rule would look like this:
Figure 15: Query results listing the cars the specific subject (Pete) owns
To test this variation of the countRelationshipInstances expression, run the query ‘list vehicles’ when viewing the knowledge map generated by the RBLang at the bottom of the article.
STRUCTURE: (subject_specific_INSTANCE, countRel, object_specific_INSTANCE)
example: e.g. countRelationshipInstance(‘Cars R Us’, owns car of specific type, ‘Middle Class’)
This type of countRelationshipInstance expression will count the “owns car of specific type” relationship instances for a specific subject concept instance/fact (Customer: Cars R Us) and specific object instance of a car type (Middle Class).
This variant of countRelationshipInstances is specific for both the subject and the object side (e.g. Pete owns a Ford Fiesta)
Note: Between specific instances you can only have one fact/instance. A single fact can only return 1 or 0 (true/false).
Figure 16: Subject (Customer) owns car of a specific type Object (Type)
In our example Rainbird will look at a specific Customer (Cars R Us) to check if a specific relationship instance/fact of the “owns car of specific type“ relationship is true. For example, Rainbird could use the expression to find out if one of the cars Cars R Us owns is a middle class vehicle.
Figure 17: Cars R Us ‘owns vehicle’ facts
Building the Rule:
- The first three Conditions form the triangle we require to define the subjects and objects:
- A %CUSTOMER owns a %VEHICLE
- A %VEHICLE has a car %TYPE
- A %CUSTOMER owns a car of a specific %TYPE
- The following countRelationshipInstance expression looks at the specific instance ‘Cars R Us’ IF there is a car of the specific %TYPE ‘Middle Class’
- The result will be displayed as 1, as the fact exist in the map
- %O has to be assigned to the a variable to display the result as an output (1=Yes or 0=No)
Figure 18: Building the rule
When running the query on ‘Cars R Us has cars of specific type’ with the subject Cars R Us the outcome would look like this:
Figure 19: The result is 1, which stands for ‘true’
To test the “subject_specific_INSTANCE, countRel, object_specific_INSTANCE” example, in the knowledge map generated by the RBLang in the bottom of the article, please run the query on the ‘Cars R US has cars of specific type’ relationship.
STRUCTURE: (ANY_subject, countRel, ANY_object_instance)
example: e.g. countRelationshipInstance(*, owns car of specific type, *)
This variant of the countRelationshipInstance expression can be used to count the relationship instances between any subject of a relationship, and any object of a relationship. In our example map, this countRelationshipInstance expression will count every existing owns vehicle relationship instance/fact between %CUSTOMER and %VEHICLE.
In the example map we have the customers ‘Pete’ who owns two cars and ‘Cars R Us’ who owns four cars. As a result we would expect a count of 6 as an outcome (see Figure 23).
Figure 20: Count all existing facts between %CUSTOMER owns vehicle %VEHICLE
This variant could also be used to count if there are any or a specific amount of existing facts on a relationship (for example, %HOSPITAL had fire incidents %INCIDENTS).
In our example Rainbird will look at all concept instances of %CUSTOMER and count the facts on owns vehicle:
Figure 21: 4 facts on Cars R Us and 2 facts on Pete = 6 total facts
Building the Rule step by step:
There are no conditions required:
- The countRelationship expression reads: Count ANY (*) fact on owns vehicle concept instance (of the Subject and Object)
- The expression is assigned to %O, so that Rainbird will display the sum of the expression as the result of the query
Figure 22: Creating the rules to count all facts of owns vehicle
The outcome of the rule would look like this:
Figure 23:Total amount of owned vehicles
To test the “ANY_subject, countRel, ANY_object_instance” example, in the knowledge map generated by the RBLang in the bottom of the article, please run the query on the relationship ‘count all vehicles’.
STRUCTURE: (ANY_subject, countRel, objectConceptVariable)
example: e.g. countRelationshipInstance*, available in, %COLOURS)
This variant of the countRelationshipInstance expression will count the relationship instances that exist between any subject concept instances, and an object concept instance selected at runtime, In our map, this expression will count the availability of ANY %VEHICLES in a %COLOUR we select at runtime.
We might want to use this expression, if for example, we were doing a stocktake at Cars R Us and wanted to know how many cars there are in a certain colour.
Figure 23: Any availability in a %COLOUR
This function could also be used to, for example, work out how many size L t-shirts there are in a wardrobe, or how many dishes on a menu are vegan.
Building the expression
In our example Rainbird will count all vehicles in a colour the user chooses at run time.
Figure 24: Facts between a vehicle concept instance and colour concept instances
Rules Steps:
- The first Condition ‘%S wants colour %COLOURS‘ is will prompt the user to select a colour of %VEHICLE
- The countRelationshipInstance expression ‘countRelationshipInstances(*,available in, %COLOURS) looks at ALL %VEHICLES ‘ in the colour (%COLOURS) the user will have selected
- The expression is assigned to %O, so that Rainbird will display the sum of the expression as the result of the query
Figure 25: Creating the rules to display the outcome in a string concatenation
The outcome of the query will be the number of vehicles in a colour chosen by the user at runtime:
Figure 26: Number of vehicles of a specific colour
To test the “ANY_subject, countRel, objectConceptVariable” expression variation, in the knowledge map generated by the RBLang in the bottom of the article, please run the query on the relationship ‘stocktake by colour’.
STRUCTURE: (ANY_subject, countRel, object_specific_concept)
example: countRelationshipInstance(*, owns car of specific type, ‘Middle Class’)
This variant of the countRelationshipInstance expression is used to count the number of relationship instances between any subject concept instances, and a specific object concept instance. In our map, the countRelationshipInstances expression will count how many specific %TYPE of cars (e.g.Middle Class) we have for ANY %CUSTOMER.
Figure 27: All Customers owns x amount of Middle Class Type vehicles
This function could also be used, for example, to count the amount of Bananas we have in a fruit bowl.
In our example Rainbird will look at any number of existing Customer to list all the ‘Middle Class‘ vehicles. We would count any Middle Class vehicle we have in the Vehicle concept as instance once.
Figure 28: Customer owns vehicle facts
Building the Rule:
No conditions are required, only an expression:
- The countRelationship expression reads: COUNT ANY owns specific TYPE of ‘Middle Class’
- The expression is assigned to %O, so that Rainbird will display the sum of the expression as the result of the query
Figure 29: Creating the rule to count the specific Object ‘Middle Class’
The outcome would look like this:
Figure 30: Output as a count of ‘Middle Class’ vehicles under %TYPE
To test the ‘ANY_subject, countRel, object_specific_concept’ expression variation, in the knowledge map generated by the RBLang in the bottom of the article, please run the query on the relationship ‘stocktake for middle class vehicles’.
STRUCTURE: (subjectConceptVariable, countRel, object_specific_instance)
example: countRelationshipInstance(%VEHICLE, car type, ‘SUV’)
This variant of the countRelationshipExpression will count the relationship instances between a concept instance entered at runtime, and a specific object concept instance. In the example map, the countRelationshipInstance expression will count the car type concept instances/facts between the subject Concept variable (Customer) of a specific object instance of car type (SUV).
Note: Between specific instances you can only have one fact/instance. A single fact can only return 1 or 0 (true/false).
Figure 31: A Customer owns a vehicle of a specific car type instance
In our example Rainbird will look at any customer (Pete). We want to confirm if Pete owns a vehicle of a specific concept Instance (SUV):
Figure 32: Pete has a VW Tiguan type SUV
Building the Rule
- The first Condition %S owns vehicle %VEHICLE is required to define the Object
- The countRelationshipInstances expression is looking for a specific instance ‘SUV’
- We then display the result as a count
- The string concatenation is assigned to %O, so that Rainbird will display the concatenation as the result of the query
Figure 33: Creating the rules to display the outcome in a string concatenation
Query result:
The outcome would look like this:
Figure 34: string concatenation if subject owns a SUV
To test the ‘subject_concept_variable, countRel, object_specific_instance’ expression variation, in the knowledge map generated by the RBLang in the bottom of the article, please run the query on the relationship ‘customer has specific type of suv’.
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.