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));
}
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");
}
}
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());
}
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));
}
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();
}
Aggregations