开发者

Accessing the process instance from Rule Tasks in JBPM 5

开发者 https://www.devze.com 2023-03-26 16:23 出处:网络
The short version: How do I get JBPM5 Rule Nodes to use a DRL file which reads and updates process variables?

The short version: How do I get JBPM5 Rule Nodes to use a DRL file which reads and updates process variables?

The long version: I have a process definition, being run under JBPM5. The start of this process looks something like this:

[Start] ---> [Rule Node] ---> [Gateway (Diverge)] ... etc

The gateway uses constraints on a variable named 'isValid'.

My Rule Node is pointing to the RuleFlowGroup 'validate', which contains only one rule:

rule "Example validation rule"
    ruleflow-group "validate"

    when
        processInstance : WorkflowProcessInstance()
    then
        processInstance.setVariable("isValid", new Boolean(false));
end

So, by my logic, if this is getting correctly processed then the gateway should always follow the "false" path.

In my Java code, I have something like the following:

KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("myProcess.bpmn"), ResourceType.BPMN2);
kbuilder.add(ResourceFactory.newClassPathResource("myRules.drl"), ResourceType.DRL);

KnowledgeBase            kbase    = kbuilder.newKnowledgeBase();
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

new Thread(new Runnable()
   {
      public void run()
      {
         ksession.fireUntilHalt();
      }
   }).start();

// start a new process instance
Map<String, Object> params = new HashMap<String, Object>();
params.put("isValid", true);
ksession.startProcess("test.processesdefinition.myProcess", params);

I can confirm the following:

  • The drl file is getting loaded into working memory, because when I put syntax errors in the file then I get errors.
  • If I include a value for "isValid" in the Java params map, the process only ever follows the path specified by Java, apparently ignoring the drools rule.
  • If I take the "isValid" parameter out of the params map, I get a runtime error.

From this I assume that the final "setVariable" line in the rule is either not executing, or is updating the wrong thing.

I think my issue is related to this statement in the official documentation:

Rule constraints do not have direct access to variables defined inside the process. It is however possible to refer to t开发者_开发技巧he 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.

However I cannot figure out how to do what is described here. How do I add the process instance into working memory in a way that would make it accessible to this first Rule Node? Rule Nodes do not seem to support on-entry behaviors, and I can't add it to the Java code because the process could very easily complete execution of the rules node before the working memory has been updated to include the process.


As you mentioned, there are several options to inserting the process instance into the working memory: - inserting it after calling startProcess() - using an action script to insert it (using "insert(kcontext.getProcessInstance()")

If calling startProcess() might already have gone over the rule task (which is probably the case in your example), and you don't have another node in front of your rule task where you could just use an on-entry/exit script to do this (so that's is hidden), I would recommend using an explicit script task before your rule task to do this.

Kris

0

精彩评论

暂无评论...
验证码 换一张
取 消