use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class MarshallingTest method testMarshallEntryPointsWithNot.
@Test
public void testMarshallEntryPointsWithNot() throws Exception {
String str = "package org.domain.test \n" + "import " + getClass().getCanonicalName() + ".*\n" + "global java.util.List list\n" + "declare A\n" + " @role( event )\n" + " @expires( 10m )\n" + "end\n" + "declare B\n" + "" + " @role( event )\n" + " @expires( 10m )\n" + "end\n" + "" + "rule a1\n" + "when\n" + " $a : A() from entry-point 'a-ep'\n" + " not B( this after[0s, 10s] $a) from entry-point 'a-ep'\n" + "then\n" + "list.add( $a );" + "end\n";
KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
KieBase kBase = loadKnowledgeBaseFromString(config, str);
KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksconf.setOption(ClockTypeOption.get("pseudo"));
ksconf.setOption(TimerJobFactoryOption.get("trackable"));
KieSession ksession = kBase.newKieSession(ksconf, null);
List list = new ArrayList();
ksession.setGlobal("list", list);
EntryPoint aep = ksession.getEntryPoint("a-ep");
aep.insert(new A());
ksession = marsallStatefulKnowledgeSession(ksession);
PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
timeService.advanceTime(3, TimeUnit.SECONDS);
ksession = marsallStatefulKnowledgeSession(ksession);
ksession.fireAllRules();
ksession = marsallStatefulKnowledgeSession(ksession);
assertEquals(0, list.size());
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class MarshallingTest method testMarshallEntryPointsWithSlidingLengthWindow.
@Test
public void testMarshallEntryPointsWithSlidingLengthWindow() throws Exception {
String str = "package org.domain.test \n" + "import " + getClass().getCanonicalName() + ".*\n" + "import java.util.List\n" + "global java.util.List list\n" + "declare A\n" + " @role( event )\n" + " @expires( 10m )\n" + "end\n" + "declare B\n" + "" + " @role( event )\n" + " @expires( 10m )\n" + "end\n" + "" + "rule a1\n" + "when\n" + " $l : List() from collect( A() over window:length(3) from entry-point 'a-ep') \n" + "then\n" + " list.add( $l );" + "end\n";
KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBaseFromString(conf, str);
KieSessionConfiguration ksconf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
ksconf.setOption(ClockTypeOption.get("pseudo"));
ksconf.setOption(TimerJobFactoryOption.get("trackable"));
KieSession ksession = createKnowledgeSession(kbase, ksconf);
List list = new ArrayList();
ksession.setGlobal("list", list);
EntryPoint aep = ksession.getEntryPoint("a-ep");
aep.insert(new A());
ksession = marsallStatefulKnowledgeSession(ksession);
aep = ksession.getEntryPoint("a-ep");
aep.insert(new A());
ksession = marsallStatefulKnowledgeSession(ksession);
list.clear();
ksession.fireAllRules();
ksession = marsallStatefulKnowledgeSession(ksession);
assertEquals(2, ((List) list.get(0)).size());
aep = ksession.getEntryPoint("a-ep");
aep.insert(new A());
ksession = marsallStatefulKnowledgeSession(ksession);
aep = ksession.getEntryPoint("a-ep");
aep.insert(new A());
ksession = marsallStatefulKnowledgeSession(ksession);
list.clear();
ksession.fireAllRules();
ksession = marsallStatefulKnowledgeSession(ksession);
assertEquals(3, ((List) list.get(0)).size());
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class BayesBeliefSystemTest method getSession.
protected KieSession getSession(String ruleFile) {
KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kBuilder.add(ResourceFactory.newClassPathResource(ruleFile), ResourceType.DRL);
if (kBuilder.hasErrors()) {
System.err.println(kBuilder.getErrors());
fail();
}
InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
kBase.addPackages(kBuilder.getKnowledgePackages());
KieSessionConfiguration ksConf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
((SessionConfiguration) ksConf).setBeliefSystemType(BeliefSystemType.DEFEASIBLE);
KieSession kSession = kBase.newKieSession(ksConf, null);
return kSession;
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class Misc2Test method testFireUntilHaltWithForceEagerActivation.
@Test(timeout = 10000L)
public void testFireUntilHaltWithForceEagerActivation() throws InterruptedException {
String drl = "global java.util.List list\n" + "rule \"String detector\"\n" + " when\n" + " $s : String( )\n" + " then\n" + " list.add($s);\n" + "end";
KieSessionConfiguration config = KieServices.Factory.get().newKieSessionConfiguration();
config.setOption(ForceEagerActivationOption.YES);
final KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession(config, null);
final Integer monitor = 42;
int factsNr = 5;
List<String> list = new NotifyingList<String>(factsNr, new Runnable() {
@Override
public void run() {
synchronized (monitor) {
monitor.notifyAll();
}
}
});
ksession.setGlobal("list", list);
// thread for firing until halt
ExecutorService thread = Executors.newSingleThreadExecutor();
thread.submit(new Runnable() {
@Override
public void run() {
ksession.fireUntilHalt();
}
});
for (int i = 0; i < factsNr; i++) {
ksession.insert("" + i);
}
// wait for rule to fire
synchronized (monitor) {
if (list.size() < factsNr) {
monitor.wait();
}
}
assertEquals(factsNr, list.size());
ksession.halt();
}
use of org.kie.api.runtime.KieSessionConfiguration in project drools by kiegroup.
the class ParallelEvaluationTest method testImmediateEventsExpiration.
@Test(timeout = 10000L)
public void testImmediateEventsExpiration() {
StringBuilder sb = new StringBuilder(400);
sb.append("global java.util.List list;\n");
sb.append("import " + MyEvent.class.getCanonicalName() + ";\n");
sb.append("declare MyEvent @role( event ) @expires( 1ms ) @timestamp( timestamp ) end\n");
for (int i = 0; i < 10; i++) {
sb.append(getRuleWithEvent(i));
}
KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
KieSession ksession = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(EventProcessingOption.STREAM, MultithreadEvaluationOption.YES).newKieSession(sessionConfig, null);
assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
List<Integer> list = new DebugList<Integer>();
ksession.setGlobal("list", list);
for (int i = 0; i < 10; i++) {
ksession.insert(new MyEvent(i, i * 2L));
}
ksession.fireAllRules();
assertEquals(10, list.size());
}
Aggregations