Introduction

In BRE we will create following rules to implement Forward Chaining:


RULE 1
RULE 2
RULE 3
IF
if Obj.ValueA > 10 && Obj.ValueB > 20
IF Obj.ValueC == 100
IF Obj.ValueC != 100
THEN
Obj.ValueC = 100
UPDATE Obj
Obj.Approved = True
Obj.Approved = False

As you can see Rule 2 and Rule 3 depends on execution of Rule1. Once Rule1 is executed fact ValueC will be updated and this will cause other rules to execute.

RULE 1

RULE 2 

FactCreator

In BRE, to test the rule in Business Rule Composer we can use .NET fact creator. It is easy to create fact creator, we just need to implement IFactCreator Interface.

Sample code:

public class ForwardChainingCreator : IFactCreator
{
    #region IFactCreator Members
    public object[] CreateFacts(RuleSetInfo ruleSetInfo)
    {
        object[] objfact = new object[1];
        BREForwardChaining.BREForwardChainingClass objperson = new BREForwardChaining.BREForwardChainingClass();
        objperson.ValueA = 50;
        objperson.ValueB = 50;
        objfact[0] = objperson;
        return objfact;
    }
    public Type[] GetFactTypes(RuleSetInfo ruleSetInfo)
    {
        return null;
    }
    #endregion
}

BRE Rule can be tested only when it is deployed but using Fact creator we can test the rule in Rule composer window as the development goes on.

Using UPDATE OR ASSERT

After execution of RULE 1 fact is updated in memory and rules associated with that fact are evaluated and executed. Update function in this scenario (Assert function also can cause such issues) will cause Rule1 to run endlessly. If rule goes in endless loop normally rules execution engine will throw following error – “Exception of type 'System.OutOfMemoryException' was thrown.” This is something important which needs to consider during developing Forward Chaining.

This can be avoided by using Retract function in Rule2 or Rule 3. Retract removes the Fact from memory and that stops the execution of rules. Also another way is that we can rewrite Rule 1 following way. You can see their is new condition added for ValueC and this will not allow rule to move in endless loop.

Calling Rules from .NET Application

Following code shows how rules can be called from .NET application. The following sample was written in console application:

BREForwardChaining.BREForwardChainingClass objForwardChainingSample = new BREForwardChaining.BREForwardChainingClass();
objForwardChainingSample.ValueA = 50;
objForwardChainingSample.ValueB = 50;
 
//Just providing Policy Name will take the latest deployed policy
Policy policy = new Policy("BREForwardChaining");
//Rule is executed
policy.Execute(objForwardChainingSample);
 
//Following are the values returned after rule execution
Console.WriteLine(objForwardChainingSample.ValueC.ToString());
Console.WriteLine(objForwardChainingSample.Approved.ToString());
Console.ReadLine();

See Also

Another important place to find an extensive amount of BizTalk related articles is the TechNet Wiki itself. The best entry point is BizTalk Server Resources on the TechNet Wiki.