Search in sources :

Example 31 with StockTick

use of org.drools.modelcompiler.domain.StockTick in project drools by kiegroup.

the class AccumulateTest method testAccumulateWithMaxCalendar.

@Test
public void testAccumulateWithMaxCalendar() {
    String str = "import " + StockTick.class.getCanonicalName() + ";\n" + "rule AccumulateMaxDate\n" + "  dialect \"java\"\n" + "  when\n" + "  $max1 : Number() from accumulate(\n" + "    StockTick($time : dueDate);\n" + "    max($time.getTime().getTime()))\n" + "then\n" + "end\n";
    KieSession ksession = getKieSession(str);
    StockTick st = new StockTick("RHT");
    st.setDueDate(Calendar.getInstance());
    ksession.insert(st);
    Assertions.assertThat(ksession.fireAllRules()).isEqualTo(1);
}
Also used : StockTick(org.drools.modelcompiler.domain.StockTick) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 32 with StockTick

use of org.drools.modelcompiler.domain.StockTick in project drools by kiegroup.

the class PatternDSLTest method testAfterOnLongFields.

@Test
public void testAfterOnLongFields() 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$1$", (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCompany(), "DROO"), alphaIndexedBy(String.class, Index.ConstraintType.EQUAL, 0, _this -> _this.getCompany(), "DROO"), reactOn("company")), pattern(var_$b).expr("$expr$2$", (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCompany(), "ACME"), alphaIndexedBy(String.class, Index.ConstraintType.EQUAL, 0, _this -> _this.getCompany(), "ACME"), reactOn("company")).expr("$expr$3$", _this -> _this.getTimeFieldAsLong(), var_$a, $a -> $a.getTimeFieldAsLong(), after(5, java.util.concurrent.TimeUnit.MILLISECONDS, 8, java.util.concurrent.TimeUnit.MILLISECONDS)), 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").setTimeField(0));
    clock.advanceTime(6, TimeUnit.MILLISECONDS);
    ksession.insert(new StockTick("ACME").setTimeField(6));
    assertEquals(1, ksession.fireAllRules());
    clock.advanceTime(4, TimeUnit.MILLISECONDS);
    ksession.insert(new StockTick("ACME").setTimeField(10));
    assertEquals(0, ksession.fireAllRules());
}
Also used : Arrays(java.util.Arrays) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) Man(org.drools.modelcompiler.domain.Man) Global(org.drools.model.Global) Toy(org.drools.modelcompiler.domain.Toy) PatternDSL.reactOn(org.drools.model.PatternDSL.reactOn) DSL(org.drools.model.DSL) Relationship(org.drools.modelcompiler.domain.Relationship) BaseModelTest.getObjectsIntoList(org.drools.modelcompiler.BaseModelTest.getObjectsIntoList) Query2Def(org.drools.model.Query2Def) PatternDSL.declarationOf(org.drools.model.PatternDSL.declarationOf) Child(org.drools.modelcompiler.domain.Child) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryResults(org.kie.api.runtime.rule.QueryResults) Assertions(org.assertj.core.api.Assertions) KieSession(org.kie.api.runtime.KieSession) QueryImpl(org.drools.core.rule.QueryImpl) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) PatternDSL.rule(org.drools.model.PatternDSL.rule) EventProcessingOption(org.kie.api.conf.EventProcessingOption) Collection(java.util.Collection) Activation(org.drools.core.spi.Activation) Index(org.drools.model.Index) Predicate1(org.drools.model.functions.Predicate1) PatternDSL.on(org.drools.model.PatternDSL.on) PatternDSL.pattern(org.drools.model.PatternDSL.pattern) List(java.util.List) Query(org.drools.model.Query) PatternDSL.or(org.drools.model.PatternDSL.or) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PatternDSL.globalOf(org.drools.model.PatternDSL.globalOf) Person(org.drools.modelcompiler.domain.Person) PatternDSL.valueOf(org.drools.model.PatternDSL.valueOf) ModelImpl(org.drools.model.impl.ModelImpl) ClassObjectType(org.drools.core.base.ClassObjectType) PatternDSL.execute(org.drools.model.PatternDSL.execute) StockTick(org.drools.modelcompiler.domain.StockTick) Pattern(org.drools.core.rule.Pattern) PatternDSL(org.drools.model.PatternDSL) PatternDSL.after(org.drools.model.PatternDSL.after) ClockType(org.drools.core.ClockType) ArrayList(java.util.ArrayList) Result(org.drools.modelcompiler.domain.Result) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Assert.assertSame(org.junit.Assert.assertSame) Adult(org.drools.modelcompiler.domain.Adult) PatternDSL.alphaIndexedBy(org.drools.model.PatternDSL.alphaIndexedBy) PatternDSL.query(org.drools.model.PatternDSL.query) PatternDSL.reactiveFrom(org.drools.model.PatternDSL.reactiveFrom) KieServices(org.kie.api.KieServices) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Woman(org.drools.modelcompiler.domain.Woman) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) PatternDSL.and(org.drools.model.PatternDSL.and) ViewItem(org.drools.model.view.ViewItem) Pair(org.apache.commons.math3.util.Pair) Variable(org.drools.model.Variable) D(org.drools.modelcompiler.dsl.pattern.D) IOException(java.io.IOException) Test(org.junit.Test) PatternDSL.accumulate(org.drools.model.PatternDSL.accumulate) PatternDSL.not(org.drools.model.PatternDSL.not) PatternDSL.when(org.drools.model.PatternDSL.when) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) PatternDSL.accFunction(org.drools.model.PatternDSL.accFunction) TimeUnit(java.util.concurrent.TimeUnit) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.drools.model.Rule) CollectSetAccumulateFunction(org.drools.core.base.accumulators.CollectSetAccumulateFunction) PatternDSL.betaIndexedBy(org.drools.model.PatternDSL.betaIndexedBy) Accumulate(org.drools.core.rule.Accumulate) DSL.supply(org.drools.model.DSL.supply) Assert.assertEquals(org.junit.Assert.assertEquals) StockTick(org.drools.modelcompiler.domain.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) KieSession(org.kie.api.runtime.KieSession) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 33 with StockTick

use of org.drools.modelcompiler.domain.StockTick in project drools by kiegroup.

the class PatternDSLTest method testNegatedAfter.

@Test
public void testNegatedAfter() 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$1$", (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCompany(), "DROO"), alphaIndexedBy(String.class, Index.ConstraintType.EQUAL, 0, _this -> _this.getCompany(), "DROO"), reactOn("company")), pattern(var_$b).expr("$expr$2$", (_this) -> org.drools.modelcompiler.util.EvaluationUtil.areNullSafeEquals(_this.getCompany(), "ACME"), alphaIndexedBy(String.class, Index.ConstraintType.EQUAL, 0, _this -> _this.getCompany(), "ACME"), reactOn("company")).expr("$expr$3$", var_$a, not(after(5, java.util.concurrent.TimeUnit.SECONDS, 8, java.util.concurrent.TimeUnit.SECONDS))), 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(0, ksession.fireAllRules());
    clock.advanceTime(4, TimeUnit.SECONDS);
    ksession.insert(new StockTick("ACME"));
    assertEquals(1, ksession.fireAllRules());
}
Also used : Arrays(java.util.Arrays) CoreMatchers.hasItem(org.hamcrest.CoreMatchers.hasItem) Man(org.drools.modelcompiler.domain.Man) Global(org.drools.model.Global) Toy(org.drools.modelcompiler.domain.Toy) PatternDSL.reactOn(org.drools.model.PatternDSL.reactOn) DSL(org.drools.model.DSL) Relationship(org.drools.modelcompiler.domain.Relationship) BaseModelTest.getObjectsIntoList(org.drools.modelcompiler.BaseModelTest.getObjectsIntoList) Query2Def(org.drools.model.Query2Def) PatternDSL.declarationOf(org.drools.model.PatternDSL.declarationOf) Child(org.drools.modelcompiler.domain.Child) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) QueryResults(org.kie.api.runtime.rule.QueryResults) Assertions(org.assertj.core.api.Assertions) KieSession(org.kie.api.runtime.KieSession) QueryImpl(org.drools.core.rule.QueryImpl) LambdaConstraint(org.drools.modelcompiler.constraints.LambdaConstraint) PatternDSL.rule(org.drools.model.PatternDSL.rule) EventProcessingOption(org.kie.api.conf.EventProcessingOption) Collection(java.util.Collection) Activation(org.drools.core.spi.Activation) Index(org.drools.model.Index) Predicate1(org.drools.model.functions.Predicate1) PatternDSL.on(org.drools.model.PatternDSL.on) PatternDSL.pattern(org.drools.model.PatternDSL.pattern) List(java.util.List) Query(org.drools.model.Query) PatternDSL.or(org.drools.model.PatternDSL.or) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PatternDSL.globalOf(org.drools.model.PatternDSL.globalOf) Person(org.drools.modelcompiler.domain.Person) PatternDSL.valueOf(org.drools.model.PatternDSL.valueOf) ModelImpl(org.drools.model.impl.ModelImpl) ClassObjectType(org.drools.core.base.ClassObjectType) PatternDSL.execute(org.drools.model.PatternDSL.execute) StockTick(org.drools.modelcompiler.domain.StockTick) Pattern(org.drools.core.rule.Pattern) PatternDSL(org.drools.model.PatternDSL) PatternDSL.after(org.drools.model.PatternDSL.after) ClockType(org.drools.core.ClockType) ArrayList(java.util.ArrayList) Result(org.drools.modelcompiler.domain.Result) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Assert.assertSame(org.junit.Assert.assertSame) Adult(org.drools.modelcompiler.domain.Adult) PatternDSL.alphaIndexedBy(org.drools.model.PatternDSL.alphaIndexedBy) PatternDSL.query(org.drools.model.PatternDSL.query) PatternDSL.reactiveFrom(org.drools.model.PatternDSL.reactiveFrom) KieServices(org.kie.api.KieServices) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Woman(org.drools.modelcompiler.domain.Woman) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) PatternDSL.and(org.drools.model.PatternDSL.and) ViewItem(org.drools.model.view.ViewItem) Pair(org.apache.commons.math3.util.Pair) Variable(org.drools.model.Variable) D(org.drools.modelcompiler.dsl.pattern.D) IOException(java.io.IOException) Test(org.junit.Test) PatternDSL.accumulate(org.drools.model.PatternDSL.accumulate) PatternDSL.not(org.drools.model.PatternDSL.not) PatternDSL.when(org.drools.model.PatternDSL.when) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) FactHandle(org.kie.api.runtime.rule.FactHandle) PatternDSL.accFunction(org.drools.model.PatternDSL.accFunction) TimeUnit(java.util.concurrent.TimeUnit) KieBaseBuilder(org.drools.modelcompiler.builder.KieBaseBuilder) ClockTypeOption(org.kie.api.runtime.conf.ClockTypeOption) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.drools.model.Rule) CollectSetAccumulateFunction(org.drools.core.base.accumulators.CollectSetAccumulateFunction) PatternDSL.betaIndexedBy(org.drools.model.PatternDSL.betaIndexedBy) Accumulate(org.drools.core.rule.Accumulate) DSL.supply(org.drools.model.DSL.supply) Assert.assertEquals(org.junit.Assert.assertEquals) StockTick(org.drools.modelcompiler.domain.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) KieBase(org.kie.api.KieBase) Model(org.drools.model.Model) KieSession(org.kie.api.runtime.KieSession) Rule(org.drools.model.Rule) ModelImpl(org.drools.model.impl.ModelImpl) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) Test(org.junit.Test)

Example 34 with StockTick

use of org.drools.modelcompiler.domain.StockTick in project drools by kiegroup.

the class NodeSharingTest method testShareCombinedConstraintOr.

@Test
public void testShareCombinedConstraintOr() throws Exception {
    // DROOLS-6330
    String str = "import " + StockTick.class.getCanonicalName() + ";" + "rule R1 when\n" + "    $a : StockTick( company == \"DROO\" )\n" + "    $b : StockTick( company == \"XXXX\" || this after[5s,8s] $a )\n" + "then\n" + "  System.out.println(\"fired\");\n" + "end\n" + "rule R2 when\n" + "    $a : StockTick( company == \"DROO\" )\n" + "    $b : StockTick( company == \"XXXX\" || this after[5s,8s] $a )\n" + "then\n" + "  System.out.println(\"fired\");\n" + "end\n";
    KieSession ksession = getKieSession(CepTest.getCepKieModuleModel(), str);
    SessionPseudoClock clock = ksession.getSessionClock();
    ksession.insert(new StockTick("DROO"));
    clock.advanceTime(6, TimeUnit.SECONDS);
    ksession.insert(new StockTick("ACME"));
    assertEquals(2, ksession.fireAllRules());
    clock.advanceTime(4, TimeUnit.SECONDS);
    ksession.insert(new StockTick("ACME"));
    assertEquals(0, ksession.fireAllRules());
    assertEquals(1, ReteDumper.collectNodes(ksession).stream().filter(JoinNode.class::isInstance).count());
}
Also used : StockTick(org.drools.modelcompiler.domain.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) JoinNode(org.drools.core.reteoo.JoinNode) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 35 with StockTick

use of org.drools.modelcompiler.domain.StockTick in project drools by kiegroup.

the class NodeSharingTest method testShareCombinedConstraintAnd.

@Test
public void testShareCombinedConstraintAnd() throws Exception {
    // DROOLS-6330
    // Note: if DROOLS-6329 is resolved, this test may not produce CombinedConstraint
    String str = "import " + StockTick.class.getCanonicalName() + ";" + "rule R1 when\n" + "    $a : StockTick( company == \"DROO\" )\n" + "    $b : StockTick( company == \"ACME\" && this after[5s,8s] $a )\n" + "then\n" + "  System.out.println(\"fired\");\n" + "end\n" + "rule R2 when\n" + "    $a : StockTick( company == \"DROO\" )\n" + "    $b : StockTick( company == \"ACME\" && this after[5s,8s] $a )\n" + "then\n" + "  System.out.println(\"fired\");\n" + "end\n";
    KieSession ksession = getKieSession(CepTest.getCepKieModuleModel(), str);
    SessionPseudoClock clock = ksession.getSessionClock();
    ksession.insert(new StockTick("DROO"));
    clock.advanceTime(6, TimeUnit.SECONDS);
    ksession.insert(new StockTick("ACME"));
    assertEquals(2, ksession.fireAllRules());
    clock.advanceTime(4, TimeUnit.SECONDS);
    ksession.insert(new StockTick("ACME"));
    assertEquals(0, ksession.fireAllRules());
    assertEquals(1, ReteDumper.collectNodes(ksession).stream().filter(JoinNode.class::isInstance).count());
}
Also used : StockTick(org.drools.modelcompiler.domain.StockTick) SessionPseudoClock(org.kie.api.time.SessionPseudoClock) JoinNode(org.drools.core.reteoo.JoinNode) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Aggregations

StockTick (org.drools.modelcompiler.domain.StockTick)35 Test (org.junit.Test)35 KieSession (org.kie.api.runtime.KieSession)35 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)29 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)4 Arrays (java.util.Arrays)4 Collection (java.util.Collection)4 List (java.util.List)4 TimeUnit (java.util.concurrent.TimeUnit)4 Assertions (org.assertj.core.api.Assertions)4 ClockType (org.drools.core.ClockType)4 Global (org.drools.model.Global)4 Model (org.drools.model.Model)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Query (org.drools.model.Query)3 Query2Def (org.drools.model.Query2Def)3 Rule (org.drools.model.Rule)3 Variable (org.drools.model.Variable)3 ModelImpl (org.drools.model.impl.ModelImpl)3