use of org.drools.compiler.StockTick in project drools by kiegroup.
the class CepEspTest method testDeserializationWithCompositeTrigger.
@Test
public void testDeserializationWithCompositeTrigger() throws InterruptedException {
String drl = "package org.drools.test;\n" + "import org.drools.compiler.StockTick; \n" + "global java.util.List list;\n" + "\n" + "declare StockTick\n" + " @role( event )\n" + " @expires( 1s )\n" + "end\n" + "\n" + "rule \"One\"\n" + "when\n" + " $event : StockTick( )\n" + " not StockTick( company == \"BBB\", this after[0,96h] $event )\n" + " not StockTick( company == \"CCC\", this after[0,96h] $event )\n" + "then\n" + "end";
KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
KieSessionConfiguration knowledgeSessionConfiguration = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
knowledgeSessionConfiguration.setOption(TimerJobFactoryOption.get("trackable"));
KieBase kb = loadKnowledgeBaseFromString(kbconf, drl);
KieSession ks = kb.newKieSession(knowledgeSessionConfiguration, null);
ks.insert(new StockTick(2, "AAA", 1.0, 0));
try {
ks = SerializationHelper.getSerialisedStatefulKnowledgeSession(ks, true, false);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class CepEspTest method testEventAssertion.
@Test(timeout = 10000)
public void testEventAssertion() throws Exception {
KieBase kbase = loadKnowledgeBase("test_CEP_SimpleEventAssertion.drl");
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
conf.setOption(ClockTypeOption.get("pseudo"));
KieSession session = createKnowledgeSession(kbase, conf);
SessionPseudoClock clock = (SessionPseudoClock) session.<SessionClock>getSessionClock();
final List results = new ArrayList();
session.setGlobal("results", results);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, 10000);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, 10010);
StockTickInterface tick3 = new StockTick(3, "ACME", 10, 10100);
StockTickInterface tick4 = new StockTick(4, "DROO", 50, 11000);
InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
clock.advanceTime(10, TimeUnit.SECONDS);
InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
clock.advanceTime(30, TimeUnit.SECONDS);
InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
clock.advanceTime(20, TimeUnit.SECONDS);
InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
clock.advanceTime(10, TimeUnit.SECONDS);
assertNotNull(handle1);
assertNotNull(handle2);
assertNotNull(handle3);
assertNotNull(handle4);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
assertTrue(handle3.isEvent());
assertTrue(handle4.isEvent());
session.fireAllRules();
assertEquals(2, ((List) session.getGlobal("results")).size());
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class CepEspTest method testDeleteExpiredEventWithTimestampAndEqualityKey.
@Test
public void testDeleteExpiredEventWithTimestampAndEqualityKey() throws Exception {
// DROOLS-1017
String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "declare StockTick\n" + " @role( event )\n" + " @timestamp( time )\n" + "end\n" + "\n" + "rule \"TestEventReceived\"\n" + "when\n" + " $st1 : StockTick( company == \"ACME\" )\n" + " not ( StockTick( this != $st1, this after[0s, 1s] $st1) )\n" + "then\n" + " delete($st1);\n" + "end";
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
KieBase kbase = helper.build(EventProcessingOption.STREAM, EqualityBehaviorOption.EQUALITY);
KieSession ksession = kbase.newKieSession(sessionConfig, null);
PseudoClockScheduler clock = ksession.getSessionClock();
clock.setStartupTime(5000L);
EventFactHandle handle1 = (EventFactHandle) ksession.insert(new StockTick(1, "ACME", 50, 0L));
clock.advanceTime(2, TimeUnit.SECONDS);
ksession.fireAllRules();
assertTrue(handle1.isExpired());
assertFalse(ksession.getFactHandles().contains(handle1));
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class CepEspTest method testPastEventExipration.
@Test
public void testPastEventExipration() throws InterruptedException {
// DROOLS-257
String drl = "package org.test;\n" + "import org.drools.compiler.StockTick;\n " + "" + "global java.util.List list; \n" + "" + "declare StockTick @role(event) @timestamp( time ) @expires( 200ms ) end \n" + "" + "rule \"slidingTimeCount\"\n" + "when\n" + " accumulate ( $e: StockTick() over window:length(10), $n : count($e) )\n" + "then\n" + " list.add( $n ); \n" + " System.out.println( \"Events in last X seconds: \" + $n );\n" + "end" + "";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
KieBaseConfiguration baseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
baseConfig.setOption(EventProcessingOption.STREAM);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(baseConfig);
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get("pseudo"));
// init stateful knowledge session
KieSession ksession = kbase.newKieSession(sessionConfig, null);
SessionPseudoClock clock = ksession.getSessionClock();
ArrayList list = new ArrayList();
ksession.setGlobal("list", list);
long now = 0;
StockTick event1 = new StockTick(1, "XXX", 1.0, now);
StockTick event2 = new StockTick(2, "XXX", 1.0, now + 240);
StockTick event3 = new StockTick(2, "XXX", 1.0, now + 380);
StockTick event4 = new StockTick(2, "XXX", 1.0, now + 500);
ksession.insert(event1);
ksession.insert(event2);
ksession.insert(event3);
ksession.insert(event4);
clock.advanceTime(220, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
clock.advanceTime(400, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertEquals(Arrays.asList(3L, 1L), list);
}
use of org.drools.compiler.StockTick in project drools by kiegroup.
the class CepEspTest method testPackageSerializationWithEvents.
@SuppressWarnings("unchecked")
@Test(timeout = 10000)
public void testPackageSerializationWithEvents() throws Exception {
// read in the source
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("test_CEP_SimpleEventAssertion.drl")), ResourceType.DRL);
// get the package
Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
assertEquals(2, pkgs.size());
// serialize the package
byte[] serializedPkg = DroolsStreamUtils.streamOut(pkgs);
pkgs = (Collection<KiePackage>) DroolsStreamUtils.streamIn(serializedPkg);
// create the kbase
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(pkgs);
// create the session
KieSessionConfiguration conf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
KieSession session = createKnowledgeSession(kbase, conf);
final List<StockTick> results = new ArrayList<StockTick>();
session.setGlobal("results", results);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, 10000);
StockTickInterface tick2 = new StockTick(2, "ACME", 10, 10010);
InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
assertNotNull(handle1);
assertNotNull(handle2);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
session.fireAllRules();
assertEquals(1, results.size());
assertEquals(tick2, results.get(0));
}
Aggregations