[rules-dev] is there a way to get "declare"s out of the working memory?

leif hanack leif.hanack at hypoport.de
Fri Oct 16 18:28:18 EDT 2009


Hi,
since Drools 5 it is possible to define a dynamic variable holder by
using the declare notation. This feature is really cool.
I can create easily helper rules, that calculate temporary values:

File: calculate_total_amount.drl

declare TotalAmount
  amount : Money
end

rule "calculate total amount"
  when
    ..
  then
    TotalAmount totalAmount = new TotalAmount();
    totalAmount.setAmount($calculatedAmount);
    insert(totalAmount);
end

Inside another file I use this helper.

File: another_rule.drl

rule "another rule"
  when
    TotalAmount( amount > 1000)
  then
     // do s.th.
end

I unit test my rule by querying a fact and verify my assertions. This
works fine, as long as the fact is a class of my factmodel.
Now my question: Is there a way to query a declared class (in this
case TotalAmount)

I tried it with some variations but without success:

import my.package.of.the.rule;
query "get total amount"
  amount : TotalAmount()
end

I thought that a declare is compiled to a class, but I can not find it
in the generated classes.
Did you have any help?

An alternative might be to create a holder class. S.th. like that

rule "calculate total amount"
  ..
  then
    TotalAmount totalAmount = new TotalAmount();
    totalAmount.setAmount($calculatedAmount);
    insert(totalAmount);
    Holder holder = new Holder();
    holder.setObject(totalAmount);
    insert(holder);
end


import my.package.holder;
query "get holder"
  holder : Holder()
end

Thanks in advance, Leif


More information about the rules-dev mailing list