Udostępnij za pośrednictwem


Long Term Facts / Fact Retriever in BizTalk Rule Engine

BRE Facts

Business is dynamic. Policies, rules, constraints are never stable and they amend/evolve with time. Processes, applications and workflows supporting any business model need to implement these changes with minimal maintenance and adoption hazards.

BizTalk Business Rule Engine (BRE) is a magnificent tool that scales out capabilities of policy/rule based applications and workflows. BRE became part of BizTalk from 2004 onwards. In BRE, business people can define “Rules”. Rules can be clubbed under “Policies”. “Facts” or say business information are passed through these policies to obtain desired business results.

Facts have two categories – Short Term Facts and Long Term Facts. Consider a loaning process of any Bank. We can think a “loan application” as short term fact. Short team facts are business information which changes per occurrence. On the contrary, “interest rates” do not change very regularly. This business information is steady and we can consider it a long term fact.

In BRE based applications/workflows, short term facts are supplied very frequently (generally with each process execution) while long term facts are supplied first time and then only when it changes. After first process execution cycle, long term facts are stored in rule engine memory and are reused over multiple execution cycles.

Long term facts are implemented using “Fact Retriever”. Fact Retriever is an entity which provisions long term facts to business policies. Only one fact retriever can be defined per published business policy.

How to Develop Fact Retriever

Fact Retriever are developed as “Class Library” and it implements Microsoft.RuleEngine.IFactRetriever” interface. It is required to code “UpdateFacts” method which loads long term facts and passes to business rule engine.

Following are steps to create fact retriever –

  1. Sample applicaton supplies a XML document as fact.

  2. Create a class library project say “SampleFactRetriverApplication”.

  3. Add a class “SampleFactRetriever”.

  4. Add following code –

using System;

using Microsoft.RuleEngine;

using System.Collections.Generic;

using System.Text;

using System.Xml;

namespace SampleFactRetriverApplication

{

    public class SampleFactRetriever : IFactRetriever

    {

        public object UpdateFacts(RuleSetInfo rulesetInfo, Microsoft.RuleEngine.RuleEngine engine, object factsHandleIn)

        {

            object factsHandleOut;

            if (factsHandleIn == null)

            {

                //create an instance of the XML object

                XmlDocument document = new XmlDocument();

                //load the document (fact)

                document.Load(@"E:\LongTermFact\TestFact1Sample.xml");

                //create and instantiate an instance of typed XML document

                TypedXmlDocument doc = new TypedXmlDocument("BTSFactRetriver.TestFact1", document);

               //pass document to rule engine

                engine.Assert(doc);

                factsHandleOut = doc;

            }

            else

                factsHandleOut = factsHandleIn;

            return factsHandleOut;

        }

  }

}

    In implementation, first XML file is loaded. Then it is converted to typed xml document. Typed xml document makes sure that schema of fact XML document is same as fact schema defined in policy. "BTSFactRetriver.TestFact1" is name space of schema. Finally typed XML document is passed to rule engine.

  1. Sign assembly and build.

  2. Register assembly to Global Assembly Cache (GAC).

  3. Fact Retriever is ready.

Configure Fact Retriever

Fact Retriever can be configured in two ways –

- In Business Rule Composer

- Programmatically using RuleSetExecutionConfiguration object.

I am going to cover composer part here.

  1. Open business rule composer.

  2. Select policy version in Policy Explorer.

  3. In Properties window, select Fact Retriever property.

  4. Click the ellipsis button in the Fact Retriever property to browse in the GAC for fact retriever object “SampleFactRetriverApplication.SampleFactRetriever”. Save.

  5. Configuration is over and you are ready to go.

Comments