skip to main content
Corticon Studio: Rule Modeling Guide : Rule dependency: Chaining and looping : Looping examples : Removing duplicated children in an association

Try Corticon Now
Removing duplicated children in an association


For a Customer->Address association (one-to-many), each address must be unique.


Compare every address associated with a customer with every other address associated with that customer, and -- when a match is found -- remove (or mark) one of the addresses.
The following example compares ALL pairs of addresses that meet a filter condition. That process occurs in no specific order so you might notice that one run starts with address 4 and address 2 (id=1 < id=4), yet the next time it runs, it might start with address 3 and address 1 (id=2 < id=3). So the results might seem different. However, all that is required is that only one of each unique address survive.
To assure that we can control the filtering process, we need unique identifier attribute values to distinguish the instances. If the address already has an attribute that is a unique identifier, then you could use that in the filter; otherwise we need to create a transient, integer attribute, id, in the Address entity in the Vocabulary:
Using our created identifier attribute, we create a Rulesheet to identify each unique address. It uses two aliases to run through the addresses associated with a given customer. The actions initialize the id, and then add an incremented id value to each associated Address in memory:
Once each address has a unique identity, the second Rulesheet will do the removal action. It iterates through the associations to identify whether an association has a match, and -- if it does -- to remove the matching association from memory, as shown:
A Ruleflow puts the two Rulesheets into sequence, as shown:
A Ruletest that uses this Ruleflow as the test subject shows the "survivors" in its output:
After this processing is done, subsequent Rulesheets in the Ruleflow will see only unduplicated addresses for each customer.
Note: Rule Statements were not requested for this process. As we are actually removing the duplicates during the execution of the rule, each removed address has been dropped from memory, and no longer has a meaningful reference when statement message is generated.
Flagging duplicate children
You might want to identify the duplicated records rather than delete them. To do so, just uncheck (or delete) the .remove action, and add an appropriate .comment value to the address. This examples uses, 'Duplicate', as shown:
When the same Ruletest runs, this time shows all the input records, with duplicated records displaying their comment value:
Note: Again, Rule Statements were not used. There are in fact three duplicates – address 4 and address 1, address 4 and address 2, address 1 and address 2 – so three messages (referencing 1, 4, and 4) would be generated since all of the addresses are still in memory. Two get marked as duplicates, and one survives. In a subsequent Rulesheet, you could delete all addresses that have been flagged as 'Duplicate'.