Search in sources :

Example 1 with StockTick

use of org.drools.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.compiler").newRule().name("r1").lhs().pattern("StockTick").constraint("company == \"RHT\"").end().end().rhs("// some comment").end().getDescr();
    KiePackage kpkg = compilePkgDescr(pkg);
    assertEquals("org.drools.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.compiler.StockTick) KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 2 with StockTick

use of org.drools.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.compiler").newRule().name("r1").lhs().pattern("StockTick").constraint("company == \"RHT\"").end().end().end().getDescr();
    KiePackage kpkg = compilePkgDescr(pkg);
    assertEquals("org.drools.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.compiler.StockTick) KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 3 with StockTick

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

the class NamedConsequencesTest method testNamedConsequenceOnEvents.

@Test
public void testNamedConsequenceOnEvents() {
    // DROOLS-641
    String drl = "import " + StockTick.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "declare StockTick \n" + "    @role( event )" + "    @timestamp( time )\n" + "end\n" + "rule R when\n" + "    $s1 : StockTick( company == \"XXX\" )\n" + "    $s2 : StockTick( price > $s1.price ) do[t1]\n" + "    $s3 : StockTick( price < $s1.price )\n" + "then\n" + "    list.add( \"t0:\" + $s3.getCompany() );\n" + "then[t1]\n" + "    list.add( \"t1:\" + $s2.getCompany() );\n" + "end\n";
    KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.insert(new StockTick(1L, "XXX", 10, 0L));
    ksession.insert(new StockTick(2L, "YYY", 15, 1L));
    ksession.insert(new StockTick(3L, "ZZZ", 5, 2L));
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.containsAll(asList("t1:YYY", "t0:ZZZ")));
}
Also used : StockTick(org.drools.compiler.StockTick) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 4 with StockTick

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

the class CepEspTest method testIdentityAssertBehaviorOnEntryPoints.

@Test(timeout = 10000)
public void testIdentityAssertBehaviorOnEntryPoints() throws IOException, ClassNotFoundException {
    StockTickInterface st1 = new StockTick(1, "RHT", 10, 10);
    StockTickInterface st2 = new StockTick(1, "RHT", 10, 10);
    StockTickInterface st3 = new StockTick(2, "RHT", 15, 20);
    final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    kbconf.setOption(EqualityBehaviorOption.IDENTITY);
    final KieBase kbase1 = loadKnowledgeBase(kbconf, "test_CEP_AssertBehaviorOnEntryPoints.drl");
    final KieSession ksession = kbase1.newKieSession();
    AgendaEventListener ael1 = mock(AgendaEventListener.class);
    ksession.addEventListener(ael1);
    EntryPoint ep1 = ksession.getEntryPoint("stocktick stream");
    FactHandle fh1 = ep1.insert(st1);
    FactHandle fh1_2 = ep1.insert(st1);
    FactHandle fh2 = ep1.insert(st2);
    FactHandle fh3 = ep1.insert(st3);
    assertSame(fh1, fh1_2);
    assertNotSame(fh1, fh2);
    assertNotSame(fh1, fh3);
    assertNotSame(fh2, fh3);
    ksession.fireAllRules();
    // must have fired 3 times, one for each event identity
    verify(ael1, times(3)).afterMatchFired(any(AfterMatchFiredEvent.class));
    ksession.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTickInterface(org.drools.compiler.StockTickInterface) StockTick(org.drools.compiler.StockTick) InternalFactHandle(org.drools.core.common.InternalFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) DefaultAgendaEventListener(org.kie.api.event.rule.DefaultAgendaEventListener) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) DebugAgendaEventListener(org.kie.api.event.rule.DebugAgendaEventListener) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 5 with StockTick

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

the class CepEspTest method testCloudModeExpiration.

@Test(timeout = 10000)
public void testCloudModeExpiration() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException, InterruptedException {
    String str = "package org.drools.cloud\n" + "import org.drools.compiler.*\n" + "declare Event\n" + "        @role ( event )\n" + "        name : String\n" + "        value : Object\n" + "end\n" + "declare AnotherEvent\n" + "        @role ( event )\n" + "        message : String\n" + "        type : String\n" + "end\n" + "declare StockTick\n" + "        @role ( event )\n" + "end\n" + "rule \"two events\"\n" + "    when\n" + "        Event( value != null ) from entry-point X\n" + "        StockTick( company != null ) from entry-point X\n" + "    then\n" + "end";
    KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.CLOUD);
    KieBase kbase = loadKnowledgeBaseFromString(config, str);
    KieSession ksession = createKnowledgeSession(kbase);
    EntryPoint ep = ksession.getEntryPoint("X");
    ep.insert(new StockTick(1, "RHT", 10, 1000));
    int rulesFired = ksession.fireAllRules();
    assertEquals(0, rulesFired);
    org.kie.api.definition.type.FactType event = kbase.getFactType("org.drools.cloud", "Event");
    Object e1 = event.newInstance();
    event.set(e1, "name", "someKey");
    event.set(e1, "value", "someValue");
    ep.insert(e1);
    rulesFired = ksession.fireAllRules();
    assertEquals(1, rulesFired);
    // let some time be spent
    Thread.currentThread().sleep(1000);
    // check both events are still in memory as we are running in CLOUD mode
    assertEquals(2, ep.getFactCount());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Aggregations

StockTick (org.drools.compiler.StockTick)20 Test (org.junit.Test)18 KieSession (org.kie.api.runtime.KieSession)18 EntryPoint (org.kie.api.runtime.rule.EntryPoint)12 ArrayList (java.util.ArrayList)10 KieBase (org.kie.api.KieBase)9 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)9 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)8 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)7 StockTickInterface (org.drools.compiler.StockTickInterface)6 InternalFactHandle (org.drools.core.common.InternalFactHandle)5 KiePackage (org.kie.api.definition.KiePackage)5 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)4 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)4 FactHandle (org.kie.api.runtime.rule.FactHandle)4 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)3 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)3 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)3 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)2 EventFactHandle (org.drools.core.common.EventFactHandle)2