Well, it's pretty common knowledge by now that BizTalk Server (from version 2004 onwards) comes with a
Business Rules Engine (BRE), but do you really know just how powerful it is? The BRE provides a mechanism to implement complex business logic in a highly versatile and optimised manner, allowing the business to update the logic without forcing redevelopment or a recompile of the solution.
So what does that mean? Let's start with a really simple case. Suppose your company takes orders from its customers and awards a 5% discount if the order total exceeds a certain amount, say $5000. You have a BizTalk solution that tracks the incoming order and manages the business processes surrounding fulfillment and billing. Now... as a developer, where would you put the discount logic? Well, you have several options; here are just a few:
- Put the discount logic in an expression shape within the orchestration
- Put the discount logic in a satellite helper (.NET) assembly called by the orchestration
- Put the discount logic in the Business Rules Engine and call it from the orchestration using a "Call Rules" shape.
Let's say that you're a "consultant"
(by Mick's definition) and you've gone with one of the first two options. Now suppose that your CEO has decided to run a special next month to promote more business, and wants to expand the existing discount to 10%, as well as offer a further discount (%15) for orders over $10,000. You think this is good news for you on the job security front because you're skills are required to re-code, recompile and test the logic. But then you observe your boss's increasingly unimpressed demeanor as you explain the cost and effort to implement the change, not too mention scheduling a full release
(with the associated system down-time... translation: loss of $$) to deploy the change. And of course you get to do the whole thing again when the temporary offer expires and you need to restore the original logic. Looking at his face now, you suddenly decide it's a good time to update your CV ...
OR... if you were smart enough to go with the third option, you can explain to your boss that he can have either you, his business analyst, or even an IT-saavy information worker update the rules using the
Business Rules Composer that came with BizTalk. The rules can be tested independently inside the Composer, and then published to any environment. And here's the best thing: rules are effective immediately upon deployment, without any recompiling required! (in many cases, you won't even have to stop your BizTalk application for the rules to be deployed and take effect) In other words, flexibility to alter the discount logic in real-time with no interruption of service. Now your boss is smiling and you just might get that Christmas bonus after all.
While this example is a pretty simple one, think about the complex set of rules that support a decision about awarding credit, or for setting a premium on an insurance policy. The BRE is incredibly adept at representing elaborate decision making processes that are typically too complex or cumbersome to model in traditional code. Combine this with the versatility demonstrated above, and you begin to appreciate the immense value that the BRE adds to your enterprise solution.
So how does the BRE work? Rules are constructed based on
Facts and
Vocabularies. A fact is a specific bit of information based on a data item, for example "Quantity" in the scenario above. Now if you were to reference this data item from an XML message using XPath, it might look something like this:
/*[local-name()='PurchaseOrder' and namespace uri()='http://schemas.ACME.com/2007/Purchasing']/*[local-name()='Item' and namespace uri()='']/*[local-name()='Quantity' and namespace-uri ()='']
Eyes starting to cross? Vision going blurry? Don't worry - you're not alone. Imagine confronting your business analyst or information worker with that kind of verbose syntax! However, the BRE allows you to define a
custom vocabulary to provide friendly names for facts like these. Add a definition for the XPath expression above and call it "Quantity", then watch that glazed expression slowly disappear from your BA's face. :-) Moreover, you can express the rules using semantics that even your CEO can understand - which means he can verify your work if he wants to.
Now that you have your facts and vocabulary defined, you can begin writing your rules, which are simply based on conditions and associated actions. So for the example above, the rules might look something like this:
IF Quantity > 10000
DiscountApplied = 0.15
IF Quantity > 5000
DiscountApplied = 0.1
IF Quantity <= 5000
DiscountApplied = 0.0
Unlike normal procedural logic like you'd write in C# code, there is no concept of an "ELSE" or "ELSE IF" branch in the BRE; a rule either fires or it doesn't. So how do you control which rule fires above if the Quantity were greater than 10,000 (since both of the first two conditions are met)? Well, actually both rules do evaluate, but you can set a priority for each rule to govern the order they are evaluated. As long as the first rule above is evaluated last, the result will be correct.
Rules can be grouped together in
Policies which are then saved, published and deployed. Once published, a policy can't be modified, but you can copy it into a new version, update it and deploy that. BizTalk always uses the latest deployed version of any rules policy. So when the special offer discount period ends, just undeploy the new version and you've instantly rolled back to the regular policy!
"Gee, the BRE is really something! But can I only use it from within BizTalk?" The million-dollar answer is... NO! The BRE is written entirely in .NET, and presents APIs that allow you to call it from any .NET class. So you can expose your rules with a Web Service (or in WCF) and access them anywhere!
"Can I get the BRE separately from BizTalk?"Mick says that the cheapest way to get the BRE is through purchasing
BizTalk RFID Services, which bundles in the BRE for free.
Useful Links:
BRE Blog by Sreedhar Pelluru, Senior Programmer Writer (Microsoft)Overview of BRE Integration with BizTalk RFIDUsing the BRE Outside of BizTalkSo... get acquainted with the BRE and you too can start making the rules!