1. Home
  2. FlexRule Runtime (SDK)
  3. Processing Collection Programatically

Processing Collection Programatically

◷ Reading Time: 2 minutes

When you have a collection of objects and would like to process than via a logic document (i.e., rules), you can process them one-by-one in your code, or you can ask the engine to process them collectively.

Processing individually

Processing a Collection

If you process the objects one-by-one, you would have something similar to the code shown below:

var engine = RuntimeEngine.FromXml(Encoding.UTF8.GetBytes(ruleDocument));
 
/*In the line below, Collection is the list of objects you are going to execute against the business logic (i.e., rule, flow, validation, decision, decision table, etc.) */
foreach(var item in collection)
{
    engine.Run(item);
}

If the number of items in the collection is big, then it is worth parallelising the execution. This is achieved simply by using standard .Net Parallel API.

Parallel

Changing the normal for and loops to a Parallel is no different at all from the normal usage pattern of Parallel.

Parallel.ForEach(collection, (item)=>
{
    engine.Run(item);
});
Managing RuntimeResult

When you need to access the result of the execution from the engine after processing, then use the concurrent dictionary with the actual object as a key to store engine results locally if you need to process them later.

ConcurrentDictionary<object, RuntimeResult> results = new ConcurrentDictionary<object, RuntimeResult>();
Parallel.ForEach(collection, (item)=>
{
    results[item] = engine.Run(item);
});
Updated on April 19, 2020

Was this article helpful?

Related Articles