Uploaded image for project: 'JBRULES'
  1. JBRULES
  2. JBRULES-1446

ClassCastException when iterating over an array using "from"

    XMLWordPrintable

    Details

      Description

      To reproduce the error, please create the two following objects:
      package test;

      public class DomainObject {

      private String message;
      private int value;
      private double value2;

      public String getMessage()

      { return message; }

      public void setMessage(String message)

      { this.message = message; }

      public int getValue()

      { return value; }

      public void setValue(int value)

      { this.value = value; }

      public double getValue2()

      { return value2; }

      public void setValue2(double value2)

      { this.value2 = value2; }

      }

      package test;

      public class DomainObjectHolder {

      DomainObject[] objects = new DomainObject[3];

      public DomainObjectHolder()

      { objects[0] = new DomainObject(); objects[0].setMessage("Message1"); objects[0].setValue(1); objects[0].setValue2(2); objects[1] = new DomainObject(); objects[1].setMessage("Message2"); objects[1].setValue(3); objects[1].setValue2(4); objects[2] = new DomainObject(); objects[2].setMessage("Message3"); objects[2].setValue(5); objects[2].setValue2(6); }

      public DomainObject[] getObjects()

      { return objects; }

      }

      Here are a few rule that will trigger the ClassCastException:

      rule "Test Rule"
      when
      $holder : DomainObjectHolder()
      $object : DomainObject( $message : message) from $holder.objects;
      then
      System.out.println("Selected Object " + $message);
      end

      rule "Test Rule"
      when
      $holder : DomainObjectHolder()
      $object : DomainObject( ) from $holder.objects;
      then
      System.out.println("Selected Object " + $object.getMessage());
      end

      This rule does not trigger the exception:

      rule "Test Rule"
      when
      $holder : DomainObjectHolder()
      $object : DomainObject( ) from $holder.objects;
      then
      System.out.println("Selected Object");
      end

      Here is the exception message:
      java.lang.ClassCastException: [Ltest.DomainObject; cannot be cast to test.DomainObject
      at org.drools.base.test.DomainObject9939622$getMessage.getValue(Unknown Source)
      at org.drools.base.ClassFieldExtractor.getValue(ClassFieldExtractor.java:127)
      at org.drools.rule.Declaration.getValue(Declaration.java:198)
      at sequitem.quanteq.rules.exit.Rule_Test_Rule_0ConsequenceInvoker.evaluate(Rule_Test_Rule_0ConsequenceInvoker.java:17)
      at org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:550)
      at org.drools.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:514)
      at org.drools.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:471)
      at org.drools.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:433)
      at sequitem.quanteq.rules.engine.StatefulEngine.execute(StatefulEngine.java:98)
      at sequitem.quanteq.rules.engine.StatefulEngine.execute(StatefulEngine.java:45)
      at sequitem.quanteq.tradesystem.RuleBasedTradeSystem.onStart(RuleBasedTradeSystem.java:186)
      at org.activequant.tradesystem.simple.TradeSystemBase.start(TradeSystemBase.java:242)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.activequant.container.context.TradeSystemContextBase.invoke(TradeSystemContextBase.java:134)
      at org.activequant.container.context.TradeSystemContextBase.access$2(TradeSystemContextBase.java:132)
      at org.activequant.container.context.TradeSystemContextBase$2.start(TradeSystemContextBase.java:152)
      at org.activequant.container.context.TradeSystemContextBase.start(TradeSystemContextBase.java:179)
      at sequitem.quanteq.backtest.exits.StandardExistBackTest.main(StandardExistBackTest.java:36)
      Exception in thread "main" java.lang.reflect.InvocationTargetException
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      at org.activequant.container.context.TradeSystemContextBase.invoke(TradeSystemContextBase.java:134)
      at org.activequant.container.context.TradeSystemContextBase.access$2(TradeSystemContextBase.java:132)
      at org.activequant.container.context.TradeSystemContextBase$2.start(TradeSystemContextBase.java:152)
      at org.activequant.container.context.TradeSystemContextBase.start(TradeSystemContextBase.java:179)
      at sequitem.quanteq.backtest.exits.StandardExistBackTest.main(StandardExistBackTest.java:36)
      Caused by: org.drools.spi.ConsequenceException: java.lang.ClassCastException: [Ltest.DomainObject; cannot be cast to test.DomainObject
      at org.drools.base.DefaultConsequenceExceptionHandler.handleException(DefaultConsequenceExceptionHandler.java:14)
      at org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:554)
      at org.drools.common.DefaultAgenda.fireNextItem(DefaultAgenda.java:514)
      at org.drools.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:471)
      at org.drools.common.AbstractWorkingMemory.fireAllRules(AbstractWorkingMemory.java:433)
      at sequitem.quanteq.rules.engine.StatefulEngine.execute(StatefulEngine.java:98)
      at sequitem.quanteq.rules.engine.StatefulEngine.execute(StatefulEngine.java:45)
      at sequitem.quanteq.tradesystem.RuleBasedTradeSystem.onStart(RuleBasedTradeSystem.java:186)
      at org.activequant.tradesystem.simple.TradeSystemBase.start(TradeSystemBase.java:242)
      ... 9 more
      Caused by: java.lang.ClassCastException: [Ltest.DomainObject; cannot be cast to test.DomainObject
      at org.drools.base.test.DomainObject9939622$getMessage.getValue(Unknown Source)
      at org.drools.base.ClassFieldExtractor.getValue(ClassFieldExtractor.java:127)
      at org.drools.rule.Declaration.getValue(Declaration.java:198)
      at sequitem.quanteq.rules.exit.Rule_Test_Rule_0ConsequenceInvoker.evaluate(Rule_Test_Rule_0ConsequenceInvoker.java:17)
      at org.drools.common.DefaultAgenda.fireActivation(DefaultAgenda.java:550)
      ... 16 more

        Gliffy Diagrams

          Attachments

            Activity

              People

              • Assignee:
                mark.proctor Mark Proctor
                Reporter:
                kkarski Krzysztof Karski
              • Votes:
                1 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: