The situation is easy. I created a rules file:
package org.domain.rules;
dialect "mvel"
import eu.ohim.fsp.core.configuration.domain.xsd.Section;
global java.lang.String sectionName;
rule "rule 1"
salience 1000
when
Section($name : nameOfTheSection)
eval(sectionName == null)
then
System.out.println("Section: " + $name+ "("+$name.length()+")");
System.out.println("Section Name: " + sectionName + "("+sectionName.length()+")");
System.out.println("Mark Details: " + sectionName.equals(null));
end
And before firing the rules I added the Section object with a valid coreName and the globals:
public void fireInserted(Section section1) {
kstateful.insert(section1);
kstateful.setGlobal("sectionName", new String("markudetails"));
kstateful.fireAllRules();
}
The result is:
Section: markudetails(12)
Section Name: markudetails(12)
Mark Details: false
QUESTION: How can it be possible? in when part is null and in then part is not null!!!
Global vars are not a part of the knowledge base, but a separate channel to push some context into the rule execution. It is not appropriate to use them in a
whenclause. The exact reason why it wasnullin your case may be hard to trace, since rule activation is completely decoupled from rule execution. The variable may simply not be bound atwhenclause evaluation time, but is bound atthenclause execution time.To summarize: don’t use globals in a
whenclause, that’s not what they are for.Your problem has an easy general solution: you can insert a configuration object into the knowledge. That object can have your desired “sectionName” property which you will then find easy to test in a
when.As an aside, it is meaningless to test for
object.equals(null)— this can never producetrue. There is also no need to usenew String("markudetails"). Instead use just"markudetails".