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 = 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);
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 Misc2Test method testMatchingEventsInStreamMode.
@Test
public void testMatchingEventsInStreamMode() {
// DROOLS-338
String drl = "import org.drools.compiler.integrationtests.Misc2Test.SimpleEvent\n" + "declare SimpleEvent\n" + " @role(event)\n" + "end\n" + "\n" + "rule \"RuleA\"\n" + "salience 5\n" + "when\n" + " $f : SimpleEvent( )\n" + "then\n" + " delete ($f);\n" + "end\n" + "\n" + "rule \"RuleB\"\n" + "when\n" + " $f : SimpleEvent( )\n" + "then\n" + "end\n";
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(kconf, drl);
KieSession ksession = kbase.newKieSession();
final AtomicInteger i = new AtomicInteger(0);
ksession.addEventListener(new DefaultAgendaEventListener() {
public void matchCreated(MatchCreatedEvent event) {
i.incrementAndGet();
}
public void matchCancelled(MatchCancelledEvent event) {
i.decrementAndGet();
}
});
ksession.insert(new SimpleEvent());
ksession.fireAllRules();
assertEquals(1, i.get());
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class Misc2Test method testLegacySalienceResolver.
@Test
public void testLegacySalienceResolver() {
// DROOLS-159
String drl = "package org.drools.test; \n" + "" + "global java.util.List list; \n " + "" + "rule X salience 10 \n" + "then\n" + " list.add( 1 ); \n" + "end\n" + "" + "rule Y salience 5 \n" + "then\n" + " list.add( 2 ); \n" + "end\n" + "";
KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
kb.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
assertFalse(kb.hasErrors());
KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
((RuleBaseConfiguration) kbconf).setConflictResolver(SalienceConflictResolver.getInstance());
InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(kbconf);
knowledgeBase.addPackages(kb.getKnowledgePackages());
KieSession knowledgeSession = knowledgeBase.newKieSession();
List list = new ArrayList();
knowledgeSession.setGlobal("list", list);
knowledgeSession.fireAllRules();
assertEquals(Arrays.asList(1, 2), list);
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class NullTest method testBindingToNullFieldWithEquality.
@Test
public void testBindingToNullFieldWithEquality() {
// JBRULES-3396
final String str = "package org.drools.compiler.test; \n" + "\n" + "global java.util.List list;" + "\n" + "declare Bean\n" + " id : String @key\n" + " field : String\n" + "end\n" + "\n" + "\n" + "rule \"Init\"\n" + "when \n" + "then\n" + " insert( new Bean( \"x\" ) );\n" + "end\n" + "\n" + "rule \"Check\"\n" + "when\n" + " $b : Bean( $fld : field )\n" + "then\n" + " System.out.println( $fld );\n" + " list.add( \"OK\" ); \n" + "end";
final KieBaseConfiguration kbConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kbConf.setOption(EqualityBehaviorOption.EQUALITY);
final KieBase kbase = loadKnowledgeBaseFromString(kbConf, str);
final KieSession ksession = kbase.newKieSession();
final java.util.List list = new java.util.ArrayList();
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertTrue(list.contains("OK"));
ksession.dispose();
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class TruthMaintenanceTest method testPrimeJustificationWithEqualityMode.
@Test(timeout = 10000)
public void testPrimeJustificationWithEqualityMode() {
String droolsSource = "package org.drools.tms.test; \n" + "declare Bar end \n" + "" + "declare Holder x : Bar end \n" + "" + "" + "rule Init \n" + "when \n" + "then \n" + " insert( new Holder( new Bar() ) ); \n" + "end \n" + "rule Justify \n" + "when \n" + " $s : Integer() \n" + " $h : Holder( $b : x ) \n" + "then \n" + " insertLogical( $b ); \n" + "end \n" + "rule React \n" + "when \n" + " $b : Bar( ) \n" + "then \n" + " System.out.println( $b ); \n" + "end \n";
// ///////////////////////////////////
KieBaseConfiguration kieConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kieConf.setOption(EqualityBehaviorOption.EQUALITY);
KieBase kbase = loadKnowledgeBaseFromString(kieConf, droolsSource);
KieSession session = kbase.newKieSession();
FactHandle handle1 = session.insert(10);
FactHandle handle2 = session.insert(20);
assertEquals(4, session.fireAllRules());
session.delete(handle1);
assertEquals(0, session.fireAllRules());
}
Aggregations