Showing posts with label BussinessRule. Show all posts
Showing posts with label BussinessRule. Show all posts

Tuesday, 10 December 2013

Using DecisionTables in BussinessRules --SOA 11g



Hi Guys

The Decision Table is a smart, compact way of clustering many IF/THEN rules together, as will be demonstrated in this article. As we dealt with simple IF/THEN,Functions in previous posts.


Demo
This article demonstrates how the decision table can be put to good use to implement the business logic behind the classical game of Dice

Rules for Dice game as defined below :

IF Player 1 ="three" Player 2="three"  Player 2 wins
IF Player 1 ="three" Player 2="one,two,four,five,six"  Player 1 wins
IF Player 1 ="one" Player 2="one"  Player 2 wins
IF Player 1 ="one" Player 2="three,two,four,five,six"  Player 1 wins
IF Player 1 ="five" Player 2="five"  Player 2 wins
IF Player 1 ="five" Player 2="three,two,four,six,one"  Player 1 wins
IF Player 1 ="six" Player 2="six"  Player 1,2  are in tie wins
IF Player 1 ="six" Player 2="three,two,four,five,six"  Player 1 wins
IF Player 1 ="two,four" Player 2="two,four" response="tryAgain"



Designing SOA Application :


Create a new XSD document, called Diceplayergame.xsd. It should be composed as follows:





 The RequestType describes the input that the Business Rule will deal with. It consists of two entries, here labeled with throwOne and throwTwo. Both contain exactly one value from the enumerated list of values Stype1. The Response type contains a single element outcomeof Stype2 that has one of three values: One_Wins | Two_Wins | tie.



Create and configure the Business Rule 

 Create new Business Rule component


 

Set the input and output for the rule based on the Request and Response type defined in the XSD document.
Double click the Business Rule component to edit the rule definition.
o Create XML Facts – go to the Facts tab, click on the green plus icon, select the complex and simple types from the XSD document to have XML Facts created from them


  Verify/Create Bucketsets as illustrated below




Configure the Decision Function




Edit default rule set; create a Decision Table

Create an action: Assert New InputGamedetailsresponse. The outcomeresponse property is parametrized (always to be determined for the rule), make sure the checkbox Always Selected is checked (to make sure this action is executed for every rule that is triggered) and press the ok button




Add conditions for InputGamedetails.throwone and InputGamedetails .throwtwo. Both conditions are associated with bucketset Roll_Dice.




Below illustrated Design of decision-table







TestCases :














HappyLearning

                                                                                                                By DeepthiReddy

Sunday, 8 December 2013

BussinessRulesEngine Using Functions --Soa 11g

Hi Guys

In this Post we will discuss in depth of BussinessRuleEngine.(Continution to  BussinessRule Part 1)

Our current rule developed in BussinessRule Part 1 approves vacations of one day in duration, requiring all other leave requests to be manually approved. Ideally, we would like to approve holidays of varying duration as long as sufficient notice has been given, for example:
  • Approve vacations of one day in duration with a start date that's two weeks or more in the future
  • Approve if for 2-3 days and more than 30 days in the future
  • Approve if 5 days or less and more than 60 days in the future

  • So we will need to write our own logic to calculate these values. Rather than embedding this logic directly in each rule, best practice dictates that we place this logic into a separate function. This not only ensures that we have a single version of the logic to implement but minimizes the size of our rules, thus making them simpler and easier to maintain. For our purposes, we will create the following functions:
  • startsIn: Which returns the number of days before the specified start date
  • leaveDuration: Which returns the number of days from the start date to the end date
To create our first function, within the rule editor, click on the Functions tab. This will list all the functions currently defined to our ruleset. To create a new function, click on the green plus icon, as shown in the following screenshot:
This will add a new function with a default name (for example, Function_1) to our list. Click on the function name to select it and update it to startIn. From the drop-down list, select the Return Type of the function, which is int in our case.

Next, we need to specify the arguments we wish to pass to our function. Click on the green plus sign, as shown in the following screenshot, and this will add an argument to our list. Here we can specify the argument name (for example, startDate), and from the drop-down list, the argument Type, which should be XMLGregorianCalendar (when creating XML facts, the JAXB processor maps the type xsd:date to javax.xml.datatype.XMLGregorianCalendar).
The list of valid types is made up of the basic types (for example, int, double, char, and so on), plus the XML facts (excluding object factories) and the Java Facts (excluding the Rules Extension Method) defined in our rules dictionary.
The final step is to implement the business logic of our function, which consists of one or more actions. We enter these actions in the Body section of the function. The first action we need to create is one that creates a local variable of type calendar, which holds the current date.
To do this, click on <insert action> within the Body section of our function. The rule editor will display a drop-down list that lists all the available actions.

Same as above create another Function,Argument for leaveduration as showm below




Now modify the Ruleset  to below





Now we are ready to test Function logic for BussinessruleEngine


HappyLearning


                                                                                                             By DeepthiReddy

Working with Bussiness Rule Engine --Soa 11g


Hi Guys

The Oracle Business Rules engine that comes as part of the SOA Suite provides a declarative mechanism for defining business rules externally to our application. This not only ensures that each rule is used in a consistent fashion, but in addition, it makes it simpler and quicker to modify. We only have to modify a rule once and can do this with almost immediate effect, thus increasing the agility of our solution.

In this article, we will introduce the new rules editor and look at how we can use it to define a decisions service to automate the approval of leave requests. Then, once we've done this, we'll see how to invoke the rule from the leave approval BPEL process.
 

Business rule concepts

Before we implement our first rule, let's briefly introduce the key components which make up a business rule. These are:
  • Facts: Represent the data or business objects that rules are applied to.
  • Rules: A rule consists of two parts, namely, an IF part that consists of one or more tests to be applied to a fact(s), and a THEN part that lists the actions to be carried out, should the test evaluate to true.
  • Rule Set: As the name implies, it is just a set of one or more related rules that are designed to work together.
  • Dictionary: A dictionary is the container of all components that make up a business rule. It holds all the Facts, Rule Sets, and Rules for a business rule.

Leave approval business rule

 To begin with, we will write a simple rule to automatically approve a leave request that is of the type Vacation and only for one day's duration. This is a pretty trivial example, but once we've done this, we will look at how to extend this rule to handle more complex examples.

Creating Soa Composite
Within JDeveloper, open up your LeaveApproval application . Open up the composite.xml file for the application and then from the Component Palette, drag-and-drop a Business Rule onto the composite.This will launch the Create Business Rules dialog, as shown in the following screenshot:



The first step is to give our dictionary a name, such as  LeaverequestRules, and a corresponding
In addition, we need to specify the Input and Output facts that we will pass to our decision service. For our purpose, we will pass in a single leave request. The rule engine will then apply the rules that we define and update the status of the leave request to either Approved or Manual

Next, click the Advanced tab. Here we can see that JDeveloper has given the default name LeaveApprovalRules_DecisionService_1 to our decision service. Give it a more meaningful name such as LeaveApprovalDecisonService.




Implementing our business rules

The rules editor allows you to view/edit the various components which make up your business rules. To select a particular component, such as Facts, Functions, Globals, and so on, just click on the corresponding tab down the left-hand side.

To add a rule, click the green plus symbol on the top-right-hand corner, and select Create Rule, as shown in the following screenshot (alternatively click on the Create Rule button, circled in the following screenshot).





For our leave approval rule, we need to define two tests, one to check that the request is only for a day in duration, which we can do by checking that the start date equals the end date, and the second to check that the request is of type Vacation.
To define the first test, click on &ltinsert test&gt. This will add the line <operand> = = <operand> under the IF statement where we can define the test condition.






The rule editor allows us to choose from the following action types:
  • assert new: We use this to create and assert a new fact, for example, a new LeaveRequest. Once asserted, the new fact will be evaluated by the rules engine against the ruleset.
  • modify: We can use this to either assign a value to a variable or a fact attribute; in our case we want to assign a status of Approved to the requestStatus property.
  • retract: This enables you to retract any of the facts matched in the pattern (for example, TLeaveRequest) so that it will no longer be evaluated as part of the ruleset.
  • call: This allows you to call a function to perform one or more actions.

The actions assert new and retract are important when we are dealing with rulesets that deal with multiple interdependent facts, as this allows us to control which facts are being evaluated by the rule engine at any particular time.
For our purposes, we want to update the status of our leave, so select modify




Calling a business rule from BPEL

Save the rule, and then switch back to our composite and double-click the LeaveRequest BPEL process to edit it. Drag a Business Rule from the BPEL Activities and Components palette into your BPEL process



Once we've specified the service, we need to specify how we want to invoke the decision service. We specify this through the Operation attribute. Here we have two options:
  • Execute function and reset the session
  • Execute function
If we choose the option Execute function and thus don't reset the session, if we were then to call the decision service several times within the same instance of our BPEL process, each new invocation would reuse the same session and would also evaluate facts asserted in any previous invocation. For our purposes, we just need to assert a single fact and run the ruleset, so accept the default value of Execute function and reset the session.



 We are ready to go with testing

In my next post we will discuss about Using Functions in BussinessRules


Happy Learning

                                                                                                                   By DeepthiReddy