use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class Misc2Test method testBetaNodeInSubnetworkInStreamMode.
@Test
public void testBetaNodeInSubnetworkInStreamMode() {
// BZ-995408
String str = "import " + Foo.class.getCanonicalName() + "\n" + "\n" + "global java.util.List context;\n" + "\n" + "declare Foo\n" + " @role( event )\n" + "end\n" + "\n" + "rule \"Rule A\"\n" + "when\n" + " $f : Foo( )\n" + " not ( Integer() from context )\n" + "then\n" + " $f.setX( 2 );\n" + "end";
KieBaseConfiguration kBaseConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kBaseConf.setOption(EventProcessingOption.STREAM);
KieBase kbase = loadKnowledgeBaseFromString(kBaseConf, str);
KieSession ksession = kbase.newKieSession();
ksession.setGlobal("context", new ArrayList() {
{
add(new Long(0));
}
});
Foo foo = new Foo();
foo.setX(1);
ksession.insert(foo);
ksession.fireAllRules();
assertEquals(2, foo.getX());
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class DeclarativeAgendaTest method getStatefulKnowledgeSession.
public KieSession getStatefulKnowledgeSession() {
String str = "";
str += "package org.domain.test \n";
str += "import " + Match.class.getName() + "\n";
str += "global java.util.List list \n";
str += "dialect 'mvel' \n";
str += "rule rule1 @department(sales) \n";
str += "when \n";
str += " $s : String( this == 'go1' ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name + ':' + $s ); \n";
str += "end \n";
str += "rule blockerAllSalesRules @activationListener('direct') \n";
str += "when \n";
str += " $s : String( this == 'go2' ) \n";
str += " $i : Match( department == 'sales' ) \n";
str += "then \n";
str += " list.add( $i.rule.name + ':' + $s ); \n";
str += " kcontext.blockMatch( $i ); \n";
str += "end \n";
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(DeclarativeAgendaOption.ENABLED);
KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
KieSession ksession = createKnowledgeSession(kbase);
return ksession;
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class DeclarativeAgendaTest method testBasicBlockOnAnnotation.
@Test(timeout = 10000)
public void testBasicBlockOnAnnotation() {
String str = "";
str += "package org.domain.test \n";
str += "import " + Match.class.getName() + "\n";
str += "global java.util.List list \n";
str += "dialect 'mvel' \n";
str += "rule rule1 @department(sales) \n";
str += "when \n";
str += " $s : String( this == 'go1' ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name + ':' + $s ); \n";
str += "end \n";
str += "rule rule2 @department(sales) \n";
str += "when \n";
str += " $s : String( this == 'go1' ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name + ':' + $s ); \n";
str += "end \n";
str += "rule rule3 @department(sales) \n";
str += "when \n";
str += " $s : String( this == 'go1' ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name + ':' + $s ); \n";
str += "end \n";
str += "rule blockerAllSalesRules @activationListener('direct') \n";
str += "when \n";
str += " $s : String( this == 'go2' ) \n";
str += " $i : Match( department == 'sales' ) \n";
str += "then \n";
str += " list.add( $i.rule.name + ':' + $s ); \n";
str += " kcontext.blockMatch( $i ); \n";
str += "end \n";
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(DeclarativeAgendaOption.ENABLED);
KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
KieSession ksession = createKnowledgeSession(kbase);
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert("go1");
FactHandle go2 = ksession.insert("go2");
ksession.fireAllRules();
assertEquals(3, list.size());
assertTrue(list.contains("rule1:go2"));
assertTrue(list.contains("rule2:go2"));
assertTrue(list.contains("rule3:go2"));
list.clear();
ksession.retract(go2);
ksession.fireAllRules();
assertEquals(3, list.size());
assertTrue(list.contains("rule1:go1"));
assertTrue(list.contains("rule2:go1"));
assertTrue(list.contains("rule3:go1"));
ksession.dispose();
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class DeclarativeAgendaTest method testMultipleBlockers.
@Test(timeout = 10000)
public void testMultipleBlockers() {
String str = "";
str += "package org.domain.test \n";
str += "import " + Match.class.getName() + "\n";
str += "global java.util.List list \n";
str += "dialect 'mvel' \n";
str += "rule rule0 @department(sales) \n";
str += "when \n";
str += " $s : String( this == 'go0' ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name + ':' + $s ); \n";
str += "end \n";
str += "rule blockerAllSalesRules1 @activationListener('direct') \n";
str += "when \n";
str += " $s : String( this == 'go1' ) \n";
str += " $i : Match( department == 'sales' ) \n";
str += "then \n";
str += " list.add( $i.rule.name + ':' + $s ); \n";
str += " kcontext.blockMatch( $i ); \n";
str += "end \n";
str += "rule blockerAllSalesRules2 @activationListener('direct') \n";
str += "when \n";
str += " $s : String( this == 'go2' ) \n";
str += " $i : Match( department == 'sales' ) \n";
str += "then \n";
str += " list.add( $i.rule.name + ':' + $s ); \n";
str += " kcontext.blockMatch( $i ); \n";
str += "end \n";
str += "rule blockerAllSalesRules3 @activationListener('direct') \n";
str += "when \n";
str += " $s : String( this == 'go3' ) \n";
str += " $i : Match( department == 'sales' ) \n";
str += "then \n";
str += " list.add( $i.rule.name + ':' + $s ); \n";
str += " kcontext.blockMatch( $i ); \n";
str += "end \n";
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(DeclarativeAgendaOption.ENABLED);
KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
KieSession ksession = createKnowledgeSession(kbase);
List list = new ArrayList();
ksession.setGlobal("list", list);
FactHandle go0 = ksession.insert("go0");
FactHandle go1 = ksession.insert("go1");
FactHandle go2 = ksession.insert("go2");
FactHandle go3 = ksession.insert("go3");
ksession.fireAllRules();
assertEquals(3, list.size());
assertTrue(list.contains("rule0:go1"));
assertTrue(list.contains("rule0:go2"));
assertTrue(list.contains("rule0:go3"));
list.clear();
ksession.retract(go3);
ksession.fireAllRules();
assertEquals(0, list.size());
ksession.retract(go2);
ksession.fireAllRules();
assertEquals(0, list.size());
ksession.retract(go1);
ksession.fireAllRules();
assertEquals(1, list.size());
assertTrue(list.contains("rule0:go0"));
ksession.dispose();
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class DeclarativeAgendaTest method testActiveInActiveChanges.
@Test(timeout = 10000)
public void testActiveInActiveChanges() {
String str = "";
str += "package org.domain.test \n";
str += "import " + Match.class.getName() + "\n";
str += "global java.util.List list \n";
str += "dialect 'mvel' \n";
str += "rule rule1 @department(sales) \n";
str += "when \n";
str += " $s : String( this == 'go1' ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name + ':' + $s ); \n";
str += "end \n";
str += "rule rule2 @department(sales) \n";
str += "when \n";
str += " $s : String( this == 'go1' ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name + ':' + $s ); \n";
str += "end \n";
str += "rule rule3 @department(sales) \n";
str += "when \n";
str += " $s : String( this == 'go1' ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name + ':' + $s ); \n";
str += "end \n";
str += "rule countActivateInActive @activationListener('direct') \n";
str += "when \n";
str += " $s : String( this == 'go2' ) \n";
str += " $active : Number( this == 1 ) from accumulate( $a : Match( department == 'sales', active == true ), count( $a ) )\n";
str += " $inActive : Number( this == 2 ) from accumulate( $a : Match( department == 'sales', active == false ), count( $a ) )\n";
str += "then \n";
str += " list.add( $active + ':' + $inActive ); \n";
str += " kcontext.halt( ); \n";
str += "end \n";
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(DeclarativeAgendaOption.ENABLED);
KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
KieSession ksession = createKnowledgeSession(kbase);
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert("go1");
FactHandle go2 = ksession.insert("go2");
ksession.fireAllRules();
assertEquals(3, list.size());
System.out.println(list);
assertTrue(list.contains("1:2"));
assertTrue(list.contains("rule1:go1"));
assertTrue(list.contains("rule2:go1"));
ksession.dispose();
}
Aggregations