activiti5/6 系列之--BpmnModel使用
BpmnModel物件,是activiti動態部署中很重要的一個物件,如果BpmnModel物件不能深入的理解,那可能如果自己需要開發一套流程設計器,使用bpmn-js使用前端或者C/S展現流程流轉而不是使用工作流引擎,就顯得力不從心。例如,web設計器:
當activiti web設計器設計的時候,儲存格式是自定義的json物件,那現在問題來了,我們怎麼把我們自己的json格式轉化為標準的bpmn需要的xml檔案呢?這一點很重要?所以這也是本節課需要重點講解的地方,大家實際開發可以舉一反三。靈活的運用到專案中。
1.1.1. BpmnModel使用
因為平時我們在使用的時候,展示流程圖沒有使用是預設的流程生成的這種方式,所以這裡座標資訊,暫時不演示,主要演示節點等的核心功能。
1.1.1.1. eclipse繪製流程
為了方便演示,這裡我們先在eclipse中繪製一個簡單的流程。具體的流程圖如下:
流程圖的xml檔案如下:直接用文字開啟bpmn檔案即可:
<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test"> <process id="process1" isExecutable="true"> <startEvent id="start1shareniu" name="start1shareniu"></startEvent> <sequenceFlow id="starttouserTask" name="starttouserTask" sourceRef="start1shareniu" targetRef="userTask1shareniu"></sequenceFlow> <userTask id="userTask1shareniu" name="userTask1shareniu"></userTask> <sequenceFlow id="userTasktoend" name="userTasktoend" sourceRef="userTask1shareniu" targetRef="endEventshareniu"></sequenceFlow> <endEvent id="endEventshareniu" name="endEventshareniu"></endEvent> </process> <bpmndi:BPMNDiagram id="BPMNDiagram_process1"> <bpmndi:BPMNPlane bpmnElement="process1" id="BPMNPlane_process1"> <bpmndi:BPMNShape bpmnElement="start1shareniu" id="BPMNShape_start1shareniu"> <omgdc:Bounds height="35.0" width="35.0" x="70.0" y="150.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="userTask1shareniu" id="BPMNShape_userTask1shareniu"> <omgdc:Bounds height="60.0" width="100.0" x="180.0" y="110.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="endEventshareniu" id="BPMNShape_endEventshareniu"> <omgdc:Bounds height="35.0" width="35.0" x="380.0" y="76.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="starttouserTask" id="BPMNEdge_starttouserTask"> <omgdi:waypoint x="87.0" y="150.0"></omgdi:waypoint> <omgdi:waypoint x="100.0" y="139.0"></omgdi:waypoint> <omgdi:waypoint x="180.0" y="140.0"></omgdi:waypoint> <bpmndi:BPMNLabel> <omgdc:Bounds height="14.0" width="100.0" x="87.0" y="150.0"></omgdc:Bounds> </bpmndi:BPMNLabel> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="userTasktoend" id="BPMNEdge_userTasktoend"> <omgdi:waypoint x="280.0" y="140.0"></omgdi:waypoint> <omgdi:waypoint x="324.0" y="129.0"></omgdi:waypoint> <omgdi:waypoint x="324.0" y="93.0"></omgdi:waypoint> <omgdi:waypoint x="380.0" y="93.0"></omgdi:waypoint> <bpmndi:BPMNLabel> <omgdc:Bounds height="14.0" width="100.0" x="414.0" y="126.0"></omgdc:Bounds> </bpmndi:BPMNLabel> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions>
1.1.1.2. 自己生成
下面的程式碼,就是生成這個bpmnmodel 核心程式碼,程式碼如下所示:
//例項化BpmnModel物件 BpmnModel bpmnModel=new BpmnModel(); //開始節點的屬性 StartEvent startEvent=new StartEvent(); startEvent.setId("start1shareniu"); startEvent.setName("start1shareniu"); //普通的UserTask節點 UserTask userTask=new UserTask(); userTask.setId("userTask1shareniu"); userTask.setName("userTask1shareniu"); //結束節點屬性 EndEvent endEvent=new EndEvent(); endEvent.setId("endEventshareniu"); endEvent.setName("endEventshareniu"); //連線資訊 List<SequenceFlow> sequenceFlows=new ArrayList<SequenceFlow>(); List<SequenceFlow> toEnd=new ArrayList<SequenceFlow>(); SequenceFlow s1=new SequenceFlow(); s1.setId("starttouserTask"); s1.setName("starttouserTask"); s1.setSourceRef("start1shareniu"); s1.setTargetRef("userTask1shareniu"); sequenceFlows.add(s1); SequenceFlow s2=new SequenceFlow(); s2.setId("userTasktoend"); s2.setName("userTasktoend"); s2.setSourceRef("userTask1shareniu"); s2.setTargetRef("endEventshareniu"); toEnd.add(s2); startEvent.setOutgoingFlows(sequenceFlows); userTask.setOutgoingFlows(toEnd); userTask.setIncomingFlows(sequenceFlows); endEvent.setIncomingFlows(toEnd); //Process物件 Process process=new Process(); process.setId("process1"); process.addFlowElement(startEvent); process.addFlowElement(s1); process.addFlowElement(userTask); process.addFlowElement(s2); process.addFlowElement(endEvent); bpmnModel.addProcess(process);
上面的程式碼,我們已經寫好了bpmnmodel繪製的流程,那我們怎麼知道對還是不對呢?下面就開始將我們的bpmnmodel物件轉化為標準的xml檔案看一下。
1.1.2. BpmnModel轉化xml
將上面的物件轉化為標準的xml程式碼如下所示:
//bpmnModel 轉換為標準的bpmn xml檔案
BpmnXMLConverter bpmnXMLConverter=new BpmnXMLConverter();
byte[] convertToXML = bpmnXMLConverter.convertToXML(bpmnModel);
String bytes=new String(convertToXML);
System.out.println(bytes);
執行程式,看一下程式的輸出如下:
<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test"> <process id="process1" isExecutable="true"> <startEvent id="start1shareniu" name="start1shareniu"></startEvent> <sequenceFlow id="starttouserTask" name="starttouserTask" sourceRef="start1shareniu" targetRef="userTask1shareniu"></sequenceFlow> <userTask id="userTask1shareniu" name="userTask1shareniu"></userTask> <sequenceFlow id="userTasktoend" name="userTasktoend" sourceRef="userTask1shareniu" targetRef="endEventshareniu"></sequenceFlow> <endEvent id="endEventshareniu" name="endEventshareniu"></endEvent> </process> <bpmndi:BPMNDiagram id="BPMNDiagram_process1"> <bpmndi:BPMNPlane bpmnElement="process1" id="BPMNPlane_process1"></bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions>
我們看到轉化的xml檔案,對比eclipse繪製流程的xml,除了座標沒有,其他的都是正確的。那我們怎麼驗證我們生成的xml是正確的呢?因為轉化成功,也不一定可以使用的。接下來看一下bpmnmodel如何驗證。
1.1.3. BpmnModel驗證
驗證的方法程式碼如下所示:
//驗證bpmnModel 是否是正確的bpmn xml檔案 ProcessValidatorFactory processValidatorFactory=new ProcessValidatorFactory(); ProcessValidator defaultProcessValidator = processValidatorFactory.createDefaultProcessValidator(); //驗證失敗資訊的封裝ValidationError List<ValidationError> validate = defaultProcessValidator.validate(bpmnModel); System.out.println(validate.size());
需要說明的是:ValidationError封裝的是驗證資訊,如果size為0說明,bpmnmodel正確,大於0,說明自定義的bpmnmodel是錯誤的,不可以使用的。
驗證還是很有必要使用的,因為流程部署的時候,我們最好驗證一次,沒有問題在部署。