Wednesday, August 06, 2008

BizTalk Business Rules Engine Handy functions in .NET

Working with the BizTalk 2006 R2 BRE api, I find myself going back to this code to start of as a base. These 2 functions get ALL of the versions of the policies or vocabularies.  If you want to work with only the latest, or the published ones, change the RulesStore.Filter enum to whatever your needs.

List of Policies

   1: public void GetPoliciesList()
   2: {
   3:     Microsoft.BizTalk.RuleEngineExtensions.RuleSetDeploymentDriver breDriver =
   4:         new Microsoft.BizTalk.RuleEngineExtensions.RuleSetDeploymentDriver();
   5:     Microsoft.RuleEngine.RuleStore breStore = breDriver.GetRuleStore();
   6:  
   7:     Microsoft.RuleEngine.RuleSetInfoCollection colPolInfo = null;
   8:     colPolInfo = breStore.GetRuleSets(RuleStore.Filter.All);
   9:     foreach (RuleSetInfo pInfo in colPolInfo)
  10:     {
  11:         Trace.WriteLine(string.Format("bts- Info = [{0}].v.{1}.{2}",
  12:             pInfo.Name, pInfo.MajorRevision, pInfo.MinorRevision));
  13:  
  14:         //get the policies to extract rules
  15:         Microsoft.RuleEngine.RuleSet pol = breStore.GetRuleSet(pInfo);
  16:         Trace.WriteLine(string.Format("bts- Count = [{0}]", pol.Rules.Count));
  17:     }
  18: }

This is how to get a list of all policies published on the BRE database.  Once you get all of the RuleSets, you can loop through each of them to retrieve the actual rules behind them.  Before getting to the rules, you need to get a RuleSet out of the RuleSetInfo.


List of Vocabularies



   1: public void GetVocabulariesList()
   2: {
   3:     Microsoft.BizTalk.RuleEngineExtensions.RuleSetDeploymentDriver breDriver = 
   4:         new Microsoft.BizTalk.RuleEngineExtensions.RuleSetDeploymentDriver();
   5:     Microsoft.RuleEngine.SqlRuleStore sqlRuleStore = (SqlRuleStore)breDriver.GetRuleStore();
   6:  
   7:     Microsoft.RuleEngine.VocabularyInfoCollection colVocInfo = null;
   8:     colVocInfo = sqlRuleStore.GetVocabularies(RuleStore.Filter.All);
   9:     foreach (VocabularyInfo vInfo in colVocInfo)
  10:     {
  11:         Trace.WriteLine(string.Format("bts- vInfo = [{0}].v.{1}.{2}",
  12:             vInfo.Name, vInfo.MajorRevision, vInfo.MinorRevision));
  13:  
  14:         //get the vocabulary to extract collection of definitions
  15:         Microsoft.RuleEngine.Vocabulary voc = sqlRuleStore.GetVocabulary(vInfo);
  16:         Trace.WriteLine(string.Format("bts- Count = [{0}]", voc.Definitions.Count));
  17:     }
  18: }

This is how to get a list of all vocabularies published on the BRE database.  If you need to see how to get to all of the definitions on a particular vocabulary, see my previous post: How to access BRE Vocabularies from .NET.


Noticed that there are some subtle differences in how you retrieve each piece of information.  For Policies you get a RuleStore while to get the Vocabularies you need to get a SQLRuleStore.


As usual, feedback is always welcome if you use this code.