[jboss-jira] [JBoss JIRA] (DROOLS-435) ClassCastException using nested accumulates
Mario Fusco (JIRA)
issues at jboss.org
Wed Aug 5 12:23:03 EDT 2015
[ https://issues.jboss.org/browse/DROOLS-435?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Mario Fusco resolved DROOLS-435.
--------------------------------
Resolution: Won't Fix
> ClassCastException using nested accumulates
> -------------------------------------------
>
> Key: DROOLS-435
> URL: https://issues.jboss.org/browse/DROOLS-435
> Project: Drools
> Issue Type: Bug
> Affects Versions: 5.6.0.Final, 6.0.1.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 was sent by Atlassian JIRA
(v6.3.15#6346)
More information about the jboss-jira
mailing list