Thursday, February 5, 2015

BizTalk using Business Rule Engine Introduction

Title: BizTalk using Business Rule Engine Introduction:
Author: Toraj Khavari
Date: Feb. 2, 2015

Objective: The Microsoft BizTalk Rule Engine (BRE) is a powerful tool with very flexible technologies. The BizTalk Rule Composer is a GUI enabling the users to create, modify, and deploy business rules to production without BizTalk Application implementation. The technologies enable business with variety of functionalities. To name a couple, the BRE contains Value Pair information (can be used for example in, email list, configuration information), and BRE has flexible product discount logic (can be utilized for example for, time, value and product dependent). In this wiki article we will explore using BRE for a key value pair and use the information inside an application Orchestration.

Article Body:
For production support, I have business requirements to implement a solution to distribute emails when there are orchestrations errors with different priorities, and provide some recommendation in the email about who can fix it, and lay the foundation to a web site that the application and system administrators can post blogs, wikis, solutions, notes, etc. There are five key value pairs needed to meet the requirements. The value pairs were instantiated in BRE and deployed. The BizTalk administrators can modify them as needed without code deployment and developers involvements.

1-      Create a schema and capture all the nodes needed. In my case it is called Adec.VendorsEDI.BRESchemas.EmailInfo.


2-      In the BizTalk Rule Composer, construct the Vocabularies to use Adec.VendorsEDI.BRESchemas Schema for Source Fact. In my case they are type System.String.


3-      Create a Policy, build the Business Rule and deploy it.


4-      Create three Messages in a new orchestration. 1) Message Type matching the BRE Vocabulary Source Fact, in my case, it is Adec.VendorsEDI.BRESchemas.FlatFileSchema1. It is called ExceptionAssignVendorsEdiAdminInfoMsg. 2) Message_XMLIn and 3) Message_XMLOut of Message Type matching the inbound and out bound schemes. In my case they are the same Adec.VendorsEDI.InboundWHSchema.X12_00401_850.


5-      The best method to communicate how to use BRE in an orchestration is to review a complete solution first; thereafter, look at each component. Let us review a complete orchestration.


a.      Add a receiving messages port. With a Message Type matching the inbound schema. In my case Adec.VendorsEDI.InboundWHSchema.X12_00401_850.


b.      Add a dynamic send port matching the outbound schema. For my case, it is the same message type as inbound. Adec.VendorsEDI.InboundWHSchema.X12_00401_850. It is called. POErrorOrchestrationDynamicSend.


c.       Set up the receiving port with Message type identified in the Orchestration Messaged. It is Message_XNLIn. Connect the Receive port (a) with the Receive shape (c).


d.      Insert a construct message. The Message Construction must construct the Message element identified in the orchestration to contain the BRE information. In my case it is ExceptionAssignVendorsEdiAdminInfoMsg.


e.      This is where it gets interesting. The best way to instantiate a message schema is using a Map. Add a Transform Shape to Construct Message, Transform_InitEdiAdminInfo.


Build a simple map with source Message_XMLIn and Destination ExceptionAssignVendorsEdiAdminInfoMsg. Use the Value in the Property window and initialize all the values for ExceptionAssignVendorsEdiAdminInfoMsg. Does not matter what Values are. We are instantiating and initializing schema nodes.


f.        Insert a Call Rule shape. Select the BRE Policy and Parameter.


By now, we have received a message, instantiate a schema with nodes matching the BRE vocabularies, and called the BRE policy.  At this point of orchestration, we have all the information in the orchestration message. In our case, it is called ExceptionAssignVendorsEdiAdminInfoMsg.
g.       Add an Expression and extract the values from the ExceptionAssignVendorsEdiAdminInfoMsg node. The following is a sample code.

// Extract the BRE Information. The Schema, Node, Properties, and Instance XPath is the best place to get the Xpath.
// Remove the "Namespace" from the XPath copied from "Instance XPath". Do not need it.
// The variables are System.String. The Xpath require string to cast the extracted value.
BiztalkAppAdminDistList = xpath(ExceptionAssignVendorsEdiAdminInfoMsg,
  "string((/*[local-name()='BREEmailInfo']/*[local-name()='BSTAppAdminDistList'])[1])");
BADistList = xpath(ExceptionAssignVendorsEdiAdminInfoMsg,
  "string((/*[local-name()='BREEmailInfo']/*[local-name()='BADistList'])[1])");
ResURL = xpath(ExceptionAssignVendorsEdiAdminInfoMsg,
  "string((/*[local-name()='BREEmailInfo']/*[local-name()='ResolutionURL'])[1])");
SMTPESubject = xpath(ExceptionAssignVendorsEdiAdminInfoMsg,
  "string((/*[local-name()='BREEmailInfo']/*[local-name()='SMTPSubject'])[1])");
SysAdminDistList = xpath(ExceptionAssignVendorsEdiAdminInfoMsg,
  "string((/*[local-name()='BREEmailInfo']/*[local-name()='SystemAdminDistList'])[1])");

// Build the msg body
tempString  = System.String.Format("VendorsEDI Error Watcher BRE Information -");
tempString  = tempString  + System.String.Format(" BiztalkAppAdminDistList is: {0}" , BiztalkAppAdminDistList);
tempString  = tempString  + System.String.Format(" BADistList is: {0}" , BADistList);
tempString  = tempString  + System.String.Format(" ResURL is: {0}" , ResURL);
tempString  = tempString  + System.String.Format(" SMTPESubject is: {0}" , SMTPESubject);
tempString  = tempString  + System.String.Format(" SysAdminDistList is: {0}" , SysAdminDistList);

// Eventlog information
System.Diagnostics.EventLog.WriteEntry("VendorsEDI", tempString , System.Diagnostics.EventLogEntryType.Information, 1000);

// If the field is Null or Empty
if(System.String.IsNullOrEmpty(BiztalkAppAdminDistList))
{
  tempString  = System.String.Format("Error Watcher Error: the BiztalkApplicationAdministersDistributionList BRE String is not valid: {0}" , "It is Null or Empty. ") + tempString ;
  // Log event. For simplicity, let us log the same message.
  System.Diagnostics.EventLog.WriteEntry("VendorsEDI", tempString , System.Diagnostics.EventLogEntryType.Error, 1009);
}


// If the field is Null or Empty
if(System.String.IsNullOrEmpty(BADistList))
{
  tempString  = System.String.Format("Error Watcher Error: the BusinessAnalystDistributionList BRE String is not valid: {0}" , "It is Null or Empty. ") + tempString ;
  // Log event. For simplicity, let us log the same message.
  System.Diagnostics.EventLog.WriteEntry("VendorsEDI", tempString , System.Diagnostics.EventLogEntryType.Error, 1009);
}


// If the field is Null or Empty
if(System.String.IsNullOrEmpty(ResURL))
{
  tempString  = System.String.Format("Error Watcher Error: the ResolutionURL BRE String is not valid: {0}" , "It is Null or Empty. ") + tempString ;
  // Log event. For simplicity, let us log the same message.
  System.Diagnostics.EventLog.WriteEntry("VendorsEDI", tempString , System.Diagnostics.EventLogEntryType.Error, 1009);
}


// If the field is Null or Empty
if(System.String.IsNullOrEmpty(SMTPESubject))
{
  tempString  = System.String.Format("Error Watcher Error: the SMTPEmailSubject BRE String is not valid: {0}" , "It is Null or Empty. ") + tempString ;
  // Log event. For simplicity, let us log the same message.
  System.Diagnostics.EventLog.WriteEntry("VendorsEDI", tempString , System.Diagnostics.EventLogEntryType.Error, 1009);
}


// If the field is Null or Empty
if(System.String.IsNullOrEmpty(SysAdminDistList))
{
  tempString  = System.String.Format("Error Watcher Error: the SystemAdministersDistributionList BRE String is not valid: {0}" , "It is Null or Empty. ") + tempString ;
  // Log event. For simplicity, let us log the same message.
  System.Diagnostics.EventLog.WriteEntry("VendorsEDI", tempString , System.Diagnostics.EventLogEntryType.Error, 1009);
}

h.      Add Construct Message with Message Constructed matching the output message.


i.         Add create email message assignment. The wiki article “BizTalk Send Email and Post Event logs” outlines the details for email creation. Please feel free to refer to it.

j.         Add Send shape and connect to the dynamic port.


6-      Build and deploy the solution. In the BizTalk admin tool, the orchestration requires receive and send port.


7-      The Dynamic Send Port uses the XML Transmit pipeline.


8-      The receive port is a typical File type with XML Receive pipeline.


We are done. The BizTalk BRE and Orchestration is fully integrated.

Hints:
-          Instantiating schema in a map is a powerful method of initializing a schema.
-          I wonder if the inbound schema can be a generic XML document. It will enable us to reuse some of our orchestration. This could be another wiki article on its own.
-          The schema node properties have the xpath for the elements.
-          Deploying Orchestration can be a bit interesting. However, it makes perfect sense when you think thorough them.
o   When deploying BizTalk Orchestration with the same Assembly Information (Assembly Version and File Version), the Orchestration/s must be in the Unenlist status.
1.      Use the BizTalk Admin to Unenlist Orchestrations.
2.      Deploy the Orchestration/s and their dependent maps, together.
o   When deploying BizTalk Orchestration with the updated Assembly Information, Assembly Version and File Version, the Orchestration/s and Dynamic Ports must be in the Unenlist status.
1.      Use the BizTalk Admin to Unenlist Orchestrations and Dynamic Ports.
2.      Deploy all the schemas.
3.      Deploy the Orchestration/s and their dependent maps, together.

Enjoy coding. When designing, always think about the maintenance and production support. Push as much as possible logic in the BRE. Now that you learned how to use BRE, use the technology.

Cheers Toraj
References:
·         Walkthrough: Invoking the Policy from an Orchestration - http://msdn.microsoft.com/en-us/library/aa995582.aspx
·         Convert xml file to string inside BizTalk map using inline XSLT  http://stephenomo.blogspot.com/2011/11/convert-xml-file-to-string-inside.html

Version 1.0.1405.01

3 comments:

  1. Biztalk Admin |Training|Job Support|Corporate|+91-741-626-7887 Training - BizTalk Administrator|Microsoft BizTalk Server|BIZTALK Online| Corporate| Classroom training|Job Support at Layman Learning www.laymanlearning.com --- Send Enquiry --- hr@laymanlearning.com training,biztalk administrator,microsoft biztalk server,biztalk online,corporate,classroom training,job support,it support,online training,corporate training,24/7support services

    ReplyDelete
  2. Nice Blog ! thanks for sharing this information.
    Business Rules Engine

    ReplyDelete