I'm passing a string into a drools rule with Drools 7:
public static void main(String[] args) {
log("Starting");
KieContainer kc = KieServices.Factory.get().getKieClasspathContainer();
KieSession ksession = kc.newKieSession("RulesKS");
ksession.insert( "asdf" );
ksession.fireAllRules();
ksession.dispose();
log("Done");
}
Note that I'm setting a "fact" with a value of asdf
to run the rule against. Here is my rule file:
package drools7
function boolean ifContains(String target, String searchFor) {
return target.indexOf(searchFor) != -1;
}
rule "First Rule"
salience 90
when
eval(ifContains("target", "et"))
then
System.out.println("Does contain");
end
Now, when I run my main method I see:
Does contain
printed to stdout, but how would I check in the drools rule file the actual fact that I inserted?
How does this work? I want to see if the fact contains "et", not a scalar value?
I would recommend you to read Drools official documentation and/or some Drools book first.
For your particular case, what you need to do is to write a pattern that matches your fact in the left hand side of the rule:
rule "First Rule"
salience 90
when
String(ifContains(this, "et"))
then
System.out.println("Does contain");
end
You can even optimize your rule to avoid using an unnecessary function definition and call by using the contains
operator:
rule "First Rule"
salience 90
when
String(this contains "et")
then
System.out.println("Does contain");
end
Hope it helps,