use of org.drools.kiesession.rulebase.InternalKnowledgeBase 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.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class DynamicRulesTest method testRuleBaseAddRemove.
@Test(timeout = 10000)
public void testRuleBaseAddRemove() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromClasspathResources("test", getClass(), kieBaseTestConfiguration);
// add and remove
Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_Dynamic1.drl").getKiePackages();
String pkgName = kpkgs.iterator().next().getName();
kbase.addPackages(kpkgs);
kbase.removeKiePackage(pkgName);
// add and remove again
kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_Dynamic1.drl").getKiePackages();
pkgName = kpkgs.iterator().next().getName();
kbase.addPackages(kpkgs);
kbase.removeKiePackage(pkgName);
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class DynamicRulesTest method testClassLoaderSwitchsUsingConf.
@Test(timeout = 10000)
public void testClassLoaderSwitchsUsingConf() throws Exception {
try {
// Creates first class loader and use it to load fact classes
ClassLoader loader1 = new SubvertedClassLoader(new URL[] { getClass().getResource("/") }, this.getClass().getClassLoader());
Class cheeseClass = loader1.loadClass("org.drools.mvel.compiler.Cheese");
InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromClasspathResourcesWithClassLoaderForKieBuilder("test", getClass(), loader1, kieBaseTestConfiguration, "test_Dynamic1.drl");
KieSession wm = kbase.newKieSession();
wm.insert(cheeseClass.newInstance());
wm.fireAllRules();
// Creates second class loader and use it to load fact classes
ClassLoader loader2 = new SubvertedClassLoader(new URL[] { getClass().getResource("/") }, this.getClass().getClassLoader());
cheeseClass = loader2.loadClass("org.drools.mvel.compiler.Cheese");
kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromClasspathResourcesWithClassLoaderForKieBuilder("test", getClass(), loader2, kieBaseTestConfiguration, "test_Dynamic1.drl");
wm = kbase.newKieSession();
wm.insert(cheeseClass.newInstance());
wm.fireAllRules();
} catch (ClassCastException cce) {
cce.printStackTrace();
fail("No ClassCastException should be raised.");
}
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class DynamicRulesTest method testDynamicRuleRemovalsUnusedWorkingMemorySubNetwork.
@Test(timeout = 10000)
public void testDynamicRuleRemovalsUnusedWorkingMemorySubNetwork() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_DynamicRulesWithSubnetwork1.drl", "test_DynamicRulesWithSubnetwork2.drl", "test_DynamicRulesWithSubnetwork.drl");
assertEquals(2, kbase.getKiePackages().size());
assertEquals(4, kbase.getPackagesMap().get("org.drools.mvel.compiler").getRules().size());
kbase.removeRule("org.drools.mvel.compiler", "Apply Discount on all books");
assertEquals(3, kbase.getPackagesMap().get("org.drools.mvel.compiler").getRules().size());
kbase.removeRule("org.drools.mvel.compiler", "like book");
assertEquals(2, kbase.getPackagesMap().get("org.drools.mvel.compiler").getRules().size());
kbase.removeKiePackage("org.drools.mvel.compiler");
assertEquals(1, kbase.getKiePackages().size());
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase 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();
}
Aggregations