Search in sources :

Example 46 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class DynamicRulesTest method testDynamicRuleAdditions.

@Test(timeout = 10000)
public void testDynamicRuleAdditions() throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromClasspathResources("test", getClass(), kieBaseTestConfiguration, "test_Dynamic1.drl");
    KieSession workingMemory = kbase.newKieSession();
    workingMemory.setGlobal("total", new Integer(0));
    final List<?> list = new ArrayList<Object>();
    workingMemory.setGlobal("list", list);
    // Adding person in advance. There is no Person() object
    // type node in memory yet, but the rule engine is supposed
    // to handle that correctly
    final PersonInterface bob = new Person("bob", "stilton");
    bob.setStatus("Not evaluated");
    workingMemory.insert(bob);
    final Cheese stilton = new Cheese("stilton", 5);
    workingMemory.insert(stilton);
    final Cheese cheddar = new Cheese("cheddar", 5);
    workingMemory.insert(cheddar);
    workingMemory.fireAllRules();
    assertEquals(1, list.size());
    assertEquals("stilton", list.get(0));
    Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_Dynamic2.drl").getKiePackages();
    kbase.addPackages(kpkgs);
    workingMemory.fireAllRules();
    assertEquals(5, list.size());
    assertEquals("stilton", list.get(0));
    assertTrue("cheddar".equals(list.get(1)) || "cheddar".equals(list.get(2)));
    assertTrue("stilton".equals(list.get(1)) || "stilton".equals(list.get(2)));
    list.clear();
    kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_Dynamic3.drl").getKiePackages();
    kbase.addPackages(kpkgs);
    // Package 3 has a rule working on Person instances.
    // As we added person instance in advance, rule should fire now
    workingMemory.fireAllRules();
    assertEquals("Rule from package 3 should have been fired", "match Person ok", bob.getStatus());
    assertEquals(1, list.size());
    assertEquals(bob, list.get(0));
    kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_Dynamic4.drl").getKiePackages();
    kbase.addPackages(kpkgs);
    workingMemory.fireAllRules();
    assertEquals("Rule from package 4 should have been fired", "Who likes Stilton ok", bob.getStatus());
    assertEquals(2, list.size());
    assertEquals(bob, list.get(1));
}
Also used : PersonInterface(org.drools.mvel.compiler.PersonInterface) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) Person(org.drools.mvel.compiler.Person) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 47 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class DynamicRulesTest method testRuleBaseAddRemoveSubNetworks.

@Test(timeout = 10000)
public void testRuleBaseAddRemoveSubNetworks() throws Exception {
    try {
        // add and remove
        InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration);
        Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_DynamicRulesWithSubnetwork.drl").getKiePackages();
        KiePackage kpkg = (KiePackage) kpkgs.toArray()[0];
        kbase.addPackages(kpkgs);
        kbase.removeKiePackage(kpkg.getName());
        // add and remove again
        kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_DynamicRulesWithSubnetwork.drl").getKiePackages();
        kpkg = (KiePackage) kpkgs.toArray()[0];
        kbase.addPackages(kpkgs);
        kbase.removeKiePackage(kpkg.getName());
    } catch (Exception e) {
        e.printStackTrace();
        fail("Should not raise any exception");
    }
}
Also used : KiePackage(org.kie.api.definition.KiePackage) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 48 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class DynamicRulesTest method testDynamicNotNode.

@Test(timeout = 10000)
public void testDynamicNotNode() throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_CollectDynamicRules1.drl");
    Environment env = EnvironmentFactory.newEnvironment();
    env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, new ObjectMarshallingStrategy[] { new IdentityPlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT) });
    KieSession ksession = kbase.newKieSession(null, env);
    List<?> results = new ArrayList<Object>();
    ksession.setGlobal("results", results);
    final Cheese a = new Cheese("stilton", 10);
    final Cheese b = new Cheese("stilton", 15);
    final Cheese c = new Cheese("stilton", 20);
    ksession.insert(a);
    ksession.insert(b);
    ksession.insert(c);
    Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_DynamicNotNode.drl").getKiePackages();
    kbase.addPackages(kpkgs);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, false);
    results = (List) ksession.getGlobal("results");
    ksession.fireAllRules();
    assertEquals(0, results.size());
    kbase.removeKiePackage("org.drools.mvel.compiler");
    ksession.retract(ksession.getFactHandle(b));
    kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_DynamicNotNode.drl").getKiePackages();
    kbase.addPackages(kpkgs);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, false);
    results = (List<?>) ksession.getGlobal("results");
    ksession.fireAllRules();
    assertEquals(1, results.size());
}
Also used : IdentityPlaceholderResolverStrategy(org.drools.serialization.protobuf.marshalling.IdentityPlaceholderResolverStrategy) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) Environment(org.kie.api.runtime.Environment) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 49 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class DynamicRulesTest method testDynamicRuleRemovalsSubNetworkAndNot.

@Test(timeout = 10000)
public void testDynamicRuleRemovalsSubNetworkAndNot() throws Exception {
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_DynamicRulesWithNotSubnetwork.drl");
    KieSession ksession = kbase.newKieSession();
    final AgendaEventListener alistener = mock(AgendaEventListener.class);
    ksession.addEventListener(alistener);
    // pattern does not match, so do not activate
    ksession.insert(new Person("toni"));
    ksession.fireAllRules();
    verify(alistener, never()).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));
    // pattern matches, so create activation
    ksession.insert(new Person("bob"));
    ksession.fireAllRules();
    verify(alistener, times(1)).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));
    // already active, so no new activation should be created
    ksession.insert(new Person("mark"));
    ksession.fireAllRules();
    verify(alistener, times(1)).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));
    kbase.removeKiePackage("org.drools.mvel.compiler");
    assertEquals(0, kbase.getKiePackages().size());
    // lets re-compile and add it again
    Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_DynamicRulesWithNotSubnetwork.drl").getKiePackages();
    kbase.addPackages(kpkgs);
    ksession.fireAllRules();
    // rule should be reactivated, since data is still in the session
    verify(alistener, times(2)).matchCreated(any(org.kie.api.event.rule.MatchCreatedEvent.class));
}
Also used : KiePackage(org.kie.api.definition.KiePackage) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) KieSession(org.kie.api.runtime.KieSession) MatchCreatedEvent(org.kie.api.event.rule.MatchCreatedEvent) Person(org.drools.mvel.compiler.Person) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 50 with InternalKnowledgeBase

use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.

the class DynamicRulesTest method testRemovePackageSubNetwork.

@Test(timeout = 10000)
public void testRemovePackageSubNetwork() throws Exception {
    KieBase kbase = KieBaseUtil.getKieBaseFromClasspathResources(getClass(), kieBaseTestConfiguration, "test_DynamicRulesWithSubnetwork.drl");
    String packageName = kbase.getKiePackages().iterator().next().getName();
    KieSession workingMemory = kbase.newKieSession();
    List<?> results = new ArrayList<Object>();
    workingMemory.setGlobal("results", results);
    Order order = new Order();
    OrderItem item1 = new OrderItem(order, 1, "Adventure Guide Brazil", OrderItem.TYPE_BOOK, 24);
    OrderItem item2 = new OrderItem(order, 2, "Prehistoric Britain", OrderItem.TYPE_BOOK, 15);
    OrderItem item3 = new OrderItem(order, 3, "Holiday Music", OrderItem.TYPE_CD, 9);
    OrderItem item4 = new OrderItem(order, 4, "Very Best of Mick Jagger", OrderItem.TYPE_CD, 11);
    OrderItem item5 = new OrderItem(order, 5, "The Master and Margarita", OrderItem.TYPE_BOOK, 29);
    order.addItem(item1);
    order.addItem(item2);
    order.addItem(item3);
    order.addItem(item4);
    order.addItem(item5);
    workingMemory.insert(order);
    workingMemory.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(3, ((List) results.get(0)).size());
    results.clear();
    InternalKnowledgeBase ruleBaseWM = (InternalKnowledgeBase) workingMemory.getKieBase();
    ruleBaseWM.removeKiePackage(packageName);
    Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", getClass(), kieBaseTestConfiguration, "test_DynamicRulesWithSubnetwork.drl").getKiePackages();
    ruleBaseWM.addPackages(kpkgs);
    workingMemory.fireAllRules();
    results = (List) workingMemory.getGlobal("results");
    assertEquals(1, results.size());
    assertEquals(3, ((List) results.get(0)).size());
    results.clear();
    ruleBaseWM.removeKiePackage(packageName);
    ruleBaseWM.addPackages(kpkgs);
    workingMemory.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(3, ((List) results.get(0)).size());
    results.clear();
    ruleBaseWM.removeKiePackage(packageName);
    ruleBaseWM.addPackages(kpkgs);
    workingMemory.fireAllRules();
    assertEquals(1, results.size());
    assertEquals(3, ((List) results.get(0)).size());
    results.clear();
}
Also used : Order(org.drools.mvel.compiler.Order) KiePackage(org.kie.api.definition.KiePackage) KieBase(org.kie.api.KieBase) OrderItem(org.drools.mvel.compiler.OrderItem) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) 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