use of org.drools.mvel.compiler.Cheese in project drools by kiegroup.
the class DynamicRulesTest method testDynamicFunction.
@Ignore("Fails with standard-drl after changing to new API. See DROOLS-6060")
@Test(timeout = 10000)
public void testDynamicFunction() throws Exception {
// JBRULES-1258 serialising a package breaks function removal -- left the serialisation commented out for now
Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_DynamicFunction1.drl").getKiePackages();
InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromClasspathResources("test", getClass(), kieBaseTestConfiguration);
kbase.addPackages(kpkgs);
// kbase = SerializationHelper.serializeObject( kbase );
KieSession workingMemory = kbase.newKieSession();
final List<?> list = new ArrayList<Object>();
workingMemory.setGlobal("list", list);
final Cheese stilton = new Cheese("stilton", 5);
workingMemory.insert(stilton);
workingMemory.fireAllRules();
assertEquals(new Integer(5), list.get(0));
// Check a function can be removed from a package.
// Once removed any efforts to use it should throw an Exception
kbase.removeFunction("org.drools.mvel.compiler.test", "addFive");
final Cheese cheddar = new Cheese("cheddar", 5);
workingMemory.insert(cheddar);
try {
workingMemory.fireAllRules();
fail("Function should have been removed and NoClassDefFoundError thrown from the Consequence");
} catch (final Throwable e) {
}
// Check a new function can be added to replace an old function
Collection<KiePackage> kpkgs2 = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_DynamicFunction2.drl").getKiePackages();
kbase.addPackages(kpkgs2);
final Cheese brie = new Cheese("brie", 5);
workingMemory.insert(brie);
workingMemory.fireAllRules();
assertEquals(new Integer(6), list.get(1));
Collection<KiePackage> kpkgs3 = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_DynamicFunction3.drl").getKiePackages();
kbase.addPackages(kpkgs3);
final Cheese feta = new Cheese("feta", 5);
workingMemory.insert(feta);
workingMemory.fireAllRules();
assertEquals(new Integer(5), list.get(2));
}
use of org.drools.mvel.compiler.Cheese in project drools by kiegroup.
the class DynamicRulesTest method testJBRULES_2206.
@Test(timeout = 10000)
public void testJBRULES_2206() {
KieBaseConfiguration config = RuleBaseFactory.newKnowledgeBaseConfiguration();
((RuleBaseConfiguration) config).setRuleBaseUpdateHandler(null);
final KieModule kieModule = KieUtil.getKieModuleFromResources(KieUtil.generateReleaseId("test"), kieBaseTestConfiguration);
InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.newKieBaseFromReleaseId(kieModule.getReleaseId(), config);
KieSession session = kbase.newKieSession();
AgendaEventListener ael = mock(AgendaEventListener.class);
session.addEventListener(ael);
for (int i = 0; i < 5; i++) {
session.insert(new Cheese());
}
Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_JBRULES_2206_1.drl").getKiePackages();
kbase.addPackages(kpkgs);
((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);
kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_JBRULES_2206_2.drl").getKiePackages();
kbase.addPackages(kpkgs);
((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.drools.mvel.compiler.Cheese in project drools by kiegroup.
the class FireAllRulesCommandTest method infiniteLoopTerminatesAtMaxTest.
@Test
public void infiniteLoopTerminatesAtMaxTest() {
String str = "";
str += "package org.drools.mvel.integrationtests \n";
str += "import " + Cheese.class.getCanonicalName() + " \n";
str += "rule StringRule \n";
str += " when \n";
str += " $c : Cheese() \n";
str += " then \n";
str += " update($c); \n";
str += "end \n";
StatelessKieSession ksession = getSession(str);
List<Command<?>> commands = new ArrayList<Command<?>>();
commands.add(CommandFactory.newInsert(new Cheese("stilton")));
FireAllRulesCommand farc = (FireAllRulesCommand) CommandFactory.newFireAllRules(10);
farc.setOutIdentifier("num-rules-fired");
commands.add(farc);
ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands));
int fired = Integer.parseInt(results.getValue("num-rules-fired").toString());
assertEquals(10, fired);
}
use of org.drools.mvel.compiler.Cheese in project drools by kiegroup.
the class FireAllRulesCommandTest method fiveRulesFiredTest.
@Test
public void fiveRulesFiredTest() {
String str = "";
str += "package org.drools.mvel.integrationtests \n";
str += "import " + Cheese.class.getCanonicalName() + " \n";
str += "rule StringRule \n";
str += " when \n";
str += " $c : Cheese() \n";
str += " then \n";
str += " System.out.println($c); \n";
str += "end \n";
StatelessKieSession ksession = getSession(str);
List<Command<?>> commands = new ArrayList<Command<?>>();
commands.add(CommandFactory.newInsert(new Cheese("stilton")));
commands.add(CommandFactory.newInsert(new Cheese("gruyere")));
commands.add(CommandFactory.newInsert(new Cheese("cheddar")));
commands.add(CommandFactory.newInsert(new Cheese("stinky")));
commands.add(CommandFactory.newInsert(new Cheese("limburger")));
commands.add(CommandFactory.newFireAllRules("num-rules-fired"));
ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands));
int fired = Integer.parseInt(results.getValue("num-rules-fired").toString());
assertEquals(5, fired);
}
use of org.drools.mvel.compiler.Cheese in project drools by kiegroup.
the class FireAllRulesCommandTest method oneRuleFiredWithDefinedMaxTest.
@Test
public void oneRuleFiredWithDefinedMaxTest() {
String str = "";
str += "package org.drools.mvel.integrationtests \n";
str += "import " + Cheese.class.getCanonicalName() + " \n";
str += "rule StringRule \n";
str += " when \n";
str += " $c : Cheese() \n";
str += " then \n";
str += " System.out.println($c); \n";
str += "end \n";
StatelessKieSession ksession = getSession(str);
List<Command<?>> commands = new ArrayList<Command<?>>();
commands.add(CommandFactory.newInsert(new Cheese("stilton")));
FireAllRulesCommand farc = (FireAllRulesCommand) CommandFactory.newFireAllRules(10);
farc.setOutIdentifier("num-rules-fired");
commands.add(farc);
ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(commands));
int fired = Integer.parseInt(results.getValue("num-rules-fired").toString());
assertEquals(1, fired);
}
Aggregations