[rules-users] rule group activation by concurrent jbpm processes

Alberto R. Galdo argaldo at gmail.com
Tue Feb 14 10:18:03 EST 2012


Hi,

  We've gained some insight about what is happening under the hood for this
kind of interaction between JBPM and Drools expert.

  Imagine a knowledge session with this set of rules:

rule "new case"
    when
        $object : Object(processed==false) from entry-point "my stream"

    then
        ProcessInstance
processInstance=kcontext.getKnowledgeRuntime().createProcessInstance("my.Process",
parameters);
        insert(processInstance);
end

rule "complete"
    ruleflow-group "process complete"
    no-loop true
    when
        $processInstance: WorkflowProcessInstance()
    then
        System.out.println("Fired!" + $processInstance);
end

And a process that runs a time consuming task before reaching a
bussinessruletask node that calls the "process complete" group of rules.

What is happening from Drool's expert point of view is the following
sequence of events:

    1 - A fact is inserted that triggers the rule "new case". An Activation
is inserted into the agenda.

    2 - As this is running in a fireUntilHalt loop then, almost
inmediatelly, the previous activation is fired and it's consquence is
evaluated leading to the insertion of the process instance as a fact in the
knowledge session. The process is now executing.

    3 - This very new fact triggers the activation of the rule "complete"
( as it's only condition is that a processinstance exists ). It's
consequence gets evaluated and as it belongs to a group of rules, an
Activation is added to the ruleFlowGroup inside the Agenda. Nothing happens
thereafter.

    4 - Another fact of the same type arrives and steps 1,2 and 3 execute
for the new fact. Now, the agenda has two Activations stored in the
ruleFlowGroup.

    5 - At this time, the task in the first process ends, the
businessruletask gets executed and JBPM calls "activateRuleFlowGroup" on
the Agenda and the rule flow group gets activated.

    6 - Inmediatelly the group of rules fires two activations ( both queued
at process fact insertion when the process was started ), when only one
process ended.

    7 - So at least one rule gets evaluated for a process instance that
didn't belong to it .. :(

Then , "We have added special logic to make sure that a variable
processInstance of type WorkflowProcessInstance
will only match to the current process instance and not to other process
instances in the Working Memory." doesn't hold to be true, as our rule is
fired *before* it's process asks Drools to do so.

We've tested this behaviour in Drools 5.1.0, 5.2.0 and 5.3.0. All fail to
fulfill the requirements in the documentation.


What's worring us is that there seems to be no difference between actions
in the agenda as they're instances of AgendaItem class wich has no means to
discriminate between Activations ( at least there's no clear path to guess
the attached process instance ).

Bug report?

If this is a bug ( or lack of use case ) we will be more than happy to
submit a patch for it, just will need some hints...



Alberto R. Galdo
argaldo at gmail.com



2012/2/13 Esteban Aliverti <esteban.aliverti at gmail.com>

> The behavior you described is how it is working in 5.3 (and it seems 5.2
> too). The rule is fired once per process instance you have in your working
> memory no matter if the others instances are, are not yet or even has
> already been in that Rule Task Node. Again, I'm not sure if this is a
> deliberated feature or a bug, but according to the documentation, it is a
> bug.
> One workaround could be that the process inserts a control fact containing
> its own id right before the Task Node is ejecuted (this could be done in  a
> listener or in the on-entry action property of the node).
> Your rule then will look like this:
>
> rule "process complete"
>     ruleflow-group "Complete task group"
> when
>         $ControlFact($id: processIntanceId)
>         $processInstance: WorkflowProcessInstance(instanceId == $id)
>
> then
>         System.out.println("processInstance.id " +
> $processInstance.getId());
>          retract ($id)
> end
>
> Best Regards,
>
>
> XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
>
> Esteban Aliverti
> - Developer @ http://www.plugtree.com
> - Blog @ http://ilesteban.wordpress.com
>
>
> 2012/2/13 Alberto R. Galdo <argaldo at gmail.com>
>
>> We're using 5.2.0 final here.
>>
>> What we are also observing is that whenever a process instance reaches a
>> businessruletask node the rule gets fired a number of times equal to the
>> number of instantiated processes even if any of those processes didn't
>> reach the bussinessruletask node.
>>
>> Does this means that whenever a businessruletask is reached in any of the
>> current process instance, the rule gets fired for *all* the instances of
>> the process and subsequent businessruletask nodes won't fire anything?
>>
>>
>>
>>
>> 2012/2/13 Esteban Aliverti <esteban.aliverti at gmail.com>
>>
>>> I had some tests working fine in jbpm 5.1 but failing in 5.3 because of
>>> this same reason. I'm not sure if this is this is a regression bug or if
>>> there is a deliberated change in the behavior.
>>> Maybe someone in the jbpm dev team can shed some light here.
>>>
>>> Best Regards,
>>>
>>> XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
>>>
>>> Esteban Aliverti
>>> - Developer @ http://www.plugtree.com
>>> - Blog @ http://ilesteban.wordpress.com
>>>
>>>
>>> 2012/2/13 Alberto R. Galdo <argaldo at gmail.com>
>>>
>>>>
>>>> According to the documentation:
>>>>
>>>> "Rule constraints do not have direct access to variables defined inside
>>>>> the process.
>>>>> It is however possible to refer to the current process instance inside
>>>>> a rule constraint,
>>>>> by adding the process instance to the Working Memory and matching for
>>>>> the process instance in your rule constraint.
>>>>> We have added special logic to make sure that a variable
>>>>> processInstance of type WorkflowProcessInstance
>>>>> will only match to the current process instance and not to other
>>>>> process instances in the Working Memory.
>>>>> Note that you are however responsible yourself to insert the process
>>>>> instance into the session and,
>>>>> possibly, to update it, for example, using Java code or an on-entry or
>>>>> on-exit or explicit action in your process.
>>>>> The following example of a rule constraint will search for a person
>>>>> with the same name as the value stored in the variable "name" of the
>>>>> process:"
>>>>>
>>>>
>>>>
>>>> This however does not seem to be true.
>>>>
>>>> The first rule is to receive a stream of objects of type MyObject.
>>>> This rule starts an associated process.
>>>>
>>>> The second rule will be called by the process (from bpmn).
>>>>
>>>> Within the process there a task that invokes an an asynchronous
>>>> service, ie,
>>>> an implementation of WorkItemHandler that contains a Thread that will
>>>> set call
>>>> manager.completeWorkItem(workItemId,results);
>>>> when 30s have passed).
>>>>
>>>> With this setup we can observe what happens when 2 MyObject instances
>>>> come into the first rule separated by a short period of time (say 5
>>>> seconds).
>>>>
>>>> When the first object comes in a process is created and started (with
>>>> process id=1)
>>>> The process passes from the start node to the callMyTask node, which
>>>> invokes the slow WorkItemHandler references by MyTask
>>>> (which we have previously registered into session.getWorkItemManager()
>>>> ).
>>>> Because the WorkItemHandler has a thread that waits 30s before
>>>> completing the work item, it just sits there doing nothing (so far
>>>> everything is ok)
>>>> and allows the engine to process other events.
>>>>
>>>> 5 seconds later we insert another MyObject into the stream.
>>>> The first rule gets fired and created another process (with process
>>>> id=2).
>>>> The process passes from the start node to the callMyTask node, which
>>>> invoke our slow service.
>>>>
>>>> We have thus two processes running in parallel.
>>>>
>>>> When the first callMyTask node completes, the next node completeTask is
>>>> invoked.
>>>>
>>>> The rule "process complete" is matched as it belongs to the rule flow
>>>> group "Complete task group".
>>>>
>>>> At this point we observe that the rule is matched twice spitting out:
>>>>
>>>>     processInstance.id 2
>>>>     processInstance.id 1
>>>> .
>>>> So the rule has matched both process instances that are in working
>>>> memory and not just the one (with process id 1)
>>>> that called the "process complete" rule.
>>>>
>>>>
>>>>
>>>> The rule file
>>>> -------------
>>>>
>>>> rule "New case"
>>>>     when
>>>>         $myobject : MyObject(processed==false) from entry-point
>>>> "myobject stream"
>>>>     then
>>>>         ProcessInstance
>>>> processInstance=kcontext.getKnowledgeRuntime().createProcessInstance("com.mycompany.process.MyProcess",
>>>> parameters);
>>>>         insert(processInstance);
>>>>
>>>> kcontext.getKnowledgeRuntime().startProcessInstance(processInstance.getId());
>>>>         modify ($myobject){
>>>>             setProcessed(true)
>>>>         }
>>>>     end
>>>>
>>>> rule "process complete"
>>>>     ruleflow-group "Complete task group"
>>>>     no-loop true
>>>>     when
>>>>         $processInstance: WorkflowProcessInstance()
>>>>     then
>>>>         System.out.println("processInstance.id " +
>>>> $processInstance.getId());
>>>>     end
>>>>
>>>>
>>>> The BPMN xml
>>>> ------------
>>>>
>>>> <?xml version="1.0" encoding="UTF-8"?>
>>>> <definitions id="Definition"
>>>>              targetNamespace="http://www.jboss.org/drools"
>>>>              typeLanguage="http://www.java.com/javaTypes"
>>>>              expressionLanguage="http://www.mvel.org/2.0"
>>>>              xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
>>>>              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>>>>              xsi:schemaLocation="
>>>> http://www.omg.org/spec/BPMN/20100524/MODEL BPMN20.xsd"
>>>>              xmlns:g="http://www.jboss.org/drools/flow/gpd"
>>>>              xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
>>>>              xmlns:dc="http://www.omg.org/spec/DD/20100524/DC"
>>>>              xmlns:di="http://www.omg.org/spec/DD/20100524/DI"
>>>>              xmlns:tns="http://www.jboss.org/drools">
>>>>
>>>>     <process processType="Private" isExecutable="true"
>>>> id="com.mycompany.process.MyProcess" name="my process" >
>>>>
>>>>         <!-- process variables -->
>>>>
>>>>         <!-- nodes -->
>>>>         <startEvent id="start" name="StartProcess" />
>>>>
>>>>         <task id="callMyTask" name="call my task" tns:taskName="MyTask"
>>>> >
>>>>             <ioSpecification>
>>>>             </ioSpecification>
>>>>         </task>
>>>>
>>>>
>>>>         <businessRuleTask id="completeTask" name="Complete slow task"
>>>> g:ruleFlowGroup="Complete task group" >
>>>>         </businessRuleTask>
>>>>
>>>>         <endEvent id="end" name="EndProcess" />
>>>>
>>>>         <!-- connections -->
>>>>         <sequenceFlow id="start-callMyTask" sourceRef="start"
>>>> targetRef="callMyTask" />
>>>>         <sequenceFlow id="callMyTask-completeTask"
>>>> sourceRef="callMyTask" targetRef="completeTask" />
>>>>         <sequenceFlow id="completeTask-end" sourceRef="completeTask"
>>>> targetRef="end" />
>>>>     </process>
>>>> </definitions>
>>>>
>>>>
>>>> What's wrong with this?  Isn't WorkflowProcessInstance() supposed to be
>>>> attached to the calling ProcessInstance?  Is there another way of getting
>>>> the group of rules fire only for the processinstance that called it?
>>>>
>>>>
>>>> Greets,
>>>>
>>>> _______________________________________________
>>>> rules-users mailing list
>>>> rules-users at lists.jboss.org
>>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>>
>>>>
>>>
>>> _______________________________________________
>>> rules-users mailing list
>>> rules-users at lists.jboss.org
>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>
>>>
>>
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>>
>>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20120214/75b5469f/attachment.html 


More information about the rules-users mailing list