activiti除了可以通过流程设计器画流程图之外,其实也可以通过代码生成流程图。
activiti的流程图核心对象就是BpmnModel,只要生成了这个BpmnModel对象,流程图就OK了。
示例代码如下:
//创建bpmn模型
BpmnModel model = new BpmnModel();
Process process = new Process();
model.addProcess(process);
process.setId("my-process");
//创建bpmn元素
process.addFlowElement(createStartEvent());
process.addFlowElement(createUserTask("task1", "First task", "fred"));
process.addFlowElement(createUserTask("task2", "Second task", "john"));
process.addFlowElement(createEndEvent());
process.addFlowElement(createSequenceFlow("start", "task1"));
process.addFlowElement(createSequenceFlow("task1", "task2"));
process.addFlowElement(createSequenceFlow("task2", "end"));
// 2.生成BPMN自动布局
new BpmnAutoLayout(model).execute();
创建task
protected UserTask createUserTask(String id, String name, String assignee) {
UserTask userTask = new UserTask();
userTask.setName(name);
userTask.setId(id);
userTask.setAssignee(assignee);
return userTask;
}
//创建箭头
protected SequenceFlow createSequenceFlow(String from, String to) {
SequenceFlow flow = new SequenceFlow();
flow.setSourceRef(from);
flow.setTargetRef(to);
return flow;
}
protected StartEvent createStartEvent() {
StartEvent startEvent = new StartEvent();
startEvent.setId("start");
return startEvent;
}
protected EndEvent createEndEvent() {
EndEvent endEvent = new EndEvent();
endEvent.setId("end");
return endEvent;
}
上面的代码是上网找的,代码生成流程图其实最麻烦的就是节点的定位问题。
而代码new BpmnAutoLayout(model).execute();就帮我们自动布局了,这样只要生成想要的节点就可以生成bpmn对象了.