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:
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);
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]));
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.