Improved Method for Calling the BRE from .NET Applications

Previously, I had posted a short article on how one may go about calling the BizTalk Business Rules Engine from standard .NET code.  Below, I’ve provided some code that improves upon this, giving a bit more flexibility than the previous example:

using System.Collections;
using System.Collections.Generic;
using System.Xml;
using Microsoft.RuleEngine;

public static void CallRules(string policyName, int? majorRevision, int? minorRevision,
  Dictionary <string, object> breFacts)
{
  Policy policy = null;
  if (majorRevision.HasValue && minorRevision.HasValue)
    policy = new Policy(policyName, (int)majorRevision, (int)minorRevision);
  else
    policy = new Policy(policyName);

  ArrayList breList = new ArrayList();
  foreach (string key in breFacts.Keys)
  {
    if (breFacts[key].GetType() == typeof(XmlDocument))
      breList.Add(new TypedXmlDocument(key, (XmlDocument)breFacts[key]));
    else
      breList.Add(breFacts[key]);
  }

  policy.Execute(breList.ToArray());
}

Note that instead of passing in an array for BRE facts, I’ve chosen to use a generic Dictionary object that uses a string as its key and allows you to pass in anything of type object. For most facts, we’ll be able to ignore the key, but in the case of an XmlDocument, we’ll need to be able to let the BRE know what the DocumentType is and we use the key for that. The reason for this is that the BRE will require a correctly identified TypedXmlDocument for XPath statements to work.

We pull items out of the Dictionary into an ArrayList so that we may then easily convert it to a simple array for consumption in the Business Rules Engine. After calling the function, all objects added to the Dictionary should be in their altered states if the BRE changed them.

Advertisements

About Ed Jones

Ed is a Connected Systems and .NET Specialist for RBA in the Twin Cities. Contact Ed

What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: