Hi guys,
I am trying to use a way to share a variable at runtime between the rules but without success.
I tried to use a global, but I understand that this is for read-only and I tried to use a fact, but that is always null.
I need to access this "something" in LHS to based my flow on it.
Let me explain what I tried.
The global version
Java code:
a. Loading the rules
Navigator navigator = new Navigator();
workingMemory.setGlobal("navigator", navigator);
b. Navigator.java
private String compass;
public String getCompass(){
return this.compass;
}
public void setCompass(String compass){
this.compass = compass;
}
....
Drools rules
.......
global Navigator navigator;
rule "Rule1"
when
condition 1
then
navigator.setCompass("west");
end
rule "Rule2"
when
eval(navigator.getCompass()=="west")
then
System.out.println("Go west");
end
Looks like Rule2 never gets executed although "Rule1" is called.
Now the fact version:
Java code:
Loading the rules
Navigator navigator = new Navigator();
workingMemory.insert(navigator);
Drools rules
rule "Rule1"
when
$navigator: Navigator()
then
$navigator.setCompass("west");
end
rule "Rule2"
when
$navigator: Navigator(compass =="west")
then
System.out.println("Go west");
end
How can I share at the runtime a variable in LHS between rules?
Thanks,
Z