[rules-users] ClassCastException due to binding

Davide Sottara dsotty at gmail.com
Tue Jun 4 11:50:58 EDT 2013


"Polymorphic bindings" are not supported, the fact that it's not
reported at compilation time,
but only throws an exception at runtime, is a bad bug (I'll open a ticket)

This said, you can split your rule in two rules - which is what Drools
would do anyway:

rule r1 when $x : Foo() then .. end
rule r2 when $x : Bar() then .. end

I'd also encourage you to follow java conventions, using capital letters
for declared types.

And there may be various alternatives to "describe why a rule fired",
both in and out the RHS of a rule.
To cite some, using AgendaEventListeners or "drools.getActivation()" in
the RHS.
Davide


On 06/04/2013 05:12 PM, bdolbeare wrote:
> 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.
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>



More information about the rules-users mailing list