use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DynamicRulesTest method testRemovePackageSubNetwork.
@Test(timeout = 10000)
public void testRemovePackageSubNetwork() throws Exception {
KieBase kbase = loadKnowledgeBase("test_DynamicRulesWithSubnetwork.drl");
String packageName = kbase.getKiePackages().iterator().next().getName();
KieSession workingMemory = createKnowledgeSession(kbase);
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 = loadKnowledgePackages("test_DynamicRulesWithSubnetwork.drl");
ruleBaseWM.addPackages(SerializationHelper.serializeObject(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(SerializationHelper.serializeObject(kpkgs));
workingMemory.fireAllRules();
assertEquals(1, results.size());
assertEquals(3, ((List) results.get(0)).size());
results.clear();
ruleBaseWM.removeKiePackage(packageName);
ruleBaseWM.addPackages(SerializationHelper.serializeObject(kpkgs));
workingMemory.fireAllRules();
assertEquals(1, results.size());
assertEquals(3, ((List) results.get(0)).size());
results.clear();
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DynamicRulesTest method testDynamicRules.
@Test(timeout = 10000)
public void testDynamicRules() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase();
KieSession session = createKnowledgeSession(kbase);
final Cheese a = new Cheese("stilton", 10);
final Cheese b = new Cheese("stilton", 15);
final Cheese c = new Cheese("stilton", 20);
session.insert(a);
session.insert(b);
session.insert(c);
Collection<KiePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicRules.drl"));
kbase.addPackages(kpkgs);
kbase = SerializationHelper.serializeObject(kbase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
session.fireAllRules();
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DynamicRulesTest method testIsolatedClassLoaderWithEnumsPkgBuilder.
@Test(timeout = 10000)
public void testIsolatedClassLoaderWithEnumsPkgBuilder() throws Exception {
try {
// Creates first class loader and use it to load fact classes
ClassLoader loader1 = new SubvertedClassLoader(new URL[] { getClass().getResource("/testEnum.jar") }, this.getClass().getClassLoader());
loader1.loadClass("org.drools.Primitives");
loader1.loadClass("org.drools.TestEnum");
// create a builder with the given classloader
KnowledgeBuilderConfiguration conf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration(null, loader1);
Collection<KiePackage> kpkgs = loadKnowledgePackages(conf, "test_EnumSerialization.drl");
// serialize out
byte[] out = DroolsStreamUtils.streamOut(kpkgs);
// adding original packages to a kbase just to make sure they are fine
KieBaseConfiguration kbaseConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(null, loader1);
InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase(kbaseConf);
kbase.addPackages(kpkgs);
KieSession ksession = createKnowledgeSession(kbase);
List list = new ArrayList();
ksession.setGlobal("list", list);
assertEquals(1, ksession.fireAllRules());
assertEquals(1, list.size());
// now, create another classloader and make sure it has access to the classes
ClassLoader loader2 = new SubvertedClassLoader(new URL[] { getClass().getResource("/testEnum.jar") }, this.getClass().getClassLoader());
loader2.loadClass("org.drools.Primitives");
loader2.loadClass("org.drools.TestEnum");
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
Collection<KiePackage> kpkgs2 = null;
try {
Thread.currentThread().setContextClassLoader(loader2);
kpkgs2 = (Collection<KiePackage>) DroolsStreamUtils.streamIn(out);
} finally {
Thread.currentThread().setContextClassLoader(ccl);
}
// create another kbase
KieBaseConfiguration kbaseConf2 = KnowledgeBaseFactory.newKnowledgeBaseConfiguration(null, loader2);
InternalKnowledgeBase kbase2 = (InternalKnowledgeBase) getKnowledgeBase(kbaseConf2);
kbase2.addPackages(kpkgs2);
ksession = createKnowledgeSession(kbase2);
list = new ArrayList();
ksession.setGlobal("list", list);
assertEquals(1, ksession.fireAllRules());
assertEquals(1, list.size());
} catch (ClassCastException cce) {
cce.printStackTrace();
fail("No ClassCastException should be raised.");
}
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DynamicRulesTest method testIsolatedClassLoaderWithEnumsContextClassloader.
@Test(timeout = 10000)
public void testIsolatedClassLoaderWithEnumsContextClassloader() throws Exception {
try {
// Creates first class loader and use it to load fact classes
ClassLoader loader1 = new SubvertedClassLoader(new URL[] { getClass().getResource("/testEnum.jar") }, this.getClass().getClassLoader());
loader1.loadClass("org.drools.Primitives");
loader1.loadClass("org.drools.TestEnum");
byte[] out = null;
// Build it using the current context
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
Collection<KiePackage> kpkgs2 = null;
try {
Thread.currentThread().setContextClassLoader(loader1);
// create a builder with the given classloader
Collection<KiePackage> kpkgs = loadKnowledgePackages("test_EnumSerialization.drl");
// serialize out
out = DroolsStreamUtils.streamOut(kpkgs);
// adding original packages to a kbase just to make sure they are fine
InternalKnowledgeBase kbase = (InternalKnowledgeBase) getKnowledgeBase();
kbase.addPackages(kpkgs);
KieSession ksession = createKnowledgeSession(kbase);
List list = new ArrayList();
ksession.setGlobal("list", list);
assertEquals(1, ksession.fireAllRules());
assertEquals(1, list.size());
} finally {
Thread.currentThread().setContextClassLoader(ccl);
}
// now, create another classloader and make sure it has access to the classes
ClassLoader loader2 = new SubvertedClassLoader(new URL[] { getClass().getResource("/testEnum.jar") }, this.getClass().getClassLoader());
loader2.loadClass("org.drools.Primitives");
loader2.loadClass("org.drools.TestEnum");
// set context classloader and use it
ccl = Thread.currentThread().getContextClassLoader();
kpkgs2 = null;
try {
Thread.currentThread().setContextClassLoader(loader2);
kpkgs2 = (Collection<KiePackage>) DroolsStreamUtils.streamIn(out);
// create another kbase
InternalKnowledgeBase kbase2 = (InternalKnowledgeBase) getKnowledgeBase();
kbase2.addPackages(kpkgs2);
KieSession ksession = createKnowledgeSession(kbase2);
List list = new ArrayList();
ksession.setGlobal("list", list);
assertEquals(1, ksession.fireAllRules());
assertEquals(1, list.size());
} finally {
Thread.currentThread().setContextClassLoader(ccl);
}
} catch (ClassCastException cce) {
cce.printStackTrace();
fail("No ClassCastException should be raised.");
}
}
use of org.kie.api.definition.KiePackage in project drools by kiegroup.
the class DynamicRulesTest method testRuleBaseAddRemove.
@Test(timeout = 10000)
public void testRuleBaseAddRemove() throws Exception {
InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase();
// add and remove
Collection<KiePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_Dynamic1.drl"));
String pkgName = kpkgs.iterator().next().getName();
kbase.addPackages(kpkgs);
kbase.removeKiePackage(pkgName);
kbase = SerializationHelper.serializeObject(kbase);
// add and remove again
kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_Dynamic1.drl"));
pkgName = kpkgs.iterator().next().getName();
kbase.addPackages(kpkgs);
kbase.removeKiePackage(pkgName);
kbase = SerializationHelper.serializeObject(kbase);
}
Aggregations