<div><br></div>   Which version of Drools are you using?<div><br></div><div>   Latest Drools versions changed the behavior of &quot;from&quot; to deal with it as you mentioned. So it will not raise ClassCastException. Instead:</div>
<div><br></div><div>$doc: Document(...) from $job.item  // only matches if the returned item is a Document</div><div><br>$msg: Message(...) from $job.item // only matches if the returned item is a Message</div><div><br></div>
<div>   I think 5.2 was the first version released with this change, but need to double check. For sure 5.3 works like that.</div><div><br></div><div>   Edson</div><div><br></div><div><br><div class="gmail_quote">2011/10/11 hsherlock <span dir="ltr">&lt;<a href="mailto:citadela@gmail.com">citadela@gmail.com</a>&gt;</span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">I have a following scenario in which a complex logic (distributed over<br>
multiple .DRL files) need to process incoming jobs as fast as possible. A<br>
job definition looks something like describing what to do (type), what to<br>
process (item) with some additional data supplied (args).<br>
<br>
enum JobType<br>
{<br>
        VALIDATE,<br>
        SEND<br>
}<br>
<br>
class Job<br>
{<br>
        String type;<br>
        Object item;<br>
        Object[] args;<br>
}<br>
<br>
In order to implement the processing I need to put the business logic into<br>
rules which fire depending on the kind of processing requested and type of<br>
the item passed:<br>
<br>
rule &quot;process [VALIDATION] on [Document]&quot;<br>
        when<br>
                $job: Job(type==JobType.VALIDATE)<br>
                $doc: Document(issued==true) from $job.item<br>
        then<br>
                insert(new DocumentAlreadyIssuedFact());<br>
end<br>
<br>
rule &quot;process [SENDING] on [Message]&quot;<br>
        when<br>
                $job: Job(type==JobType.SEND)<br>
                $msg: Message() from $job.item<br>
        then<br>
                //do the sending<br>
end<br>
<br>
Unfortunately this does not work and results in ClassCastException being<br>
thrown, because it looks that Drools tries to cast any passed item to the<br>
class expected by the particular rule. In my opinion behaviour one can<br>
expect from Drools here is to first match the item class to the one expected<br>
by the rule and on success to perform the type casting.<br>
Is it a Drools bug or a missing feature?<br>
<br>
So after stidying the documention and example code I found only one way to<br>
workaround this – by using eval and instanceof which should have its<br>
performance implications due to extensive use of the eval.<br>
<br>
rule &quot;process [VALIDATION] on [Message]&quot;<br>
        when<br>
                $job: Job(type==”validate”)<br>
                $msg: Message() from $job.item<br>
        then<br>
                //do the validation<br>
end<br>
<br>
rule &quot;process [SENDING] on [Message]&quot;<br>
        when<br>
                $job: Job(type==”send”)<br>
                $msg: Message() from $job.item<br>
        then<br>
                //do the sending<br>
end<br>
<br>
Such an implementation works, but I see it as some form of workaround as my<br>
feeling is that it will not allow the optimizations of Drools to shine.<br>
<br>
Please recommend more effective and elegant way of implementing this.<br>
<br>
Thank you in advance<br>
<font color="#888888"><br>
--<br>
View this message in context: <a href="http://drools.46999.n3.nabble.com/Typecasting-problem-tp3412494p3412494.html" target="_blank">http://drools.46999.n3.nabble.com/Typecasting-problem-tp3412494p3412494.html</a><br>
Sent from the Drools: User forum mailing list archive at Nabble.com.<br>
<br>
_______________________________________________<br>
rules-users mailing list<br>
<a href="mailto:rules-users@lists.jboss.org">rules-users@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/rules-users" target="_blank">https://lists.jboss.org/mailman/listinfo/rules-users</a><br>
</font></blockquote></div><br><br clear="all"><div><br></div>-- <br>  Edson Tirelli<br>  JBoss Drools Core Development<br>  JBoss by Red Hat @ <a href="http://www.jboss.com">www.jboss.com</a><br>
</div>