<br>Hi, <br><br> We've gained some insight about what is happening under the hood for this kind of interaction between JBPM and Drools expert.<br><br> Imagine a knowledge session with this set of rules:<br><br>rule "new case"<br>
when <br> $object : Object(processed==false) from entry-point "my stream" <br> then<br> ProcessInstance processInstance=kcontext.getKnowledgeRuntime().createProcessInstance("my.Process", parameters);<br>
insert(processInstance);<br>end<br><br>rule "complete"<br> ruleflow-group "process complete"<br> no-loop true<br> when<br> $processInstance: WorkflowProcessInstance()<br> then<br>
System.out.println("Fired!" + $processInstance);<br>end<br><br>And a process that runs a time consuming task before reaching a bussinessruletask node that calls the "process complete" group of rules.<br>
<br>What is happening from Drool's expert point of view is the following sequence of events:<br><br> 1 - A fact is inserted that triggers the rule "new case". An Activation is inserted into the agenda.<br>
<br>
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.<br>
<br>
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.<br>
<br>
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.<br><br> 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.<br>
<br> 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.<br><br> 7 - So at least one rule gets evaluated for a process instance that didn't belong to it .. :(<br>
<br>Then , "We have added special logic to make sure that a variable processInstance of type WorkflowProcessInstance <br>
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.<br><br>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.<br>
<br><br>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 ).<br>
<br>Bug report? <br><br>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...<br><br><br><br clear="all">Alberto R. Galdo<br><a href="mailto:argaldo@gmail.com" target="_blank">argaldo@gmail.com</a><br>
<br>
<br><br><div class="gmail_quote">2012/2/13 Esteban Aliverti <span dir="ltr"><<a href="mailto:esteban.aliverti@gmail.com" target="_blank">esteban.aliverti@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
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.<div>
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).</div><div>Your rule then will look like this:</div>
<div>
<div><br></div><div><span>rule "process complete"</span><br><span> ruleflow-group "Complete task group"</span><span> </span></div></div><div><span>when</span></div><div> $ControlFact($id: processIntanceId)<br>
<span> $processInstance: WorkflowProcessInstance(instanceId == $id)</span><div><br><span>then</span><br><span> System.out.println("</span><span>processInstance.id " + $processInstance.getId());</span></div>
</div>
<div> retract ($id)<br><span>end</span></div><div><div><div><span><br></span></div><div><span>Best Regards,</span></div><div><div><br><div><br>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<br>
<br>Esteban Aliverti<br>
- Developer @ <a href="http://www.plugtree.com" target="_blank">http://www.plugtree.com </a><br>- Blog @ <a href="http://ilesteban.wordpress.com" target="_blank">http://ilesteban.wordpress.com</a><br>
<br><br><div class="gmail_quote">2012/2/13 Alberto R. Galdo <span dir="ltr"><<a href="mailto:argaldo@gmail.com" target="_blank">argaldo@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
We're using 5.2.0 final here.<br><br>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.<br>
<br>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?<div>
<div><br>
<br><br><br>2012/2/13 Esteban Aliverti <span dir="ltr"><<a href="mailto:esteban.aliverti@gmail.com" target="_blank">esteban.aliverti@gmail.com</a>></span><br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
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.<div>Maybe someone in the jbpm dev team can shed some light here.</div>
<div><br></div><div>Best Regards, <br clear="all"><br>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<br><br>Esteban Aliverti<br>- Developer @ <a href="http://www.plugtree.com" target="_blank">http://www.plugtree.com </a><br>- Blog @ <a href="http://ilesteban.wordpress.com" target="_blank">http://ilesteban.wordpress.com</a><br>
<br><br><div class="gmail_quote">2012/2/13 Alberto R. Galdo <span dir="ltr"><<a href="mailto:argaldo@gmail.com" target="_blank">argaldo@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div><div>
<br><span></span>According to the documentation:<br><br><blockquote style="margin:0pt 0pt 0pt 6.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">"Rule constraints do not have direct access to variables defined inside the process. <br>
It is however possible to refer to the current process instance inside a rule constraint, <br>by adding the process instance to the Working Memory and matching for the process instance in your rule constraint. <br>We have added special logic to make sure that a variable processInstance of type WorkflowProcessInstance <br>
will only match to the current process instance and not to other process instances in the Working Memory. <br>Note that you are however responsible yourself to insert the process instance into the session and, <br>possibly, to update it, for example, using Java code or an on-entry or on-exit or explicit action in your process. <br>
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:"<br></blockquote><br><br>This however does not seem to be true.<br>
<br>The first rule is to receive a stream of objects of type MyObject.<br>This rule starts an associated process.<br><br>The second rule will be called by the process (from bpmn).<br><br>Within the process there a task that invokes an an asynchronous service, ie,<br>
an implementation of WorkItemHandler that contains a Thread that will set call <br>manager.completeWorkItem(workItemId,results);<br>when 30s have passed).<br><br>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).<br>
<br>When the first object comes in a process is created and started (with process id=1)<br>The process passes from the start node to the callMyTask node, which invokes the slow WorkItemHandler references by MyTask <br>(which we have previously registered into session.getWorkItemManager() ). <br>
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)<br>and allows the engine to process other events.<br><br>5 seconds later we insert another MyObject into the stream.<br>
The first rule gets fired and created another process (with process id=2).<br>The process passes from the start node to the callMyTask node, which invoke our slow service.<br><br>We have thus two processes running in parallel.<br>
<br>When the first callMyTask node completes, the next node completeTask is invoked.<br><br>The rule "process complete" is matched as it belongs to the rule flow group "Complete task group".<br><br>At this point we observe that the rule is matched twice spitting out:<br>
<br> processInstance.id 2<br> processInstance.id 1<br>.<br>So the rule has matched both process instances that are in working memory and not just the one (with process id 1) <br>that called the "process complete" rule.<br>
<br><br><br>The rule file<br>-------------<br><br>rule "New case"<br> when <br> $myobject : MyObject(processed==false) from entry-point "myobject stream" <br> then<br> ProcessInstance processInstance=kcontext.getKnowledgeRuntime().createProcessInstance("com.mycompany.process.MyProcess", parameters);<br>
insert(processInstance);<br> kcontext.getKnowledgeRuntime().startProcessInstance(processInstance.getId());<br> modify ($myobject){<br> setProcessed(true)<br> }<br> end<br> <br>
rule "process complete"<br> ruleflow-group "Complete task group"<br> no-loop true<br> when<br> $processInstance: WorkflowProcessInstance()<br> then<br> System.out.println("processInstance.id " + $processInstance.getId());<br>
end<br> <br><br>The BPMN xml<br>------------<br> <br><?xml version="1.0" encoding="UTF-8"?> <br><definitions id="Definition"<br> targetNamespace="<a href="http://www.jboss.org/drools" target="_blank">http://www.jboss.org/drools</a>"<br>
typeLanguage="<a href="http://www.java.com/javaTypes" target="_blank">http://www.java.com/javaTypes</a>"<br> expressionLanguage="<a href="http://www.mvel.org/2.0" target="_blank">http://www.mvel.org/2.0</a>"<br>
xmlns="<a href="http://www.omg.org/spec/BPMN/20100524/MODEL" target="_blank">http://www.omg.org/spec/BPMN/20100524/MODEL</a>"<br> xmlns:xsi="<a href="http://www.w3.org/2001/XMLSchema-instance" target="_blank">http://www.w3.org/2001/XMLSchema-instance</a>"<br>
xsi:schemaLocation="<a href="http://www.omg.org/spec/BPMN/20100524/MODEL" target="_blank">http://www.omg.org/spec/BPMN/20100524/MODEL</a> BPMN20.xsd"<br> xmlns:g="<a href="http://www.jboss.org/drools/flow/gpd" target="_blank">http://www.jboss.org/drools/flow/gpd</a>"<br>
xmlns:bpmndi="<a href="http://www.omg.org/spec/BPMN/20100524/DI" target="_blank">http://www.omg.org/spec/BPMN/20100524/DI</a>"<br> xmlns:dc="<a href="http://www.omg.org/spec/DD/20100524/DC" target="_blank">http://www.omg.org/spec/DD/20100524/DC</a>"<br>
xmlns:di="<a href="http://www.omg.org/spec/DD/20100524/DI" target="_blank">http://www.omg.org/spec/DD/20100524/DI</a>"<br> xmlns:tns="<a href="http://www.jboss.org/drools" target="_blank">http://www.jboss.org/drools</a>"><br>
<br> <process processType="Private" isExecutable="true" id="com.mycompany.process.MyProcess" name="my process" ><br><br> <!-- process variables --><br> <br>
<!-- nodes --><br> <startEvent id="start" name="StartProcess" /><br> <br> <task id="callMyTask" name="call my task" tns:taskName="MyTask" ><br>
<ioSpecification><br> </ioSpecification><br> </task><br> <br> <br> <businessRuleTask id="completeTask" name="Complete slow task" g:ruleFlowGroup="Complete task group" ><br>
</businessRuleTask><br> <br> <endEvent id="end" name="EndProcess" /><br><br> <!-- connections --><br> <sequenceFlow id="start-callMyTask" sourceRef="start" targetRef="callMyTask" /><br>
<sequenceFlow id="callMyTask-completeTask" sourceRef="callMyTask" targetRef="completeTask" /><br> <sequenceFlow id="completeTask-end" sourceRef="completeTask" targetRef="end" /><br>
</process><br></definitions><br><br><br>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?<br>
<br><br clear="all">Greets,<br>
<br></div></div>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br></div>
<br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br>
</div></div><br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br></div></div></div>
</div></div><br>_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org" target="_blank">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
<br></blockquote></div><br>