JBoss 系列四十五:jBPM5示例之 Global Variable

RiskyAccount是一较复杂的流程,通过drools规则判断账户是否为危险账户,如果账户中的额度小于0则该账户为危险账户,RiskyAccount示例主要目的包括:
  • 演示流程嵌套,主流程运行子流程
  • 演示jBPM流程与drools规则的整合
  • 演示主流程与子流程之间共享全局变量

RiskyAccount流程如下:

JBoss 系列四十五:jBPM5示例之 Global Variable_第1张图片

如上,主流程Start节点为Script Task,运行时执行如下Java代码:

System.out.println("Mainprocess - Start, " + "Process ID: " + kcontext.getProcessInstance().getId());
RiskyAccounts account = (RiskyAccounts)  kcontext.getKnowledgeRuntime().getGlobal("risky");
System.out.println("Current risky account: "+ account);
Account acc = new Account(-20, "Kobe Bryant");
account.add(acc);
System.out.println("Add  " + acc + " to risky acount list");

主流程中SubProcess节点,启动子流程,子流程中Print Rsiky Account节点中获取全局变量,输出当前的危险用户:

System.out.println("Current risky account: "+kcontext.getKnowledgeRuntime().getGlobal("risky"));

我们使用drools规则判断账户是否为危险账户,subprocess-enoughMoney.drl内容如下:

package org.jbpm.quickstarts

import org.jbpm.quickstarts.subprocess.Account
import org.jbpm.quickstarts.subprocess.RiskyAccounts

global RiskyAccounts risky;
rule "enoughMoney"
when
$account : Account( money < 0 )
then
  System.out.println( "Not enough money on the account!");
  risky.add($account);
end

  • RiskyAccount流程运行代码:https://github.com/kylinsoong/.../SubProcessStart.java
  • RiskyAccount流程所涉及到的流程和规则:subprocess-main.bpmn, subprocess-sub.bpmn,subprocess-enoughMoney.drl
运行org.jbpm.quickstarts.subprocess.SubProcessStart可以运行RiskyAccount流程,SubProcessStart代码如下:

package org.jbpm.quickstarts.subprocess;

import org.drools.runtime.StatefulKnowledgeSession;
import org.jbpm.quickstarts.QuickStartBase;


public class SubProcessStart extends QuickStartBase{

	public void test() {
		StatefulKnowledgeSession ksession = createKnowledgeSessionWithDrl("quickstarts/subprocess-enoughMoney.drl", "quickstarts/subprocess-main.bpmn", "quickstarts/subprocess-sub.bpmn");
		
		// Set the Global variable
		RiskyAccounts risky = new RiskyAccounts();
		ksession.setGlobal( "risky", risky );
		
		// Fire the enoughMoney rule
		ksession.insert(new Account(-10, "Kylin Soong"));
		ksession.fireAllRules();
		
		// Start the Main Process
		ksession.startProcess("org.jbpm.quickstarts.subprocess-main");
	}
	
	public static void main(String[] args) {
		new SubProcessStart().test();
	}

}

RiskyAccount流程执行结果输出如下:

Current risky account: 
  [money=-10, name=Kylin Soong]
Add  [money=-20, name=Kobe Bryant] to risky acount list
Subprocess - Start, Process ID: 2
Current risky account: 
  [money=-10, name=Kylin Soong]  [money=-20, name=Kobe Bryant]
Subprocess - Done, Process ID: 2
Mainprocess - Done, Process ID: 1


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