13 Replies Latest reply on Aug 12, 2011 6:16 AM by olddave

    Unclear on how to get a Process param into my WorkItem

    olddave Newbie

      Hi,

       

      I am defining my workflows using the BPMN2 Eclipse tool. When I run my WorkItem I need to get access to a parameter defined in the Process. I use this, which is not good because many Process may be running witht this same parameter in thei Stateful session.

       

      [code]

           AClass myContext = null;

           Iterator<ProcessInstance> it = ksession.getProcessInstances().iterator();

           while(it.hasNext()) {

                ProcessInstance procInst =it.next();

                Map<String,Object> contxt =  procInst.getProcess().getMetaData();

                if(contxt.containsValue("myContext")) {

                     myContext = (AClass) contxt.get("myContext");

            }

            if(myContext != null)

                service.run(aGrp, myContext);

            manager.completeWorkItem(workItem.getId(), null);

      [/code]

       

      salaboy pointed to his general examples for a similar question and I looked in all of them and could not see where the recommended way of accessing a Process parameter is shown.

       

      Thx.

       

      David

        • 1. Re: Unclear on how to get a Process param into my WorkItem
          Demian Calcaprina Master

          I think the idea is that you declare the process variables as a parameter of the node, and then use

          workItem.getParameter("...")

           

          Demian

          • 2. Re: Unclear on how to get a Process param into my WorkItem
            olddave Newbie

            I tried that. So in my Process parameters definition in the property sheet for the Process I entered

            myContext as an Object type and the fully qualified class name.

             

            Then in the "Parameter Mapping" section of my custom WorkItem I put the same "Parameter" name "myContext" and the fully qualified class name again in the "Variable" part.

             

            I get this

             

            Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to com.example.tdm.AClass

             

            Ed

            • 3. Re: Unclear on how to get a Process param into my WorkItem
              olddave Newbie

              I just realised you think this parameter is for this Workitem only. It is not, one of my tasks populates 2 of the fields in this parameter and it needs to be passed onto a workitem later in the Process which contributes values to 2 other fields in the parameter and so on.

               

              That is why it is at Process level and so on. I would have thought this would be quite a common requirement?

               

              Ed

              • 4. Re: Unclear on how to get a Process param into my WorkItem
                Demian Calcaprina Master

                Yes, you define the item type:

                <itemDefinition id="_myVariableItem" structureRef="com.MyVariableClass" />

                 

                Then, define the process variables:

                  <property id="myVariable" itemSubjectRef="_myVariableItem"/>

                 

                And then you declare it as inputs or outputs of the nodes that will needs it for input, needs to modify it (will be output), or both:

                 

                <task ...

                <dataInput id="_17_myVariableInput" name="myVariable" /> -> this is the name that will be used inside the work item handler.

                ..

                <inputSet>

                ...

                          <dataInputRefs>_17_myVariableInput</dataInputRefs>

                ...

                </inputSet>

                <dataInputAssociation>

                        <sourceRef>myVariable</sourceRef> -> this is the name of the process variable

                        <targetRef>_17_myVariableInput</targetRef>

                      </dataInputAssociation>

                 

                And for output:

                 

                <dataOutput id="_17_myVariableOutput" name="myVariable" /> -> name that will be passed in the key of the .completeWorkItem result map.

                ...

                <outputSet>

                          <dataOutputRefs>_17_myVariableOutput</dataOutputRefs>

                </outputSet>

                ...

                <dataOutputAssociation>

                        <sourceRef>_17_myVariableOutput</sourceRef>

                        <targetRef>myVariable</targetRef> -> here we assign the internal variable to process variable

                      </dataOutputAssociation>

                 

                I usually make it writing bpmn file, and not eclipse, but I think that should work with Parameter Mapping.

                 

                Regards,

                 

                Demian

                • 5. Re: Unclear on how to get a Process param into my WorkItem
                  olddave Newbie

                  Well it seems that this is where the problem lies. My users will never edit xml, they will use the property sheet of my workitem in the BPMN2 designer. Which looks like this

                   

                  propertysheet.png

                  As you can see there are no inputSet dataInputAssociation etc. Ignore the double period in the classname it is just for example.

                   

                  I made another post about this. Answers here assume that people are editing xml, whereas if you have a nice graphical designer the end users are likely to use that. But I have found no where that explains how you map input and output for parameters using the property sheet.

                   

                  Ed

                  • 6. Re: Unclear on how to get a Process param into my WorkItem
                    Demian Calcaprina Master

                    In Parameter Mapping, I think you have to put:

                    {param=processVar,...}

                    where param is the name of the parameter used inside the task and processVar is the process variable (you should define it in process properties sheet).

                     

                    And the same for Result Mapping.

                     

                    Hope it helps,

                     

                    Demian

                    • 7. Re: Unclear on how to get a Process param into my WorkItem
                      olddave Newbie

                      Yes, that is right, except as I said it does not work.

                       

                      I had Process Parameter "myContext" as an Object type and the fully qualified class name "com.example.tdm.workflow.AClass".

                      I had WorkItem Parameter Mapping  "myContext" and the fully qualified class name "com.example.tdm.workflow.AClass".

                       

                      When I enter my


                      public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {

                       

                       


                      AClass aObject = (AClass)workItem.getParameter("myContext");

                       

                      There is no aObject, it is null. So I figure I am doing something wrong.

                       

                      Thx.

                       

                      Ed

                      • 8. Re: Unclear on how to get a Process param into my WorkItem
                        Demian Calcaprina Master

                        But I think in the parameter mapping you don't have to put the full class name, just the param name and the process variable name.

                        • 9. Re: Unclear on how to get a Process param into my WorkItem
                          olddave Newbie

                          Tried that too.

                          {code}

                          AClass
                          myContext = (AClass)workItem.getParameter("myContext");

                          {/code}

                          When the fully qualified classname as the value I'd get this:

                           

                          Caused by: java.lang.ClassCastException: java.lang.Class cannot be cast to com.example.tdm.workflow.AClass

                           

                          When I simply used the name of the Process parameter it returned null.

                           

                          Maybe one of the developers could chip in at this point. The documentation that I have read simply does not cover this stuff, unless you want to edit xml

                           

                          Ed

                          • 10. Re: Unclear on how to get a Process param into my WorkItem
                            Esteban Aliverti Master

                            Can you share your process definition?  Also, if you debug your WorkItemHandler code, what is the value of workItem.getParameter("myContext")?

                            • 11. Re: Unclear on how to get a Process param into my WorkItem
                              olddave Newbie

                              I cannot really share the full xml at the moment. But here is the generate xml when I use Process parameter workflowContext with variable com.example.tdm.workflow.AClass and the WorkItem with a parameter workflowContext and variable workflowContext

                              {code}

                              ...

                                <itemDefinition id="_workflowContextItem" structureRef="com.example.tdm.workflow.AClass" />

                               

                               

                                <process processType="Private" isExecutable="true" id="_94ff85d0-5463-4b53-b5ce-3fa3c60eaf8d" name="Load_Stuff" >

                               

                               

                                  <!-- process variables -->

                                  <property id="workflowContext" itemSubjectRef="_workflowContextItem"/>

                               

                              ...

                               

                                  <task id="_3" name="Load_Test" tns:taskName="ETLGroup" >

                                    <ioSpecification>

                                      <dataInput id="_3_workflowContextInput" name="workflowContext" />

                                      <dataOutput id="_3_workflowContextOutput" name="workflowContext" />

                                      <inputSet>

                                        <dataInputRefs>_3_workflowContextInput</dataInputRefs>

                                      </inputSet>

                                      <outputSet>

                                        <dataOutputRefs>_3_workflowContextOutput</dataOutputRefs>

                                      </outputSet>

                                    </ioSpecification>

                                    <dataInputAssociation>

                                      <sourceRef>workflowContext</sourceRef>

                                      <targetRef>_3_workflowContextInput</targetRef>

                                    </dataInputAssociation>

                                    <dataOutputAssociation>

                                      <sourceRef>_3_workflowContextOutput</sourceRef>

                                      <targetRef>workflowContext</targetRef>

                                    </dataOutputAssociation>

                                  </task>

                              ...

                              {code}

                               

                              What I get in the WorkItem is a String value "workflowContext" not the Process parameter of the same name. This is using the org.drools.eclipse 5.2.0 plugin designer. I cannot see any association between the Process parameter and the WorkItem parameters.

                               

                              Ed

                              • 12. Re: Unclear on how to get a Process param into my WorkItem
                                Esteban Aliverti Master

                                I have prepared a simple test showing how to pass parameters to a Work Item. You can find it here: https://github.com/esteban-aliverti/JBPM-Samples/tree/master/Simple-WorkItem-Sample  Please take a look at it.

                                • 13. Re: Unclear on how to get a Process param into my WorkItem
                                  olddave Newbie

                                  Hi,

                                   

                                  This confirmed that there are no structural differences between what the org.drools.eclipse plugin produces and what you coded.

                                   

                                  I found the problem. I had accidently used the wrong case for the first character of parameter "workflowContext" for my workitem parameter in the UI. When you dismiss the dialog the Property sheet does NOT update until you tab or click out of that property, so it looked fine and I clicked on another Task node without realizing my mistake. I ran my tests with the wrong parameter name and had not checked seen the incorrect name in the xml. I fixed the name in the xml and re-ran my test, and it worked!

                                   

                                  Thank you

                                   

                                  Ed