paed01 / bpmn-engine

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

multiInstance Subprocess just take the first item #18

Closed rkmax closed 7 years ago

rkmax commented 7 years ago

I have a process with a sub process, i dont know the reason why jus take the fist item of the collection and then ends the loop

## Workflow

```xml mongodb://localhost/jane_document documents name Urgent like ${result[0]} SequenceFlow_1hfi6b8 SequenceFlow_1h7sr5l SequenceFlow_new_records SequenceFlow_no_records ${variables.item} SequenceFlow_new_records SequenceFlow_1urzsh8 ECN Alert (new Date()) SequenceFlow_0eqxdn8 SequenceFlow_127mgc7 sendSMS ECN Alert - Tata Project 12 +19374018810 +16504229484 SequenceFlow_127mgc7 SequenceFlow_0mktnsy this.variables.item[this.variables.index].userId this.variables.item[this.variables.index] ${result[0]} SequenceFlow_1j72bta SequenceFlow_0eqxdn8 SequenceFlow_0mktnsy SequenceFlow_1j72bta SequenceFlow_no_records SequenceFlow_1h7sr5l PT10S 0]]> SequenceFlow_1hfi6b8 this.variables.records.length === 0 SequenceFlow_1urzsh8 Wait 10 Seconds ```

## Code

const Bpmn = require('bpmn-engine');
const sourceXml = "..." // workflow;
const engine = new Bpmn.Engine({
  source       : sourceXml,
  moddleOptions: {
    camunda: require('camunda-bpmn-moddle/resources/camunda')
  }
});

engine.execute({
  services : {
    dummy: (executionContext, serviceCallback) => {
      const result = executionContext['dummy'] || ['dummy'];

      serviceCallback(null, result);
    }
  },
  variables: {}
}, (err, instance) => {
  if (err) return console.log(err);
  instance.once('end', () => {
    console.log('instance process');
  });
});
paed01 commented 7 years ago

Hi @rkmax,

Long time no see. :)

Well, I think i figured it out. It was a combination of square brackets and bad readme. Have a look at the comments:

<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="1.6.0">
  <bpmn:process id="Process_1" name="ECN_Alert" isExecutable="true">
    <bpmn:serviceTask id="checkKeyword" name="Check records with keyword">
      <bpmn:extensionElements>
        <camunda:properties>
          <camunda:property name="service" value="dummy" />
        </camunda:properties>
        <camunda:inputOutput>
          <camunda:inputParameter name="url">mongodb://localhost/jane_document</camunda:inputParameter>
          <camunda:inputParameter name="table">documents</camunda:inputParameter>
          <camunda:inputParameter name="keywords">
            <camunda:map>
              <camunda:entry key="column">name</camunda:entry>
              <camunda:entry key="keyword">Urgent</camunda:entry>
              <camunda:entry key="operator">like</camunda:entry>
            </camunda:map>
          </camunda:inputParameter>
          <camunda:inputParameter name="dummy">

            <!--  One too many bracket pairs resulted in an array with one array item -->

            <camunda:script scriptFormat="javascript"><![CDATA[ [
  { userId: 20, username: 'rkmax', email: 'rkmax@example', mobileNr: '+123456789' },
  { userId: 35, username: 'paed01', email: 'paed01@example', mobileNr: '+123456799' },
  { userId: 46, username: 'acabrera04', email: 'acabrera@example', mobileNr: '+123456709' }
]
]]></camunda:script>
          </camunda:inputParameter>
          <camunda:outputParameter name="records">${result[0]}</camunda:outputParameter>
        </camunda:inputOutput>
      </bpmn:extensionElements>
      <bpmn:incoming>SequenceFlow_1hfi6b8</bpmn:incoming>
      <bpmn:incoming>SequenceFlow_1h7sr5l</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_new_records</bpmn:outgoing>
      <bpmn:outgoing>SequenceFlow_no_records</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:sequenceFlow id="SequenceFlow_1hfi6b8" sourceRef="StartEvent_1" targetRef="checkKeyword" />
    <bpmn:subProcess id="SubProcess_0ejq0my" name="ECN Alert">
      <bpmn:extensionElements>
        <camunda:inputOutput>

          <!-- I'll have a look why this is undefined? It shouldn't be... -->

          <camunda:inputParameter name="itema">${variables.item}</camunda:inputParameter>
        </camunda:inputOutput>
      </bpmn:extensionElements>
      <bpmn:incoming>SequenceFlow_new_records</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_1urzsh8</bpmn:outgoing>
      <bpmn:multiInstanceLoopCharacteristics isSequential="true" camunda:collection="${variables.records}" />
      <bpmn:sequenceFlow id="SequenceFlow_127mgc7" sourceRef="createEvent" targetRef="sendAlert" />
      <bpmn:serviceTask id="createEvent" name="Create Alert">
        <bpmn:extensionElements>
          <camunda:inputOutput>
            <camunda:inputParameter name="name">ECN Alert</camunda:inputParameter>
            <camunda:inputParameter name="subject">
              <camunda:script scriptFormat="javascript"><![CDATA['ECN Alert from ' + this.variables.customer.username]]></camunda:script>
            </camunda:inputParameter>
            <camunda:inputParameter name="timestamp">
              <camunda:script scriptFormat="javascript">(new Date())</camunda:script>
            </camunda:inputParameter>
          </camunda:inputOutput>
          <camunda:properties>
            <camunda:property name="service" value="dummy" />
          </camunda:properties>
        </bpmn:extensionElements>
        <bpmn:incoming>SequenceFlow_0eqxdn8</bpmn:incoming>
        <bpmn:outgoing>SequenceFlow_127mgc7</bpmn:outgoing>
      </bpmn:serviceTask>
      <bpmn:serviceTask id="sendAlert" name="Send message">
        <bpmn:extensionElements>
          <camunda:field name="service">
            <camunda:string>sendSMS</camunda:string>
          </camunda:field>
          <camunda:inputOutput>
            <camunda:inputParameter name="body">ECN Alert - Tata Project 12</camunda:inputParameter>
            <camunda:inputParameter name="from">+19374018810</camunda:inputParameter>
            <camunda:inputParameter name="to">+16504229484</camunda:inputParameter>
          </camunda:inputOutput>
          <camunda:properties>
            <camunda:property name="service" value="dummy" />
          </camunda:properties>
        </bpmn:extensionElements>
        <bpmn:incoming>SequenceFlow_127mgc7</bpmn:incoming>
        <bpmn:outgoing>SequenceFlow_0mktnsy</bpmn:outgoing>
      </bpmn:serviceTask>
      <bpmn:sequenceFlow id="SequenceFlow_0eqxdn8" sourceRef="customerInfo" targetRef="createEvent" />
      <bpmn:serviceTask id="customerInfo" name="Get User data">
        <bpmn:extensionElements>
          <camunda:inputOutput>

              <!-- No need for index since item is set by loop --->

            <camunda:inputParameter name="user_id">
              <camunda:script scriptFormat="javascript">this.variables.item.userId</camunda:script>
            </camunda:inputParameter>
            <camunda:inputParameter name="dummy">
              <camunda:script scriptFormat="javascript">this.variables.item</camunda:script>
            </camunda:inputParameter>

            <camunda:outputParameter name="customer">${result[0]}</camunda:outputParameter>
          </camunda:inputOutput>
          <camunda:properties>
            <camunda:property name="service" value="dummy" />
          </camunda:properties>
        </bpmn:extensionElements>
        <bpmn:incoming>SequenceFlow_1j72bta</bpmn:incoming>
        <bpmn:outgoing>SequenceFlow_0eqxdn8</bpmn:outgoing>
      </bpmn:serviceTask>
      <bpmn:endEvent id="EndEvent_066banc">
        <bpmn:incoming>SequenceFlow_0mktnsy</bpmn:incoming>
      </bpmn:endEvent>
      <bpmn:sequenceFlow id="SequenceFlow_0mktnsy" sourceRef="sendAlert" targetRef="EndEvent_066banc" />
      <bpmn:startEvent id="StartEvent_1u4gdfz">
        <bpmn:outgoing>SequenceFlow_1j72bta</bpmn:outgoing>
      </bpmn:startEvent>
      <bpmn:sequenceFlow id="SequenceFlow_1j72bta" sourceRef="StartEvent_1u4gdfz" targetRef="customerInfo" />
    </bpmn:subProcess>
    <bpmn:intermediateCatchEvent id="IntermediateThrowEvent_1b0httn">
      <bpmn:incoming>SequenceFlow_no_records</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_1h7sr5l</bpmn:outgoing>
      <bpmn:timerEventDefinition>
        <bpmn:timeDuration xsi:type="bpmn:tFormalExpression">PT10S</bpmn:timeDuration>
      </bpmn:timerEventDefinition>
    </bpmn:intermediateCatchEvent>
    <bpmn:sequenceFlow id="SequenceFlow_1h7sr5l" sourceRef="IntermediateThrowEvent_1b0httn" targetRef="checkKeyword" />
    <bpmn:sequenceFlow id="SequenceFlow_new_records" name="New records&#10;&#10;" sourceRef="checkKeyword" targetRef="SubProcess_0ejq0my">
      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression" language="javascript"><![CDATA[this.variables.records.length > 0]]></bpmn:conditionExpression>
    </bpmn:sequenceFlow>
    <bpmn:startEvent id="StartEvent_1">
      <bpmn:outgoing>SequenceFlow_1hfi6b8</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:sequenceFlow id="SequenceFlow_no_records" name="No records&#10;&#10;" sourceRef="checkKeyword" targetRef="IntermediateThrowEvent_1b0httn">
      <bpmn:conditionExpression xsi:type="bpmn:tFormalExpression" language="javascript">this.variables.records.length === 0</bpmn:conditionExpression>
    </bpmn:sequenceFlow>
    <bpmn:endEvent id="EndEvent_1c4pv9o">
      <bpmn:incoming>SequenceFlow_1urzsh8</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:sequenceFlow id="SequenceFlow_1urzsh8" sourceRef="SubProcess_0ejq0my" targetRef="EndEvent_1c4pv9o" />
    <bpmn:textAnnotation id="TextAnnotation_1pra9kr">    <bpmn:text>Wait 10 Seconds</bpmn:text>
</bpmn:textAnnotation>
    <bpmn:association id="Association_1doxeqj" sourceRef="IntermediateThrowEvent_1b0httn" targetRef="TextAnnotation_1pra9kr" />
  </bpmn:process>
rkmax commented 7 years ago

@paed01 Thank you so much! all is working now, btw

 <camunda:inputParameter name="itema">${variables.item}</camunda:inputParameter>

I think it's ok that is undefined because in that level item doesn't exist yet