Sorting Collections of Objects in OpenRules

We added more user-friendly sorting capabilities to the Release 10.1.0 of OpenRules. There are no need to use Java to define objects in the array of list of business objects that should be sorted inside a decision model. Let’s consider a simple example of sorting the array of “Passengers” using their frequent flier status and a number of miles.

You can use a regular decision table that defines how compare any two elements of an array (or a list) by adding [sort <ArrayName>] at the end of its signature row. Let’s look at this sample:

For each pair of passengers “Passenger1” and “Passenger2” this table selects a preferred passenger in the last column of the type “ActionPrefer”. When the statuses of both passengers are the same, the number of frequent miles serves as a tiebreaker. When even the miles are the same, you may use “=” or “Same” (or any other word different from Passenger1 and Passenger2). There is no need to define “Passenger1” and “Passenger2” in the glossary that simply looks as below:

Here the array “Passengers” by itself is a decision variable defined inside the business concept “Problem”. The glossary does not include variables “Passenger1” and “Passenger2” as they are local variables used only inside the table “SortPassengers”. Their names are formed by the type “Passenger” of the array of “Passengers” plus the numbers 1 and 2.

The project “SortPassengers” and a more complex project “FlightRebooking” can be found in the standard workspace OpenRulesSamples. I recommend you to look at the simplified “FlightRebooking” that now does not require Java at all. Another sample project “SortProducts” demonstrates how to sort arrays of objects defined in the Java class Product that need to be Comparable.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.