title: How to merge variables from parallel flows in Activiti?
body: <p>Currently I have a sub-process which uses fork/join mechanism to create parallel flows. Lest assume that there are two flows: <strong>A, B</strong>. Each of that flows takes as input variables complex object <strong>CONTEXT</strong>. Also each of that flows make some calculation and updates <strong>CONTEXT</strong> inside. As a output each of flows return updated <strong>CONTEXT</strong>. The problem here is that in <strong>Join</strong> point, last result of <strong>CONTEXT</strong> overrides previous one. Lets assume that flow <strong>A</strong> fill be finished first with result <strong>CONTEXT_1</strong> and flow <strong>B</strong> will return <strong>CONTEXT_2</strong>. So final result will be <strong>CONTEXT_2</strong> and all changes from flow <strong>A</strong> will be lost.</p>
  
  <p><strong>The question here is - how to merge results from two flows?</strong>
  <a href="https://i.stack.imgur.com/mVMJz.png" rel="noreferrer"><img src="https://i.stack.imgur.com/mVMJz.png" alt="enter image description here"></a></p>
  
  <p><strong>UPDATE:</strong>
  From my observations passed variable (CONTEXT) from SuperProcess to SubProcess are copied(CONTEXT') and after subProcess is finished, new value of passed variable(CONTEXT') will take place of original (CONTEXT).</p>
  
  <p>In the example below I mean that all passed variables have the same name.</p>
  
  <p>Example:</p>
  
  <ol>
  <li>SuperProcess <strong>P1</strong> (Variable: <strong>CONTEXT</strong>) calls SubProcess <strong>P2</strong>(variables are passed by copy);</li>
  <li><p>SubProcess <strong>P2</strong> (Variable: <strong>CONTEXT'</strong>) creates two parallel flows(Tasks) <strong>A</strong>, <strong>B</strong>(variables are passed by copy);</p></li>
  <li><p><strong>A</strong> Task (Variable: <strong>CONTEXT_1</strong>) updates value of variable, finishes execution and returns variable;</p>
  
  <p>3.1. <strong>CONTEXT_1</strong> takes place of variable <strong>CONTEXT'</strong> so <strong>P2</strong> can see only this new value as names of this variables the same;</p></li>
  <li><p>Meanwhile <strong>B</strong> Task (Variable: <strong>CONTEXT_2</strong>) is still working and after some time updates variable, finishes execution and returns variable;</p>
  
  <p>4.1. <strong>CONTEXT_2</strong> takes place of variable <strong>CONTEXT_1</strong> so <strong>P2</strong> can see only this new value as names of this variables the same;  </p></li>
  <li>SubProcess <strong>P2</strong> (Variable: <strong>CONTEXT_2</strong>) finish the execution and returns new veriable to SuperProcess.
  Result -> <strong>CONTEXT_1</strong> is lossed.</li>
  </ol>
  
  <p>My aim scenario:</p>
  
  <ol>
  <li>SuperProcess <strong>P1</strong> (Variable: <strong>CONTEXT</strong>) calls SubProcess <strong>P2</strong>(variables are passed by copy);</li>
  <li><p>SubProcess <strong>P2</strong> (Variable: <strong>CONTEXT'</strong>) creates two parallel flows(Tasks) <strong>A</strong>, <strong>B</strong>(variables are passed by copy);</p></li>
  <li><p><strong>A</strong> Task (Variable: <strong>CONTEXT_1</strong>) updates value of variable, finishes execution and returns variable;</p>
  
  <p>3.1. <strong>CONTEXT_1</strong> and <strong>CONTEXT</strong> are merged into <strong>CONTEXT_M1</strong>, in other words, only new changes of <strong>CONTEXT_1</strong> will be applied to <strong>CONTEXT</strong>.</p></li>
  <li><p>Meanwhile <strong>B</strong> Task (Variable: <strong>CONTEXT_2</strong>) is still working and after some time updates variable, finishes execution and returns variable;</p>
  
  <p>4.1. <strong>CONTEXT_2</strong> and <strong>CONTEXT_M1</strong> are merged  into <strong>CONTEXT_M2</strong>, in other words, only new changes of <strong>CONTEXT_2</strong> will be applied to <strong>CONTEXT_M1</strong> so previous update will be not lost; </p></li>
  <li>SubProcess <strong>P2</strong> (Variable: <strong>CONTEXT_M2</strong>) finish the execution and returns new veriable to SuperProcess.
  Result -> <strong>CONTEXT_M2</strong>. All changes are saved.</li>
  </ol>