paed01 / bpmn-engine

BPMN 2.0 execution engine. Open source javascript workflow engine.
MIT License
885 stars 167 forks source link

Get result from EndEvent block #25

Closed neonxp closed 5 years ago

neonxp commented 7 years ago

Hello! I have new noob question :) . How can I get result of script execution on end event? I mean that I want to set Input parameters on EndEvent block:

2017-04-20 11 58 17

And what I should to do to get all Input parameters and values of end block (variables.temp is example, can be anything else)?

listener.on('leave', (task) => {
    if (task.type === 'bpmn:EndEvent') {
      resolve({
        type: 'end',
        result: ... <-- How to get all input parameters and values of EndEvent block?
      })
      engine.stop();
    }
});
paed01 commented 7 years ago

I've seen this in the camunda modeler. But I'm a bit confused about the expected behavior of an endEvent input field. There is no output!? What is the purpose? Or maybe I've got the whole input/output behavior backwards? :P

We could off course decide what the expected behavior should be. The input could to saved to variables. It's a bit weird but doable. Do you have a suggestion?

neonxp commented 7 years ago

I'm confused too. For me best behavior looks like this:

listener.on('leave', (task, **message**) => {
    if (task.type === 'bpmn:EndEvent') {
      resolve({
        type: 'end',
        result: **message** // message = object of «input parameter : value» pairs
      })
      engine.stop();
    }
});

Just like service block ( (message, callback) => { console.log('Input is', message); callback(); } ) :)

Is it possible?

neonxp commented 7 years ago

P.S. Is it normal, that EndEvent block doesn't emit «start» event?

...
START bpmn:ParallelGateway
ENTER bpmn:ParallelGateway
ENTER bpmn:ServiceTask
START bpmn:ServiceTask
ENTER bpmn:ParallelGateway
LEAVE bpmn:ServiceTask
ENTER bpmn:Task
START bpmn:Task
ENTER bpmn:EndEvent <---
LEAVE bpmn:Task
LEAVE bpmn:ParallelGateway
LEAVE bpmn:EndEvent <---
paed01 commented 7 years ago

Actually it already works.

const BpmnEngine = require('bpmn-engine');
const {EventEmitter} = require('events');

const processXml = `
<?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:camunda="http://camunda.org/schema/1.0/bpmn">
  <process id="theProcess" isExecutable="true">
    <startEvent id="start" />
    <endEvent id="end">
      <extensionElements>
        <camunda:InputOutput>
          <camunda:inputParameter name="data">\${variables.statusCode}</camunda:inputParameter>
        </camunda:InputOutput>
      </extensionElements>
    </endEvent>
    <sequenceFlow id="flow1" sourceRef="start" targetRef="end" />
  </process>
</definitions>`;

const listener = new EventEmitter();

listener.on('end', (activity) => {
  if (activity.isEnd) {
    console.log(`${activity.type} <${activity.id}> input is`, activity.getInput());
  }
});

const engine = new BpmnEngine.Engine({
  source: processXml,
  moddleOptions: {
    camunda: require('camunda-bpmn-moddle/resources/camunda')
  }
});
engine.execute({
  listener,
  variables: {
    statusCode: 200
  }
}, (err) => {
  if (err) console.log(err)
});
engine.once('end', (def) => {
  console.log('completed', def.variables);
});

Regarding start event. I guess I found it weird to emit start on end. Do you need it?

paed01 commented 5 years ago

@neonxp after 18 months of refactoring I think I have resolved this issue. Can you make another attempt? See the referenced commit on how to test. Oh, btw, all activities now fire start :)

neonxp commented 5 years ago

@paed01 Hello! Thank you for update. But I'm not working at company where we used bpmn-engine now. I will try to ping my ex colleagues to test it.

paed01 commented 5 years ago

I see, well thank you for your contributions to this repo. I will close the issue.

So, a company is using it!? I’m honored. :)