Calculating Aggregated Values for Collections of Business Objects

The latest DMCommunity.org Challenge “Classify Department Employees” deals with calculation of aggregated values for arrays of business objects such as Employees. Below I describe an OpenRules solution for this challenge.

Here is the problem description:

Bruce Silver has already submitted his DMN-based solution that essentially relies on the Boxed Context and other DMN Compliance Level 3 “heavy weight” constructs. Previously, I had already shown how small enhancements of the regular DMN decision tables could allow a business user to decision models which can iterate through collections of objects without committing to complex boxed expressions and explicit for..return loops with indexes. So, this challenge provides an opportunity to demonstrate the power of core DMN  constructs again.

To iterate through an array of employees, we will use two decision tables:

The first table “EvaluateAllEmployees” has type “DecisionTableIterate” that requires to execute rules “EvaluateOneEmployee” for every Employee from the array “Employees”.

The second decision table is a regular DMN multi-hit decision table that for the currently considered employee will do the following:

Rule 1:

  1. Unconditionally adds the Employee’s Salary to the aggregated variable “Total Salary”
  2. Unconditionally assigns the maximal/minimal salaries to the aggregated variables “Max Salary” and “Min Salary”
  3. Unconditionally accumulate the total “Number of Employees”

Rule 2:

  1. For employees with Salary larger or equal to 85000 with define their “Wealth Category” as HighPaid
  2. Accumulate the total “Number of High-Paid Employees”

Rule 3:

  1. For employees with Salary smaller than 85000 with define their “Wealth Category” as Regular.

We will apply these rules to multiple test cases, so it’s necessary to initialize the aggregated variables before applying these rules. It can be done using this simple decision table:

We set an initial value for “Min Salary” to 1,000,000 to make sure that it is larger than a salary of any employee: so, it will be replaced with the actual minimal salary.

To calculate the “Average Salary” we will use the decision table

Instead of this OpenRules-specific table of the type “DecisionTableAssign”, we could use a regular DMN decision table (a literal expression with FEEL):

Now we can specify our top-level decision as follows:

To complete our decision model we need to add all used decision variables to a glossary that will contain two business concepts “Employee” and “Department”:

Now let’s add test data and execute our decision model. Here is the proper Datatype tables:

Here is a Data table that corresponds to the Challenge example:

We will split “allEmployees” between two departments:

And finally we may define two test cases for each department with expected results:

Now we may execute our decision model against these test cases using the default “run.bat”. Here are the execution protocol:

RUN TEST: Test 1
Decision DetermineAggregatedValues: Initialize Aggregated Values
Assign: Max Salary = 0 [0]
Assign: Min Salary = 1000000 [1000000]
Assign: Total Salary = 0 [0]
Assign: Number Of Employees = 0 [0]
Assign: Number Of High-Paid Employees = 0 [0]
ERROR in Glossary: cannot find business concept associated with the property <Salary>
Decision DetermineAggregatedValues: Evaluate All Department Employees
Iterate over array Employees using rules EvaluateOneEmployee
Conclusion: Total Salary += Salary [20000]
Conclusion: Max Salary Max Salary [20000]
Conclusion: Min Salary Min Salary [20000]
Conclusion: Number Of Employees += 1 [1]
Conclusion: Wealth Category Is Regular [Regular]
Conclusion: Total Salary += Salary [170000]
Conclusion: Max Salary Max Salary [150000]
Conclusion: Min Salary Min Salary [20000]
Conclusion: Number Of Employees += 1 [2]
Conclusion: Number Of High-Paid Employees += 1 [1]
Conclusion: Wealth Category Is HighPaid [HighPaid]
Conclusion: Total Salary += Salary [205000]
Conclusion: Max Salary Max Salary [150000]
Conclusion: Min Salary Min Salary [20000]
Conclusion: Number Of Employees += 1 [3]
Conclusion: Wealth Category Is Regular [Regular]
Conclusion: Total Salary += Salary [280000]
Conclusion: Max Salary Max Salary [150000]
Conclusion: Min Salary Min Salary [20000]
Conclusion: Number Of Employees += 1 [4]
Conclusion: Wealth Category Is Regular [Regular]
Conclusion: Total Salary += Salary [390000]
Conclusion: Max Salary Max Salary [150000]
Conclusion: Min Salary Min Salary [20000]
Conclusion: Number Of Employees += 1 [5]
Conclusion: Number Of High-Paid Employees += 1 [2]
Conclusion: Wealth Category Is HighPaid [HighPaid]
Conclusion: Total Salary += Salary [430000]
Conclusion: Max Salary Max Salary [150000]
Conclusion: Min Salary Min Salary [20000]
Conclusion: Number Of Employees += 1 [6]
Conclusion: Wealth Category Is Regular [Regular]
Decision DetermineAggregatedValues: Calculate Average Salary
Assign: Average Salary = Total Salary / Number Of Employees [71666.66666666667]
Validating results for the test <Test 1>
Test 1 was successful

RUN TEST: Test 2
Decision DetermineAggregatedValues: Initialize Aggregated Values
Assign: Max Salary = 0 [0]
Assign: Min Salary = 1000000 [1000000]
Assign: Total Salary = 0 [0]
Assign: Number Of Employees = 0 [0]
Assign: Number Of High-Paid Employees = 0 [0]
Decision DetermineAggregatedValues: Evaluate All Department Employees
Iterate over array Employees using rules EvaluateOneEmployee
Conclusion: Total Salary += Salary [65000]
Conclusion: Max Salary Max Salary [65000]
Conclusion: Min Salary Min Salary [65000]
Conclusion: Number Of Employees += 1 [1]
Conclusion: Wealth Category Is Regular [Regular]
Conclusion: Total Salary += Salary [150000]
Conclusion: Max Salary Max Salary [85000]
Conclusion: Min Salary Min Salary [65000]
Conclusion: Number Of Employees += 1 [2]
Conclusion: Number Of High-Paid Employees += 1 [1]
Conclusion: Wealth Category Is HighPaid [HighPaid]
Conclusion: Total Salary += Salary [185000]
Conclusion: Max Salary Max Salary [85000]
Conclusion: Min Salary Min Salary [35000]
Conclusion: Number Of Employees += 1 [3]
Conclusion: Wealth Category Is Regular [Regular]
Conclusion: Total Salary += Salary [225000]
Conclusion: Max Salary Max Salary [85000]
Conclusion: Min Salary Min Salary [35000]
Conclusion: Number Of Employees += 1 [4]
Conclusion: Wealth Category Is Regular [Regular]
Conclusion: Total Salary += Salary [245000]
Conclusion: Max Salary Max Salary [85000]
Conclusion: Min Salary Min Salary [20000]
Conclusion: Number Of Employees += 1 [5]
Conclusion: Wealth Category Is Regular [Regular]
Conclusion: Total Salary += Salary [266000]
Conclusion: Max Salary Max Salary [85000]
Conclusion: Min Salary Min Salary [20000]
Conclusion: Number Of Employees += 1 [6]
Conclusion: Wealth Category Is Regular [Regular]
Decision DetermineAggregatedValues: Calculate Average Salary
Assign: Average Salary = Total Salary / Number Of Employees [44333.333333333336]
Validating results for the test <Test 2>
Test 2 was successful
All 2 tests succeeded!

Advertisements

About jacobfeldman

CTO at www.openrules.com http://www.linkedin.com/in/jacobfeldmanopenrules
This entry was posted in DMN, Use Cases. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s