[jboss-jira] [JBoss JIRA] (DROOLS-435) ClassCastException using nested accumulates

Mario Fusco (JIRA) issues at jboss.org
Tue Feb 18 05:51:47 EST 2014


Mario Fusco created DROOLS-435:
----------------------------------

             Summary: ClassCastException using nested accumulates
                 Key: DROOLS-435
                 URL: https://issues.jboss.org/browse/DROOLS-435
             Project: Drools
          Issue Type: Bug
      Security Level: Public (Everyone can see)
    Affects Versions: 6.0.1.Final, 5.6.0.Final
            Reporter: Mario Fusco
            Assignee: Mario Fusco
            Priority: Critical


Supposing I have a data model as the following

{code}
    public static class Driver {
        private final String name;
        private List<Car> cars = new ArrayList<Car>();
        public Driver(String name) { this.name = name; }
        public String getName() { return name; }
        public void addCar(Car car) { cars.add(car); }
        public List<Car> getCars() { return cars; }
    }
    public static class Car {
        private final int speed;
        public Car(int speed) { this.speed = speed; }
        public int getSpeed() { return speed; }
    }
{code}

where each Driver can own multiple cars. I would like to calculate the average of the speed of all the Cars as I did with the following nested accumulates 

{code}
    @Test
    public void testNestedAccumulate() {
        String str =
                "import java.util.*;\n" +
                "import " + Driver.class.getCanonicalName() + ";\n" +
                "import " + Car.class.getCanonicalName() + ";\n" +
                "rule R when\n" +
                "accumulate ( Car( s: speed ) from accumulate ( Driver( c: cars ), " +
                "                                               init(List list = new ArrayList();)," +
                "                                               action(list.addAll(c);)," +
                "                                               reverse(list.removeAll(c);)," +
                "                                               result(list) ),\n" +
                "                               avg: average(s) )\n" +
                "then\n" +
                "    System.out.println( avg );\n" +
                "end\n";

        KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

        Driver mario = new Driver("mario");
        mario.addCar(new Car(100));
        mario.addCar(new Car(200));
        Driver mark = new Driver("mark");
        mark.addCar(new Car(300));
        mark.addCar(new Car(400));

        ksession.insert(mario);
        ksession.insert(mark);
        ksession.fireAllRules();
    }
{code}

Note that splitting the nested accumulate in 2 separated accumulates as it follows fixes the problem.

{code}
    @Test
    public void testAccumulates() {
        String str =
                "import java.util.*;\n" +
                "import " + Driver.class.getCanonicalName() + ";\n" +
                "import " + Car.class.getCanonicalName() + ";\n" +
                "rule R when\n" +
                "$cars : List() from accumulate ( Driver( c: cars ), " +
                "                                 init(List list = new ArrayList();)," +
                "                                 action(list.addAll(c);)," +
                "                                 reverse(list.removeAll(c);)," +
                "                                 result(list) )\n" +
                "accumulate ( Car( $s: speed ) from $cars,\n" +
                "             $avg: average($s) )\n" +
                "then\n" +
                "    System.out.println( $cars );\n" +
                "    System.out.println( $avg );\n" +
                "end\n";

        KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

        Driver mario = new Driver("mario");
        mario.addCar(new Car(100));
        mario.addCar(new Car(200));
        Driver mark = new Driver("mark");
        mark.addCar(new Car(300));
        mark.addCar(new Car(400));

        ksession.insert(mario);
        ksession.insert(mark);
        ksession.fireAllRules();
    }
{code}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira


More information about the jboss-jira mailing list