[rules-users] ClassCastException due to binding

bdolbeare bdolbeare at yahoo.com
Tue Jun 4 11:12:09 EDT 2013


We have a need to support binds for conditional or elements so that we can
describe why a rule fired in the then clause of a rule.  The following DRL
is a simple example of what we are trying to do.  There are two versions of
our rule in the DRL:

*rule "WORKING:  find with or"*:  works but only because the first variable
we try to bind in the or expression is of type "target" which is a super
class of the other elements in the or expression.  

*rule "BROKEN:  find with or"*:  causes a class cast exception when the
"bar" pattern is matched because the then clause tries to store the bar
object in handle "x" which it thinks is of type "foo".  

Is there a way to define the object type for the bind variable?  

Is there any other way to make something like this work?
 


package tests

import org.apache.log4j.Logger;

global Logger log

declare target
	notreal : boolean 
end

declare foo extends target
	foovalue : String
end

declare bar extends target
	barvalue : String
end  

rule "insertdata"
	when
	then
		insert(new foo(false, "a"));		
		insert(new bar(false, "b"));		
end

rule "WORKING:  find with or"
	when
	( or x: target(notreal) 
	x: foo(foovalue == "a") 
	x: bar(barvalue == "b")
	)
	then
		log.info("i found object: " + x);
end

rule "BROKEN:  find with or"
	when
	( or
	x: foo(foovalue == "a") 
	x: bar(barvalue == "b")
	)
	then
		log.info("i found object: " + x);
end



Exception executing consequence for rule "find with or" in tests:
java.lang.ClassCastException: tests.bar cannot be cast to tests.foo
	at
org.drools.runtime.rule.impl.DefaultConsequenceExceptionHandler.handleException(DefaultConsequenceExceptionHandler.java:39)
	at org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:1283)
	at org.drools.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:1209)
	at org.drools.common.DefaultAgenda.fireAllRules(DefaultAgenda.java:1442)
	at
org.drools.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:710)
	at
org.drools.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:674)
	at
org.drools.impl.StatefulKnowledgeSessionImpl.fireAllRules(StatefulKnowledgeSessionImpl.java:230)
	at
org.drools.impl.StatelessKnowledgeSessionImpl.execute(StatelessKnowledgeSessionImpl.java:278)
	at tests.DroolsTest.testStateless(DroolsTest.java:120)
	at tests.DroolsTest.runTest(DroolsTest.java:74)
	at tests.DroolsTest.main(DroolsTest.java:59)
Caused by: java.lang.ClassCastException: tests.bar cannot be cast to
tests.foo
	at
tests.Rule_find_with_or_411ee1c99de54340945b2b707ad0a576DefaultConsequenceInvoker.evaluate(Unknown
Source)
	at org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:1273)
	... 9 more



--
View this message in context: http://drools.46999.n3.nabble.com/ClassCastException-due-to-binding-tp4024115.html
Sent from the Drools: User forum mailing list archive at Nabble.com.


More information about the rules-users mailing list