use of org.kie.api.KieBaseConfiguration 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.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class CepEspTest method testTimedRuleWithAccumulate.
@Test
@Ignore("Cannot reproduce with pseudoclock and takes too long with system clock")
public void testTimedRuleWithAccumulate() {
// BZ-1083103
String drl = "import " + SynthEvent.class.getCanonicalName() + "\n" + "import java.util.Date\n" + "\n" + "declare SynthEvent\n" + " @role( event )\n" + " @timestamp( timestamp )\n" + "end\n" + "\n" + "declare EventCounter\n" + " @role( event )\n" + " @timestamp( timestamp )\n" + " id : long\n" + " key : String\n" + " timestamp : Date\n" + "end\n" + "\n" + "rule \"Create counter\"\n" + "when\n" + "$e : SynthEvent() from entry-point \"synth\"\n" + "then\n" + " entryPoints[\"counters\"].insert(new EventCounter( $e.getId(), \"event\", $e.getTimestamp() ) );\n" + "end\n" + "\n" + "rule \"Count epm\"\n" + "timer ( cron: 0/10 * * * * ? )\n" + // "timer ( int: 2s 1s )\n" +
"when\n" + " Number( $count : intValue ) from accumulate(\n" + " EventCounter( key == \"event\" ) over window:time( 60s ) from entry-point \"counters\", count(1) )\n" + "then\n" + " System.out.println(\"[\" + new Date() + \"] epm = \" + $count );\n" + "end\n " + "";
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());
final KieSession ksession = kbase.newKieSession();
EntryPoint synthEP = ksession.getEntryPoint("synth");
new Thread() {
public void run() {
System.out.println("[" + new Date() + "] start!");
ksession.fireUntilHalt();
}
}.start();
long counter = 0;
while (true) {
counter++;
synthEP.insert(new SynthEvent(counter));
try {
Thread.sleep(20L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if ((counter % 1000) == 0) {
System.out.println("Total events: " + counter);
}
}
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class CepEspTest method testModifyInStreamMode.
@Test
public void testModifyInStreamMode() {
// BZ-1012933
String drl = "import org.drools.compiler.integrationtests.CepEspTest.SimpleFact;\n" + "global java.util.List list;\n" + "declare SimpleFact\n" + " @role( event )\n" + "end\n" + "\n" + "rule \"MyRule\"\n" + "when\n" + " $f : SimpleFact( status == \"NOK\" )\n" + "then\n" + " list.add(\"Firing\");" + " $f.setStatus(\"OK\");\n" + " update ($f);\n" + "end\n";
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(kconf, drl);
KieSession ksession = kbase.newKieSession();
List list = new ArrayList();
ksession.setGlobal("list", list);
SimpleFact fact = new SimpleFact("id1");
ksession.insert(fact);
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("OK", fact.getStatus());
}
use of org.kie.api.KieBaseConfiguration 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.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class CepEspTest method testExpiredEventWithPendingActivations.
@Test
public void testExpiredEventWithPendingActivations() throws Exception {
String drl = "package org.drools.drools_usage_pZB7GRxZp64;\n" + "\n" + "declare time_Var\n" + " @role( event )\n" + " @expires( 1s )\n" + " value : Long\n" + "end\n" + "\n" + "declare ExpiringEvent_Var\n" + " @role( event )\n" + " @expires( 10s )\n" + " value : Double\n" + "end\n" + "\n" + "declare window ExpiringEvent_Window1 ExpiringEvent_Var() over window:length(1) end\n" + "\n" + "rule \"Expring variable - Init\"\n" + "activation-group \"ExpiringEvent\"\n" + " when\n" + " $t : time_Var($now : Value != null) over window:length(1)\n" + "\n" + " not ExpiringEvent_Var()\n" + "\n" + " then\n" + " System.out.println($now + \" : Init\");\n" + " insert(new ExpiringEvent_Var(0.0));\n" + "end\n" + "\n" + "rule \"Expiring variable - Rule 1\"\n" + "activation-group \"ExpiringEvent\"\n" + " when\n" + " $t : time_Var($now : Value != null) over window:length(1)\n" + "\n" + " ExpiringEvent_Var(this before $t, $previousValue : Value < 1.0) from window ExpiringEvent_Window1\n" + "\n" + " then\n" + " System.out.println($now + \" : Rule 1\");\n" + " insert(new ExpiringEvent_Var(1.0));\n" + "\n" + "end\n" + "\n" + "rule \"Expiring variable - Rule 2\"\n" + "activation-group \"ExpiringEvent\"\n" + " when\n" + " $t : time_Var($now : Value != null) over window:length(1)\n" + "\n" + " ExpiringEvent_Var(this before $t, $previousValue : Value) from window ExpiringEvent_Window1\n" + "\n" + " then\n" + " System.out.println($now + \" : Rule 2\");\n" + " insert(new ExpiringEvent_Var($previousValue));\n" + "\n" + "end";
KieServices kieServices = KieServices.Factory.get();
KieBaseConfiguration kieBaseConf = KieServices.Factory.get().newKieBaseConfiguration();
kieBaseConf.setOption(EventProcessingOption.STREAM);
KieBase kieBase = new KieHelper().addContent(drl, ResourceType.DRL).build(kieBaseConf);
KieSessionConfiguration config = kieServices.newKieSessionConfiguration();
config.setOption(ClockTypeOption.get("pseudo"));
KieSession session = kieBase.newKieSession(config, null);
SessionPseudoClock clock = session.getSessionClock();
FactType time_VarType = kieBase.getFactType("org.drools.drools_usage_pZB7GRxZp64", "time_Var");
clock.advanceTime(1472057509000L, TimeUnit.MILLISECONDS);
Object time_Var = time_VarType.newInstance();
time_VarType.set(time_Var, "value", 1472057509000L);
session.insert(time_Var);
session.fireAllRules();
for (int i = 0; i < 10; i++) {
clock.advanceTime(1, TimeUnit.SECONDS);
Object time_VarP1 = time_VarType.newInstance();
time_VarType.set(time_VarP1, "value", clock.getCurrentTime());
session.insert(time_VarP1);
session.fireAllRules();
}
clock.advanceTime(1, TimeUnit.SECONDS);
session.fireAllRules();
clock.advanceTime(1, TimeUnit.HOURS);
Object time_VarP1 = time_VarType.newInstance();
time_VarType.set(time_VarP1, "value", clock.getCurrentTime());
session.insert(time_VarP1);
session.fireAllRules();
clock.advanceTime(1, TimeUnit.HOURS);
Object time_VarP2 = time_VarType.newInstance();
time_VarType.set(time_VarP2, "value", clock.getCurrentTime());
session.insert(time_VarP2);
session.fireAllRules();
clock.advanceTime(1, TimeUnit.HOURS);
session.fireAllRules();
// actually should be empty..
for (Object o : session.getFactHandles()) {
if (o instanceof EventFactHandle) {
EventFactHandle eventFactHandle = (EventFactHandle) o;
assertFalse(eventFactHandle.isExpired());
}
}
}
Aggregations