BPMN2.0 automatically starts the simulation process

Idea: BPMN process simulation starts, mainly by generating tokens and starting token simulation

The opening of the process simulation requires a key tool: bpmn-js-token-simulation, which needs to be downloaded first

Note: The BPMN2.0 process simulation tool version is different, and the startup method is also different. You need to read the source code or official documents by yourself. This article is mainly about the simulation tool of version 0.10.0.

 "bpmn-js-token-simulation": "^0.10.0",

Introduce in your BPMN components

// 模拟流转流程
import tokenSimulation from "bpmn-js-token-simulation";

Load it into a BPMN object:

computed: { 
  additionalModules() {
      const Modules = [];
      ....
      // 模拟流转模块
      if (this.simulation) {
        Modules.push(tokenSimulation);
      }
      ...
   return Modules;
  },
}
mounted() {
    /*初始化Bpmn*/
    this.initBpmnModeler();
},
methods: {
    initBpmnModeler() {
      this.bpmnModeler = new BpmnModeler({
        ...
        additionalModules: this.additionalModules,
        ...
      });
       //此处开启模拟
       this.processSimulation();
    },

Let's start the simulation during initialization, this is the first step

Step 1: Turn on the simulation

this.simulation && this.bpmnModeler.get("toggleMode").toggleMode();
this.simulationStatus = !this.simulationStatus;

Step Two: Create a Token and Execute

Before the token is created, we must have an awareness: Where is the starting node of BPMN? How many starting nodes are there? Which node do we need to turn on?

Therefore, our first step is to get all the start nodes, here we need to use the tool: elementRegistry, through which we can get all the element objects

// 获取BPMN模拟器中的ElementRegistry对象
const elementRegistry = this.bpmnModeler.get('elementRegistry');
// 获取所有的开始事件节点
const startEvents = elementRegistry.filter(element => {
   return element.type === 'bpmn:StartEvent';
});

By filtering, we get all the start nodes

With the start task node, tokens can be created and activated in a targeted manner.

To create a token, you need to use the tool tokenSimulationBehavior, which provides behavior settings for BPMN simulation execution.

By printing we can see that he has a lot of processing functions.

 Here we see the bpmn:StartEvent event, let's see what's in it

 nice, we can see that there is a generate function on his prototype, which can be used to generate tokens.

How should the generate function be used?

Let's take a look at his source code:

StartEventHandler.prototype.generate = function(context) {
  var self = this;

  var element = context.element,
      parentProcessInstanceId = context.parentProcessInstanceId;

  var outgoingSequenceFlows = element.outgoing.filter(function(outgoing) {
    return is(outgoing, 'bpmn:SequenceFlow');
  });

  // create new process instance
  var parent = element.parent,
      processInstanceId = this._processInstances.create(parent, parentProcessInstanceId);

  outgoingSequenceFlows.forEach(function(connection) {
    self._animation.createAnimation(connection, processInstanceId, function() {
      self._eventBus.fire(CONSUME_TOKEN_EVENT, {
        element: connection.target,
        processInstanceId: processInstanceId
      });
    });
  });

  if (is(element.parent, 'bpmn:SubProcess')) {
    return;
  }

  var startEvents = this._elementRegistry.filter(function(element) {
    return is(element, 'bpmn:StartEvent');
  });

  this._eventBus.fire(UPDATE_ELEMENTS_EVENT, {
    elements: startEvents
  });
};

Here we can see that he needs a context,

  • element: the current start event object
  • parentProcessInstanceId: ID of the parent process instance (process definition)

 Then you can know, we can create a token by passing in these two objects

OK, in this case, we can define objects in a targeted manner

pick a start event

 let context={
  element:startEvents[0],
  parentProcessInstanceId:startEvents[0].parent.id
}

After the context is passed in, the start task of this node is successfully created

const tokenSimulationBehavior  =  this.bpmnModeler.get('tokenSimulationBehavior');
tokenSimulationBehavior.handlers["bpmn:StartEvent"].generate(context)

The final effect is as follows: 

Supongo que te gusta

Origin blog.csdn.net/weixin_42078172/article/details/131035574
Recomendado
Clasificación