skip to main content
Corticon Studio: Rule Modeling Guide : Collections : Sorted aliases
 

Try Corticon Now

Sorted aliases

You can create a special kind of alias in the Scope section of a Rulesheet. The technique uses the specialized Sequence operator ->next against a Sorted Alias (a special cached Sequence) inside a filter expression. The Rulesheet is set into a Ruleflow that iterates to bind the alias in each successive invocation to the next element in the sequence.
The following example shows a Rulesheet based on the Cargo Vocabulary. We brought the Cargo entity and its weight attribute into the scope:
The operators sortedBy and sortedByDesc enable sorting ascending or descending order of the numeric or alphabetic values of the attribute in the set of data. Note that an attribute with a Boolean data type is not valid for this operation.
Dragging the sortedBy operator and dropping it (you cannot type it in) on the attribute weight places it in the scope yet shows an error:
The error message notes that a sorted alias node requires an alias name. When we enter an alias name, the scope is complete.
We add a filter expression to establish that, when we iterate through the list, each pass will present the next sequential item in the sorted set. We defined this by dragged sortedBy from the scope to filter line 1, and then appended the ->next operator. We added a rule statement based on sorted load that echoes the weight so we can see the results in our tests.
We saved the Rulesheet and created a Ruleflow, adding in our Rulesheet. Then we dragged an Iterative operation to the Rulesheet in the Ruleflow and saved it.
We created a Ruletest with a few Cargo items, each with a weight that we expect to sequence numerically when we run the test. Each iteration posts a message, and that message (the corresponding Rule Statement) contains the embedded attribute load weight. Since each member of the load collection will “trigger” the nonconditional rule, and even though the elements will be processed in no particular order, we expect to see a set of resulting messages with load weight in order. Running the tests repeatedly outputs the weights in ascending order every time.
If we change the operator to sortByDesc, the results are shown in descending order by weight, as expected.