[jboss-jira] [JBoss JIRA] (DROOLS-1227) Drools cannot be configured to expire Events properly

Jochen Welle (JIRA) issues at jboss.org
Tue Mar 7 08:54:00 EST 2017


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

Jochen Welle commented on DROOLS-1227:
--------------------------------------

Sorry, I cannot attach the test. Follows inline:

{code:java}
package testpackage;

import static org.junit.Assert.assertEquals;

import java.util.concurrent.TimeUnit;

import org.drools.core.ClockType;
import org.drools.core.time.SessionPseudoClock;
import org.junit.Before;
import org.junit.Test;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.Message;
import org.kie.api.builder.model.KieBaseModel;
import org.kie.api.builder.model.KieModuleModel;
import org.kie.api.builder.model.KieSessionModel;
import org.kie.api.conf.EventProcessingOption;
import org.kie.api.definition.type.FactType;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.kie.api.runtime.conf.ClockTypeOption;

public class ExpirationTest {

    KieContainer kieContainer;

    @Before
    public void setup() {
        KieServices kieServices = KieServices.Factory.get();
        // create KieModule programmatically (instead of using kmodule.xml):
        KieModuleModel kieModuleModel = kieServices.newKieModuleModel();

        KieBaseModel kieBaseModel1 = kieModuleModel.newKieBaseModel( "KBase1 ")
                .setDefault( true )
                .setEventProcessingMode( EventProcessingOption.STREAM );
                //.setEqualsBehavior( EqualityBehaviorOption.EQUALITY )

        KieSessionModel ksessionModel1 = kieBaseModel1.newKieSessionModel( "KSession1" )
                .setDefault( true )
                .setType( KieSessionModel.KieSessionType.STATEFUL )
                .setClockType( ClockTypeOption.get(ClockType.PSEUDO_CLOCK.toString()) );

        // add KModuleXML to KieFileSystem:
        KieFileSystem kfs = kieServices.newKieFileSystem();
        kfs.writeKModuleXML(kieModuleModel.toXML());

        String str = "package testpackage\n"
            + "declare BuyOrderEvent\n"
            + "@role(event)\n"
            + "  id : int\n"
            + "end\n"
            + "declare AckEvent\n"
            + "@role(event)\n"
            + "  id : int\n"
            + "end\n"
            + "rule \"correlate orders\"\n"
            + "when\n"
            + "  $bo : BuyOrderEvent( $id : id ) \n"
            + "  $ae : AckEvent( id == $id, this after[0,10s] $bo )\n"
            + "then\n"
            + "  System.out.println(\"buy and ack correlated\");\n"
            + "end\n";

        kfs.write("src/main/resources/testpackage/testExpiration.drl", str);
        KieBuilder kieBuilder = kieServices.newKieBuilder( kfs ).buildAll(); // build KieModule and add it to the KieRepository (Singleton)
        assertEquals( 0, kieBuilder.getResults().getMessages( Message.Level.ERROR ).size() );

        // If a release id is not defined (with KieFileSystem.writePomXML()) the default release id is used:
        assertEquals(kieServices.getRepository().getDefaultReleaseId(), kieBuilder.getKieModule().getReleaseId());
        kieContainer = kieServices.newKieContainer(kieBuilder.getKieModule().getReleaseId());
    }

    @Test
    public void testBuyAck() throws Exception {

        KieBase kieBase = kieContainer.getKieBase();
        KieSession kieSession = kieContainer.newKieSession();
        SessionPseudoClock clock = kieSession.getSessionClock();

        FactType buyType = kieBase.getFactType("testpackage", "BuyOrderEvent");
        FactType ackType = kieBase.getFactType("testpackage", "AckEvent");
        Object buy = buyType.newInstance();
        buyType.set(buy, "id", 1);
        Object ack = ackType.newInstance();
        ackType.set(ack, "id", 1);

        kieSession.insert(buy);
        kieSession.insert(ack);
        assertEquals(2, kieSession.getFactCount()); // we have two facts inserted

        kieSession.fireAllRules();
        clock.advanceTime(11, TimeUnit.SECONDS);
        kieSession.fireAllRules();
        for (Object fh : kieSession.getFactHandles()) {
            System.out.println(fh.toString());
        }
        // BuyOrderEvent is removed but AckEvent not, according to documentation both should be removed.
        assertEquals(0, kieSession.getFactCount());

        kieSession.dispose();
    }

    @Test
    public void testAcks() throws Exception {

        KieBase kieBase = kieContainer.getKieBase();
        KieSession kieSession = kieContainer.newKieSession();
        SessionPseudoClock clock = kieSession.getSessionClock();

        FactType ackType = kieBase.getFactType("testpackage", "AckEvent");
        Object ack = ackType.newInstance();
        ackType.set(ack, "id", 42);
        kieSession.insert(ack);
        clock.advanceTime(1, TimeUnit.MILLISECONDS);
        kieSession.fireAllRules();
        for (Object fh : kieSession.getFactHandles()) {
            System.out.println(fh.toString());
        }
        clock.advanceTime(11, TimeUnit.SECONDS);
        kieSession.fireAllRules();
        for (Object fh : kieSession.getFactHandles()) {
            System.out.println(fh.toString());
        }

        assertEquals(0, kieSession.getFactCount());
        kieSession.dispose();
    }
}
{code}



> Drools cannot be configured to expire Events properly
> -----------------------------------------------------
>
>                 Key: DROOLS-1227
>                 URL: https://issues.jboss.org/browse/DROOLS-1227
>             Project: Drools
>          Issue Type: Bug
>    Affects Versions: 6.2.0.Final
>         Environment: Windows, Java SE 1.8
>            Reporter: Cristobal Arellano
>            Assignee: Mario Fusco
>            Priority: Critical
>         Attachments: expire_without_temporal_constraint.drl
>
>
> Hello,
> I want to configure Drools (CEP) to expire events when no longer needed. There are two scenarios:
> ==SCENARIO A==
> I configured Event with no explicit expires. In this scenario, if there is a rule with temporal constraints, the expiration is automatically calculated based on the constrains.  If there is a rule with no temporal constraints, the expiration is INFINITE. The following example shows the scenario:
> dialect "mvel"
> declare Event
> @role(event)
> end
> rule "ExampleRule1"
> when
> ( $a : Event(name == "event a") 
> then
> System.out.println("ExampleRule1Triggered");
> end
> rule "ExampleRule2"
> when
> ( $a : Event(name == "event a") ) and
> ( $b : Event((name == "event b") && (this after [1ms, 15s] $a)) )
> then
> System.out.println("ExampleRule2Triggered");
> end
> With the previous Event definition:
> * If only ExampleRule1 loaded, expires INFINITE. Expected expires 0. ERROR?
> * If ExampleRule1 loaded and ExampleRule2 loaded, expires 15s. Expected expires 15. OK!
> To solve this situation a tried the following scenario:
> == SCENARIO B==
> I configured Event with explicit expires. In this scenario, if there is a rule with temporal constraints, the expiration is not taken into account because it is overriden by the explicit expires. The following example shows the scenario:
> dialect "mvel"
> declare Event
> @role(event)
> @expires(0s)
> end
> rule "ExampleRule1"
> when
> ( $a : Event(name == "event a") 
> then
> System.out.println("ExampleRule1Triggered");
> end
> rule "ExampleRule2"
> when
> ( $a : Event(name == "event a") ) and
> ( $b : Event((name == "event b") && (this after [1ms, 15s] $a)) )
> then
> System.out.println("ExampleRule2Triggered");
> end
> With the previous Event definition:
> * ExampleRule1 is triggered and event removed. OK!
> * ExampleRule2 is not triggered inserting two events because the first one expires. ERROR?
> I suppose that SCENARIO B is not factible because explicit expires overrides implicit expires (according to issue DROOLS-586).
> Could you please help me to solve this situation? Should Drools set inferred expiration time to 1ms when there are rules with no temporal constraints?



--
This message was sent by Atlassian JIRA
(v7.2.3#72005)


More information about the jboss-jira mailing list