Rules --> Advanced Expressions -->

sumObjects

The sumObject expression is used to count the total amount of number concept instances/facts either within the map or entered by the user when querying the map.

The sumObject expression can only be used with number concepts and is particularly useful when the number of concept instances in a map is likely to change or is unknown. 

To demonstrate the SumObject expression, this article will demonstrate how to build a map that uses the expression to calculate the total monthly cost of four bills the user has to pay . The map will use the following expression: 

sumObjects(%S, owes bill, *)

The SumObject expression will be used because the amount of bills a person may have to pay monthly may vary between users. 

Rainbird will calculate the total value by:

  • Asking the user which bills are due this month
  • Asking the cost of each bill
  • Summing up the values to give an answer (the overall cost)

First, create the following concepts:

Concepts:

Person – a string concept with no instances created (Rainbird will ask the user for a name at the start of the query, which will create a concept instance under the ‘Person’ concept)

Bill Type – a sting concept that concept instances for each type of bill will be created under

Bill Value – a number concept (the value of each of the bills)

Next, link the concepts together with relationships, as shown in Figure 1:

Relationships:

Person -> has bill -> Bill Type (set to plural)

Bill Type -> has value -> Bill Value

Person -> owes bill -> Bill Value (set to plural)

Person -> has total bill value -> Bill Value

 

Figure 1: Concepts and Relationships

 

Create the Concept instances, as shown in figure 2, for the Bill Type concept.

Figure 2: Bill Type concept instances

You could also create facts between the bills and bill value concepts on the  ‘has value’, if, for example, bills were a fixed amount each month.

There are two rules in this knowledge map.

The first rule will determine how much a bill will cost. The rule will be created on the Person -> owes bill -> Bill Value relationship. The outcome of the query will be a number of figures equal to the number of concept instances selected.

The first rule has the relationships has bill and has value as conditions in the rule:

Figure 3: Main rule: A person has a bill, which has a value

Now, if we were to run the query on the ‘owes bill’ relationship, Rainbird would ask us for our name, the bill we have to pay, the value of the bill, and then return that value as a result. For example:

Tom -> has bill -> Electricity and Electricity -> has value -> 100 results in Tom -> owes bill -> 100

This rule is run for each instance the user selects for the bill types. If the user only selected ‘Electricity’ in the drop down menu then it would be run once for ‘Electricity’. If however the user selected ‘Electricity’ and ‘Gas’ then it would be run once for ‘Electricity’ and once for ‘Gas’.

The second rule is created on the Person -> has total bill value -> Bill Value relationship. The second rule determines the overall cost of all of the bills combined using the sumObjects expression:

Figure 4: Creating the sumObjects expression

Rainbird will take all the values that have been generated on the ‘owes bill’ relationship and sum the values together.

For example:

If Tom has Gas and Electricity bills to pay this month but nothing is due for Water or Broadband, Tom would select Gas and Electricity from the drop down.

Rainbird will ask Tom to enter the value of the Gas bill (e.g. 100) and the value of the Electricity bill (e.g. 50).

Rainbird will sum these figures and return an outcome of 150:

Figure 5: Outcome of query

The RBLang below will generate the map built 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

The main query is built on the rule ‘has total bill value’. The outcome of the query will be the sum of the bills entered at run-time.

Article Feedback form
Did you find this article useful?

Version 1.02 – Last Update: 18/03/2021

Bitnami