Friday, September 07, 2007

How to access BRE Vocabularies from .NET

Let's assume that you need to access a Biztalk Business rules vocabulary definitions from a .NET assembly. You need to understand how the BRE API is setup to handle this.

Let's create a vocabulary with a definition constant of type string:



Now create the definition with the type of string as follows:



Exporting this vocabulary using the Business Rules Engine Deployment Wizard, the xml will look like this: [notice all 6 fields in green...]

1
<?xml version="1.0" encoding="utf-8"?>
    2 <brl xmlns="http://schemas.microsoft.com/businessruleslanguage/2002">
    3   <vocabulary id="1d3ad67e-1ed3-46ed-88a7-23199eef8fcd" name="Sample_ValidNames" uri="" description="">
    4     <version major="1" minor="0" description="" modifiedby="LocalDC\awing" date="2007-09-07T21:55:45.545-04:00" />
    5     <vocabularydefinition id="cdff8b38-e565-4942-b76f-72161449a457" name="Definition1_DefinitionName" description="Definition1_DefinitionDescription">
    6       <literaldefinition type="string">
    7         <string>Definition1_DefinitionValue</string>
    8       </literaldefinition>
    9       <formatstring language="en-US" string="Definition1_DefinitionDisplayName" />
   10     </vocabularydefinition>
   11   </vocabulary>
   12 </brl>

To access the different part of this vocabulary, I use this C# code to access all of the parts for this vocabulary definition.

Add the namespaces:

    1 using System.Diagnostics;
    2 using Microsoft.RuleEngine;

then copy the following code:

    1         public void GetAllDefinitionsFromVocabulary(string vocabularyName)
    2         {
    3             RuleStore breStore;
    4             RuleSetDeploymentDriver breDriver = new RuleSetDeploymentDriver();
    5 
    6             breStore = breDriver.GetRuleStore();
    7             Trace.WriteLine(string.Format("bts- ## Vocabulary:[{0}] ##", vocabularyName));
    8             Trace.WriteLine("bts-");
    9 
   10             VocabularyInfoCollection breVocInfoCol;
   11             breVocInfoCol = breStore.GetVocabularies(vocabularyName, RuleStore.Filter.All);
   12             foreach (VocabularyInfo oVoc in breVocInfoCol)
   13             {
   14                 Vocabulary _vocab = breStore.GetVocabulary(oVoc);                
   15 
   16                 foreach (VocabularyDefinition vocDefn in _vocab.Definitions)
   17                 {
   18                     ///string x = o["en-US"].Format;
   19                     Trace.WriteLine(string.Format("bts- Definition.Id: [{0}]", vocDefn.Id));
   20                     Trace.WriteLine(string.Format("bts- Definition.Name: [{0}]", vocDefn.Name));
   21                     Trace.WriteLine(string.Format("bts- Definition.Description: [{0}]", vocDefn.Description));
   22 
   23                     LiteralDefinition literalDefinition = vocDefn as LiteralDefinition;
   24 
   25                     //literaldefinition
   26                     if (literalDefinition != null)
   27                     {
   28                         if (literalDefinition.Value != null)
   29                         {
   30                             Trace.WriteLine(string.Format("bts- literal: [{0}]", literalDefinition.Value.ToString()));
   31                         }
   32                     }
   33 
   34                     //formatstring definition
   35                     foreach (FormatString frm in vocDefn.FormatStrings)
   36                     {
   37                         Trace.WriteLine(string.Format("bts- frm.Language: [{0}]", frm.Language));
   38                         Trace.WriteLine(string.Format("bts- frm.Format: [{0}]", frm.Format));
   39                     }
   40 
   41                     Trace.WriteLine("bts- ----------------------------------");
   42                 }
   43             }

So if I call my method with the vocabulary name = Sample_ValidNames. Then in DebugView you can see that we get all 6 fields that we set on the wizard and on the XML exported file.

    1 [5844] bts- ## Vocabulary:[Sample_ValidNames] ## 
    2 [5844] bts- 
    3 [5844] bts- Definition.Id: [cdff8b38-e565-4942-b76f-72161449a457] 
    4 [5844] bts- Definition.Name: [Definition1_DefinitionName] 
    5 [5844] bts- Definition.Description: [Definition1_DefinitionDescription] 
    6 [5844] bts- literal: [Definition1_DefinitionValue] 
    7 [5844] bts- frm.Language: [en-US] 
    8 [5844] bts- frm.Format: [Definition1_DefinitionDisplayName] 
    9 [5844] bts- ---------------------------------- 
   10 [5844] bts- Done 

There, hope this helps someone. [dont forget to post your comment if this code helps you out.. ;)

2 comments:

Du said...

Hello,

Found your post to be very helpful. A question I have, as a novice BRE user, is how do I specify which BRE I am using? How do I specify the server name and credentials to access the BRE of which I have just deployed my Policy?

I am attempting this with C# and VS2005.

Thanks!

Arnulfo Wing said...

thanks for the kind words. It is my understanding that the BRE api takes on the configured connection. So you will need to get the BRE composer working first, and then use the API.

You might want to try to use the Microsoft.RuleEngine.RuleStore. There is a CREDENTIALS class in there that might be what you need (I have not try that myself).

If you want to find out which BRE you are connected to, open the BRE composer and select LOAD from the Rule Store Menu. That will give you the server and the Credentials being used.

Let me know if this helps.

thanks,

A>Wing