JBPM之JPdl小叙

JPdl:jbpm流程定义文件,下面是一个简单的流程定义文件:

<?xml version="1.0" encoding="UTF-8"?>

<process name="Purchase order" xmlns="http://jbpm.org/4.4/jpdl">

  <start>
<transition to="Verify supplier" />
</start> <state name="Verify supplier">
<transition name="Supplier ok" to="Check supplier data" />
<transition name="Supplier not ok" to="Error" />
</state> <decision name="Check supplier data">
<transition name="nok" to="Error" />
<transition name="ok" to="Completed" />
</decision> <end name="Completed" /> <end name="Error" /> </process>

下面对JPdl中各个Element进行介绍

(1) process

attribution:1.name:这个流程的名字,是必须的

2.key

3.version

(2)start

1.name:这个activity的名称,开始activity是每个流程的第一个执行的activity所以可以没有name

[2]transition

(3)states

<state name="a">
<transition to="b" />
</state>

(4)带分支的states

<state name="wait for response">
<transition name="accept" to="submit document" />
<transition name="reject" to="try again" />
</state>
ProcessInstance processInstance = executionService
.startProcessInstanceByKey("StateChoice");
通过process的name来得到这个process的instance,然后使用这个instance来做一些操作
String executionId = processInstance
.findActiveExecutionIn("wait for response")
.getId();
得到name等于wwait for response的states
processInstance = executionService.signalExecutionById(executionId, "accept");
这个states接受一个外界为accept的信号,然后根据transition的name来选择转向下一个states

(5) decision:takes one path of many alternatives outgoings,只能转向其中的一个节点

 

<decision name="evaluate document">
<transition to="submit document">
<condition expr="#{content=="good"}" />
</transition>
<transition to="try again">
<condition expr="#{content=="not so good"}" />
</transition>
<transition to="give up" />
</decision>

de cision.transition.condit JBPM之JPdl小叙ion表示转向下一个节点的条件设置,expr="#{content =="good"}"

Map<String, Object> variables = new HashMap<String, Object>();
variables.put("content", "good");
ProcessInstance processInstance =
executionService.startProcessInstanceByKey("DecisionConditions", variables);

通过对varibles的设置可以控制流程下一个节点的转向

(6)decision expr

<decision name="evaluate document" expr="#{content}" >
<transition name="good" to="submit document" />
<transition name="bad" to="try again" />
<transition name="ugly" to="give up" />
</decision>

通过在执行流程的过程中设置content的值可以控制"evaluate document"下一个节点转向

Map<String, Object> variables = new HashMap<String, Object>();
variables.put("content", "good");
ProcessInstance processInstance =
executionService.startProcessInstanceByKey("DecisionExpression", variables);

(7)decisiton handler:通过自定义的程序来控制转向

 <decision name="evaluate document">
<handler class="org.jbpm.examples.decision.handler.ContentEvaluation" />
<transition name="good" to="submit document" />
<transition name="bad" to="try again" />
<transition name="ugly" to="give up" />
</decision>

接下来创建 ContentEvaluation这个类,实现DecisionHandler这个接口

public class ContentEvaluation implements DecisionHandler {

  public String decide(OpenExecution execution) {
String content = (String) execution.getVariable("content");
if (content.equals("you're great")) {
return "good";
}
if (content.equals("you gotta improve")) {
return "bad";
}
return "ugly";
}
}

(8)fork and join(同时)

1.fork activity allows a single path of execution to be of split into two or more branches which can execute

activities concurrently(所有这些分支会被同时执行)

2.join actvity allows multipart paths of execution to join into one path which execute when all path execute

success(join是由多个分支流入,一个活动流出,必须等到前面流入的分支都执行完成再会留到下一个节点,他们之间的关系相当于"与"的关系)

(9)end 流程的结束,一个流程可以定义多个不同的结束节点

<end name="ok"/>
<end name="bad request"/>
<end name="internal server error"/>
 <end name="ok" state="completed"/>  返回到名称为completed的states

(10)task:create a task for a person in the task component

<task name="review"
assignee="#{order.owner}">
你也可以assignee = "jeck",把这个task分配给了jeck这个人
<transition to="wait" />
</task>
public class Order implements Serializable {

  String owner;

  public Order(String owner) {
this.owner = owner;
} public String getOwner() {
return owner;
} public void setOwner(String owner) {
this.owner = owner;
}
}
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("order", new Order("johndoe")); //order相当于指向这个Order对象的一个引用,order.owner来获取Order对象的owner这个属性
ProcessInstance processInstance = executionService
.startProcessInstanceByKey("TaskAssignee", variables);
List<Task> taskList = taskService.findPersonalTasks("johndoe");
上面的代码可以根据某个人的Id来得到这个用户所有相关的task

(11)可以将一个task分配给一个用户组,只要用户在这个组里面就要去完成这个task,like follow code:

<task name="review"
candidate-groups="sales-dept"> <!-- candidate-groups 表示这个组的名称--> <transition to="wait" />
identityService.createGroup("sales-dept");   //创建一个用户组

identityService.createUser("johndoe", "johndoe", "John", "Doe");  //创建一个用户
identityService.createMembership("johndoe", "sales-dept"); //将这个用户和这个组相关联 identityService.createUser("joesmoe", "joesmoe", "Joe", "Smoe");
identityService.createMembership("joesmoe", "sales-dept");

(12)task assignment handler

<task name="review" g="96,16,127,52">
<assignment-handler class="org.jbpm.examples.task.assignmenthandler.AssignTask">
<field name="assignee">
<string value="johndoe" /> <!--设置默认值-->
</field>
</assignment-handler>

<transition to="wait" />
</task>
public class AssignTask implements AssignmentHandler {

  String assignee;

  public void assign(Assignable assignable, OpenExecution execution) {
assignable.setAssignee(assignee);
}
}

(13)task swimlanes:可以将多个task定义为一个swimlanes,然后将这个泳道分配给用户或者用户组
(1)name(requerid)

(2)assignee

(3)candidate-groups

(4)candidate-users

<swimlane name="sales representative"
candidate-groups="sales-dept" /><!--将这个泳道分配给sales-dept用户组-->
<start>
<transition to="enter order data" />
</start> <task name="enter order data"
swimlane="sales representative"> <!--表示这个task属于sales representative这个泳道--> <transition to="calculate quote"/>
</task> <task
name="calculate quote"
swimlane="sales representative">
 <!--表示这个task属于sales representative这个泳道-->
</task>

(14)java 和 script自动执行的活动

(15)mail发邮件的活动

上一篇:深度学习中的类别激活热图可视化


下一篇:九章算法系列(#2 Binary Search)-课堂笔记