[jboss-jira] [JBoss JIRA] (DROOLS-624) Passive queries behave in a reactive way

Mario Fusco (JIRA) issues at jboss.org
Fri Oct 3 12:49:11 EDT 2014


Mario Fusco created DROOLS-624:
----------------------------------

             Summary: Passive queries behave in a reactive way
                 Key: DROOLS-624
                 URL: https://issues.jboss.org/browse/DROOLS-624
             Project: Drools
          Issue Type: Bug
            Reporter: Mario Fusco
            Assignee: Mark Proctor


The batch evaluation performed by phreak doesn't allow to take count of the sequence of the inserts performed into a kiesession between 2 consecutive fireAllRules. This means that the following test case succeeds as expected

{code}
    @Test
    public void testPassiveQuery2() throws Exception {
        String str =
                "global java.util.List list\n" +
                "query Q (Integer i)\n" +
                "    String( this == i.toString() )\n" +
                "end\n" +
                "rule R @Eager(true) when\n" +
                "    $i : Integer()\n" +
                "    ?Q( $i; )\n" +
                "then\n" +
                "    list.add( $i );\n" +
                "end\n";

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

        List<Integer> list = new ArrayList<Integer>();
        ksession.setGlobal("list", list);

        ksession.insert("1");
        ksession.insert(1);
        ksession.fireAllRules();
        assertEquals(1, list.size());
    }
{code}

but you got an activatoin even if the fact matched by the passive query is inserted (the String in this case) is inserted after. So the following test fails:

{code}
    @Test
    public void testPassiveQuery3() throws Exception {
        String str =
                "global java.util.List list\n" +
                "query Q (Integer i)\n" +
                "    String( this == i.toString() )\n" +
                "end\n" +
                "rule R @Eager(true) when\n" +
                "    $i : Integer()\n" +
                "    ?Q( $i; )\n" +
                "then\n" +
                "    list.add( $i );\n" +
                "end\n";

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

        List<Integer> list = new ArrayList<Integer>();
        ksession.setGlobal("list", list);

        ksession.insert(1);
        ksession.insert("1");
        ksession.fireAllRules();
        System.out.println(list);
        assertEquals(0, list.size());
    }
{code}

Note that performing a further fireAllRules between the 2 insertions force the evaluation of the lefttTuple before the insertion of the fact matched by the passive query and the fixes the problem as demonstrated by this last test.

{code}
    @Test
    public void testPassiveQuery4() throws Exception {
        String str =
                "global java.util.List list\n" +
                "query Q (Integer i)\n" +
                "    String( this == i.toString() )\n" +
                "end\n" +
                "rule R @Eager(true) when\n" +
                "    $i : Integer()\n" +
                "    ?Q( $i; )\n" +
                "then\n" +
                "    list.add( $i );\n" +
                "end\n";

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

        List<Integer> list = new ArrayList<Integer>();
        ksession.setGlobal("list", list);

        ksession.insert(1);
        ksession.fireAllRules();
        ksession.insert("1");
        ksession.fireAllRules();
        System.out.println(list);
        assertEquals(0, list.size());
    }
{code}



--
This message was sent by Atlassian JIRA
(v6.3.1#6329)


More information about the jboss-jira mailing list