[rules-users] Typecasting problem

hsherlock citadela at gmail.com
Tue Oct 11 09:10:06 EDT 2011


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 [Document]"
	when
		$job: Job(type==JobType.VALIDATE)
		eval(($job.item instanceof Document))
		$doc: Document(issued==true) from $job.item
	then
		insert(new DocumentAlreadyIssuedFact());
end

rule "process [SENDING] on [Message]"
	when
		$job: Job(type==JobType.SEND)
		eval(($job.item instanceof Message))
		$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-tp3412518p3412518.html
Sent from the Drools: User forum mailing list archive at Nabble.com.




More information about the rules-users mailing list