[rules-users] Re: Is this scenario suitable for using Drools?

Cheng Wei Lee zhenwei.li at gmail.com
Wed Apr 23 04:36:43 EDT 2008


Thanks Mike!

I'm a total newbie here with Drools, so what I'm going to do is to take your
solution and try it out.

Cheers!

On Wed, Apr 23, 2008 at 4:28 PM, Anstis, Michael (M.) <manstis1 at ford.com>
wrote:

>  Thanks Steve,
>
> This fixed the problem!
>
> For Cheng Wei Lee's benefit this is the complete solution (I had to fix
> another part):-
>
> public class DBUtils {
>
>     public Collection getNumbers() {
>
>         Integer[] numbers = new Integer[10];
>         for (int i = 0; i < numbers.length; i++) {
>             numbers[i] = (int)Math.random() * 100;
>         }
>         return Arrays.asList(numbers);
>     }
> }
>
> rule "a1"
>     when
>         $max : Number() from accumulate( Number( $v : intValue ) from
> dbutils.getNumbers(), max($v) )
>     then
>         System.out.println($max.intValue());
>     end
>
> With kind regards,
>
> Mike
>
>  ------------------------------
> *From:* rules-users-bounces at lists.jboss.org [mailto:
> rules-users-bounces at lists.jboss.org] *On Behalf Of *Steven Williams
> *Sent:* 23 April 2008 09:07
> *To:* Rules Users List
> *Subject:* Re: [rules-users] Re: Is this scenario suitable for using
> Drools?
>
> change DBUtils to return a Collection and it should work I think.
>
> On Wed, Apr 23, 2008 at 5:50 PM, Anstis, Michael (M.) <manstis1 at ford.com>
> wrote:
>
> >  So, in my usual helpful manner I thought Drools would be a perfect
> > match and put together what I thought would be a simple example:-
> >
> > package com.sample
> >
> > global com.sample.DBUtils dbutils;
> > rule "a1"
> >     when
> >         $max : Integer() from accumulate( Integer( $v : intValue ) from
> > dbutils.getNumbers(), max($v) )
> >     then
> >         System.out.println($max);
> > end
> >
> >
> > public class DBUtils {
> >
> >     public Integer[] getNumbers() {
> >
> >         Integer[] numbers = new Integer[10];
> >         for (int i = 0; i < numbers.length; i++) {
> >             numbers[i] = (int) (Math.random() * 100);
> >         }
> >         return numbers;
> >     }
> > }
> >
> > However this led to some errors:-
> >
> > org.drools.RuntimeDroolsException: java.lang.ClassCastException:
> > [Ljava.lang.Integer; incompatible with java.lang.Integer
> >  at org.drools.rule.Accumulate.accumulate(Accumulate.java:131)
> >  at
> > org.drools.reteoo.AccumulateNode.assertTuple(AccumulateNode.java:127)
> >  at
> > org.drools.reteoo.CompositeTupleSinkAdapter.createAndPropagateAssertTuple(CompositeTupleSinkAdapter.java:73)
> >  at
> > org.drools.reteoo.LeftInputAdapterNode.assertObject(LeftInputAdapterNode.java:116)
> >  at
> > org.drools.reteoo.SingleObjectSinkAdapter.propagateAssertObject(SingleObjectSinkAdapter.java:22)
> >  at
> > org.drools.reteoo.ObjectTypeNode.assertObject(ObjectTypeNode.java:153)
> >  at org.drools.reteoo.Rete.assertObject(Rete.java:177)
> >  at
> > org.drools.reteoo.ReteooRuleBase.assertObject(ReteooRuleBase.java:192)
> >  at
> > org.drools.reteoo.ReteooWorkingMemory$WorkingMemoryReteAssertAction.execute(ReteooWorkingMemory.java:179)
> >  at
> > org.drools.common.AbstractWorkingMemory.executeQueuedActions(AbstractWorkingMemory.java:1292)
> >  at
> > org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:891)
> >  at
> > org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:858)
> >  at
> > org.drools.common.AbstractWorkingMemory.insert(AbstractWorkingMemory.java:659)
> >  at com.sample.DroolsTest.main(DroolsTest.java:36)
> > Caused by: java.lang.ClassCastException: [Ljava.lang.Integer;
> > incompatible with java.lang.Integer
> >  at
> > org.drools.base.java.lang.Integer968047027$intValue.getIntValue(Unknown
> > Source)
> >  at
> > org.drools.base.ClassFieldExtractor.getIntValue(ClassFieldExtractor.java:197)
> >  at org.drools.rule.Declaration.getIntValue(Declaration.java:230)
> >  at
> > com.sample.Rule_a1_0AccumulateExpression0Invoker.evaluate(Rule_a1_0AccumulateExpression0Invoker.java:16)
> >  at
> > org.drools.base.accumulators.JavaAccumulatorFunctionExecutor.accumulate(JavaAccumulatorFunctionExecutor.java:74)
> >  at org.drools.rule.Accumulate.accumulate(Accumulate.java:123)
> >  ... 13 more
> > Have I done something really stupid and is Drools a fit?
> >
> > Cheers,
> >
> > Mike
> >
> >  ------------------------------
> > *From:* rules-users-bounces at lists.jboss.org [mailto:
> > rules-users-bounces at lists.jboss.org] *On Behalf Of *Cheng Wei Lee
> > *Sent:* 22 April 2008 17:27
> > *To:* rules-users at lists.jboss.org
> > *Subject:* [rules-users] Re: Is this scenario suitable for using Drools?
> >
> >   I've 2 algorithms to calculate the cost of a product. At any one time,
> > there is only 1 algorithm in use. Initially algorithm 1 will be the default.
> > Subsequently, the decision to use which algorithm will depend on customers
> > feedback. The algorithms are:
> >
> > Algorithm 1:
> > Cost = MAX(P1, T1, P2, T2, P3, T3, ...)
> >
> > Algorithm 2:
> > Cost = MIN(P1, P2, P3, ...) + MIN(T1, T2, T3, ...)
> >
> > The values of P1, P2, ... are stored within a database. The number of Ps
> > & Ts are unknown but can be determined by querying the database, Would
> > drools be a good option to use to store the algorithms? If so, how could I
> > be able to retrieve the values of P1, P2, etc from the database from within
> > drools?
> >
> > Thanks!
> >
> >
> > _______________________________________________
> > rules-users mailing list
> > rules-users at lists.jboss.org
> > https://lists.jboss.org/mailman/listinfo/rules-users
> >
> >
>
>
> --
> Steven Williams
>
> Supervising Consultant
>
> Object Consulting
> Office: 8615 4500 Mob: 0439 898 668 Fax: 8615 4501
> steven.williams at objectconsulting.com.au
> www.objectconsulting.com.au
>
> consulting | development | training | support
> our experience makes the difference
>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20080423/e29d9519/attachment.html 


More information about the rules-users mailing list