use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.
the class CepEspTest method testSerializationBeforeFireWithWindowLength.
@Test
public void testSerializationBeforeFireWithWindowLength() {
// DROOLS-953
final String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "global java.util.List list\n" + "declare StockTick\n" + " @role( event )\n" + "end\n" + "\n" + "rule ReportLastEvent when\n" + " $e : StockTick() over window:length(1)\n" + "then\n" + " list.add($e.getCompany());\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
List<String> list = new ArrayList<>();
ksession.setGlobal("list", list);
ksession.insert(new StockTick(1, "ACME", 50));
ksession.insert(new StockTick(2, "DROO", 50));
ksession.insert(new StockTick(3, "JBPM", 50));
try {
ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
} catch (final Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
list = new ArrayList<>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("JBPM", list.get(0));
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.
the class CepEspTest method testEventExpiration.
private void testEventExpiration(final String afterBoundary, final String windowTime) {
final String drl = "package org.drools.compiler\n" + "import " + StockTick.class.getCanonicalName() + ";\n" + "global java.util.List results;\n" + "\n" + "declare StockTick \n" + " @role( event )\n" + " @timestamp( dateTimestamp )\n" + " // this will override individual rule requirements\n" + " @expires( 10m )\n" + "end\n" + "\n" + "rule \"Check event\"\n" + "when\n" + " $st1 : StockTick( company == \"ACME\" )\n" + " $st2 : StockTick( company == \"RHT\", this after[0," + afterBoundary + "] $st1 )\n" + "then\n" + " results.add( $st1 );\n" + "end\n" + "\n" + "rule \"Check event2\"\n" + "when\n" + " $st1 : StockTick( company == \"RHT\" ) over window:time( " + windowTime + " )\n" + "then\n" + " results.add( $st1 );\n" + "end\n";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
final Map<ObjectType, ObjectTypeNode> objectTypeNodes = ((RuleBase) kbase).getRete().getObjectTypeNodes(EntryPointId.DEFAULT);
final ObjectTypeNode node = objectTypeNodes.get(new ClassObjectType(StockTick.class));
assertNotNull(node);
// the expiration policy @expires(10m) should override the temporal operator usage
assertEquals(TimeIntervalParser.parse("10m")[0] + 1, node.getExpirationOffset());
}
use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.
the class CepEspTest method testDeleteExpiredEvent.
@Test
public void testDeleteExpiredEvent() {
// BZ-1274696
final String drl = "import " + StockTick.class.getCanonicalName() + "\n" + "declare StockTick\n" + " @role( event )\n" + "end\n" + "\n" + "rule \"TestEventReceived\"\n" + "no-loop\n" + "when\n" + " $st1 : StockTick( company == \"ACME\" )\n" + " not ( StockTick( this != $st1, this after[0s, 1s] $st1) )\n" + "then\n" + " delete($st1);\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession(KieSessionTestConfiguration.STATEFUL_PSEUDO.getKieSessionConfiguration(), null);
try {
final PseudoClockScheduler clock = ksession.getSessionClock();
final EventFactHandle handle1 = (EventFactHandle) ksession.insert(new StockTick(1, "ACME", 50));
ksession.fireAllRules();
clock.advanceTime(2, TimeUnit.SECONDS);
ksession.fireAllRules();
assertTrue(handle1.isExpired());
assertFalse(ksession.getFactHandles().contains(handle1));
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.
the class CepEspNegativeCloudTest method testCloudModeExpiration.
@Test(timeout = 10000)
public void testCloudModeExpiration() throws InstantiationException, IllegalAccessException, InterruptedException {
final String drl = "package org.drools.cloud\n" + "import " + StockTick.class.getCanonicalName() + "\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";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession();
try {
final EntryPoint ep = ksession.getEntryPoint("X");
ep.insert(new StockTick(1, "RHT", 10, 1000));
int rulesFired = ksession.fireAllRules();
assertEquals(0, rulesFired);
final FactType event = kbase.getFactType("org.drools.cloud", "Event");
final 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.sleep(1000);
// check both events are still in memory as we are running in CLOUD mode
assertEquals(2, ep.getFactCount());
} finally {
ksession.dispose();
}
}
use of org.drools.testcoverage.common.model.StockTick in project drools by kiegroup.
the class CepEspTest method testRetractFromWindow.
@Test
public void testRetractFromWindow() {
// DROOLS-636
final String drl = "import " + StockTick.class.getCanonicalName() + ";\n " + "declare StockTick\n" + " @role( event )\n" + "end\n" + "rule R1 when\n" + " $i: Integer()\n" + " $s: StockTick( price > 10 )\n" + "then\n" + " modify($s) { setPrice(8) };\n" + "end\n" + "rule R2 when\n" + " $s: StockTick( price > 15 ) over window:length(1)\n" + "then\n" + "end";
final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("cep-esp-test", kieBaseTestConfiguration, drl);
final KieSession ksession = kbase.newKieSession();
try {
ksession.insert(42);
ksession.insert(new StockTick(1L, "DROOLS", 20));
ksession.fireAllRules();
} finally {
ksession.dispose();
}
}
Aggregations