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

Michael Neale michael.neale at gmail.com
Sat Oct 17 16:49:05 EDT 2009


Yes there is a FactType class which represents the class (like a  
dynabean) and it has helper method to get at values etc.

Sent from my phone.

On 17/10/2009, at 9:28 AM, leif hanack <leif.hanack at hypoport.de> wrote:

> 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
> _______________________________________________
> rules-dev mailing list
> rules-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-dev


More information about the rules-dev mailing list