Search in sources :

Example 51 with InternalKnowledgeBase

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));
}
Also used : KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 52 with InternalKnowledgeBase

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);
}
Also used : KiePackage(org.kie.api.definition.KiePackage) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 53 with InternalKnowledgeBase

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.");
    }
}
Also used : URLClassLoader(java.net.URLClassLoader) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 54 with InternalKnowledgeBase

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());
}
Also used : InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 55 with InternalKnowledgeBase

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();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) RuleBaseConfiguration(org.drools.core.RuleBaseConfiguration) InternalAgenda(org.drools.core.common.InternalAgenda) KiePackage(org.kie.api.definition.KiePackage) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) KieModule(org.kie.api.builder.KieModule) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Aggregations

InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)268 Test (org.junit.Test)213 KieSession (org.kie.api.runtime.KieSession)136 ArrayList (java.util.ArrayList)99 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)98 KiePackage (org.kie.api.definition.KiePackage)82 List (java.util.List)55 CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)39 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)34 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)33 InternalFactHandle (org.drools.core.common.InternalFactHandle)30 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)24 Cheese (org.drools.mvel.compiler.Cheese)24 FactType (org.kie.api.definition.type.FactType)23 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)22 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)21 BetaMemory (org.drools.core.reteoo.BetaMemory)18 PackageDescr (org.drools.drl.ast.descr.PackageDescr)18 Resource (org.kie.api.io.Resource)18 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)17