Biztalk rules engine example


  • Validating Incoming Data Using the BizTalk Business Rules Engine
  • MS BRE Demystified – Part 1 Forward Chaining for optional XML Facts
  • BizTalk Business Rule Engine
  • Migrating Business Rules Policies from BizTalk Server 2006/2006 R2 to BizTalk Server 2013 R2
  • Validating Incoming Data Using the BizTalk Business Rules Engine

    In a business organization, if you defined and implemented a business process it will not change over a period of time, but contrary to this, business rules can change frequently. In nut shell we need to separate configurable business rules from business process to make changes frequently. BizTalk provides Business Rule engine to define business rules and make changes in the logic at run time.

    Conditions can be either true or false. Multiple conditions can be combined to provide for complex computations. Policy: Policies are logical set of rules. You can have multiple versions of a single policy. Each policy can be executed or tested as single unit. You compose a version of a policy, publish it, and deploy it. You do not have to recompile or modify Orchestrations, or other business processes that are using specific business policies.

    A deployed policy can be executed from an Orchestration as part of decision making in business process. Once it is published, a Business Rule is immutable and can be altered only by generating a new version. Facts and Vocabularies: As the first step to the creation of business rules, you should identify the different data involved in the execution of a particular rule. Facts are the data that acts in the creation of conditions in rules. Vocabularies are user-defined names for the facts used in rule conditions and actions.

    Vocabulary definitions make the rules easier to read, understand. BizTalkRuleEngineDb has tables and stored procedures to store and retrieve policies Rule Engine DLL: This is a run time dll that is implemented to the rule engine and executes the rules. Business Rule Engine Execution Stages Facts are asserted: In the first stage rule engine is initialized by set of data that are facts. Evaluate Rules: Once Facts are asserted, the rule engine will start to evaluate each rule.

    Adding Action to Agenda: If rules are evaluated to true then an action is added to the list. This list of action is known as Agenda. Execute Agenda Actions: once adding to the Agenda is over, the actions will be sorted and executed based on the priority Priorities are set at rules Facts are retracted.

    MS BRE Demystified – Part 1 Forward Chaining for optional XML Facts

    The Setup First, I had to get my environment set up. In this scenario, the content is actually metadata about a physical file that arrives into the company. Next, I have a really simple database table that can be used for lookups.

    Next I need a. First, I need to be able to concatenate two strings. Deal with it. After adding references to the Microsoft. Xml, I wrote the following quick-and-dirty code. CreateElement "Error" ; newError. Before building my rules in the BRE, I wanted to establish a friendly vocabulary for my XML nodes, database columns and helper functions. Setting Default Values The first requirement was to be able to set default values on nodes.

    I just check for either an empty string or null value, and set the default value. Doing Lookups The next requirement was to take values from the inbound message, perform a lookup and fill in an empty node from the message. In the rule below, I say that if the value in the XML message equals a value from the database, and another value also equals a value from the database, then set the XML node Investigator Name equal to another column in the database.

    What this generates behind the scenes when you execute the rule is this: So the proper T-SQL is built and executed to grab my lookup value.

    I want this rule to fire at all times, so my condition is set to something that will always be true unless math itself starts failing. I take a static file path location and then append the file name of the document coming into my company and turn that whole thing into an absolute path to the file. Catching and Recording Business Exceptions The final requirement was to detect business validation errors and record them.

    So, we can use my custom built function from earlier to create rules that look for business errors and add nodes to the message itself which outline the error details. The Result So what happens when I pass in a document that flexes all these conditions?

    I start off with an instance document that only has a few values. NET class. When the rule policy is finished executing, my input XML is changed to this: You can see that all the rules we built earlier got applied successfully.

    So yes, you can use the BRE to do some pretty easy-to-maintain business validation. Share this:.

    In this case ideally, as the Rule 2 sets the value of A to 10, Rule 1 should be fired. This is forward chaining at a very level. In case of MS BRE, forward chaining can achieved using the update and assert function provided with the engine. As the name suggest, update is used to update the value of the existing data facts that is already loaded in the working memory of the rule engine.

    Assert is used to load new data into the working memory of the rule engine. At the same time, using update function on A, the value of A is updated to 10 in the working memory of the rule engine.

    This update triggers the rule engine to reevaluate all the rules which had conditions checking values of A in this case Rule 1. Consider a simple XSD as shown below. The BRE does it with following intentions, when the XML facts are asserted into the working memory, BRE will assert all the facts that it finds under Req to the rules defined in our system.

    BizTalk Business Rule Engine

    I am just scratching the surface here. Forward Chaining Now let us put our new found knowledge to test by defining the rule System we discussed while understanding the Forward chaining above. The rules are shown as below. I am going to assert following XML message to the rule engine to test the rule system that we created. Forward Chaining works!! Following images shows how the Rule 1 reads the field A from the request It is expecting the field A which is read using XPath field be present in the XML message always.

    Hence when the filed is missing from the input, BRE engine threw an error. As the name suggests, the exist predicate checks the existence of the xpath in the document that was asserted to the BRE. Let us drag the xpath of the field A from Facts Explorer and into our rule. Following are our Rule 1 and Rule 2 respectively Let us again feed following message to the rule engine.

    Why did the exist predicate not work with the xpath that we dragged and dropped from the schema?? If we examine the condition in the Rule 1 shared above, we see that the Xpath we are checking in the exist predicate is But the XPath with which we are reading and equating the value of field A to 20 is It is clear now that both of the XPaths are different and hence will be handled as two different nodes in the tree structure that the Rule engine will create.

    Now as we understand this, what we need to do is modify the Xpath in the exist predicate. The modified Rules look as follows. Now each of the condition in the Rule is pointing to the same Xpath. Hence, when the rule engine transforms these rules into the tree structure, it will create only one node for the Field A and one node For Field B. If the MetaInstructions that have been instantiated by the InstructionLoaderPolicy expose methods that return values rather than void then you can include those in the conditions section as well.

    ContextInstructions vocabulary which allows one to read any of the context properties specified in the BTS. NET namespace, choosing whether to return blank or thrown an exception if the property is not found.

    In the actions section of the rule you can specify what actions you want to carry out, making use of methods exposed by the instantiated MetaInstructions which return void. ContextInstructions vocabulary which allows for any context property to be set as long as its name and namespace are known. An ExecutionPolicy policy can be made up of multiple rules, each one with different sets of conditions and actions.

    An ExecutionPolicy might be shared between multiple pipelines, only firing rules for each pipeline di ko logo ne choda on the ApplicationContext that they pass. SampleInstructions is added to the GAC. ContextInstructions vocabulary to the local Business Rules Engine database and definitions contained within can then be used within policies.

    This vocabulary contains definitions that are used to manipulate the context of the message being processed. Below is an explanation of each vocabulary definition. These definition allow you to choose the relevant context property within the contained property schema namespace from a drop down box containing a list of all the supported context properties, thus removing the need for the implementer to know the names of the context property names and namespaces, and also removing the chance of typos.

    If the context property is not found on the message you can specify if the return value should be blank or the default for whatever. NET type the property corresponds to or if an exception should be thrown.

    GetCustomContextProperty - This definition allows you to access any context properties on the message being processed by the pipeline by specifying the property name and namespace of the property.

    If the context property is not found you can specify if the return value should be blank or the default for whatever. RemoveCustomContextProperty - This definition allows you to remove any supported context property some out of the box context properties can't be removed from the message being processed by the pipeline by specifying the name and namespace of the property to be removed. These definitions allow you to set any value remember that this value doesn't have to be static, you can use vocabularies to dynamically set the value as well to the relevant context properties.

    You can also choose whether you want to promote the context property or just write the value to it. SetCustomContextProperty - This definition allows you to set any value to any context property, the property being specified with the property name and namespace and by choosing the. NET type of the property with a drop down.

    SetCustomContextPropertyFromSSO - This definition allows you to set a value that is read in from a specified SSO key in a specified SSO application to any context property, the property being specified with the property name and namespace and by choosing the. Using this definition against a message multiple times will still only result in the XML document being loaded into memory once. HelperInstructions vocabulary to the local Business Rules Engine database and definitions contained within can then be used within policies.

    This vocabulary contains definitions that are best described as helper methods that can be used within conditions and actions.

    Migrating Business Rules Policies from BizTalk Server 2006/2006 R2 to BizTalk Server 2013 R2

    GetCurrentDateTime - This definition gets the current date and time, allowing you to specify the format it should be fetched in. GetFileExtension - This definition returns the file extension for a specified filename.

    GetLowercaseString - This definition converts the string parameter to lowercase. GetStringLength - This definition returns the length of a string.

    GetUppercaseString - This definition converts the string parameter to uppercase. It allows you to specify whether an exception should be thrown if the expression doesn't evaluate. ReplaceSubstring - This definition allows you to replace a substring in a given string with another string. RoundCurrentTime - This definition allows you to round the current time up to a given interval, also allowing you to specify the format the time should be returned in.

    This can be quite handy for non-sequential convoy type batching scenarios. StringContains - This definition checks whether a string contains the specified substring. ValidateStringLength - This definition throws an exception if the length of a string does not exactly match a specified value.

    Since this defintion returns a void it is really only intended for usage in the Action section of a rule. Writing your own custom Instruction Create a new.


    thoughts on “Biztalk rules engine example

    1. I am sorry, that has interfered... At me a similar situation. Let's discuss. Write here or in PM.

    Leave a Reply

    Your email address will not be published. Required fields are marked *