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);
}
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);
}
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")));
}
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();
}
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());
}
Aggregations