use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class MarshallingTest method testMarshallWithNot.
@Test
public void testMarshallWithNot() throws Exception {
String str = "import " + getClass().getCanonicalName() + ".*\n" + "rule one\n" + "when\n" + " A()\n" + " not(B())\n" + "then\n" + "System.out.println(\"a\");\n" + "end\n" + "\n" + "rule two\n" + "when\n" + " A()\n" + "then\n" + "System.out.println(\"b\");\n" + "end\n";
KieBaseConfiguration config = RuleBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
KieBase kBase = loadKnowledgeBaseFromString(config, str);
KieSession ksession = kBase.newKieSession();
ksession.insert(new A());
MarshallerFactory.newMarshaller(kBase).marshall(new ByteArrayOutputStream(), ksession);
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class FactHandleMarshallingTest method createKnowledgeBase.
private InternalKnowledgeBase createKnowledgeBase() {
KieBaseConfiguration config = RuleBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
return KnowledgeBaseFactory.newKnowledgeBase(config);
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class MarshallingTest method testMarshallEntryPointsWithExpires.
@Test
public void testMarshallEntryPointsWithExpires() 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( 10s )\n" + "end\n" + "declare B\n" + "" + " @role( event )\n" + " @expires( 10s )\n" + "end\n" + "" + "declare C\n" + " @role( event )\n" + " @expires( 15s )\n" + "end\n" + "" + "rule a1\n" + "when\n" + " $a : A() from entry-point 'a-ep'\n" + "then\n" + "list.add( $a );" + "end\n" + "" + "rule b1\n" + "when\n" + " $b : B() from entry-point 'b-ep'\n" + "then\n" + "list.add( $b );" + "end\n" + "" + "rule c1\n" + "when\n" + " $c : C() from entry-point 'c-ep'\n" + "then\n" + "list.add( $c );" + "end\n";
KieBaseConfiguration config = RuleBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
KieBase kBase = loadKnowledgeBaseFromString(config, str);
KieSessionConfiguration ksconf = RuleBaseFactory.newKnowledgeSessionConfiguration();
ksconf.setOption(ClockTypeOption.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);
EntryPoint bep = ksession.getEntryPoint("b-ep");
bep.insert(new B());
ksession = marsallStatefulKnowledgeSession(ksession);
EntryPoint cep = ksession.getEntryPoint("c-ep");
cep.insert(new C());
ksession = marsallStatefulKnowledgeSession(ksession);
ksession.fireAllRules();
ksession = marsallStatefulKnowledgeSession(ksession);
assertEquals(3, list.size());
aep = ksession.getEntryPoint("a-ep");
assertEquals(1, aep.getFactHandles().size());
bep = ksession.getEntryPoint("b-ep");
assertEquals(1, bep.getFactHandles().size());
cep = ksession.getEntryPoint("c-ep");
assertEquals(1, cep.getFactHandles().size());
PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
timeService.advanceTime(11, TimeUnit.SECONDS);
ksession = marsallStatefulKnowledgeSession(ksession);
ksession.fireAllRules();
ksession = marsallStatefulKnowledgeSession(ksession);
aep = ksession.getEntryPoint("a-ep");
assertEquals(0, aep.getFactHandles().size());
bep = ksession.getEntryPoint("b-ep");
assertEquals(0, bep.getFactHandles().size());
cep = ksession.getEntryPoint("c-ep");
assertEquals(1, cep.getFactHandles().size());
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class MarshallingTest method testMarshallEntryPointsWithSlidingTimeWindow.
@Test
@Ignore("beta4 phreak")
public void testMarshallEntryPointsWithSlidingTimeWindow() 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:time(30s) from entry-point 'a-ep') \n" + "then\n" + " list.add( $l );" + "end\n";
KieBaseConfiguration conf = RuleBaseFactory.newKnowledgeBaseConfiguration();
conf.setOption(EventProcessingOption.STREAM);
final KieBase kbase = loadKnowledgeBaseFromString(conf, str);
KieSessionConfiguration ksconf = RuleBaseFactory.newKnowledgeSessionConfiguration();
ksconf.setOption(ClockTypeOption.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());
PseudoClockScheduler timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
timeService.advanceTime(15, TimeUnit.SECONDS);
ksession = marsallStatefulKnowledgeSession(ksession);
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(4, ((List) list.get(0)).size());
timeService = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
timeService.advanceTime(20, TimeUnit.SECONDS);
ksession = marsallStatefulKnowledgeSession(ksession);
list.clear();
ksession.fireAllRules();
assertEquals(2, ((List) list.get(0)).size());
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class UnmarshallingTest method initializeKnowledgeBase.
private KieBase initializeKnowledgeBase(String rule) {
// Setup knowledge base
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newReaderResource(new StringReader(rule)), ResourceType.DRL);
if (kbuilder.hasErrors()) {
throw new RuntimeException(kbuilder.getErrors().toString());
}
KieBaseConfiguration config = RuleBaseFactory.newKnowledgeBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(RuleBaseFactory.newRuleBase(config));
knowledgeBase.addPackages(kbuilder.getKnowledgePackages());
return knowledgeBase;
}
Aggregations