jbpm5.1介绍(7)

Junit测试评估流程

评估流程的界面如下:

这个示例里边用到了Script Task,Service Task和User Task

Log执行记录日志的功能,打印出一段信息

RegisterRequest调用外部预定义的程序或者其它引擎的程序

Two调用人工任务

看一下调用的测试程序

public void testEvaluationProcess() throws Exception {

        KnowledgeBase kbase = createKnowledgeBase("BPMN2-EvaluationProcess.bpmn2");

        StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);

        ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler());

        ksession.getWorkItemManager().registerWorkItemHandler("RegisterRequest", new SystemOutWorkItemHandler());

        Map<String, Object> params = new HashMap<String, Object>();

        params.put("employee", "UserId-12345");

        ProcessInstance processInstance = ksession.startProcess("Evaluation", params);

        assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);

    }

 在程序中注册工作处理程序

ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler());
ksession.getWorkItemManager().registerWorkItemHandler("RegisterRequest", new SystemOutWorkItemHandler());

看一下SystemOutWorkItemHandler中的内容

public class SystemOutWorkItemHandler implements WorkItemHandler {

 

    public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {

        System.out.println("Executing work item " + workItem);

        manager.completeWorkItem(workItem.getId(), null);

    }

 

    public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {

        System.out.println("Aborting work item " + workItem);

        manager.abortWorkItem(workItem.getId());

    }

 

}

看一下WorkItemHandler,这个接口

Method Summary
void abortWorkItem(WorkItem workItem, WorkItemManager manager) 
The given work item should be aborted.
void executeWorkItem(WorkItem workItem, WorkItemManager manager) 
The given work item should be executed.

有两个方法,一个是要执行的工作任务,一个是忽略的工作任务

运行junit测试,会发现输出了

Loading process BPMN2-EvaluationProcess.bpmn2
Processing evaluation for employee UserId-12345
Executing work item WorkItem 1 [name=RegisterRequest, state=0, processInstanceId=1, parameters{employeeId=UserId-12345}]
Executing work item WorkItem 2 [name=Human Task, state=0, processInstanceId=1, parameters{}]
先是执行RegisterRequest这个里边的executeWorkItem方法

然后执行了Human Task里的executeWorkItem方法

最后使用

assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);

判断状态完成,执行成功

 

Junit测试评估流程2

评估流程的界面如下,同样的流程,打开BPMN2-EvaluationProcess2.bpmn2文件

同前边的示例一下,看一下测试2的示例程序

public void testEvaluationProcess2() throws Exception {

        KnowledgeBase kbase = createKnowledgeBase("BPMN2-EvaluationProcess2.bpmn2");

        StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);

        ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler());

        Map<String, Object> params = new HashMap<String, Object>();

        params.put("employee", "UserId-12345");

        ProcessInstance processInstance = ksession.startProcess("com.sample.evaluation", params);

        assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);

    }

 执行完成后的结果如下:

Loading process BPMN2-EvaluationProcess2.bpmn2
Executing work item WorkItem 1 [name=Human Task, state=0, processInstanceId=1, parameters{ActorId=UserId-12345, Comment=You need to perform a self evaluation, Skippable=false, Content=, TaskName=Performance Evaluation, Priority=1}]
Executing work item WorkItem 2 [name=Human Task, state=0, processInstanceId=1, parameters{ActorId=john, Comment=You need to perform an evaluation for employee UserId-12345, Skippable=false, Content=, TaskName=Performance Evaluation, Priority=1}]
Executing work item WorkItem 3 [name=Human Task, state=0, processInstanceId=1, parameters{ActorId=mary, Comment=You need to perform an evaluation for employee UserId-12345, Skippable=false, Content=, TaskName=Performance Evaluation, Priority=1}]
每个人的工作任务自动向下执行。

 

Junit测试评估流程3

评估流程的界面如下,同第一个是一样的

看一下示例程序

public void testEvaluationProcess3() throws Exception {

        KnowledgeBase kbase = createKnowledgeBase("BPMN2-EvaluationProcess3.bpmn2");

        StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);

        ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler());

        ksession.getWorkItemManager().registerWorkItemHandler("RegisterRequest", new SystemOutWorkItemHandler());

        Map<String, Object> params = new HashMap<String, Object>();

        params.put("employee", "john2");

        ProcessInstance processInstance = ksession.startProcess("Evaluation", params);

        assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);

    }

 执行之后看结果

Loading process BPMN2-EvaluationProcess3.bpmn2
Processing evaluation for employee john2
Executing work item WorkItem 1 [name=Human Task, state=0, processInstanceId=1, parameters{}]

可以看到只执行了Human Task,默认情况下注册进来的多个Handler如果不指定的话会默认执行Human Task,如果指定了就先指定执行的,如第一个示例再执行Human Task

 

Junit测试用户任务

接下来的示例是测试用户任务的示例,打开BPMN2-UserTask.bpmn2文件,流程定义文件如下

指定了ActorId为john

看测试用例程序

public void testUserTask() throws Exception {

        KnowledgeBase kbase = createKnowledgeBase("BPMN2-UserTask.bpmn2");

        StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);

        TestWorkItemHandler workItemHandler = new TestWorkItemHandler();

        ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);

        ProcessInstance processInstance = ksession.startProcess("UserTask");

        assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);

        ksession = restoreSession(ksession, true);

        WorkItem workItem = workItemHandler.getWorkItem();

        assertNotNull(workItem);

        assertEquals("john", workItem.getParameter("ActorId"));

        ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);

        assertProcessInstanceCompleted(processInstance.getId(), ksession);

    }

 先定义了一个TestWorkItemHandler,然后将这个handler定义为人工任务

然后判断节点状态是否是激活状态

然后判断流程中执行的是john在执行这个任务并且完成,最后判断流程完成

TestWorkItemHandler中的内容如下:

public static class TestWorkItemHandler implements WorkItemHandler {

         

        private List<WorkItem> workItems = new ArrayList<WorkItem>();

         

        public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {

            workItems.add(workItem);

        }

         

        public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {

        }

         

        public WorkItem getWorkItem() {

            System.out.println("执行test workitem");

            if (workItems.size() == 0) {

                return null;

            }

            if (workItems.size() == 1) {

                WorkItem result = workItems.get(0);

                this.workItems.clear();

                return result;

            } else {

                throw new IllegalArgumentException("More than one work item active");

            }

        }

         

        public List<WorkItem> getWorkItems() {

            List<WorkItem> result = new ArrayList<WorkItem>(workItems);

            workItems.clear();

            return result;

        }

         

    }

执行结果如下显示

Loading process BPMN2-UserTask.bpmn2
执行test workitem

 

Junit测试泳道

接下来的示例是一个测试泳道的例子,泳道的概念可以理解成一个角色或者一个组,就是相当于一组人

看流程图

定义了泳道“MyLane”

在hello上指定了ActorId是john,在Goodbye上没有指定人

看流程示例

public void testLane() throws Exception {

        KnowledgeBase kbase = createKnowledgeBase("BPMN2-Lane.bpmn2");

        StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);

        TestWorkItemHandler workItemHandler = new TestWorkItemHandler();

        ksession.getWorkItemManager().registerWorkItemHandler("Human Task",

                workItemHandler);

        ProcessInstance processInstance = ksession.startProcess("UserTask");

        assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);

        ksession = restoreSession(ksession, true);

        ksession.getWorkItemManager().registerWorkItemHandler("Human Task",

                workItemHandler);

        WorkItem workItem = workItemHandler.getWorkItem();

        assertNotNull(workItem);

        assertEquals("john", workItem.getParameter("ActorId"));

        Map<String, Object> results = new HashMap<String, Object>();

        results.put("ActorId", "mary");

        ksession.getWorkItemManager().completeWorkItem(workItem.getId(),

                results);

        ksession = restoreSession(ksession, true);

        ksession.getWorkItemManager().registerWorkItemHandler("Human Task",

                workItemHandler);

        workItem = workItemHandler.getWorkItem();

        assertNotNull(workItem);

        assertEquals("mary", workItem.getParameter("ActorId"));

        ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);

        assertProcessInstanceCompleted(processInstance.getId(), ksession);

    }

 定义任务并且执行,并且可以看到第一个用户任务的执行人是john,然后向里传入了第二个用户,mary

这个用户将被赋到泳道MyLane上

然后在看执行用户变成了mary,最后流程结束

看输出结果

Loading process BPMN2-Lane.bpmn2
执行test workitemWorkItem 1 [name=Human Task, state=0, processInstanceId=1, parameters{ActorId=john}]
执行test workitemWorkItem 2 [name=Human Task, state=0, processInstanceId=1, parameters{ActorId=mary}]

 

Junit测试异或流程

接下来的示例是测试gateway的异或流程,看一下示例

流程图如下

就是说只需要执行其中的一个流程,就可以让流程继续向下执行,这个示例是根据传入的变量X来看向哪执行

看测试代码

public void testExclusiveSplit() throws Exception {

        KnowledgeBase kbase = createKnowledgeBase("BPMN2-ExclusiveSplit.bpmn2");

        StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);

        ksession.getWorkItemManager().registerWorkItemHandler("Email",

                new SystemOutWorkItemHandler());

        Map<String, Object> params = new HashMap<String, Object>();

        params.put("x", "First");

        params.put("y", "Second");

        ProcessInstance processInstance = ksession.startProcess(

                "com.sample.test", params);

        assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);

    }

 执行测试流程结果如下

Loading process BPMN2-ExclusiveSplit.bpmn2
x=First
Executing work item WorkItem 1 [name=Email, state=0, processInstanceId=1, parameters{Body=This is an urgent email First, Subject=Urgent email !, To=you@mail.com, From=me@mail.com}]
即当流程执行到其中有一个后则流程自动向下执行

 

Junit测试异或流程高级1

接下来的示例是测试,看如下流程定义

这个是根据动态传入的看结点的值是不是a如果是a则走上边的流程,如果不是则走下边的流程

public void testExclusiveSplitXPathAdvanced() throws Exception {

        KnowledgeBase kbase = createKnowledgeBase("BPMN2-ExclusiveSplitXPath-advanced.bpmn2");

        StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);

        ksession.getWorkItemManager().registerWorkItemHandler("Email",

                new SystemOutWorkItemHandler());

        Map<String, Object> params = new HashMap<String, Object>();

        Document doc = DocumentBuilderFactory.newInstance()

                .newDocumentBuilder().newDocument();

        Element hi = doc.createElement("hi");

        Element ho = doc.createElement("ho");

        hi.appendChild(ho);

        Attr attr = doc.createAttribute("value");

        ho.setAttributeNode(attr);

        attr.setValue("a");

        params.put("x", hi);

        params.put("y", "Second");

        ProcessInstance processInstance = ksession.startProcess(

                "com.sample.test", params);

        assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);

    }

 根据传入的结点执行流程

执行结果如下

Loading process BPMN2-ExclusiveSplitXPath-advanced.bpmn2
x=[hi: null]
Executing work item WorkItem 1 [name=Email, state=0, processInstanceId=1, parameters{Body=you@mail.com, Subject=Urgent email !, To=you@mail.com, From=me@mail.com}]

 

Junit测试异或流程高级2

接下来的示例是测试,如下流程图

流程结点定义

还是根据值来判断流向,这次只指定x

看测试程序

public void testExclusiveSplitXPathAdvanced2() throws Exception {

        KnowledgeBase kbase = createKnowledgeBase("BPMN2-ExclusiveSplitXPath-advanced-vars-not-signaled.bpmn2");

        StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);

        ksession.getWorkItemManager().registerWorkItemHandler("Email",

                new SystemOutWorkItemHandler());

        Map<String, Object> params = new HashMap<String, Object>();

        Document doc = DocumentBuilderFactory.newInstance()

                .newDocumentBuilder().newDocument();

        Element hi = doc.createElement("hi");

        Element ho = doc.createElement("ho");

        hi.appendChild(ho);

        Attr attr = doc.createAttribute("value");

        ho.setAttributeNode(attr);

        attr.setValue("a");

        params.put("x", hi);

        params.put("y", "Second");

        ProcessInstance processInstance = ksession.startProcess(

                "com.sample.test", params);

        assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);

    }

 执行的流程结果

Loading process BPMN2-ExclusiveSplitXPath-advanced-vars-not-signaled.bpmn2
y=Second
Executing work item WorkItem 1 [name=Email, state=0, processInstanceId=1, parameters{Body=you@mail.com, Subject=Urgent email !, To=you@mail.com, From=me@mail.com}]

Junit测试异或流程带参数

与上边几个程序基本一致,直接上代码吧

public void testExclusiveSplitXPathAdvancedWithVars() throws Exception {

        KnowledgeBase kbase = createKnowledgeBase("BPMN2-ExclusiveSplitXPath-advanced-with-vars.bpmn2");

        StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);

        ksession.getWorkItemManager().registerWorkItemHandler("Email",

                new SystemOutWorkItemHandler());

        Map<String, Object> params = new HashMap<String, Object>();

        Document doc = DocumentBuilderFactory.newInstance()

                .newDocumentBuilder().newDocument();

        Element hi = doc.createElement("hi");

        Element ho = doc.createElement("ho");

        hi.appendChild(ho);

        Attr attr = doc.createAttribute("value");

        ho.setAttributeNode(attr);

        attr.setValue("a");

        params.put("x", hi);

        params.put("y", "Second");

        ProcessInstance processInstance = ksession.startProcess(

                "com.sample.test", params);

        assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);

    }

 执行结果

Loading process BPMN2-ExclusiveSplitXPath-advanced-with-vars.bpmn2
x=[hi: null]
Executing work item WorkItem 1 [name=Email, state=0, processInstanceId=1, parameters{Body=you@mail.com, Subject=Urgent email !, To=you@mail.com, From=me@mail.com}]

 

Junit测试异或流程优先和默认

下面的两个流程和上边基本一致只是细节上有些不同,一个是根据优先级流向一个是根据默认值流向

优先

public void testExclusiveSplitPriority() throws Exception {

        KnowledgeBase kbase = createKnowledgeBase("BPMN2-ExclusiveSplitPriority.bpmn2");

        StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);

        ksession.getWorkItemManager().registerWorkItemHandler("Email",

                new SystemOutWorkItemHandler());

        Map<String, Object> params = new HashMap<String, Object>();

        params.put("x", "First");

        params.put("y", "Second");

        ProcessInstance processInstance = ksession.startProcess(

                "com.sample.test", params);

        assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);

    }

 默认

public void testExclusiveSplitDefault() throws Exception {

        KnowledgeBase kbase = createKnowledgeBase("BPMN2-ExclusiveSplitDefault.bpmn2");

        StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);

        ksession.getWorkItemManager().registerWorkItemHandler("Email",

                new SystemOutWorkItemHandler());

        Map<String, Object> params = new HashMap<String, Object>();

        params.put("x", "NotFirst");

        params.put("y", "Second");

        ProcessInstance processInstance = ksession.startProcess(

                "com.sample.test", params);

        assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);

    }

 分别的执行结果如下:

优先

Loading process BPMN2-ExclusiveSplitPriority.bpmn2
y=Second
Executing work item WorkItem 1 [name=Email, state=0, processInstanceId=1, parameters{Body=This is an urgent email First, Subject=Urgent email !, To=you@mail.com, From=me@mail.com}]

默认

Loading process BPMN2-ExclusiveSplitDefault.bpmn2
y=Second
Executing work item WorkItem 1 [name=Email, state=0, processInstanceId=1, parameters{Body=This is an urgent email NotFirst, Subject=Urgent email !, To=you@mail.com, From=me@mail.com}]

时间: 2024-10-13 09:48:08

jbpm5.1介绍(7)的相关文章

jbpm5.1介绍(2)

快速开始  首先下载jBPM,http://sourceforge.net/projects/jbpm/files/ 可以有选择性的下载: bin:jBPM的二进制文件和它们的依赖包 src:核心模块的源码 gwt-console:jBPM的控制台,包括服务端和客户端 docs:文档 examples:一些jBPM的例子,可以导入到Eclipse installer: jBPM的安装,下载和安装一个jBPM的示例 installer-full:所有的包括demo,jar包等待的完整工程 一些有用

jbpm5.1介绍(4)

eclipse插件 需求Eclipse的3.6或更新版本 Graphiti框架,使用更新站点 可以直接下载更新Graphiti http://download.eclipse.org/graphiti/updates/0.7.1/ 更新bpmn2.0插件 http://codehoop.com/bpmn2 也可以直接从网站上下载后到本地更新,下载地址 https://github.com/droolsjbpm/bpmn2-eclipse-editor 使用eclipse创建hellojbpm 打

jbpm5.1介绍(3)

在您好的应用程序中使用一个新的流程 流程处理  (1)你需要建立一个知识库,其中包含过程定义 KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilder.add( ResourceFactory.newClassPathResource("MyProcess.bpmn2"),               ResourceType.BPMN2 ); 加入你的进程生成器(可以添加多个

jbpm5.1介绍(1)

介绍 jBPM是一个灵活的业务流程管理(BPM)套件.这使得业务分析师和开发人员之间的桥梁.传统的BPM引擎有一个重点,是有限的非技术人员. jBPM的有两个重点:它提供了一种方式,企业用户和开发人员喜欢它的流程管理功能. jBPM是什么 jBPM是以流程图为导向的工作流管理系统. jBPM的核心是一个轻量级,可扩展的工作流引擎在纯Java编写的,可让您执行业务流程,采用最新的BPMN 2.0规范.它可以运行在任何Java环境中,嵌入在您的应用程序或服务. 流程语言 jBPM以BPMN 2.0为

jbpm5.1介绍(5)

看几个jbpm5中带的示例程序吧,包括了很多我们在日常生活中的场景 循环示例 本示例是一个在外部传入的变量,通过传入的变量来判断循环次数的演示程序,看一下流程定义的内容 如图: 初始化的时候设置变量i的值为0,然后进入流程结点,选择的是XOR,就是异或的意思,在循环中输出变量i的值并且加1, 在后面的选择节点上判断变量i的值,如果小于count则循环继续,如果大于i那么就进行完成结点,输出结点中的内容. 运行程序LoopingExample,程序内容如下: public class Loopin

jbpm5.1介绍(12)

GWT是什么 如今,编写网络应用程序是一个单调乏味且易于出错的过程.开发人员可能要花费 90% 的时间来处理浏览器行话.此外,构建.重复使用以及维护大量 JavaScript 代码库和 AJAX 组件可能困难且不可靠.Google Web Toolkit (GWT) 允许开发人员使用 Java 编程语言快速构建和维护复杂而又高性能的 JavaScript 前端应用程序,从而降低了开发难度,尤其是与 Eclipse Google 插件结合使用时,优势更明显. google的官方说的很详细 http

jbpm5.1介绍(11)

Jbpm-gwt-console源码编译 从svn下载,svn的下载地址是 http://anonsvn.jboss.org/repos/soag/bpm-console/tags/bpm-console-2.1 下载之后使用mvn进行编译 当中遇到本很多问题,可以通过以下的网站进行jar包的查找和处理 https://repository.sonatype.org/index.html#welcome mvn仓库 http://www.java2s.com/ jar包查找 http://gre

jbpm5.1介绍(10)

Junit测试异常事件触发 下面的示例中测试在程序中触发异常事件的流程,流程如下 测试程序 public void testTimerBoundaryEventInterrupting() throws Exception { KnowledgeBase kbase = createKnowledgeBase("BPMN2-TimerBoundaryEventInterrupting.bpmn2"); StatefulKnowledgeSession ksession = create

jbpm5.1介绍(6)

Junit测试的mini流程helloworld 这是一个在demo中使用的Script Task做的简单示例,在执行到这个任务结点的时候自动输出"hello world" 打开BPMN2-MinimalProcess.bpmn2文件 流程如上图,流程开始的时候自动执行这个节点,然后在节点上执行输出的命令 看一看测试程序 public void testMinimalProcess() throws Exception {         KnowledgeBase kbase = c

jbpm5.1介绍(9)

Junit测试调用子流程 下面的示例中测试在程序中调用其它程序的子流程的示例,需要加载两个配置文件 和 需要指定属性 下面是调用的示例程序 public void testCallActivity() throws Exception {         System.out.println("Loading process BPMN2-CallActivity.bpmn2");         KnowledgeBuilder kbuilder = KnowledgeBuilderF