Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

8. Applying Rules > Using the BRE outside of BizTalk

Using the BRE outside of BizTalk

Using the BRE outside of BizTalk Out of the box, BizTalk Server offers the BRE and it can be installed with or without the BizTalk Server runtime. It can be used separately, but you will still need a BizTalk license (similar to the scenario using the AppFabric Connect feature explained in Chapter 6, BizTalk AppFabric Connect). The BRE is essentially an independent product which can be accessed by its own APIs and tools. Through the .NET application, you can use the engine with no need for orchestrations or other BizTalk objects. In this recipe, you will create: A schema that will provide information on an employee, an instance of a schema will serve as input for the applicationA policy that will determine employee benefitsA .NET application calling the policy Although the business rules engine comes as part of the BizTalk Server, this recipe will show that .NET assemblies outside the BizTalk environment can call into it. The created external application is allowed to use the same rule framework that BizTalk uses. It will show that organizations can consolidate their business rules functionality onto one platform. Getting ready To run this, the sample Business Rule Engine component must be installed. You can download the code accompanied with this book regarding this recipe. How to do it... The following steps illustrate how to create an XML schema: Open Visual Studio 2010.In the New Project dialog box, from the list of installed templates, click on BizTalk Projects. In the right pane, select Empty BizTalk Server Project. For the project name, enter an appropriate name and then click on OK.Right-click on Solution and select New Item.A new dialog will appear and select Schema.Provide an appropriate name for the schema for example, Employee.Rename the root node to Employee.Right-click on Employee, and select Insert Schema Node. Then select Child record and name it as Detail.Create the following elements: FirstName (data type — xs:string)LastName (data type — xs:string)FullName (data type — xs:string)EmployeeNumber (data type — xs:string)SocialSecurityNumber (data type — xs:string)Contract (data type — xs:string)Department (data type — xs:string)Manager (data type — xs:string) Right-click on Employee and select Insert Schema Node. Then select Child record and name it as Benefits.Create the following elements: Rating (data type — xs:string)Bonus (data type — xs:int)Holidays (data type — xs:int)SalaryUpgrade (data type — xs:boolean) Refer to the following screenshot: Sign the project with a strong name. Build and deploy the BizTalk project.See to it that the assembly is in the GAC. Now you have your schema ready, you can create a policy using the Business Rule Composer: Open the Business Rule Composer.Right-click on Policies in the Policy Explorer pane and click on Add New Policy.Name it as Assess Benefits.In Facts Explorer, select the XML Schemas tab.Select the Schemas node and right-click on it. Then click on Browse.Navigate to the Employee schema and click on Open.You will now see the Employee schema in Facts Explorer: In Policy Explorer, right-click on Version 1.0 and select Add New Rule.Name it as Bonus Rule.In the Conditions pane, right-click and select Predicates | Equal.From Facts Explorer select the Employee schema, drag the Rating element to argument1.Select argument2 and enter A.In the Then pane, drag the Bonus element and set the value to 1000.Drag Holidays below the Bonus element and set the value to 1.Drag SalaryUpgrade below the Holidays element and set the value to True: Right-click on Version 1.0 and click on Save.Right-click on Version 1.0 and click on Publish.Right-click on Version 1.0 and click on Deploy. Now you have your policy ready and published in the rule store: Right-click on Solution in Visual Studio and select Add | New Project.In the New Project dialog box, from the list of installed templates, click on Visual C#. In the right pane, select Windows Form Application. For the project name, enter an appropriate name and then click on OK.Drag a button to the form and name it as Request.Drag a text below button, name it appropriately, and set the Multiline property to True.Double-click on the Request button and the Code Behind pane will appear in Visual Studio.Add a project reference to the Microsoft.RuleEngine.dll assembly, which contains the classes required to call the BRE. This assembly can be found in $\Program Files\Common Files\Microsoft BizTalk.Add the following to the using statement above the namespace: //To use the XmlDocument class using System.Xml; //To use the TypedXmlDocument and Policy classes using Microsoft.RuleEngine; Add the following code below the button and click on Event: // Create instance xmldocument object XmlDocument xmlDoc = new XmlDocument(); //Load xml file xmlDoc.Load(@"C:\BizTalk Server 2010 Cookbook\Chapter8\ BTS.Cookbook.BRE.Schemas\Xml File\Employee Dan Janssen.xml"); // Create the input parameter for the Assess Benefits policy based on a typed BizTalk schema (fully qualified .NET type) TypedXmlDocument typedXmlDoc = new TypedXmlDocument ("BTS.Cookbook.BRE.Schemas.Employee", xmlDoc); // Create the Assess Benefits policy object Policy policy = new Policy("Assess Benefits"); //Call Policy policy.Execute(typedXmlDoc); //Show results txtResult.Text = typedXmlDoc.Document.OuterXml; The location of your input XML may vary from the above code snippet.Now, you can run the .NET application by right-clicking on the project, selecting Debug, and then selecting Start New Instance. How it works... To be able to call into the rules engine, you need to reference the Microsoft.RuleEngine.dll assembly, which contains classes used to access the rules framework, including those to execute policies as you can see in the code sample. In this recipe, you created a schema which imported into the Business Rule Composer as facts to work with the composing rules. An Assess Benefits policy was created which contains one rule. The rule evaluates one condition which has to be met to fire off actions that sets values in the elements in the Benefits. This policy is saved, published in the rule store, and deployed. In the .NET application, a message is loaded into the XmlDocument instance. Subsequently, a Microsoft.RuleEngine.TypedXmlDocument instance is created specifying the document type (the fully qualified .NET type name) and the XML document instance. The message in a single fact is going to be offered to the policy and be evaluated. An instance of the policy object is created with the name of the policy created in the Business Rule Composer. Policy is executed with the Microsoft.RuleEngine.TypedXmlDocument instance and the outcome is shown in the .NET application. It is also possible to use code to call the policy in .NET in the Expression shape in an orchestration. There's more... On the DevX website you will find an extensive article, Programming with the Microsoft Business Rules Framework (http://www.devx.com/codemag/Article/40489/1763/page/1), by Rick Garibay. It explains about programming with Microsoft Business Rules. Another good resource is the blog by Charles Young, a BizTalk MVP, that has a lot of content on the BRE. You can find it at http://geekswithblogs.net/cyoung/category/2771.aspx. See also This recipe is an alternative way of using the BRE, while the BRE can be used from the BizTalk runtime. For this you can refer to the Calling rules in an orchestration recipe discussed earlier in this chapter.

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint