[rules-users] Typecasting problem

Edson Tirelli ed.tirelli at gmail.com
Tue Oct 11 13:23:43 EDT 2011


   Which version of Drools are you using?

   Latest Drools versions changed the behavior of "from" to deal with it as
you mentioned. So it will not raise ClassCastException. Instead:

$doc: Document(...) from $job.item  // only matches if the returned item is
a Document

$msg: Message(...) from $job.item // only matches if the returned item is a
Message

   I think 5.2 was the first version released with this change, but need to
double check. For sure 5.3 works like that.

   Edson


2011/10/11 hsherlock <citadela at gmail.com>

> I have a following scenario in which a complex logic (distributed over
> multiple .DRL files) need to process incoming jobs as fast as possible. A
> job definition looks something like describing what to do (type), what to
> process (item) with some additional data supplied (args).
>
> enum JobType
> {
>        VALIDATE,
>        SEND
> }
>
> class Job
> {
>        String type;
>        Object item;
>        Object[] args;
> }
>
> In order to implement the processing I need to put the business logic into
> rules which fire depending on the kind of processing requested and type of
> the item passed:
>
> rule "process [VALIDATION] on [Document]"
>        when
>                $job: Job(type==JobType.VALIDATE)
>                $doc: Document(issued==true) from $job.item
>        then
>                insert(new DocumentAlreadyIssuedFact());
> end
>
> rule "process [SENDING] on [Message]"
>        when
>                $job: Job(type==JobType.SEND)
>                $msg: Message() from $job.item
>        then
>                //do the sending
> end
>
> Unfortunately this does not work and results in ClassCastException being
> thrown, because it looks that Drools tries to cast any passed item to the
> class expected by the particular rule. In my opinion behaviour one can
> expect from Drools here is to first match the item class to the one
> expected
> by the rule and on success to perform the type casting.
> Is it a Drools bug or a missing feature?
>
> So after stidying the documention and example code I found only one way to
> workaround this – by using eval and instanceof which should have its
> performance implications due to extensive use of the eval.
>
> rule "process [VALIDATION] on [Message]"
>        when
>                $job: Job(type==”validate”)
>                $msg: Message() from $job.item
>        then
>                //do the validation
> end
>
> rule "process [SENDING] on [Message]"
>        when
>                $job: Job(type==”send”)
>                $msg: Message() from $job.item
>        then
>                //do the sending
> end
>
> Such an implementation works, but I see it as some form of workaround as my
> feeling is that it will not allow the optimizations of Drools to shine.
>
> Please recommend more effective and elegant way of implementing this.
>
> Thank you in advance
>
> --
> View this message in context:
> http://drools.46999.n3.nabble.com/Typecasting-problem-tp3412494p3412494.html
> Sent from the Drools: User forum mailing list archive at Nabble.com.
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>



-- 
  Edson Tirelli
  JBoss Drools Core Development
  JBoss by Red Hat @ www.jboss.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20111011/940001d3/attachment.html 


More information about the rules-users mailing list