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)
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.
Figure 3: Main rule: A person has a bill, which has a valueNow, 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 expressionRainbird 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
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.
Version 1.02 – Last Update: 18/03/2021