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/MODELBPMN2...
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,