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是錯誤的,不可以使用的。
驗證還是很有必要使用的,因為流程部署的時候,我們最好驗證一次,沒有問題在部署。