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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations