Search in sources :

Example 66 with SessionPseudoClock

use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.

the class FlowTest method testDeclaredSlidingWindow.

@Test
public void testDeclaredSlidingWindow() {
    org.drools.model.WindowReference var_DeclaredWindow = window(org.drools.model.WindowDefinition.Type.TIME, 5, java.util.concurrent.TimeUnit.SECONDS, StockTick.class, (_this) -> _this.getCompany().equals("DROO"));
    final Variable<StockTick> var_$a = declarationOf(StockTick.class, "$a", var_DeclaredWindow);
    org.drools.model.Rule rule = rule("R").build(on(var_$a).execute(($a) -> {
        System.out.println($a.getCompany());
    }));
    Model model = new ModelImpl().addRule(rule);
    KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model, EventProcessingOption.STREAM);
    KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieSession ksession = kieBase.newKieSession(sessionConfig, null);
    SessionPseudoClock clock = ksession.getSessionClock();
    clock.advanceTime(2, TimeUnit.SECONDS);
    ksession.insert(new StockTick("DROO"));
    clock.advanceTime(2, TimeUnit.SECONDS);
    ksession.insert(new StockTick("DROO"));
    clock.advanceTime(2, TimeUnit.SECONDS);
    ksession.insert(new StockTick("ACME"));
    clock.advanceTime(2, TimeUnit.SECONDS);
    ksession.insert(new StockTick("DROO"));
    assertEquals(2, ksession.fireAllRules());
}
Also used : Arrays(java.util.Arrays) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) FlowDSL.eval(org.drools.model.FlowDSL.eval) ObjectOutput(java.io.ObjectOutput) Man(org.drools.modelcompiler.domain.Man) Global(org.drools.model.Global) FlowDSL.not(org.drools.model.FlowDSL.not) Toy(org.drools.modelcompiler.domain.Toy) FlowDSL.and(org.drools.model.FlowDSL.and) FlowDSL.accFunction(org.drools.model.FlowDSL.accFunction) Relationship(org.drools.modelcompiler.domain.Relationship) BaseModelTest.getObjectsIntoList(org.drools.modelcompiler.BaseModelTest.getObjectsIntoList) Assert.assertThat(org.junit.Assert.assertThat) Query2Def(org.drools.model.Query2Def) Child(org.drools.modelcompiler.domain.Child) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) AccumulateFunction(org.kie.api.runtime.rule.AccumulateFunction) FlowDSL.from(org.drools.model.FlowDSL.from) QueryResults(org.kie.api.runtime.rule.QueryResults) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) FlowDSL.reactiveFrom(org.drools.model.FlowDSL.reactiveFrom) Assertions(org.assertj.core.api.Assertions) KieSession(org.kie.api.runtime.KieSession) FlowDSL.declarationOf(org.drools.model.FlowDSL.declarationOf) TargetPolicy(org.drools.modelcompiler.domain.TargetPolicy) FlowDSL.or(org.drools.model.FlowDSL.or) EventProcessingOption(org.kie.api.conf.EventProcessingOption) Collection(java.util.Collection) FlowDSL.on(org.drools.model.FlowDSL.on) Customer(org.drools.modelcompiler.domain.Customer) Serializable(java.io.Serializable) ConstraintType(org.drools.model.Index.ConstraintType) List(java.util.List) Query(org.drools.model.Query) FlowDSL.executeScript(org.drools.model.FlowDSL.executeScript) InternationalAddress(org.drools.modelcompiler.oopathdtables.InternationalAddress) Assert.assertFalse(org.junit.Assert.assertFalse) KnowledgeBaseFactory(org.drools.core.impl.KnowledgeBaseFactory) FlowDSL.bind(org.drools.model.FlowDSL.bind) FlowDSL.valueOf(org.drools.model.FlowDSL.valueOf) Person(org.drools.modelcompiler.domain.Person) ObjectInput(java.io.ObjectInput) InOperator(org.drools.model.operators.InOperator) FlowDSL.when(org.drools.model.FlowDSL.when) ModelImpl(org.drools.model.impl.ModelImpl) FlowDSL.globalOf(org.drools.model.FlowDSL.globalOf) StockTick(org.drools.modelcompiler.domain.StockTick) Employee.createEmployee(org.drools.modelcompiler.domain.Employee.createEmployee) ClockType(org.drools.core.ClockType) FlowDSL.accumulate(org.drools.model.FlowDSL.accumulate) ArrayList(java.util.ArrayList) Result(org.drools.modelcompiler.domain.Result) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Adult(org.drools.modelcompiler.domain.Adult) Employee(org.drools.modelcompiler.domain.Employee) FlowDSL.execute(org.drools.model.FlowDSL.execute) Woman(org.drools.modelcompiler.domain.Woman) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) FlowDSL.rule(org.drools.model.FlowDSL.rule) FlowDSL.expr(org.drools.model.FlowDSL.expr) Variable(org.drools.model.Variable) FlowDSL.query(org.drools.model.FlowDSL.query) Address(org.drools.modelcompiler.domain.Address) FlowDSL.window(org.drools.model.FlowDSL.window) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) FlowDSL.input(org.drools.model.FlowDSL.input) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) TimeUnit(java.util.concurrent.TimeUnit) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) Assert.assertNull(org.junit.Assert.assertNull) FlowDSL.forall(org.drools.model.FlowDSL.forall) Rule(org.drools.model.Rule) Query1Def(org.drools.model.Query1Def) Assert.assertEquals(org.junit.Assert.assertEquals) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) StockTick(org.drools.modelcompiler.domain.StockTick) KieBase(org.kie.api.KieBase) Rule(org.drools.model.Rule) Model(org.drools.model.Model) KieSession(org.kie.api.runtime.KieSession) ModelImpl(org.drools.model.impl.ModelImpl) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 67 with SessionPseudoClock

use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.

the class CepTest method testExpires.

@Test
public void testExpires() throws Exception {
    String str = "package org.drools.compiler;\n" + "import " + StockFact.class.getCanonicalName() + ";\n" + "\n" + "declare StockFact\n" + "    @role( value = event )\n" + "    @expires( value = 2s, policy = TIME_SOFT )\n" + "end\n" + "\n" + "rule \"expiration\"\n" + "when\n" + "   StockFact( company == \"DROO\" )\n" + "then\n" + "end";
    KieSession ksession = getKieSession(getCepKieModuleModel(), str);
    SessionPseudoClock clock = ksession.getSessionClock();
    ksession.insert(new StockFact("DROO"));
    clock.advanceTime(1, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, ksession.getObjects().size());
    clock.advanceTime(2, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(0, ksession.getObjects().size());
}
Also used : StockFact(org.drools.modelcompiler.domain.StockFact) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 68 with SessionPseudoClock

use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.

the class CepTest method testExpireEventOnEndTimestampWithDeclaredEvent.

@Test
public void testExpireEventOnEndTimestampWithDeclaredEvent() throws Exception {
    String str = "package org.drools.compiler;\n" + "import " + StockFact.class.getCanonicalName() + ";\n" + "global java.util.List resultsAfter;\n" + "\n" + "declare StockFact\n" + "    @role( event )\n" + "    @duration( duration )\n" + "end\n" + "\n" + "rule \"after[60,80]\"\n" + "when\n" + "$a : StockFact( company == \"DROO\" )\n" + "$b : StockFact( company == \"ACME\", this after[60,80] $a )\n" + "then\n" + "       resultsAfter.add( $b );\n" + "end";
    KieSession ksession = getKieSession(getCepKieModuleModel(), str);
    SessionPseudoClock clock = ksession.getSessionClock();
    List<StockTick> resultsAfter = new ArrayList<StockTick>();
    ksession.setGlobal("resultsAfter", resultsAfter);
    // inserting new StockTick with duration 30 at time 0 => rule
    // after[60,80] should fire when ACME lasts at 100-120
    ksession.insert(new StockFact("DROO", 30));
    clock.advanceTime(100, TimeUnit.MILLISECONDS);
    ksession.insert(new StockFact("ACME", 20));
    ksession.fireAllRules();
    assertEquals(1, resultsAfter.size());
}
Also used : StockFact(org.drools.modelcompiler.domain.StockFact) StockTick(org.drools.modelcompiler.domain.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 69 with SessionPseudoClock

use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.

the class CepTest method testDeclareAndExpires.

@Test
public void testDeclareAndExpires() throws Exception {
    String str = "package org.drools.compiler;\n" + "declare StockFact\n" + "    @role( value = event )\n" + "    @expires( value = 2s, policy = TIME_SOFT )\n" + "    company : String\n" + "    duration : long\n" + "end\n" + "\n" + "rule \"expiration\"\n" + "when\n" + "   StockFact( company == \"DROO\" )\n" + "then\n" + "end";
    KieSession ksession = getKieSession(getCepKieModuleModel(), str);
    SessionPseudoClock clock = ksession.getSessionClock();
    FactType stockFactType = ksession.getKieBase().getFactType("org.drools.compiler", "StockFact");
    Object DROO = stockFactType.newInstance();
    stockFactType.set(DROO, "company", "DROO");
    ksession.insert(DROO);
    clock.advanceTime(1, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(1, ksession.getObjects().size());
    clock.advanceTime(2, TimeUnit.SECONDS);
    ksession.fireAllRules();
    assertEquals(0, ksession.getObjects().size());
}
Also used : SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 70 with SessionPseudoClock

use of org.kie.api.time.SessionPseudoClock in project drools by kiegroup.

the class CepTest method testNotAfter.

@Test
public void testNotAfter() throws Exception {
    String str = "import " + StockTick.class.getCanonicalName() + ";" + "rule R when\n" + "    $a : StockTick( company == \"DROO\" )\n" + "    not( StockTick( company == \"ACME\", this after[5s,8s] $a ) )\n" + "then\n" + "  System.out.println(\"fired\");\n" + "end\n";
    KieSession ksession = getKieSession(getCepKieModuleModel(), str);
    SessionPseudoClock clock = ksession.getSessionClock();
    ksession.insert(new StockTick("DROO"));
    clock.advanceTime(6, TimeUnit.SECONDS);
    ksession.insert(new StockTick("ACME"));
    clock.advanceTime(10, TimeUnit.SECONDS);
    assertEquals(0, ksession.fireAllRules());
    ksession.insert(new StockTick("DROO"));
    clock.advanceTime(3, TimeUnit.SECONDS);
    ksession.insert(new StockTick("ACME"));
    clock.advanceTime(10, TimeUnit.SECONDS);
    assertEquals(1, ksession.fireAllRules());
}
Also used : StockTick(org.drools.modelcompiler.domain.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

SessionPseudoClock (org.kie.api.time.SessionPseudoClock)74 Test (org.junit.Test)66 KieSession (org.kie.api.runtime.KieSession)52 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)39 KieBase (org.kie.api.KieBase)37 ArrayList (java.util.ArrayList)30 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)20 MessageEvent (org.drools.testcoverage.common.model.MessageEvent)15 EntryPoint (org.kie.api.runtime.rule.EntryPoint)15 StockTick (org.drools.compiler.StockTick)14 Message (org.drools.testcoverage.common.model.Message)14 List (java.util.List)13 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)13 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)12 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)11 StockTick (org.drools.modelcompiler.domain.StockTick)10 OrderEvent (org.drools.compiler.OrderEvent)6 QueryResults (org.kie.api.runtime.rule.QueryResults)4 KieHelper (org.kie.internal.utils.KieHelper)4 StockTickInterface (org.drools.compiler.StockTickInterface)3