use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class MarshallingTest method testSerializeAddRemove_NoClassDefFoundError.
/*
* I have tried both the scenarios
* 1. Remove a rule from a pkg.
* 2. Remove a pkg
*
* But both cases after inserting associated data points (i.e data points which are used to fire/activate the removed rule)
* session.fireAllRules() is throwing NoClassDefFoundError
*/
@Test
public void testSerializeAddRemove_NoClassDefFoundError() throws Exception {
// Create a rulebase, a session, and test it
Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Dynamic1_0.drl");
kpkgs = SerializationHelper.serializeObject(kpkgs);
InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
kBase.addPackages(kpkgs);
kBase = SerializationHelper.serializeObject(kBase);
List results = new ArrayList();
KieSession session = kBase.newKieSession();
session.setGlobal("results", results);
InternalFactHandle stilton1 = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
InternalFactHandle brie1 = (InternalFactHandle) session.insert(new Cheese("brie", 10));
session.fireAllRules();
assertEquals(1, results.size());
assertEquals(stilton1.getObject(), results.get(0));
// serialize session and rulebase
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
// dynamic add pkgs
kpkgs = loadKnowledgePackages("../test_Dynamic3_0.drl");
kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
results.clear();
InternalFactHandle stilton2 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
InternalFactHandle brie2 = (InternalFactHandle) session.insert(new Cheese("brie", 20));
InternalFactHandle bob1 = (InternalFactHandle) session.insert(new Person("bob", 20));
InternalFactHandle bob2 = (InternalFactHandle) session.insert(new Person("bob", 30));
session.fireAllRules();
assertEquals(3, results.size());
assertEquals(stilton2.getObject(), results.get(0));
assertEquals(bob2.getObject(), results.get(1));
assertEquals(bob1.getObject(), results.get(2));
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
results.clear();
// CASE 1: remove rule
kBase.removeRule("org.drools.compiler.test", "like stilton");
InternalFactHandle stilton3 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
InternalFactHandle brie3 = (InternalFactHandle) session.insert(new Cheese("brie", 20));
InternalFactHandle bob3 = (InternalFactHandle) session.insert(new Person("bob", 20));
InternalFactHandle bob4 = (InternalFactHandle) session.insert(new Person("bob", 30));
session.fireAllRules();
assertEquals(2, results.size());
assertEquals(bob4.getObject(), results.get(0));
assertEquals(bob3.getObject(), results.get(1));
// now recreate the rulebase, deserialize the session and test it
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
results.clear();
// CASE 2: remove pkg
kBase.removeKiePackage("org.drools.compiler.test");
InternalFactHandle stilton4 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
InternalFactHandle brie4 = (InternalFactHandle) session.insert(new Cheese("brie", 20));
InternalFactHandle bob5 = (InternalFactHandle) session.insert(new Person("bob", 20));
InternalFactHandle bob6 = (InternalFactHandle) session.insert(new Person("bob", 30));
session.fireAllRules();
assertEquals(2, results.size());
assertEquals(bob6.getObject(), results.get(0));
assertEquals(bob5.getObject(), results.get(1));
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
results.clear();
session.setGlobal("results", results);
InternalFactHandle stilton5 = (InternalFactHandle) session.insert(new Cheese("stilton", 30));
InternalFactHandle brie5 = (InternalFactHandle) session.insert(new Cheese("brie", 30));
InternalFactHandle bob7 = (InternalFactHandle) session.insert(new Person("bob", 30));
InternalFactHandle bob8 = (InternalFactHandle) session.insert(new Person("bob", 40));
session.fireAllRules();
assertEquals(2, results.size());
assertEquals(bob8.getObject(), results.get(0));
assertEquals(bob7.getObject(), results.get(1));
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
session.dispose();
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class MarshallingTest method testSerializeAdd2.
/*
* Here I am inserting data points which are not used by any rule (e.g Person).
* Later adding rule (e.g. Rule: 'match Person') for those data points.
*
* Result: Pkg/Rule addition is failing with ClassCastException
*/
@Test
public void testSerializeAdd2() throws Exception {
// Create a rulebase, a session, and test it
Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Dynamic1_0.drl");
kpkgs = SerializationHelper.serializeObject(kpkgs);
InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
kBase.addPackages(kpkgs);
kBase = SerializationHelper.serializeObject(kBase);
List results = new ArrayList();
KieSession session = kBase.newKieSession();
session.setGlobal("results", results);
InternalFactHandle stilton1 = (InternalFactHandle) session.insert(new Cheese("stilton", 10));
session.insert(new Cheese("brie", 10));
InternalFactHandle bob = (InternalFactHandle) session.insert(new Person("bob", 10));
// fire rules
session.fireAllRules();
// check the results are correct
assertEquals(1, results.size());
assertEquals(stilton1.getObject(), results.get(0));
// serialize session and rulebase
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
// dynamically add a new package
kpkgs = loadKnowledgePackages("../test_Dynamic3_0.drl");
kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
InternalFactHandle stilton2 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
session.insert(new Cheese("brie", 20));
InternalFactHandle mark = (InternalFactHandle) session.insert(new Person("mark", 20));
session.fireAllRules();
results = (List) session.getGlobal("results");
assertEquals(4, results.size());
assertEquals(stilton2.getObject(), results.get(1));
assertEquals(bob.getObject(), results.get(2));
assertEquals(mark.getObject(), results.get(3));
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
// dispose session
session.dispose();
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class UnlinkingTest method multipleJoinsUsingSameOTN.
@Test
public void multipleJoinsUsingSameOTN() throws Exception {
KieBase kbase = loadKnowledgeBase("test_LRUnlinking.drl");
kbase = SerializationHelper.serializeObject(kbase);
final KieSession wmOne = kbase.newKieSession();
final KieSession wmTwo = kbase.newKieSession();
final List<Person> listOne = new ArrayList<Person>();
final List<Person> listTwo = new ArrayList<Person>();
wmOne.setGlobal("results", listOne);
wmTwo.setGlobal("results", listTwo);
Person name = new Person();
Person likes = new Person();
Person age = new Person();
Person hair = new Person();
Person happy = new Person();
Person match = new Person();
name.setName("Ana");
likes.setLikes("Chocolate");
age.setAge(30);
hair.setHair("brown");
happy.setHappy(true);
match.setName("Leo");
match.setLikes("Chocolate");
match.setAge(30);
match.setHair("brown");
match.setHappy(true);
// WM One - first round of inserts
wmOne.insert(name);
wmOne.insert(likes);
wmOne.insert(age);
wmOne.fireAllRules();
assertEquals("Should not have fired", 0, listOne.size());
// WM Two - first round o inserts
wmTwo.insert(name);
wmTwo.insert(likes);
wmTwo.insert(age);
wmTwo.fireAllRules();
assertEquals("Should not have fired", 0, listTwo.size());
wmOne.insert(hair);
wmOne.insert(happy);
InternalFactHandle matchHandle = (InternalFactHandle) wmOne.insert(match);
wmOne.fireAllRules();
assertTrue("Should have fired", listOne.size() > 0);
assertEquals("Should have inserted the match Person", matchHandle.getObject(), listOne.get(0));
wmTwo.fireAllRules();
assertEquals("Should not have fired", 0, listTwo.size());
wmTwo.insert(hair);
wmTwo.insert(happy);
wmTwo.insert(match);
wmTwo.fireAllRules();
assertTrue("Should have fired", listTwo.size() > 0);
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class StreamsTest method testEventAssertion.
@Test(timeout = 10000)
public void testEventAssertion() throws Exception {
// read in the source
KieBase kbase = loadKnowledgeBase("test_EntryPoint.drl");
// final RuleBase ruleBase = loadRuleBase( reader );
KieSessionConfiguration conf = SessionConfiguration.newInstance();
((SessionConfiguration) conf).setClockType(ClockType.PSEUDO_CLOCK);
KieSession session = kbase.newKieSession(conf, null);
final List results = new ArrayList();
session.setGlobal("results", results);
StockTickInterface tick1 = new StockTick(1, "DROO", 50, System.currentTimeMillis());
StockTickInterface tick2 = new StockTick(2, "ACME", 10, System.currentTimeMillis());
StockTickInterface tick3 = new StockTick(3, "ACME", 10, System.currentTimeMillis());
StockTickInterface tick4 = new StockTick(4, "DROO", 50, System.currentTimeMillis());
InternalFactHandle handle1 = (InternalFactHandle) session.insert(tick1);
InternalFactHandle handle2 = (InternalFactHandle) session.insert(tick2);
InternalFactHandle handle3 = (InternalFactHandle) session.insert(tick3);
InternalFactHandle handle4 = (InternalFactHandle) session.insert(tick4);
assertNotNull(handle1);
assertNotNull(handle2);
assertNotNull(handle3);
assertNotNull(handle4);
assertTrue(handle1.isEvent());
assertTrue(handle2.isEvent());
assertTrue(handle3.isEvent());
assertTrue(handle4.isEvent());
session.fireAllRules();
assertEquals(0, results.size());
StockTickInterface tick5 = new StockTick(5, "DROO", 50, System.currentTimeMillis());
StockTickInterface tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis());
StockTickInterface tick7 = new StockTick(7, "ACME", 15, System.currentTimeMillis());
StockTickInterface tick8 = new StockTick(8, "DROO", 50, System.currentTimeMillis());
EntryPoint entry = session.getEntryPoint("StockStream");
InternalFactHandle handle5 = (InternalFactHandle) entry.insert(tick5);
InternalFactHandle handle6 = (InternalFactHandle) entry.insert(tick6);
InternalFactHandle handle7 = (InternalFactHandle) entry.insert(tick7);
InternalFactHandle handle8 = (InternalFactHandle) entry.insert(tick8);
assertNotNull(handle5);
assertNotNull(handle6);
assertNotNull(handle7);
assertNotNull(handle8);
assertTrue(handle5.isEvent());
assertTrue(handle6.isEvent());
assertTrue(handle7.isEvent());
assertTrue(handle8.isEvent());
session.fireAllRules();
assertEquals(1, results.size());
assertSame(tick7, results.get(0));
}
use of org.drools.core.common.InternalFactHandle in project drools by kiegroup.
the class StreamsTest method testModifyRetracOnEntryPointFacts.
@Test(timeout = 10000)
public void testModifyRetracOnEntryPointFacts() throws Exception {
// read in the source
KieBase kbase = loadKnowledgeBase("test_modifyRetractEntryPoint.drl");
KieSession session = kbase.newKieSession();
final List<? extends Number> results = new ArrayList<Number>();
session.setGlobal("results", results);
StockTickInterface tick5 = new StockTick(5, "DROO", 50, System.currentTimeMillis());
StockTickInterface tick6 = new StockTick(6, "ACME", 10, System.currentTimeMillis());
StockTickInterface tick7 = new StockTick(7, "ACME", 30, System.currentTimeMillis());
StockTickInterface tick8 = new StockTick(8, "DROO", 50, System.currentTimeMillis());
EntryPoint entry = session.getEntryPoint("stream1");
InternalFactHandle handle5 = (InternalFactHandle) entry.insert(tick5);
InternalFactHandle handle6 = (InternalFactHandle) entry.insert(tick6);
InternalFactHandle handle7 = (InternalFactHandle) entry.insert(tick7);
InternalFactHandle handle8 = (InternalFactHandle) entry.insert(tick8);
assertNotNull(handle5);
assertNotNull(handle6);
assertNotNull(handle7);
assertNotNull(handle8);
assertTrue(handle5.isEvent());
assertTrue(handle6.isEvent());
assertTrue(handle7.isEvent());
assertTrue(handle8.isEvent());
session.fireAllRules();
System.out.println(results);
assertEquals(2, results.size());
assertEquals(30, ((Number) results.get(0)).intValue());
assertEquals(110, ((Number) results.get(1)).intValue());
// the 3 non-matched facts continue to exist in the entry point
assertEquals(3, entry.getObjects().size());
// but no fact was inserted into the main session
assertEquals(0, session.getObjects().size());
}
Aggregations