[jboss-jira] [JBoss JIRA] (DROOLS-897) Binding variable or condition

Mario Fusco (JIRA) issues at jboss.org
Thu Aug 27 14:06:05 EDT 2015


    [ https://issues.jboss.org/browse/DROOLS-897?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13102877#comment-13102877 ] 

Mario Fusco edited comment on DROOLS-897 at 8/27/15 2:05 PM:
-------------------------------------------------------------

I have reproduced the problem you described in this way.

{code}
    public static class Parent {
        public String value;

        public String getValue() {
            return value;
        }

        public void setValue( String value ) {
            this.value = value;
        }
    }

    public static class ChildA extends Parent { }
    public static class ChildB extends Parent { }

    @Test
    public void testInheritance() throws Exception {
        // DROOLS-897
        String drl =
                "import " + Parent.class.getCanonicalName() + "\n" +
                "import " + ChildA.class.getCanonicalName() + "\n" +
                "import " + ChildB.class.getCanonicalName() + "\n" +
                "rule \"Variable matches field\" when\n" +
                "    (\n" +
                "    ChildA(value == null, $entity : this) or \n" +
                "    ChildB(value == null, $entity : this)\n" +
                "    )\n" +
                "then\n" +
                "    modify( $entity ) { setValue(\"Done!\"); }\n" +
                "end\n";

        KieSession ksession = new KieHelper().addContent( drl, ResourceType.DRL )
                                             .build()
                                             .newKieSession();

        ksession.insert( new ChildB() );
        ksession.fireAllRules();
    }
{code}

I'll try to fix this issue asap. However since all your classes extend the same base class the correct way to do this is pattern matching directly on that base classes as it follows, thus avoiding all the ugly ORs in your rule.

{code}
    @Test
    public void testInheritance2() throws Exception {
        // DROOLS-897
        String drl =
                "import " + Parent.class.getCanonicalName() + "\n" +
                "import " + ChildA.class.getCanonicalName() + "\n" +
                "import " + ChildB.class.getCanonicalName() + "\n" +
                "rule \"Variable matches field\" when\n" +
                "    Parent(value == null, $entity : this)\n" +
                "then\n" +
                "    modify( $entity ) { setValue(\"Done!\"); }\n" +
                "end\n";

        KieSession ksession = new KieHelper().addContent( drl, ResourceType.DRL )
                                             .build()
                                             .newKieSession();

        ksession.insert( new ChildB() );
        ksession.fireAllRules();
    }
{code}


was (Author: mfusco):
I have reproduced the problem you described in this way.

{code}
    public static class Parent {
        public String value;

        public String getValue() {
            return value;
        }

        public void setValue( String value ) {
            this.value = value;
        }
    }

    public static class ChildA extends Parent { }
    public static class ChildB extends Parent { }

    @Test
    public void testInheritance() throws Exception {
        // DROOLS-897
        String drl =
                "import " + Parent.class.getCanonicalName() + "\n" +
                "import " + ChildA.class.getCanonicalName() + "\n" +
                "import " + ChildB.class.getCanonicalName() + "\n" +
                "rule \"Variable matches field\" when\n" +
                "    (\n" +
                "    ChildA(value == null, $entity : this) or \n" +
                "    ChildB(value == null, $entity : this)\n" +
                "    )\n" +
                "then\n" +
                "    modify( $entity ) { setValue(\"Done!\"); }\n" +
                "end\n";

        KieSession ksession = new KieHelper().addContent( drl, ResourceType.DRL )
                                             .build()
                                             .newKieSession();

        ksession.insert( new ChildB() );
        ksession.fireAllRules();
    }
{code}

I'll try to fix this issue asap. However since all your classes extend the same base class the correct way to do this is pattern matching directly on that base classes as it follows, thus avoiding all the ugly ORs in your rule.

{code}
    @Test
    public void testInheritance2() throws Exception {
        // DROOLS-897
        String drl =
                "import " + Parent.class.getCanonicalName() + "\n" +
                "import " + ChildA.class.getCanonicalName() + "\n" +
                "import " + ChildB.class.getCanonicalName() + "\n" +
                "rule \"Variable matches field\" when\n" +
                "    (\n" +
                "    Parent(value == null, $entity : this)\n" +
                "    )\n" +
                "then\n" +
                "    modify( $entity ) { setValue(\"Done!\"); }\n" +
                "end\n";

        KieSession ksession = new KieHelper().addContent( drl, ResourceType.DRL )
                                             .build()
                                             .newKieSession();

        ksession.insert( new ChildB() );
        ksession.fireAllRules();
    }
{code}

> Binding variable or condition
> -----------------------------
>
>                 Key: DROOLS-897
>                 URL: https://issues.jboss.org/browse/DROOLS-897
>             Project: Drools
>          Issue Type: Bug
>          Components: core engine
>    Affects Versions: 6.2.0.Final
>         Environment: Windows 8
>            Reporter: Sante Stanisci
>            Assignee: Mario Fusco
>              Labels: dynamic, instanciation, variable
>         Attachments: rule.drl, workaround.drl
>
>
> In this example I have more classes that extends EntityBase (my class). In my intention, i would assign to variable $entity one of this class that is inserted in Ksession fact.
> I insert fact of CtbMovrCoan class
> In when statement of the rule at row 16, variable $entity is correctely, 
> but in then statement variable $entity becomes JtbRLavt class (first declared in then cond) and this behavior throw a ClassCastException, obviously.
> Can Help me?



--
This message was sent by Atlassian JIRA
(v6.3.15#6346)


More information about the jboss-jira mailing list