use of org.drools.modelcompiler.domain.StockTick in project drools by kiegroup.
the class PatternDSLTest method testAfterWithAnd.
@Test
public void testAfterWithAnd() throws Exception {
Variable<StockTick> var_$a = declarationOf(StockTick.class, "$a");
Variable<StockTick> var_$b = declarationOf(StockTick.class, "$b");
Rule rule = rule("R").build(pattern(var_$a).expr("$expr$3$", (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCompany(), "DROO"), alphaIndexedBy(java.lang.String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getCompany(), "DROO"), reactOn("company")), pattern(var_$b).and().expr("$expr$5$", (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCompany(), "ACME"), alphaIndexedBy(java.lang.String.class, org.drools.model.Index.ConstraintType.EQUAL, 0, _this -> _this.getCompany(), "ACME"), reactOn("company")).expr("$expr$6$", var_$a, after(5L, java.util.concurrent.TimeUnit.SECONDS, 8L, java.util.concurrent.TimeUnit.SECONDS)).endAnd(), execute(() -> {
System.out.println("fired");
}));
Model model = new ModelImpl().addRule(rule);
KieBase kieBase = KieBaseBuilder.createKieBaseFromModel(model, EventProcessingOption.STREAM);
KieSessionConfiguration conf = KieServices.get().newKieSessionConfiguration();
conf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = kieBase.newKieSession(conf, null);
SessionPseudoClock clock = ksession.getSessionClock();
ksession.insert(new StockTick("DROO"));
clock.advanceTime(6, TimeUnit.SECONDS);
ksession.insert(new StockTick("ACME"));
assertEquals(1, ksession.fireAllRules());
clock.advanceTime(4, TimeUnit.SECONDS);
ksession.insert(new StockTick("ACME"));
assertEquals(0, ksession.fireAllRules());
}
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 testAfterOnLongFieldsBindingFirst.
@Test
public void testAfterOnLongFieldsBindingFirst() throws Exception {
String str = "import " + StockTick.class.getCanonicalName() + ";\n" + "declare StockTick @timestamp(timeFieldAsLong) end\n" + "rule R when\n" + " $a : StockTick( company == \"DROO\" )\n" + " StockTick( company == \"ACME\", $a.timeFieldAsLong after[5,8] timeFieldAsLong )\n" + "then\n" + " System.out.println(\"fired\");\n" + "end\n";
KieSession ksession = getKieSession(getCepKieModuleModel(), str);
SessionPseudoClock clock = ksession.getSessionClock();
ksession.insert(new StockTick("ACME").setTimeField(0));
ksession.insert(new StockTick("DROO").setTimeField(6));
assertEquals(1, ksession.fireAllRules());
ksession.insert(new StockTick("DROO").setTimeField(10));
assertEquals(0, ksession.fireAllRules());
}
use of org.drools.modelcompiler.domain.StockTick in project drools by kiegroup.
the class CepTest method testLiteral.
@Test
public void testLiteral() throws Exception {
String str = "import " + StockTick.class.getCanonicalName() + ";" + "declare StockTick @timestamp(timeFieldAsLong) end\n" + "rule R when\n" + " $a : StockTick( timeFieldAsLong after \"01-Jan-2016\" )\n" + "then\n" + " System.out.println(\"fired\");\n" + "end\n";
KieSession ksession = getKieSession(getCepKieModuleModel(), str);
long time = LocalDateTime.of(2020, 1, 1, 0, 0, 0).atZone(ZoneId.of("UTC")).toInstant().getEpochSecond() * 1000;
ksession.insert(new StockTick("DROO").setTimeField(time));
assertEquals(1, ksession.fireAllRules());
}
use of org.drools.modelcompiler.domain.StockTick in project drools by kiegroup.
the class CepTest method testBeforeOnLongFieldsWithDifferentMethod.
@Test
public void testBeforeOnLongFieldsWithDifferentMethod() throws Exception {
String str = "import " + StockTick.class.getCanonicalName() + ";\n" + "import " + StockTickEx.class.getCanonicalName() + ";\n" + "declare StockTick @timestamp(timeFieldAsLong) end\n" + "declare StockTickEx @timestamp(timeFieldExAsLong) end\n" + "rule R when\n" + " $a : StockTickEx( company == \"DROO\" )\n" + " StockTick( company == \"ACME\", timeFieldAsLong before[5,8] $a.timeFieldExAsLong )\n" + "then\n" + " System.out.println(\"fired\");\n" + "end\n";
KieSession ksession = getKieSession(getCepKieModuleModel(), str);
SessionPseudoClock clock = ksession.getSessionClock();
ksession.insert(new StockTick("ACME").setTimeField(0));
ksession.insert(new StockTickEx("DROO").setTimeFieldEx(6));
assertEquals(1, ksession.fireAllRules());
ksession.insert(new StockTickEx("DROO").setTimeFieldEx(10));
assertEquals(0, ksession.fireAllRules());
}
Aggregations