use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class DeclarativeAgendaTest method testMultipleBlockersWithUnblockAll.
@Test(timeout = 10000)
public void testMultipleBlockersWithUnblockAll() {
// This test is a bit wierd as it recurses. Maybe unblockAll is not feasible...
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 @Propagation(EAGER) @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( kcontext.rule.name + ':' + $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( kcontext.rule.name + ':' + $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( kcontext.rule.name + ':' + $i.rule.name + ':' + $s ); \n";
str += " kcontext.blockMatch( $i ); \n";
str += "end \n";
str += "rule unblockAll @activationListener('direct') \n";
str += "when \n";
str += " $s : String( this == 'go4' ) \n";
str += " $i : Match( department == 'sales', active == true ) \n";
str += "then \n";
str += " list.add( kcontext.rule.name + ':' + $i.rule.name + ':' + $s ); \n";
str += " kcontext.unblockAllMatches( $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());
System.out.println(list);
assertTrue(list.contains("blockerAllSalesRules1:rule0:go1"));
assertTrue(list.contains("blockerAllSalesRules2:rule0:go2"));
assertTrue(list.contains("blockerAllSalesRules3:rule0:go3"));
list.clear();
FactHandle go4 = ksession.insert("go4");
ksession.fireAllRules();
System.out.println(list);
assertEquals(5, list.size());
assertTrue(list.contains("unblockAll:rule0:go4"));
assertTrue(list.contains("rule0:go0"));
assertTrue(list.contains("blockerAllSalesRules1:rule0:go1"));
assertTrue(list.contains("blockerAllSalesRules2:rule0:go2"));
assertTrue(list.contains("blockerAllSalesRules3:rule0:go3"));
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class DeclarativeAgendaTest method testExplicitUndercutWithDeclarativeAgenda.
@Test
public void testExplicitUndercutWithDeclarativeAgenda() {
String drl = "package org.drools.test;\n" + "\n" + "import org.kie.api.runtime.rule.Match;\n" + "\n" + "global java.util.List list;\n" + "\n" + "declare Foo\n" + " type : String\n" + " value : double\n" + "end\n" + "\n" + "declare Bar\n" + " type : String\n" + " total : double\n" + "end\n" + "\n" + "\n" + "rule \"Init\"\n" + "when\n" + "then\n" + " insert( new Foo( \"first\", 10 ) );\n" + " insert( new Foo( \"first\", 11 ) );\n" + " insert( new Foo( \"second\", 20 ) );\n" + " insert( new Foo( \"second\", 22 ) );\n" + " insert( new Foo( \"third\", 30 ) );\n" + " insert( new Foo( \"third\", 40 ) );\n" + "end\n" + "\n" + "rule \"Accumulate\"\n" + "salience 100\n" + "dialect \"mvel\"\n" + " when\n" + " $type : String() from [ \"first\", \"second\", \"third\" ]\n" + " accumulate ( Foo( type == $type, $value : value ),\n" + " $total : sum( $value );\n" + " $total > 0 )\n" + " then\n" + " insert(new Bar($type, $total));\n" + "end\n" + "\n" + "rule \"handle all Bars of type first\"\n" + "@Undercuts( others )\n" + " when\n" + " $bar : Bar( type == 'first', $total : total )\n" + " then\n" + " System.out.println( \"First bars \" + $total );\n" + " list.add( $total );\n" + "end\n" + "\n" + "rule \"handle all Bars of type second\"\n" + "@Undercuts( others )\n" + " when\n" + " $bar : Bar( type == 'second', $total : total )\n" + " then\n" + " System.out.println( \"Second bars \" + $total );\n" + " list.add( $total );\n" + "end\n" + "\n" + "rule \"others\"\n" + " when\n" + " $bar : Bar( $total : total )\n" + " then\n" + " System.out.println( \"Other bars \" + $total );\n" + " list.add( $total );\n" + "end\n" + "\n" + "\n" + "rule \"Undercut\"\n" + "@Direct \n" + "when\n" + " $m : Match( $handles : factHandles )\n" + " $v : Match( rule.name == $m.Undercuts, factHandles == $handles )\n" + "then\n" + " System.out.println( \"Activation of rule \" + $m.getRule().getName() + \" overrides \" + $v.getRule().getName() + \" for tuple \" + $handles );\n" + " kcontext.cancelMatch( $v );\n" + "end\n" + "\n" + "\n";
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(DeclarativeAgendaOption.ENABLED);
KieBase kbase = loadKnowledgeBaseFromString(kconf, drl);
KieSession ksession = createKnowledgeSession(kbase);
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(Arrays.asList(21.0, 42.0, 70.0), list);
ksession.dispose();
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class DynamicRulesTest method testJBRULES_2206.
@Test(timeout = 10000)
public void testJBRULES_2206() {
KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
((RuleBaseConfiguration) config).setRuleBaseUpdateHandler(null);
InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase(config);
KieSession session = createKnowledgeSession(kbase);
AgendaEventListener ael = mock(AgendaEventListener.class);
session.addEventListener(ael);
for (int i = 0; i < 5; i++) {
session.insert(new Cheese());
}
kbase.addPackages(loadKnowledgePackages("test_JBRULES_2206_1.drl"));
((InternalAgenda) session.getAgenda()).evaluateEagerList();
// two matching rules were added, so 2 activations should have been created
verify(ael, times(2)).matchCreated(any(MatchCreatedEvent.class));
int fireCount = session.fireAllRules();
// both should have fired
assertEquals(2, fireCount);
kbase.addPackages(loadKnowledgePackages("test_JBRULES_2206_2.drl"));
((InternalAgenda) session.getAgenda()).evaluateEagerList();
// one rule was overridden and should activate
verify(ael, times(3)).matchCreated(any(MatchCreatedEvent.class));
fireCount = session.fireAllRules();
// that rule should fire again
assertEquals(1, fireCount);
session.dispose();
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class Misc2Test method testAgendaGroupSalience.
@Test(timeout = 10000)
public void testAgendaGroupSalience() {
// BZ-999360
String str = "global java.util.List ruleList\n" + "\n" + "rule first\n" + "salience 2\n" + "when\n" + "then\n" + " ruleList.add(\"first\");\n" + " drools.getKnowledgeRuntime().getAgenda().getAgendaGroup(\"agenda\").setFocus();\n" + "end\n" + "\n" + "rule second\n" + "agenda-group \"agenda\"\n" + "when\n" + " $s : String( this == 'fireRules' )\n" + "then\n" + " ruleList.add(\"second\");\n" + "end\n" + "\n" + "rule third\n" + "salience 1\n" + "when\n" + " $s : String( this == 'fireRules' )\n" + "then\n" + " ruleList.add(\"third\");\n" + "end\n";
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(DeclarativeAgendaOption.ENABLED);
KieBase kbase = loadKnowledgeBaseFromString(kconf, str);
KieSession ksession = kbase.newKieSession();
ArrayList<String> ruleList = new ArrayList<String>();
ksession.setGlobal("ruleList", ruleList);
ksession.insert("fireRules");
ksession.fireAllRules();
assertEquals(ruleList.get(0), "first");
assertEquals(ruleList.get(1), "second");
assertEquals(ruleList.get(2), "third");
}
use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.
the class AbductionTest method testBacktracking.
@Test
@Ignore("Not implemented yet")
public void testBacktracking() {
String droolsSource = "package org.drools.abductive.test; \n" + "import org.kie.api.runtime.rule.Match;\n" + "" + "declare Foo " + "@Abducible " + " id : Integer @key " + "end " + "query bar( Integer $id ) " + " @Abductive( target=Foo.class, backtracking=true ) " + " $id := Integer() " + "end " + "rule Check " + "when " + " bar( $i ; ) " + "then " + " System.out.println( 'YES ' + $i ); " + "end " + "rule Check2 " + "when " + " bar( $i ; ) " + "then " + " System.out.println( 'HAH ' + $i ); " + "end " + "rule Init " + "when " + "then" + " insert( new Integer( 1 ) ); " + " insert( new Integer( 2 ) ); " + "end " + "";
// ///////////////////////////////////
KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
kconf.setOption(DeclarativeAgendaOption.ENABLED);
KieSession session = getSessionFromString(droolsSource, kconf);
session.fireAllRules();
for (Object o : session.getObjects()) {
System.out.println(">>> " + o);
}
}
Aggregations