Articles
|
SOA Best Practices: The BPEL Cookbook |
|
A Services-Oriented Approach to Business Rules Development
In this first installment of the SOA Best Practices: The BPEL Cookbook, learn how to reduce maintenance costs and improve organizational flexibility through a services-oriented approach to business rules development and management.
Many organizations are moving from an object-oriented paradigm for managing business processes toward a service-oriented approach; indeed, services are becoming the fundamental elements of application development. At the same time, Business Process Execution Language (BPEL) has become the de facto standard for orchestrating these services and managing flawless execution of business process. The confluence of these trends is presenting some interesting opportunities for more flexible, cost-effective management of business processes. Most business processes—loan-approval processes being a good example—contain multiple decision points. At these decision points, certain criteria are evaluated. Based on these criteria or business rules, business processes change their behavior. In essence, these business rules drive the business process. Frequently, these rules are embedded within the business process itself or inside custom Java code, which can cause several problems down the road:
Rules engines and BPEL are complementary technologies. Oracle BPEL Process Manager provides high-level tools to visualize, design, and manage BPEL process flows, whereas third-party rule engines allow complicated business logic to be expressed in English-like syntax and edited by nonprogrammer domain experts. In this first installment of The BPEL Cookbook, I'll provide best practices for separation of process and rules based on my team's own experience. Using code examples, I'll also offer a development approach and change management strategy for integrating BPEL with a rules engine. Finally, I'll explain how this architecture leads to proper separation of logic across different layers. Separating Rules from Processes Integrating a rules engine within a process management framework requires some investment up front. Before you attempt this integration, it is important to delineate rules from process. Hence, a major decision in system architecture is how to implement business policies, business processes, and supporting business logic. Indeed, the architect must communicate or invent best practices so that designers know where each of the relevant technologies—BPEL, business rules, Java/Web services—should be applied when designing system functionality. As illustrated in Figure 1, business logic is spread across three different layers of the IT infrastructure: business process, Web services, and rules. Let's address each in turn.
Figure 1 Architecture: separating rules from processes
Web services implement functional and domain logic. Functional methods are typically stateless and medium-grained. Web services may for example contain utility methods, entity operations, and inquiry methods for system data. You can implement Web services using multiple technologies and hide differences among implementation platforms. Thus, this layer is well suited for:
To illustrate the development process, we will use the example of a business process called Eligibility Process. This process assesses the eligibility of a family for a specific healthcare program. Depending on the attributes of the family (income, total number of children), it assigns the family to Healthcare Program 1 or Healthcare Program 2. During the analysis phase, logic is categorized into different buckets based on volatility and complexity. As discussed in the previous section, rules typically model complex return structures that require multiple business validations as well as policies that change frequently or that affect large parts of the organization. In contrast, departmental or organizational processes are modeled in the business process layer. The typical development process comprises three steps:
Figure 2 Development and maintenance roles
To start the template-based rule creation process, the analyst connects to the rule repository. When he or she has opened the rule package from the repository, they will have access to its BOM. ILOG supports the definition of high-level rules through its Business Application Language (BAL). The analyst can edit an existing rule or create new rules at this point. Rules may be modified through a template during the maintenance phase, which limit what modifications can be made to the rule. The rule editor has a default template and allows a developer to create conditions and action using the IF–THEN construct. As shown in Figure 3, the business analyst is creating a rule that checks the total number of children in the family. If this variable equals 2, then the family qualifies for Healthcare Program 1. Rule returns the value true in datatype eligibilityResult.
Figure 3 Creating a rule that checks the total number of children in the family The code example below shows the implementation of Eligibility ruleset.
// Eligibility ruleset receives an account and calculates eligibility results for multiple programs
ruleset eligibility {
in Account account;
out List eligibilityResultsList = new ArrayList();
}
// calculate program 1 then program 2 eligibility
flowtask Program1_TaskFlow {
body = {
Program1_Setup;
Program1_Eligibility;
Program2_Setup;
Program2_Eligibility;
}
};
// Determine which rules are setup rules for program 1
ruletask Program1_Setup{
body = select(?rule) {
if("Program1_Setup".getTask(?rule) )
return true;
else
return false;
}
}
// Determine which rules are eligibility rules for program 1
ruletask Program1_Eligibility{
body = select(?rule) {
if("Program1_Eligibility".getTask(?rule) )
return true;
else
return false;
}
}
// Create an eligibility result (JAXB object) for program 1
rule Progam1.CreateEligibilityResult {
property task = " Program1_Setup";
when {
?account: Account();
?person: Person();
}
then {
bind ?result = new EligibilityResult ();
?result.setPersonId(?person.getPersonId());
?result.setProgramType(ProgramEnum.PROGRAM1);
?result.setEligible(Boolean.FALSE);
modify ?person { getEligibilityResults().add(?result); };
}
};
// simple rule make person eligible if over 6 years old and returns
// result back in finalResults map
rule Program1.AgeQualification {
property task = " Program1_Eligibility";
when {
?person: Person(?age: getAge().intValue(););
evaluate(?age >= 6);
}
modify ?result {setEligible(Boolean.TRUE); };
finalResults.add(?result);
}
};
2. Expose the ruleset as a Web service. Rules engines such as JRules provide tools to generate wrapper Web services or Session Beans to wrap a newly developed ruleset. Web services developers will be instrumental in creating a wrapper to expose ruleset as a Web service.
XML is a key standard for integrate rules, EJBs, BPEL process flows, and Web services. BPEL uses XML natively to access data while Web services use it to serialize data (and will use it unmodified in Doc/Literal invocations). XML can be used directly in rules. By marshalling, XML can be transformed directly into a JAXB object tree. Rules can be executed with native Java Objects. Web services should import XML Schema in their respective WSDL definitions. Generated DTO objects from XML Schema (e.g. JAXB) can also help ensure that data is translated smoothly without conversion errors.Eligibility Web service provides the translation from XML to JAXB and then invokes the Eligibility Rules Delegate Session Bean. To hide the complexity of invoking JRules custom libraries, you would create a session façade. This approach makes the implementation rule engine "agnostic;" the system could be easily ported to a new rule engine provider. Eligibility Delegate Session Bean makes an RMI call to Eligibility façade Session Bean. This session bean invokes the Eligibility ruleset in the RuleApp package using ruleSession.executeRules("RuleApp/eligibility").
import ilog.rules.bres.session.IlrRuleExecutionResult;
import ilog.rules.bres.session.IlrStatelessRuleSession;
import ilog.rules.bres.session.ejb.IlrManagedRuleSessionProvider;
import ilog.rules.bres.session.util.IlrRuleSessionHelper;
.
.
.
public List assessEligibility(AccountRoot account) {
List eligibilityList = null;
// get stateless rulesession instance
IlrStatelessRuleSession ruleSession = null;
try {
ruleSession = IlrManagedRuleSessionProvider.getProvider()
.createStatelessRuleSession();
} catch (ilog.rules.bres.session.IlrRuleSessionCreationException ce) {
String msg = "Failed to retrieve RuleSession Provider";
throw new InfrastructureException(msg, ce);
}
// pass borrower and credit as "in" parameters of the stateless session.
IlrRuleSessionHelper helper = new IlrRuleSessionHelper(false);
helper.addParameter("account", account);
try{
// execute rules and handle results
IlrRuleExecutionResult res = ruleSession.executeRules("/RuleApp/ eligibility", null,
helper.getJavaClassResolver(this), helper.getParameters());
eligibilityList = (List)res.parametersOut.getHandle("finalResults").getValue();
} catch(Throwable t) {
String msg = "Failed to execute rule!";
throw new InfrastructureException(msg, t);
}
return eligibilityList;
}
3. Invoke the ruleset Web service from BPEL. After all custom system components are developed, it's time for developers to integrate the system with the BPEL engine. Legacy systems and new custom components are orchestrated by BPEL process flows. Issues with compatibility, datatype conversion, and Web service protocols would be addressed at this time. Process flow developers and/or system integrators would implement the orchestration flows inside Oracle BPEL Process Designer.
For example, BPEL will invoke the underlying Eligibility Web service using the code below.
<assign name="setAccount">
<copy>
<from variable="BPELInput" part="payload"
query="/tns:EligibilityProcessRequest/tns:Account">
</from>
<to part="parameters"
query="/nsxml0:assessEligibility/nsxml0:Account"
variable="webservice_account"/>
</copy>
</assign>
<invoke name="CallEligibilityWebservice" partnerLink="EligibilityWebservice"
portType="nsxml0:EligibilityService" operation=" assessEligibility "
inputVariable="webservice_account" outputVariable="webservice_eligibilityResults"/>
Maintenance phase. As for the maintenance phase—the longest phase of any project—moving business rules out of Java code and into the rules engine makes maintenance much more manageable. As I explained previously, business managers can modify rules at run time using a graphical interface, and business rules and BPEL processes can change independently of each other.
Executing JRules with Oracle BPEL Process Manager
Clearly, the design and development of rules, Web services, and BPEL processes involve multiple different technologies. In this section, I will discuss how these technologies work together at runtime to execute
Eligibility Process. Although this example is based on Oracle BPEL Process Manager and ILOG JRules specifically, it is applicable to many other environments.
The rule engine invocation occurs across three tiers (see Figure 4): BPEL invoking the rules Web service, rules Web service invoking the rules engine, rules engine application code receiving input and returning results.
Figure 4 Rule engine invocation in action
Figure 5 Integrating rules into a J2EE platform via Web services Kevin Geminiuc
[
kgeminiuc@yahoo.com] currently works as a senior software architect in Denver. Over the last 15 years Kevin has worked as a systems architect, technical manager, developer, and hardware engineer. Kevin's technical interests include SOA, RFID, AVL, and genetic software.
|