Activiti动态修改流程

时间:2019-09-03 来源:www.288mj.com

Activiti支持动态生成和修改的过程,即不使用图形编辑器,而是使用程序生成过程或修改现有过程。

例如,我们有以下过程,我们希望通过动态修改进程直接在User Task和End事件中插入服务任务。

i1.go2yd.comimage.php?url=0Md9qrdwZf

程序修改过程的副作用是新生成的元素在编辑器中不可见,因为没有布局信息,效果如下所示。

i1.go2yd.comimage.php?url=0Md9qrrmyY

当然,您也可以在修改过程时重新布局,这将影响原始布局。效果如下所示。

i1.go2yd.comimage.php?url=0Md9qrqsMF

具体代码如下

导入java.io.FileInputStream;

导入java.io.InputStream;

导入java.nio.file.Files;

导入java.nio.file.Paths;

导入javax.xml.stream.XMLInputFactory;

导入org.activiti.bpmn.model.Process;

导入org.activiti.bpmn.BpmnAutoLayout;

导入org.activiti.bpmn.converter.BpmnXMLConverter;

导入org.activiti.bpmn.model.BpmnModel;

导入org.activiti.bpmn.model.SequenceFlow;

导入org.activiti.bpmn.model.ServiceTask;

导入org.activiti.bpmn.model.UserTask;

公共课测试{

Public static void main(String args)抛出异常{

InputStream inputStream=new FileInputStream(

E: workspaceeclipseactivitisrcmainresourcesbefore.bpmn);

BpmnXMLConverter converter=new BpmnXMLConverter;

XMLInputFactory factory=XMLInputFactory.newInstance;

BpmnModel bpmnModel=converter.convertToBpmnModel(factory.createXMLStreamReader(inputStream));

ServiceTask hookTask=new ServiceTask;

hookTask.setName(截距);

hookTask.setId(钩);

process.addFlowElement(hookTask);

process.findFlowElementsOfType(UserTask.class)。forEach(task - {

task.getOutgoingFlows.forEach(flow - {

SequenceFlow toHook=new SequenceFlow;

toHook.setSourceRef(task.getId);

toHook.setTargetRef(钩);

process.addFlowElement(toHook);

SequenceFlow fromHook=new SequenceFlow;

fromHook.setSourceRef(钩);

fromHook.setTargetRef(flow.getTargetRef);

process.addFlowElement(fromHook);

process.removeFlowElement(flow.getId);

});

});

这行代码是重新布局

新的BpmnAutoLayout(bpmnModel).execute;

Files.write(Paths.get(E: workspaceeclipseactivitisrcmainresourcesafter.bpmn),

converter.convertToXML(bpmnModel));

}

}