-
1. Re: Accessing process variables in rule-task
leon_ May 31, 2011 5:18 AM (in response to leon_)I found this related thread http://drools.46999.n3.nabble.com/Rule-tasks-and-gwt-console-td1711764.html, but the answer does not make things much clearer for me...
Where do I have to change these things, in my ProcessTest.java?
-
2. Re: Accessing process variables in rule-task
eaa May 31, 2011 12:48 PM (in response to leon_)I'm not sure if I understood the question, but if what you need is to access process variables inside your rules, you can use something like this:
rule A ruleflow-group "ABC" when $process: WorkflowProcessInstance() then Map variable = (Map)((WorkflowProcessInstance)kcontext.getKnowledgeRuntime().getProcessInstance($process.getId())).getVariable("variableName"); end
-
3. Re: Accessing process variables in rule-task
leon_ Jun 1, 2011 4:52 AM (in response to eaa)Thanks very much for your reply Esteban.
I am sorry for being a bit unclear. It is my writing style, and I am working on it to define problems in a more clear way :-)
So, if I have a variable "monkey" in my BPMN process (which is added to the map btw), where the user has given a value to in an .ftl form, let's say "chimp". And I want a rule to fire based on the value "chimp" I would write something like:
rule A
ruleflow-group "chimps_are_nice"
when
(Map)((WorkflowProcessInstance) kcontext.getKnowledgeRuntime().getProcessInstance(WorkflowProcessInstance(.getId())).getVariable("monkey") == "chimp";
then
(Map) (WorkflowProcessInstance (kcontext.getKnowledgeRuntime().getProcessInstance(WorkflowProcessInstance().getId()).setVariable("character") = "nice";
I realise I can store the path into a variable to shorten the code. But this is what it would come down to I guess?
I am retrieving the variable from the map, is this neccesary or can it be ommitted?I assum to set variables I use setVariable, am I right?
-
4. Re: Accessing process variables in rule-task
eaa Jun 1, 2011 7:23 AM (in response to leon_)The LHS of your rule is wrong. I think you need to use an eval() if you want to invoke methods that contains parameters.
I also remembered another way to get process variables in the RHS, but I don't remember if it has some caevats:
when
$processInstance: WorkflowProcessInstance()
then
String monkey = (String)$processInstance.getVariable("monkey"); //This will return "chimp"
$processInstance.setVariable("character","nice");
end
-
5. Re: Accessing process variables in rule-task
leon_ Jun 1, 2011 8:13 AM (in response to eaa)Thnx again Esteban!
It seems that the class WorkflowProcessInstance cannot use the methods get/setVariable. Googling on it brings me e.g. to the v3 of jbpm showing that only ContextInstance can use get and set.
Which brings me to the question if "kcontext" can be used in a DRL file without imports. If not, what should be imported and must kcontext be declared in the drl file?
I still have not found a way to access, eval and change process variables in a drl file...
-
6. Re: Accessing process variables in rule-task
leon_ Jun 1, 2011 3:05 PM (in response to leon_)The following rules do not seem to fire. I have included ksession.fireUntilHalt() in my procestest. Do I miss anything else?!
rule "regel_overheid"
ruleflow-group "RegelsRechtwijzer"
when
$proces : WorkflowProcessInstance()
$partij : String() from (String)$proces.getVariable("conflictpartij");
eval($partij == "overheid");
then
((WorkflowProcessInstance)kcontext.getKnowledgeRuntime().getProcessInstance($proces.getId())).setVariable("conflictpartij", "kantonrechter");
end
rule "debug_regel"
ruleflow-group "RegelsRechtwijzer"
#include attributes such as "salience" here...
when
$proces : WorkflowProcessInstance();
then
$proces.setVariable("conflictoplossing", "Kantonrechter");
update($proces);
end
-
7. Re: Accessing process variables in rule-task
eaa Jun 5, 2011 7:26 PM (in response to leon_)Sorry, I forgot to mention that the WorkflowProcessInstance object is not a fact by default. You need to insert it before the Rule Node is executed. One way to do this is to add a Script Task node that executes:
kcontext.getKnowledgeRuntime().insert(kcontext.getProcessInstance());
-
8. Re: Accessing process variables in rule-task
leon_ Jun 7, 2011 5:37 AM (in response to eaa)So you did, it seems :-) It is working now, I cannot thank you enough for sharing this information.
I think ppl with similar problems will solve it reading this thread. If not, I am happy to share the information!
-
9. Re: Accessing process variables in rule-task
subbass00 Mar 19, 2012 8:37 PM (in response to eaa)Hello Esteban,
I have the same problem what leon has. I have added to my fisrt human task at the entry action the kcontext.getKnowledgeRuntime().insert(kcontext.getProcessInstance()); script. My rule currently look like this :
import com.sample.User;
import org.drools.runtime.process.WorkflowProcessInstance
rule "AgeCheck3"
ruleflow-group "AgeCheck"
when
processInstance : WorkflowProcessInstance()
age: Integer() from (Integer)((WorkflowProcessInstance)processInstance).getVariable("Age");
eval(age < 18);
then
processInstance.setVariable("Name", new String("Denied"));
System.out.println(age);
end
If I am removing the age declaration with eval(true) the rule is fired and the name is set to Denied. The compiler accept the declaration, no errors during te running phase, but the rule is not fired.
Can you help me plese? I am doing my master thesis and this is also a small part of it.
Thank you in advance.
Kind regards, Hunor
-
10. Re: Accessing process variables in rule-task
thomas.setiabudi Oct 9, 2012 2:39 AM (in response to leon_)Hi,
Is it possible to do this from guvnor?
How do you import org.drools.runtime.process.WorkflowProcessInstance in the rule file?
Note: I am using JBPM 5.3
Regards,
Thomas Setiabudi
-
11. Re: Accessing process variables in rule-task
madhu.tech92 Mar 18, 2019 5:34 AM (in response to leon_)I'm inserting process into Working memory from Script node but I'm getting NLP When ever I'm trying setVariable from my rule, I've fallowed the step as said above.
this is my Rule and I'm using JBPM 5.5.0.
package com.pft.workflow.core.bpmengine;
import org.jbpm.workflow.instance.WorkflowProcessInstance;
rule "Rule_002"
ruleflow-group "GlobalConf002"
dialect "java"
no-loop true
when
$proces : WorkflowProcessInstance();
then
System.out.println("starting GlobalConf002 Group...........");
$proces.setVariable("gbl_input","OK");
$proces.setVariable("gbl_input2","OK");
System.out.println("GlobalConf002 Group...........");
retract($proces);
end
Thanks in advance.
-
12. Re: Accessing process variables in rule-task
haddad.raouf Mar 30, 2019 7:03 AM (in response to madhu.tech92)Another solution is to insert process instance from DefaultProcessEventListener this way
@Override
public void beforeNodeTriggered(ProcessNodeTriggeredEvent event) {
if (event.getNodeInstance() instanceof StartNodeInstance) {
event.getKieRuntime().insert(event.getProcessInstance());
}
}
it should insert a WorkflowProcessInstance Fact into working memory,
Even more if you want to insert only once the process instance you can check if the fact exist or not before insertring, because $processInstance: WorkflowProcessInstance() will trigger the rule as many time as you have process instances in working memory.
Another way also to prevent multiple trigger is to condition the $processInstance: WorkflowProcessInstance() with the id of your process