1 Reply Latest reply on Aug 3, 2006 7:58 PM by panihill

    Drools 3.0 and constraints

    allenabq

      Maybe I've just not found the documentation that explains how to deal with this.

      I'm using Drools and want to compare to values in the same object as a condition for executing the RHS.

      For example I might have:

      public class Battery {
       private double charge;
      
       public void setCharge(double charge) {
       this.charge = charge;
       }
      
       public double getCharge() {
       return charge;
       }
      }
      
      public class DoubleA extends Battery {
       private double optimal;
      
       public double getOptimal(){
       return optimal;
       }
      }


      In my main function I set up all my pieces to get the rule base working with some instantiated objects. Lets say for DoubleA #1, I say the charge is .68 and the optimal value is .75. For DoubleA #2, the charge is .72 and the optimal value is .70.

      In my rule base, I want to monitor all my DoubleA batteries for when they are less than optimal. It would make sense that I would write the rule somehow like this...

      rule "DoubleA less than optimal"
       when
       b : DoubleA(charge < optimal);
       then
       b.chargeBattery();
      end


      However, this does not work. I get an error message in Eclipse saying "Unable to return Declaration for identifier 'optimal'". In haven't been able to find anything in the documentation about comparing the internal values of single objects, and the contraints on the RHS of the comparison operator only appears to work for literals or bound variables. I've tired many different permutations of making a bound variable out of 'optimal', but nothing seems to work, and I'm stumped. There HAS to be a way to do this. I can't imagine it was not thought of.

      Any help appreciated!

      Allen


        • 1. Re: Drools 3.0 and constraints

          One simple thing to do would be add a method to class that compares the fields and returns a boolean.

          Otherwise, you can use the 'eval' - see documentation.

          when
          b : DoubleA( )
          eval (getcharge() < getOptimal() )
          then...