[jboss-jira] [JBoss JIRA] (DROOLS-1347) Explicit expiration of event not effective - regression from 6.4 to 6.5

Matteo Mortari (JIRA) issues at jboss.org
Fri Oct 28 11:04:00 EDT 2016


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

Matteo Mortari edited comment on DROOLS-1347 at 10/28/16 11:03 AM:
-------------------------------------------------------------------

Simplified reproducer with assertions, aligned with OP's case:

{code:java}
public class RuleTest {
    static final Logger LOG = LoggerFactory.getLogger(RuleTest.class);

    @Test
    public void test() throws InstantiationException, IllegalAccessException {
        KieServices kieServices = KieServices.Factory.get();

        KieContainer kContainer = kieServices.getKieClasspathContainer();
        Results verifyResults = kContainer.verify();
        for (Message m : verifyResults.getMessages()) {
            LOG.info("{}", m);
        }

        LOG.info("Creating kieBase with STREAM option");
        KieBaseConfiguration kieBaseConf = kieServices.newKieBaseConfiguration();
        kieBaseConf.setOption( EventProcessingOption.STREAM );
        KieBase kieBase = kContainer.newKieBase(kieBaseConf);

        LOG.info("There should be rules: ");
        for ( KiePackage kp : kieBase.getKiePackages() ) {
            for (Rule rule : kp.getRules()) {
                LOG.info("kp " + kp + " rule " + rule.getName());
            }
        }

        LOG.info("Creating kieSession");
        KieSessionConfiguration config = kieServices.newKieSessionConfiguration();
        config.setOption( ClockTypeOption.get("pseudo") );
        KieSession session = kieBase.newKieSession(config, null);
        SessionPseudoClock clock = session.getSessionClock();
        
        session.addEventListener(new DebugRuleRuntimeEventListener() {
            @Override
            public void objectDeleted(ObjectDeletedEvent event) {
                System.out.println(event.getOldObject() + " " + "x");
            }

            @Override
            public void objectInserted(ObjectInsertedEvent event) {
                System.out.println("x" + " " + event.getObject());
            }

            @Override
            public void objectUpdated(ObjectUpdatedEvent event) {
                System.out.println(event.getOldObject() + " " + event.getObject());
            }
        });
        session.addEventListener(new DefaultAgendaEventListener() {
            @Override
            public void afterMatchFired(AfterMatchFiredEvent event) {
                System.out.println("fired: "+event.getMatch().getRule());
            }
        });
        
        LOG.info("Populating globals");
        List<String> check = new ArrayList<String>();
        session.setGlobal("list", check);
        
        LOG.info("Now running data");
        
        System.out.println("---a---");
        clock.advanceTime(1000L, TimeUnit.MILLISECONDS);
        session.insert( new Character('a') );
        session.fireAllRules();
        print(session);
        assertTrue(check.contains("Rexpected"));
        assertFalse(check.contains("Ravoid"));
        
        System.out.println("---b---");
        clock.advanceTime(3600000L, TimeUnit.MILLISECONDS);
        session.insert( new Character('b') );
        session.fireAllRules();
        print(session);
        assertTrue(check.contains("Rexpected"));
        assertFalse(check.contains("Ravoid"));
        
        
    }

    private void print(KieSession session) {
        System.out.println("Session facts:");
        for ( FactHandle fh : session.getFactHandles() ) {
            InternalFactHandle ifh = (InternalFactHandle) fh;
            System.out.println(" > "+ifh.getObject()+" ["+ifh.getObjectClassName()+"] FH: "+fh);
        }
    }
}
{code}

And rules:

{code:java}
package org.drools.DROOLS_1347;

global java.util.List list;

declare Character
@role( event )
@expires( 1s )
end

declare String
@role( event )
@expires( 10m )
end

rule "Rexpected"
when
    $t : Character() over window:length(1) 
    not String() 
then
    insert("good");
    list.add("Rexpected");
end

rule "Ravoid"
when
    $t : Character() over window:length(1) 
    exists String(this before $t)
    not String(this coincides $t)
then
    insert("bad");
    list.add("Ravoid");
end
{code}

and output:

{code}
2016-10-28 17:01:57,314 INFO  [org.drools.compiler.kie.builder.impl.ClasspathKieProject] (main) Found kmodule: file:/home/mmortari/git/DROOLS-1347/target/classes/META-INF/kmodule.xml
2016-10-28 17:01:57,527 WARN  [org.drools.compiler.kie.builder.impl.ClasspathKieProject] (main) Unable to find pom.properties in /home/mmortari/git/DROOLS-1347/target/classes
2016-10-28 17:01:57,536 INFO  [org.drools.compiler.kie.builder.impl.ClasspathKieProject] (main) Recursed up folders, found and used pom.xml /home/mmortari/git/DROOLS-1347/pom.xml
2016-10-28 17:01:57,543 INFO  [org.drools.compiler.kie.builder.impl.KieRepositoryImpl] (main) KieModule was added: FileKieModule[releaseId=org.drools:DROOLS-1347:0.0.1-SNAPSHOT,file=/home/mmortari/git/DROOLS-1347/target/classes]
2016-10-28 17:01:58,390 INFO  [org.drools.DROOLS_1347.RuleTest] (main) Creating kieBase with STREAM option
2016-10-28 17:01:58,454 INFO  [org.drools.DROOLS_1347.RuleTest] (main) There should be rules: 
2016-10-28 17:01:58,454 INFO  [org.drools.DROOLS_1347.RuleTest] (main) kp [Package name=org.drools.DROOLS_1347] rule Rexpected
2016-10-28 17:01:58,454 INFO  [org.drools.DROOLS_1347.RuleTest] (main) kp [Package name=org.drools.DROOLS_1347] rule Ravoid
2016-10-28 17:01:58,454 INFO  [org.drools.DROOLS_1347.RuleTest] (main) Creating kieSession
2016-10-28 17:01:58,499 INFO  [org.drools.DROOLS_1347.RuleTest] (main) Populating globals
2016-10-28 17:01:58,500 INFO  [org.drools.DROOLS_1347.RuleTest] (main) Now running data
---a---
x a
x good
fired: [Rule name=Rexpected, agendaGroup=MAIN, salience=0, no-loop=false]
Session facts:
 > a [java.lang.Character] FH: 5:1:504858437:97:1:DEFAULT:NON_TRAIT:java.lang.Character
 > good [java.lang.String] FH: 5:2:775386112:3178685:2:DEFAULT:NON_TRAIT:java.lang.String
---b---
x b
x bad
fired: [Rule name=Ravoid, agendaGroup=MAIN, salience=0, no-loop=false]
Session facts:
 > b [java.lang.Character] FH: 5:3:391630194:98:3:DEFAULT:NON_TRAIT:java.lang.Character
 > bad [java.lang.String] FH: 5:4:1146825051:97285:4:DEFAULT:NON_TRAIT:java.lang.String
{code}

Please notice despite the assertion fails, no expired events are present in the session anyway after all the rules have been fired and fireAllRules() has returned.


was (Author: tari_manga):
Simplified reproducer with assertions, aligned with OP's case:

{code:java}
public class RuleTest {
    static final Logger LOG = LoggerFactory.getLogger(RuleTest.class);

    @Test
    public void test() throws InstantiationException, IllegalAccessException {
        KieServices kieServices = KieServices.Factory.get();

        KieContainer kContainer = kieServices.getKieClasspathContainer();
        Results verifyResults = kContainer.verify();
        for (Message m : verifyResults.getMessages()) {
            LOG.info("{}", m);
        }

        LOG.info("Creating kieBase with STREAM option");
        KieBaseConfiguration kieBaseConf = kieServices.newKieBaseConfiguration();
        kieBaseConf.setOption( EventProcessingOption.STREAM );
        KieBase kieBase = kContainer.newKieBase(kieBaseConf);

        LOG.info("There should be rules: ");
        for ( KiePackage kp : kieBase.getKiePackages() ) {
            for (Rule rule : kp.getRules()) {
                LOG.info("kp " + kp + " rule " + rule.getName());
            }
        }

        LOG.info("Creating kieSession");
        KieSessionConfiguration config = kieServices.newKieSessionConfiguration();
        config.setOption( ClockTypeOption.get("pseudo") );
        KieSession session = kieBase.newKieSession(config, null);
        SessionPseudoClock clock = session.getSessionClock();
        
        session.addEventListener(new DebugRuleRuntimeEventListener() {
            @Override
            public void objectDeleted(ObjectDeletedEvent event) {
                System.out.println(event.getOldObject() + " " + "x");
            }

            @Override
            public void objectInserted(ObjectInsertedEvent event) {
                System.out.println("x" + " " + event.getObject());
            }

            @Override
            public void objectUpdated(ObjectUpdatedEvent event) {
                System.out.println(event.getOldObject() + " " + event.getObject());
            }
        });
        session.addEventListener(new DefaultAgendaEventListener() {
            @Override
            public void afterMatchFired(AfterMatchFiredEvent event) {
                System.out.println("fired: "+event.getMatch().getRule());
            }
        });
        
        LOG.info("Populating globals");
        List<String> check = new ArrayList<String>();
        session.setGlobal("list", check);
        
        LOG.info("Now running data");
        
        System.out.println("---a---");
        clock.advanceTime(1000L, TimeUnit.MILLISECONDS);
        session.insert( new Character('a') );
        session.fireAllRules();
        print(session);
        assertTrue(check.contains("Rexpected"));
        assertFalse(check.contains("Ravoid"));
        
        System.out.println("---b---");
        clock.advanceTime(3600000L, TimeUnit.MILLISECONDS);
        session.insert( new Character('b') );
        session.fireAllRules();
        print(session);
        assertTrue(check.contains("Rexpected"));
        assertFalse(check.contains("Ravoid"));
        
        
    }

    private void print(KieSession session) {
        System.out.println("Session facts:");
        for ( FactHandle fh : session.getFactHandles() ) {
            InternalFactHandle ifh = (InternalFactHandle) fh;
            System.out.println(" > "+ifh.getObject()+" ["+ifh.getObjectClassName()+"] FH: "+fh);
        }
    }
}
{code}

And rules:

{code:java}
package org.drools.DROOLS_1347;

global java.util.List list;

declare Character
@role( event )
@expires( 1s )
end

declare String
@role( event )
@expires( 10m )
end

rule "Rexpected"
when
    $t : Character() over window:length(1) 
    not String() 
then
    insert("good");
    list.add("Rexpected");
end

rule "Ravoid"
when
    $t : Character() over window:length(1) 
    exists String(this before $t)
    not String(this coincides $t)
then
    insert("bad");
    list.add("Ravoid");
end
{code}


> Explicit expiration of event not effective - regression from 6.4 to 6.5
> -----------------------------------------------------------------------
>
>                 Key: DROOLS-1347
>                 URL: https://issues.jboss.org/browse/DROOLS-1347
>             Project: Drools
>          Issue Type: Bug
>          Components: core engine
>    Affects Versions: 6.5.0.Final
>            Reporter: Thibault Daoulas
>            Assignee: Matteo Mortari
>
> I just ugraded my project from Drools 6.4.0.Final to 6.5.0.Final and have now quite a few tests on rules that fail, all have in common that they test the expiration of events, where an event that should have been removed from the working memory is still present.



--
This message was sent by Atlassian JIRA
(v7.2.2#72004)


More information about the jboss-jira mailing list