use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testTimeAndLengthWindowConflict.
@Test(timeout = 10000)
public void testTimeAndLengthWindowConflict() throws Exception {
// JBRULES-3671
String drl = "package org.drools.compiler;\n" + "\n" + "import java.util.List\n" + "\n" + "global List timeResults;\n" + "global List lengthResults;\n" + "\n" + "declare OrderEvent\n" + " @role( event )\n" + "end\n" + "\n" + "rule \"collect with time window\"\n" + "when\n" + " $list : List( empty == false ) from collect(\n" + " $o : OrderEvent() over window:time(30s) )\n" + "then\n" + " timeResults.add( $list.size() );\n" + "end\n" + "\n" + "rule \"collect with length window\"\n" + "when\n" + " accumulate (\n" + " $o : OrderEvent( $tot : total ) over window:length(3)," + " $avg : average( $tot ) )\n" + "then\n" + " lengthResults.add( $avg );\n" + "end\n";
final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbconf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBaseFromString(kbconf, drl);
KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksconf.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieSession ksession = createKnowledgeSession(kbase, ksconf);
List<Number> timeResults = new ArrayList<Number>();
List<Number> lengthResults = new ArrayList<Number>();
ksession.setGlobal("timeResults", timeResults);
ksession.setGlobal("lengthResults", lengthResults);
SessionPseudoClock clock = (SessionPseudoClock) ksession.<SessionClock>getSessionClock();
// 5 seconds
clock.advanceTime(5, TimeUnit.SECONDS);
ksession.insert(new OrderEvent("1", "customer A", 70));
ksession.fireAllRules();
System.out.println(lengthResults);
assertTrue(lengthResults.contains(70.0));
// 10 seconds
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.insert(new OrderEvent("2", "customer A", 60));
ksession.fireAllRules();
System.out.println(lengthResults);
assertTrue(lengthResults.contains(65.0));
// Third interaction: advance clock and assert new data
// 10 seconds
clock.advanceTime(10, TimeUnit.SECONDS);
ksession.insert(new OrderEvent("3", "customer A", 50));
ksession.fireAllRules();
System.out.println(lengthResults);
assertTrue(lengthResults.contains(60.0));
// Fourth interaction: advance clock and assert new data
// 60 seconds
clock.advanceTime(60, TimeUnit.SECONDS);
ksession.insert(new OrderEvent("4", "customer A", 25));
ksession.fireAllRules();
System.out.println(lengthResults);
// assertTrue( lengthResults.contains( 45 ) );
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testSerializationDeserliaizationWithRectractedExpireFact.
@Test(timeout = 10000)
public void testSerializationDeserliaizationWithRectractedExpireFact() {
// DROOLS-1328
String drl = "package " + TestEvent.class.getPackage().getName() + "\n" + "declare " + TestEvent.class.getCanonicalName() + "\n" + " @role( event ) \n" + " @expires( 60d ) \n" + "end\n" + "rule \"retract test rule\"\n" + "salience 10 \n" + "when\n" + " $e : TestEvent() over window:length(1)\n" + "then\n" + " delete($e);\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);
KieSession ksession = kbase.newKieSession(sessionConfig, null);
ksession.insert(new TestEvent("test1"));
ksession.fireAllRules();
KieSession kieSessionDeserialized = null;
try {
kieSessionDeserialized = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true, false);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
ksession.dispose();
kieSessionDeserialized.insert(new TestEvent("test2"));
kieSessionDeserialized.fireAllRules();
}
use of org.kie.api.runtime.KieSessionConfiguration 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.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testPropertyReactiveWithDurationOnRule.
@Test
public void testPropertyReactiveWithDurationOnRule() {
// DROOLS-2238
String drl = "package org.drools.test " + " " + "declare Bean " + " @PropertyReactive " + " label : String " + " active : boolean " + "end " + " " + " " + "rule Init " + "when " + "then " + " insert( new Bean( \"aaa\", true ) ); " + "end " + " " + "rule Close " + " duration (100) " + "when " + " $b : Bean( label == \"aaa\" ) " + "then " + " modify( $b ) { " + " setActive( false ); " + " } " + "end" + " ";
KieBase kieBase = new KieHelper().addContent(drl, ResourceType.DRL).build(EventProcessingOption.STREAM);
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get("pseudo"));
KieSession ksession = kieBase.newKieSession(sessionConfig, null);
assertEquals(1, ksession.fireAllRules());
((SessionPseudoClock) ksession.getSessionClock()).advanceTime(200, TimeUnit.MILLISECONDS);
assertEquals(1, ksession.fireAllRules(10));
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class CepEspTest method testCEPNamedConsTimers.
@Test
public void testCEPNamedConsTimers() throws InterruptedException {
String drl = "package org.drools " + "global java.util.List list; " + "declare Msg " + " @role( event ) " + " sender : String @key " + "end " + "rule Init " + "when " + " $s : String() " + "then " + " System.out.println( 'Msg ' + $s ); " + " insert( new Msg( $s ) ); " + "end " + "rule 'Viol' " + "when " + " $trigger : Msg( 'John' ; ) " + " not Msg( 'Peter' ; this after[0, 100ms] $trigger ) do[viol]" + "then " + " list.add( 0 ); " + "then[viol] " + " list.add( -2 ); " + "end " + "";
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
KieHelper helper = new KieHelper();
helper.addContent(drl, ResourceType.DRL);
KieSession ksession = helper.build(EventProcessingOption.STREAM).newKieSession(sessionConfig, null);
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert("John");
ksession.fireAllRules();
assertTrue(list.isEmpty());
((PseudoClockScheduler) ksession.getSessionClock()).advanceTime(1000, TimeUnit.MILLISECONDS);
ksession.fireAllRules();
assertTrue(list.contains(-2));
assertTrue(list.contains(0));
}
Aggregations