kedar
kedar

Reputation: 103

Drools rules with boolean variable

I want to declare a boolean variable in my drl file and wants to write a rule based on the value of that variable. I am not able to find any good example for this.

I tried like:

declare Flag
    flag: Boolean
end

In one of the rule, I am modifying like:

  flag = Boolean.TRUE;

and my rule is:

rule "<210> Determine flag"
    when
        Flag(flag == true)
    ...
end

But it is giving me error as flag cannot be resolved.

Upvotes: 1

Views: 5647

Answers (2)

shagun akarsh
shagun akarsh

Reputation: 159

You can use the following:

rule "Test"
   when
      a: TestClass( getFlag())
   then
      //some action
end

Also see this answer: Drools Rule - writing rule against boolean field, name starting with "is"

Upvotes: 1

laune
laune

Reputation: 31290

You cannot declare variables in the usual way as (I think) you are trying to do. Please read the Drools documentation and distinguish binding variables, global variables, facts and their fields and right hand side local variables (as within a static Java method).

To see how declare with a boolean field works, use the following DRL code:

declare Flag
  flag: Boolean
end

rule "hoist a Flag"
when
    not Flag()
then
    insert( new Flag( true ) );
end

rule "a true Flag"
when
    Flag( flag )
then
    System.out.println( "The Flag.flag is true." );
end

Upvotes: 4

Related Questions