This article contains the following sections:
Looping through a Collection
Any time you loop through or iterate a collection of any type, there are some restrictions to consider. Inside the loop, you may change an individual member, but you may not change the collection itself.
For example, consider LineItems, a collection of LineItem entities in the InvoiceSample Rule Application.
As you loop through LineItems, examining each LineItem, you may change LineItem fields such as Quantity, but you may not remove the LineItem from the LineItems collection.
In InRule, this restriction applies to the For Each and the Execute Member Rule Set actions. While inside the loop, you may not:
- Add Collection Member
- Clear Collection
- Copy Collection (copying into the collection)
- Remove Collection Member
- Sort Collection
Prerequisites | A valid Entity |
Namespaces | InRule.Runtime |
Classes | Field, Collection, Entity |
See Also |
Retrieving a Rule Application, Creating a RuleSession, Creating Entities |
Entity based collections
// loop over all the payments collection members
foreach (EntityCollectionMember payment in mortgageEntity.Collections["Payments"])
{
// Write out some of the entity field values
Console.WriteLine(payment.Fields["PaymentDate"].Value.ToString());
Console.WriteLine(payment.Fields["Amount"].Value.ToString());
Console.WriteLine(payment.Fields["RemainingBalance"].Value.ToString());
}
An alternative way to loop through the entities themselves is as follows:
// Check if the collection of entities
if (collection is InRule.Runtime.EntityCollection)
{
// process a collection of entities
foreach (EntityCollectionMember member in collection)
{
// Get the entity the member refers to
Entity memberEntity = member.Value.ToEntity();
// Iterate the fields
foreach (Field field in memberEntity.Fields)
{
// get the fields info
Console.WriteLine("FieldName:" + field.Name);
Console.WriteLine("FieldValue:" + field.Value.ToString());
}
}
}
Complex collections
// Loop through the members in the complex collection AdditionalCharges
foreach (CollectionMember charge in mortgageEntity.Collections["AdditionalCharges"])
{
// write the field values
Console.WriteLine(charge.Fields["Name"].Value.ToString());
Console.WriteLine(charge.Fields["Amount"].Value.ToString());
}
Collection indexing is 0-based through the SDK and 1-based for referencing within rule
Adding a Member to a Collection
Prerequisites | A valid Entity |
Namespaces | InRule.Runtime |
Classes | Collection, Entity |
See Also | Retrieving a Rule Application, Creating a RuleSession, Creating Entities |
Adding a new member to a Collection
// Create top level entity
Entity invoiceEntity = ruleSession.CreateEntity("Invoice");
// Get reference to the LineItems collection
Collection lineItemCollection = invoiceEntity.Collections["LineItems"]
// Add a new line item to the collection
CollectionMember lineItem = (lineItemCollection.Add());
// Get underlying entity
Entity lineItemEntity = lineItem.Value.ToEntity();
// Update the line item fields on the newly added member
lineItemEntity.Fields["ProductID"].Value = 2;
lineItemEntity.Fields["Quantity"].Value = 10;
Adding an existing member to a Collection
// Create top level entity
Entity invoiceEntity = ruleSession.CreateEntity("Invoice");
// Get reference to the LineItems collection
Collection lineItemCollection = invoiceEntity.Collections["LineItems"];
// Create a LineItem and populate the fields
Entity lineItemEntity = ruleSession.CreateEntity("LineItem");
lineItemEntity.Fields["ProductID"].Value = 3;
lineItemEntity.Fields["Quantity"].Value = 100;
// Append the new member to the collection
lineItemCollection.Add(lineItemEntity);
Resolving Field Types at Runtime
Prerequisites |
None |
Namespaces | InRule.Runtime |
Classes | FileSystemRuleApplicationReference, RuleSession, Entity |
References | InRule.Runtime.dll, InRule.Common.dll |
To resolve the field type, you can use the following code:
// Test if this field refers to an entity
if (field.GetType() == typeof(InRule.Runtime.EntityField))
{
}
In a similar way, you can test if a collection contains entities:
// Test if this is a collection of entities
if (collection.GetType() == typeof(InRule.Runtime.EntityCollection))
{
}
Comments
0 comments
Please sign in to leave a comment.