use of org.drools.modelcompiler.domain.StockTick 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.drools.modelcompiler.domain.StockTick 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.drools.modelcompiler.domain.StockTick 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());
}
use of org.drools.modelcompiler.domain.StockTick in project drools by kiegroup.
the class CepTest method testSlidingWindow.
@Test
public void testSlidingWindow() throws Exception {
String str = "import " + StockTick.class.getCanonicalName() + ";\n" + "rule R when\n" + " $a : StockTick( company == \"DROO\" ) over window:length( 2 )\n" + "then\n" + " System.out.println(\"fired\");\n" + "end\n";
KieSession ksession = getKieSession(getCepKieModuleModel(), str);
SessionPseudoClock clock = ksession.getSessionClock();
clock.advanceTime(1, TimeUnit.SECONDS);
ksession.insert(new StockTick("DROO"));
clock.advanceTime(1, TimeUnit.SECONDS);
ksession.insert(new StockTick("DROO"));
clock.advanceTime(1, TimeUnit.SECONDS);
ksession.insert(new StockTick("ACME"));
clock.advanceTime(1, TimeUnit.SECONDS);
ksession.insert(new StockTick("DROO"));
assertEquals(2, ksession.fireAllRules());
}
use of org.drools.modelcompiler.domain.StockTick in project drools by kiegroup.
the class CepTest method testDeclaredSlidingWindow.
@Test
public void testDeclaredSlidingWindow() throws Exception {
String str = "import " + StockTick.class.getCanonicalName() + ";\n" + "declare window DeclaredWindow\n" + " StockTick( company == \"DROO\" ) over window:time( 5s )\n" + "end\n" + "rule R when\n" + " $a : StockTick() from window DeclaredWindow\n" + "then\n" + " System.out.println($a.getCompany());\n" + "end\n";
KieSession ksession = getKieSession(getCepKieModuleModel(), str);
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());
}
Aggregations