[rules-users] Drools for spreadsheet style calculation

Ming Fang mingfang at mac.com
Wed Jun 13 20:19:49 EDT 2007


Hi Drools users,

I'm looking to use Drools to implement spreadsheet style calculation.
Here is an example with cells A, B, C
A = 1;
B = A + 1;
C = A + B;

One way of implementing this is to create a named fact I called Cell.

class Cell{
	String name;
	double value;
	//left out constructor and getters/setters
}

Then the rules can look like this

rule "A"
     when
         Cell(name == "a")
     then
	//no-op
end

rule "B"
     salience -20
     no-loop
     when
         Cell(a: value, name == "a")
         b: Cell(name == "b")
     then
	b.setValue(a + 1);
         modify(b);
end

rule "C"
     salience -30
     no-loop
     when
         Cell(a: value, name == "a")
         Cell(b: value, name == "b")
         c: Cell(name == "c")
     then
	c.setValue(a + b);
         modify(c);
         System.out.println("C");
end

Now for this to work I need to first insert the three Cells, a, b, c.
Then anything I update a or b, c will be recalculated.

I want to hear what the experts think of think approach.
Is this optimal?
Can Drools automatically figure out the dependency? (In this case C  
depends on A, B and therefore has the lowest salience value).
Can we add a initialization section in the DRL file? (like a static  
init block).

--ming




More information about the rules-users mailing list