Share via


Business Rule Engine - Implementing BRE Response With Details for Each Failed Rule

BackGround

I intend to write this article to provide the new users with basic background of using various artifacts withing BRE. Also i have covered few pointers that may be handywhile developing BRE. A small sample is covered in this article for a business scenario where we need to perform multiple rule validation and would require to get all thefailure scenarios in the response message.


Basic Concepts

The Business Rules Framework is a Microsoft .NET-compliant class library. It provides an efficient inference engine that can link highly readable, declarative, semanticallyrich rules to any business objects (.NET components), XML documents, or database tables. Application developers can build business rules by constructing rules fromsmall building blocks of business logic (small rule sets) that operate on information (facts) contained in .NET objects, database tables, and XML documents. This designpattern promotes code reuse, design simplicity, and modularity of business logic. In addition, the rule engine does not impose on the architecture or design of businessapplications. In fact, you can add rule technology to a business application by directly invoking the rule engine, or you can have external logic that invokes your businessobjects without modifying them. In short, the technology enables developers to create and maintain applications with minimal effort.

Creating a Vocabulary

Vocabulary is a collection of definitions for the facts used in rule conditions and actions.

  1. Using .net classes
  • .Net Helper classes must be static and serializable.
  • Non-static classes are treated just like any other fact so instance must be provided at execution time otherwise any rules or actions won’t fire because there is nofact to evaluate it.
  • Class must be deployed in GAC to be used in BRE.

       2.  Using Database

  • We can either get or set the value based on the condition from the database.

       3.  Using XML Schemas

  • Make sure to make the Element Form Default Property of Schema to Qualified
  • Document Type property is mandatory for any schema to be defined in vocabulary so that it can be used as parameter in call rules shape in Orchestration.

It’s generally ProjectName.XSDName.

  • If you want to make the entire schema as a definition then select the Root Name of the schema while selecting the node during vocabulary creation and selectthe element if you want to read the specific node from the schema.

Creating a Policy

  • Policy is a logical grouping of rules.
  • Policy once deployed should be added to the application manually to make it work.
  • We can copy the policy to create new version and make updates on the fly.

Creating a Rule

  • If the condition evaluates to True Then the corresponding actions are executed.
  • Facts are the data upon which Rules operate.
  • Update function causes the Fact to be re-asserted and all rules to be evaluated again.

Testing a policy

  • Check for condition evaluated and Rules fired status while testing BRE, If both are present then the rule will be fired from the Orchestration when called.
  • You can use the DebugTrackingInterceptor class to track the policy execution details present in the Microsoft.RuleEngine.dll. The following samplecode shows how to create an instance of the DebugTrackingInterceptor class, and use it when executing the policy.
DebugTrackingInterceptor dti = new DebugTrackingInterceptor("c:\\Trace.txt");
policy.Execute(facts, dti);

Policy Execution Strategy in BRE

The rule engine uses the three-stage Condition Evaluation-Conflict Resolution-Action Execution algorithm to execute policies. In the Condition Evaluation stage, the rule engine evaluates the conditions in all the rules in the policy and adds the rules whose conditions evaluate to true to the agenda. In the second stage, the Conflict Resolution Criteria stage, the rules whose conditions evaluate to true are added to the agenda in order based on their priority. 

Sample Step-By-Step Creation of Business Rule

Scenario:  We need to have a generic response from all the rules fired and the response must contain all the error scenarios in its response.

Creating Schema

Create a Sample schema that you can use for your requirement:-

<? Xml version="1.0" encoding="utf-16"?>

<xs:schema xmlns="http://SampleBRE.ResponseSchema" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" elementFormDefault="qualified" targetNamespace="http://SampleBRE.ResponseSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="Response">

    <xs:complexType>

      <xs:sequence>

        <xs:element name="Status" type="xs:string" />

        <xs:element minOccurs="0" maxOccurs="1" name="Errors">

          <xs:complexType>

            <xs:sequence>

              <xs:element minOccurs="0" maxOccurs="unbounded" name="Error">

                <xs:complexType>

                  <xs:sequence>

                    <xs:element name="Number" type="xs:string" />

                    <xs:element name="Message" type="xs:string" />

                    <xs:element name="Content" type="xs:string" />

                    <xs:element name="Value" type="xs:string" />

                  </xs:sequence>

                </xs:complexType>

              </xs:element>

            </xs:sequence>

          </xs:complexType>

        </xs:element>

      </xs:sequence>

    </xs:complexType>

  </xs:element>

</xs:schema>

Note that I have made ElementFormDefault to Qualified. This results in all the elements having a target namespace and will make using BRE tool easier. You can set it through Schema properties Pane.

                                      

Now we need to create vocabularies that we would need to evaluate the incoming data and also to set the values in action section.

Now to evaluate the rule we can use the vocabularies as per our need and we can construct the response action schema as below :-

Pointers

  • XmlHelper class is present in Microsoft.RuleEngine.dll.
  • ./*[local-name()='Error' and position()=last()] :- selects the last para child of the context node
  • If we have multiples rules then error from each rule will be appended in the output schema

To call the BRE rule passing an xml document, we need to first construct the xml message :

Then we can use a call rule shape to pass this message as parameter

mSg_Rcv is the message which we want to validate against the rules
mSg_BreResp is the error Response we want to receive for all the failure scenarios.

Conclusion

In this way, we can create a BRE rule and also get a response for all the failure rules in one response.

References

http://msdn.microsoft.com/en-us/library/ms947044.aspx
http://www.codeproject.com/Articles/33143/Developing-with-the-Microsoft-Business-Rule-Engine
http://msdn.microsoft.com/en-us/library/aa561216.aspx
http://msdn.microsoft.com/en-us/library/microsoft.ruleengine.xmlhelper.aspx
http://msdn.microsoft.com/en-us/library/aa995545.aspx

See Also

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