Here are some of the changes to BRE in BizTalk 2006.
Summary:
-
Support for generic types and generic methods
-
Support for nullable types 2
-
Accessing nested members of a class 4
-
Type Casting support 5
-
Invoking Static Members of a Class 6
-
Overriding registry key setting with application configuration file.
-
GetDeploymentDriver method added to Configuration class
-
Clear method added to the Policy class
-
IFactRemover interface added.
-
SqlTimeOut registry key added.
-
Arithmetic and logical operators support double promotion.
Attachment: Word document with the following information
The rule engine supports using specialized generic types and specialized generic methods in a rule. It does not support using generic types and generic methods themselves in a rule. For example, in a business rule you can use List<int>, but not List<T> (from the System.Collections.Generic namespace in the .NET class library). Currently, the Business Rule Composer tool does not support creating rules by using specialized generic types and specialized generic methods. You must create the rules programmatically by using the rule engine object model. The following sample code demonstrates how to use the List generic class in a business rule:
// Create the condition list IF 1 == 1
Equal eq = new Equal(new Constant(1), new Constant(1));
// Create the action list
ActionCollection ac = new ActionCollection();
//Create class binding and class member bindings
ClassBinding lstClass = new ClassBinding(typeof(System.Collections.Generic.List<int>));
ArgumentCollection argc = new ArgumentCollection();
argc.Add(new Constant(3));
ClassMemberBinding add = new ClassMemberBinding("Add", lstClass, argc);
// Wrapping the .NET binding as a user function
UserFunction uf = new UserFunction(add);
ac.Add(uf);
// Create the rule
Rule rl = new Rule("AddToList", eq, ac);
// Create the policy
RuleSet rs = new RuleSet("GenericTest");
rs.Rules.Add(rl);
// Create the .NET List object
List<int> lst = new List<int>();
lst.Add(1);
lst.Add(2);
// Print the list before executing the policy
Console.WriteLine("Contents of the lists before executing the policy");
foreach (int i in lst)
Console.WriteLine(i);
PolicyTester pt = new PolicyTester(rs);
pt.Execute(lst);
// Print the list after executing the policy
Console.WriteLine("Contents of the lists before executing the policy");
foreach (int i in lst)
Console.WriteLine(i);
The rule engine supports using nullable types in a business rule. You can use nullable types in .NET class bindings, XML bindings, and database bindings. Currently, the Business Rule Composer tool does not support using nullable types in a business rule. You can use the nullable types when creating rules programmatically.
You can create a class member binding to a property or a field whose type is a nullable type. You can also create a class member binding to a method that takes a parameter of nullable type and/or returns a value of nullable type. The following sample code demonstrates how to access a nullable field, and how to access a return value of nullable type from a method in a business rule. If you execute a console application with the following code as it is, you will see that the value of the prop field is set to 5. If you do not initialize the prop field in the class or initialize it to null and run the code, you will see that the value of the prop field is set to 1.
using Microsoft.RuleEngine;
namespace UseNullableAsm
{
class Program
{
public class Class1
{
public int? prop = 1;
private int? prop2 = 4;
public int? GetProp2()
{
return prop2;
}
}
static void Main(string[] args)
{
//Create the class binding for the Class1 class
ClassBinding cbCls1 = new ClassBinding(typeof(Class1));
//Create the class member binding to the GetProp2 method of Cls1 class
ClassMemberBinding cmGetProp2 = new ClassMemberBinding("GetProp2", cbCls1);
//Create the class member binding to the to GET the value of prop
ClassMemberBinding cmGetProp = new ClassMemberBinding("prop", cbCls1);
//Create arguments for the prop1 field, which is prop1 + GetProp2()
UserFunction ufGetProp = new UserFunction(cmGetProp);
Add addArg = new Add(ufGetProp, new UserFunction(cmGetProp2));
ArgumentCollection al1 = new ArgumentCollection();
al1.Add(addArg);
//Set the value of prop to prop1 + cmGetPro2()
ClassMemberBinding cmProp = new ClassMemberBinding("prop", cbCls1, al1);
//Create a userfunction based on cmProp and add to the action collection
UserFunction ufProp = new UserFunction(cmProp);
ActionCollection ac = new ActionCollection();
ac.Add(ufProp);
//Create the condition list IF prop == 1
Equal eq = new Equal(ufGetProp, new Constant(1));
//Create the rule
// If (prop == 1)
// Then prop = prop + GetProp2()
Rule rl = new Rule("NullableTestRule", eq, ac);
//Create the condition list IF prop != 1
NotEqual neq = new NotEqual(ufGetProp, new Constant(1));
//Set the value of prop to prop to 1
Constant ct = new Constant(1);
ArgumentCollection al2 = new ArgumentCollection();
al2.Add(ct);
//Create class member binding to prop field with argument value 1
ClassMemberBinding cmSetProp = new ClassMemberBinding("prop", cbCls1, al2);
//Create a userfunction based on cmSetProp and add to the action collection
UserFunction ufSetProp = new UserFunction(cmSetProp);
ActionCollection ac2 = new ActionCollection();
ac2.Add(ufSetProp);
//Create the second rule
// If (prop != 1)
// Then prop = 1
Rule rl2 = new Rule("NullableTestRule2", neq, ac2);
//Create the policy and add both the rules to the policy
RuleSet rs = new RuleSet("NulableTestPolicy");
rs.Rules.Add(rl);
rs.Rules.Add(rl2);
//Create the .NET object fact
Class1 cls1Obj = new Class1();
//Print the value of the field prop before executing the policy
Console.WriteLine("Value of the prop field is " + cls1Obj.prop);
//Execute the policy
PolicyTester pt = new PolicyTester(rs);
pt.Execute(cls1Obj);
//Print the value of the field prop after executing the policy
Console.WriteLine("Value of the prop field is " + cls1Obj.prop);
}
}
}
You can also use nullable types in database bindings. The following sample code fragment shows you how to use a nullable type in database bindings.
DataColumnBinding dcBinding = new DataColumnBinding(“col”, typeof(int?), dbBinding);
Similarly, you can use nullable types in XML bindings. The following sample code fragment shows how to use a nullable type in XML bindings.
XMLDocumentFieldBinding xfb1 = new XMLDocumentFieldBinding(typeof(int?),"ID",xdb);
The rule engine allows you to use a nested property or method of an object in a rule. For example, suppose you have a class named AClass, which has a property named B of type BClass, which has a field named C. The rule engine allows you to build rules accessing the field C by using the A.B.C syntax. However, it is possible to use this syntax only when building the rules programmatically, not when using the Business Rule Composer tool. The following sample code demonstrates how to use a property of an object, which is a property of another object:
// Create the condition list IF 1 == 1
Equal eq = new Equal(new Constant(1), new Constant(1));
// Create the action collection
ActionCollection ac = new ActionCollection();
// Create class binding and class member binding to cField
// Set the value of cField to "Changed"
Constant chg = new Constant("Changed");
ArgumentCollection argCol = new ArgumentCollection();
argCol.Add(chg);
ClassBinding lstClass = new ClassBinding(typeof(AClass));
ClassMemberBinding bBinding = new ClassMemberBinding("bObj", lstClass);
ClassMemberBinding CBinding = new ClassMemberBinding("cField", bBinding,argCol);
UserFunction uf_C = new UserFunction(CBinding);
ac.Add(uf_C);
// Create the rule
Rule rl = new Rule("ChangeCField", eq, ac);
// Create the policy
RuleSet rs = new RuleSet("NestedNodeTest");
rs.Rules.Add(rl);
//Create the facts
AClass aObj = new AClass();
Console.WriteLine("The value of aObj.bObj.cField BEFORE executing the policy");
Console.WriteLine(aObj.bObj.cField);
//Execute the policy
PolicyTester tester = new PolicyTester(rs);
tester.Execute(aObj);
Console.WriteLine("The value of aObj.bObj.cField AFTER executing the policy");
Console.WriteLine(aObj.bObj.cField);
You can use the Cast method of the ClassMemberBinding class to convert an object of one type to an object of another compatible type. Currently, the Business Rule Composer tool does not support creating rules by using the Cast method. You must create the rules programmatically by using the rule engine object model to take advantage of this feature. The following sample code demonstrates how to use the Cast method to convert an instance of the System.Object class to an instance of the Cls2 class.
using Microsoft.RuleEngine;
namespace RuleTypeCasting
{
class Cls1