views:

320

answers:

1

I created two "Hello World" processes to give the subProcess nodes a whirl. I'm having trouble getting output from the subProcess back to the main process. I'm hoping someone can enlighten me on what I'm doing wrong as I can't find any documentation or examples that shed light on why mine doesn't work.

In my main process, I have the following (note that I've snipped headers, footers, and the positional x, y, height, width attributes):

 ... snip...

 <header>
   <variables>
     <variable name="name" >
      <type name="org.drools.process.core.datatype.impl.type.StringDataType" />
       <value>World</value>
     </variable>
     <variable name="length" >
       <type name="org.drools.process.core.datatype.impl.type.IntegerDataType" />
       <value>0</value>
     </variable>
   </variables>
 </header>

 ... snip...

 <subProcess id="4" name="SubHello"
             processId="subhello" waitForCompletion="true" >
 <mapping type="in" from="name" to="name" />
 <mapping type="out" from="length" to="length" />
</subProcess>

 ... snip...

And here is the simple subhello SubProcess, which simply takes the input and prints it out, and then gets the input length to return it back out:

 ... snip...

 <header>
   <variables>
     <variable name="name" >
      <type name="org.drools.process.core.datatype.impl.type.StringDataType" />
       <value></value>
     </variable>
     <variable name="length" >
       <type name="org.drools.process.core.datatype.impl.type.IntegerDataType" />
       <value></value>
     </variable>
   </variables>
 </header>

 <nodes>
   <start id="1" name="Start" />
   <end id="2" name="End" />
   <actionNode id="3" name="Action" >
       <action type="expression" dialect="mvel" >
System.out.println(name + ", " + length + ", in SubProcess, before");
length = name.length;
System.out.println(length + ", in SubProcess, after");
       </action>
   </actionNode>
 </nodes>

 ... snip...

This is as per how I interpreted the doc and examples. The needed variables are declared on both the main process and the subprocess, then just use the subProcess in/out mapping elements to set the from and to attributes.

The problem is.... while name got passed in to the subProcess without issue, trying to get length back to the main process failed. The length in the subProcess was successfully modified. But on exit, length in the main process did not change.

What am I doing wrong? Pointers and explanations are much appreciated. Thanks.

+1  A: 

The problem is that your action does not change the length variable. It merely changes the local variable length inside your action. To change the value of the variable, use kcontext.setVariable("length", name.length());

You should also update to the latest Drools 5.1 M1 release, as that includes a fix for an issue with out mappings in case the subprocess is completely synchronous (as is the case in your example).

Kris Verlaenen

Kris Verlaenen
Thanks for the fix. I would just like to note that the docs need to be clearer that I actually have to call context.setVariable() in an Action node in order set the value of a process variable. The Action node doc doesn't mention that doing length = name.length in it just sets a local variable. From the SubFlow doc, I had simply assumed that configuring length = length would've been enough. But thanks again! I'm looking into event processing next.
aberrant80

related questions