Search in sources :

Example 6 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 7 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)

Example 8 with StockTick

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

the class LengthSlidingWindowTest method testCompilationFailureWithUnknownWindow.

@Test
public void testCompilationFailureWithUnknownWindow() {
    // DROOLS-841
    String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "global java.util.List list;\n" + "declare StockTick @role( event ) end\n" + "declare window RhtStocksWindow\n" + "    StockTick() over window:length( 3 )\n" + "end\n" + "rule R\n" + "when \n" + "   accumulate( StockTick( company == \"RHT\", $price : price ) from window AbcStocksWindow; $total : sum($price) )\n" + "then \n" + "    list.add($total);\n" + "end \n";
    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
    final KieBuilder kieBuilder = KieUtil.getKieBuilderFromKieFileSystem(kieBaseTestConfiguration, kfs, false);
    assertEquals(1, kieBuilder.getResults().getMessages().size());
}
Also used : StockTick(org.drools.mvel.compiler.StockTick) KieFileSystem(org.kie.api.builder.KieFileSystem) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 9 with StockTick

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

the class LengthSlidingWindowTest method checkPrice.

private void checkPrice(String drl, double expectedPrice) {
    KieSessionConfiguration sessionConfig = RuleBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, drl);
    KieSession ksession = kbase.newKieSession(sessionConfig, null);
    List<Double> list = new ArrayList<Double>();
    ksession.setGlobal("list", list);
    ksession.insert("RHT");
    ksession.insert(new StockTick(1L, "RHT", 10.0));
    ksession.insert(new StockTick(2L, "RHT", 10.0));
    ksession.insert(new StockTick(3L, "ABC", 20.0));
    ksession.insert(new StockTick(4L, "RHT", 10.0));
    ksession.insert(new StockTick(5L, "XYZ", 20.0));
    ksession.insert(new StockTick(6L, "XYZ", 20.0));
    ksession.insert(new StockTick(7L, "RHT", 10.0));
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(expectedPrice, (double) list.get(0), 0.01);
}
Also used : StockTick(org.drools.mvel.compiler.StockTick) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration)

Example 10 with StockTick

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

the class DescrBuilderTest method testRuleRHSOptional.

@Test
public void testRuleRHSOptional() throws InstantiationException, IllegalAccessException {
    PackageDescr pkg = DescrFactory.newPackage().name("org.drools.mvel.compiler").newRule().name("r1").lhs().pattern("StockTick").constraint("company == \"RHT\"").end().end().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)

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