JBoss 系列四十六:jBPM5示例之 CustomerEvaluation

CustomerEvaluation流程是一个较复杂的流程,我们多次使用Gateway,以及Rule Task,流程示意如下图:

JBoss 系列四十六:jBPM5示例之 CustomerEvaluation_第1张图片

  • 如上图流程运行时第一个节点Initialize节点为Script Task,流程运行时执行Java代码,如下为要执行的代码段
System.out.println("Entering " + kcontext.getNodeInstance().getNodeName() +" Node");

if (request == null) {
	System.out.println("There as no evaluation objects defined, adding default ones for demo purposes.");
	
	// define request object.
	request = new Request("1");
	request.setPersonId("kylin");
	request.setAmount(5000);
	kcontext.setVariable("request", request);	
	
	// define person object.
	person = new Person("kylin", "Kylin Soong");
	kcontext.setVariable("person", person);
}

System.out.println("Leaving " + kcontext.getNodeInstance().getNodeName() +" Node");


  • 第二个节点为Gateway,判断是否是under age或adult,如果年龄小于18为under age,大于或等于18为adult,如下为相关判断条件代码:
System.out.println("Gateway: " + kcontext.getNodeInstance().getNodeName() + " adult");
return person.getAge() > 17;
System.out.println("Gateway: " + kcontext.getNodeInstance().getNodeName() + " under age");
return person.getAge() < 18;

  • 如果年龄小于18为under age,则进入到Underaged节点,Underaged节点为Script Task,流程运行时执行Java代码,如下为要执行的代码段:

System.out.println("Entering " + kcontext.getNodeInstance().getNodeName() +" Node");

// Setting our request to invalid.
System.out.println("Detected and reporting invalid request.");
kcontext.setVariable("validRequest", false);
request.setInvalid("Customer is underaged.");

System.out.println("Set validRequest to: " + kcontext.getVariable("validRequest"));

System.out.println("Leaving " + kcontext.getNodeInstance().getNodeName() +" Node");

System.out.println("Process ended in End Minor Node.");

  • 如果年龄大于或等于18为adult,则进入到Adult Customer节点,Adult Customer节点为Script Task,流程运行时执行Java代码,如下为要执行的代码段:

System.out.println("Entering " + kcontext.getNodeInstance().getNodeName() +" Node");

// Setting request to valid.
System.out.println("Detected and reporting valid request");
kcontext.setVariable("validRequest", true);
request.setValid();

System.out.println("Set validRequest to: " + kcontext.getVariable("validRequest"));

// Insert Person and Request into our session for the rules to use.
insert(person);
insert(request);

System.out.println("Leaving " + kcontext.getNodeInstance().getNodeName() +" Node");

System.out.println("Entering Finance Rules Node");

  • Adult Customer节点执行完成后执行Rule Task Finance Rules节点,Finance Rules节点执行rules 文件customerEvaluationFinanceRules.drl,customerEvaluationFinanceRules.drl文件的内容如下:

package org.jbpm.quickstarts;

rule "Check Amount Customer" ruleflow-group "Validation"
    when
        r: Request();
        Person(id == r.personId, r.amount < 1000);
    then
    	System.out.println("Under funded customer");
        r.setInvalid("Poor customer");
        System.out.println("Set Request invalid comment to: Poor customer.");
        System.out.println("Customer request= " + r);
end

  • Finance Rules节点执行完成后进入Gateway节点,该节点判断是否为loaded或broke,该节点使用如下代码判段进入loaded分支:

System.out.println("Leaving Finance Rules Node.");

System.out.println("Gateway: " + kcontext.getNodeInstance().getNodeName());

// determine results of fincancial rules decision by checking if request is valid.
if (request.isValid()) {

    System.out.println("Determined request is valid, heading to Rich Customer Node");
	return true;
}

System.out.println("Determined request is NOT valid, heading to Poor Customer Node");
System.out.println("Request validity reason is: " + request.getInvalidReason());
return request.isValid();

如下为进入broke分支的代码段:

System.out.println("Leaving Finance Rules Node.");

System.out.println("Gateway: " + kcontext.getNodeInstance().getNodeName() + " broke");
System.out.println("Detected  request is invalid, heading to Poor Customer Node");
System.out.println("Request not valid reason is: " + request.getInvalidReason());
return true;

  • 如果流程进入loaded分支,Rich Customer节点被执行,Rich Customer为Script Task,流程运行时如下代码段执行:

System.out.println("Entering " + kcontext.getNodeInstance().getNodeName() +" Node");

// Setting request to valid.
System.out.println("Detected and reporting valid request");
System.out.println("Customer has amount: " + request.getAmount() + " in the bank.");
kcontext.setVariable("validRequest", true);


System.out.println("Leaving " + kcontext.getNodeInstance().getNodeName() +" Node");

System.out.println("Process ended in End Rich Customer Node.");

如果流程进入broke分支,Poor Customer节点被执行,Poor Customer为Script Task,流程运行时如下代码段执行:

System.out.println("Entering " + kcontext.getNodeInstance().getNodeName() +" Node");

// Setting request to invalid.
System.out.println("Customer has amount: " + request.getAmount() + " in the bank.");
kcontext.setVariable("validRequest", false);

System.out.println("Leaving " + kcontext.getNodeInstance().getNodeName() +" Node");

System.out.println("Process ended in End Poor Customer Node.");

  • CustomerEvaluation流程运行代码:https://github.com/kylinsoong/.../CustomerEvaluationStart.java
  • CustomerEvaluation流程:https://github.com/kylinsoong/.../customerEvaluation.bpmn

运行org.jbpm.quickstarts.evaluation.CustomerEvaluationStart可执行CustomerEvaluation流程,CustomerEvaluationStart中假设了机制情况,可以执行流程的每个节点,运行CustomerEvaluationStart结果输出如下:

=========================================
= Starting Process Underaged Test Case. =
=========================================
Entering Initialize Node
Leaving Initialize Node
Gateway: Gateway under age
Gateway: Gateway adult
Entering Underaged Node
Detected and reporting invalid request.
Set validRequest to: false
Leaving Underaged Node
Process ended in End Minor Node.
==========================================
= Starting Process Poor Adult Test Case. =
==========================================
Entering Initialize Node
Leaving Initialize Node
Gateway: Gateway under age
Gateway: Gateway adult
Entering Adult Customer Node
Detected and reporting valid request
Set validRequest to: true
Leaving Adult Customer Node
Entering Finance Rules Node
Under funded customer
Set Request invalid comment to: Poor customer.
Customer request= Request [id=1, personId=kylin, amount=2, valid=false, invalidReason=Poor customer, canceled=false]
Leaving Finance Rules Node.
Gateway: Gateway
Determined request is NOT valid, heading to Poor Customer Node
Request validity reason is: Poor customer
Leaving Finance Rules Node.
Gateway: Gateway broke
Detected  request is invalid, heading to Poor Customer Node
Request not valid reason is: Poor customer
Entering Poor Customer Node
Customer has amount: 2 in the bank.
Leaving Poor Customer Node
Process ended in End Poor Customer Node.
==========================================
= Starting Process Rich Adult Test Case. =
==========================================
Entering Initialize Node
Leaving Initialize Node
Gateway: Gateway under age
Gateway: Gateway adult
Entering Adult Customer Node
Detected and reporting valid request
Set validRequest to: true
Leaving Adult Customer Node
Entering Finance Rules Node
Leaving Finance Rules Node.
Gateway: Gateway
Determined request is valid, heading to Rich Customer Node
Entering Rich Customer Node
Detected and reporting valid request
Customer has amount: 2000 in the bank.
Leaving Rich Customer Node
Process ended in End Rich Customer Node.
=============================================
= Starting Process Empty Request Test Case. =
=============================================
Entering Initialize Node
There as no evaluation objects defined, adding default ones for demo purposes.
Leaving Initialize Node
Gateway: Gateway under age
Gateway: Gateway adult
Entering Underaged Node
Detected and reporting invalid request.
Set validRequest to: false
Leaving Underaged Node
Process ended in End Minor Node.



你可能感兴趣的:(jboss,jbpm,BPM,JBPM5)