Search in sources :

Example 1 with StockTick

use of org.drools.mvel.compiler.StockTick in project drools by kiegroup.

the class DeserializationWithCompositeTriggerTest method testSerializationAndDeserialization.

/**
 * Verifies that serialization of a rule with composite trigger does not fail on
 * org.drools.core.time.impl.CompositeMaxDurationTrigger class serialization.
 */
@Test
public void testSerializationAndDeserialization() throws Exception {
    this.ksession.insert(new StockTick(2, "AAA", 1.0, 0));
    this.ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
    Assertions.assertThat(this.ksession).isNotNull();
}
Also used : StockTick(org.drools.mvel.compiler.StockTick) Test(org.junit.Test)

Example 2 with StockTick

use of org.drools.mvel.compiler.StockTick in project drools by kiegroup.

the class DescrBuilderTest method testTopLevelAccumulate.

@Test
public void testTopLevelAccumulate() throws InstantiationException, IllegalAccessException {
    PackageDescr pkg = DescrFactory.newPackage().name("org.drools.mvel.compiler").newRule().name("r1").lhs().accumulate().source().pattern("StockTick").constraint("company == \"RHT\"").bind("$p", "price", false).end().end().function("sum", "$sum", false, "$p").function("count", "$cnt", false, "$p").end().end().rhs("// some comment").end().getDescr();
    KiePackage kpkg = compilePkgDescr(pkg, "org.drools.mvel.compiler");
    assertEquals("org.drools.mvel.compiler", kpkg.getName());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Collections.singletonList(kpkg));
    KieSession ksession = kbase.newKieSession();
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    ksession.insert(new StockTick(1, "RHT", 80, 1));
    ksession.insert(new StockTick(2, "RHT", 100, 10));
    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
    ArgumentCaptor<AfterMatchFiredEvent> cap = ArgumentCaptor.forClass(AfterMatchFiredEvent.class);
    verify(ael).afterMatchFired(cap.capture());
    assertThat(((Number) cap.getValue().getMatch().getDeclarationValue("$sum")).intValue(), is(180));
    assertThat(((Number) cap.getValue().getMatch().getDeclarationValue("$cnt")).intValue(), is(2));
}
Also used : StockTick(org.drools.mvel.compiler.StockTick) KiePackage(org.kie.api.definition.KiePackage) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 3 with StockTick

use of org.drools.mvel.compiler.StockTick in project drools by kiegroup.

the class DescrBuilderTest method testRule.

@Test
public void testRule() throws InstantiationException, IllegalAccessException {
    PackageDescrBuilder packBuilder = DescrFactory.newPackage().name("org.drools.mvel.compiler").newRule().name("r1").lhs().and().or().pattern("StockTick").constraint("price > 100").end().pattern("StockTick").constraint("price < 10").end().end().pattern("StockTick").constraint("company == \"RHT\"").end().end().end().rhs("    System.out.println(\"foo\");\n").end();
    PackageDescr pkg = packBuilder.getDescr();
    String drl = new DrlDumper().dump(packBuilder.getDescr());
    System.out.println(drl);
    KiePackage kpkg = compilePkgDescr(pkg);
    assertEquals("org.drools.mvel.compiler", kpkg.getName());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Collections.singletonList(kpkg));
    KieSession ksession = kbase.newKieSession();
    ksession.insert(new StockTick(1, "RHT", 80, 1));
    int rules = ksession.fireAllRules();
    assertEquals(0, rules);
    ksession = kbase.newKieSession();
    ksession.insert(new StockTick(2, "RHT", 150, 1));
    rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : PackageDescrBuilder(org.drools.drl.ast.dsl.PackageDescrBuilder) StockTick(org.drools.mvel.compiler.StockTick) KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) DrlDumper(org.drools.mvel.DrlDumper) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 4 with StockTick

use of org.drools.mvel.compiler.StockTick in project drools by kiegroup.

the class DescrBuilderTest method testRuleRHSComment.

@Test
public void testRuleRHSComment() throws InstantiationException, IllegalAccessException {
    PackageDescr pkg = DescrFactory.newPackage().name("org.drools.mvel.compiler").newRule().name("r1").lhs().pattern("StockTick").constraint("company == \"RHT\"").end().end().rhs("// some comment").end().getDescr();
    KiePackage kpkg = compilePkgDescr(pkg);
    assertEquals("org.drools.mvel.compiler", kpkg.getName());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Collections.singletonList(kpkg));
    KieSession ksession = kbase.newKieSession();
    ksession.insert(new StockTick(1, "RHT", 80, 1));
    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}
Also used : StockTick(org.drools.mvel.compiler.StockTick) KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 5 with StockTick

use of org.drools.mvel.compiler.StockTick in project drools by kiegroup.

the class LifecycleTest method testExpires.

@Test
public void testExpires() throws Exception {
    EntryPoint entryPoint = kieSession.getEntryPoint("EventStream");
    StockTick event = new StockTick();
    FactHandle handle = entryPoint.insert(event);
    assertTrue(entryPoint.getFactHandles().contains(handle));
    kieSession.fireAllRules();
    assertTrue(entryPoint.getFactHandles().contains(handle));
    advanceTime(5, TimeUnit.SECONDS);
    kieSession.fireAllRules();
    assertFalse(entryPoint.getFactHandles().contains(handle));
}
Also used : StockTick(org.drools.mvel.compiler.StockTick) FactHandle(org.kie.api.runtime.rule.FactHandle) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Aggregations

StockTick (org.drools.mvel.compiler.StockTick)13 Test (org.junit.Test)11 KieSession (org.kie.api.runtime.KieSession)10 EntryPoint (org.kie.api.runtime.rule.EntryPoint)7 ArrayList (java.util.ArrayList)5 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)5 KieBase (org.kie.api.KieBase)5 KiePackage (org.kie.api.definition.KiePackage)5 PackageDescr (org.drools.drl.ast.descr.PackageDescr)4 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)3 KieBaseTestConfiguration (org.drools.testcoverage.common.util.KieBaseTestConfiguration)2 List (java.util.List)1 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)1 ExecutorService (java.util.concurrent.ExecutorService)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 PseudoClockScheduler (org.drools.core.time.impl.PseudoClockScheduler)1 PackageDescrBuilder (org.drools.drl.ast.dsl.PackageDescrBuilder)1 DrlDumper (org.drools.mvel.DrlDumper)1 SpecialString (org.drools.mvel.compiler.SpecialString)1 StockTickInterface (org.drools.mvel.compiler.StockTickInterface)1