use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class StatelessSessionTest method getSession2.
private StatelessKieSession getSession2(final Resource resource) throws Exception {
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(resource, ResourceType.DRL);
if (kbuilder.hasErrors()) {
System.out.println(kbuilder.getErrors());
}
assertFalse(kbuilder.hasErrors());
final Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(pkgs);
kbase = SerializationHelper.serializeObject(kbase);
final StatelessKieSession session = kbase.newStatelessKieSession();
session.setGlobal("list", this.list);
session.setGlobal("cheesery", this.cheesery);
return session;
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class MarshallingTest method testSignedSerialization4.
/*
* A client environment configured to use signed serialization
* should refuse any non-signed serialized rulebase
*/
@Test
public void testSignedSerialization4() throws Exception {
Collection<KiePackage> kpkgs = loadKnowledgePackages(null, false, "../test_Dynamic1_0.drl");
kpkgs = SerializationHelper.serializeObject(kpkgs);
InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
kBase.addPackages(kpkgs);
kBase = SerializationHelper.serializeObject(kBase);
try {
// set only the deserialisation properties, but not the serialization
setPublicKeyProperties();
kBase = SerializationHelper.serializeObject(kBase);
fail("Should not deserialize an unsigned rulebase on an environment configured to work with signed rulebases.");
} catch (Exception e) {
// success
} finally {
unsetPublicKeyProperties();
}
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class MarshallingTest method testSerializeWorkingMemoryAndRuleBase3.
@Test
public void testSerializeWorkingMemoryAndRuleBase3() throws Exception {
// has the first newStatefulSession after the ruleBase is serialised
Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Serializable.drl");
InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
KieSession session = kBase.newKieSession();
kBase.addPackages(kpkgs);
session.setGlobal("list", new ArrayList());
final Person bob = new Person("bob");
session.insert(bob);
// serialise a hashmap with the RuleBase as a key, after WM population
Map map = new HashMap();
map.put("x", kBase);
map = SerializationHelper.serializeObject(map);
kBase = (InternalKnowledgeBase) map.get("x");
// now try serialising with a fully populated wm from a serialised rulebase
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
org.kie.api.definition.rule.Rule[] rules = (org.kie.api.definition.rule.Rule[]) kBase.getPackage("org.drools.compiler.test").getRules().toArray(new org.kie.api.definition.rule.Rule[0]);
assertEquals(4, rules.length);
assertEquals("match Person 1", rules[0].getName());
assertEquals("match Person 2", rules[1].getName());
assertEquals("match Person 3", rules[2].getName());
assertEquals("match Integer", rules[3].getName());
assertEquals(1, session.getObjects().size());
assertEquals(bob, IteratorToList.convert(session.getObjects().iterator()).get(0));
assertEquals(2, ((InternalAgenda) session.getAgenda()).agendaSize());
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
session.fireAllRules();
final List list = (List) session.getGlobal("list");
assertEquals(3, list.size());
// because of agenda-groups
assertEquals(new Integer(4), list.get(0));
assertEquals(2, session.getObjects().size());
assertTrue(IteratorToList.convert(session.getObjects().iterator()).contains(bob));
assertTrue(IteratorToList.convert(session.getObjects().iterator()).contains(new Person("help")));
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class MarshallingTest method testSerializeAdd3.
/*
* Works Fine if both the scenarios mentioned above are skipped.
*/
@Test
public void testSerializeAdd3() 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);
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(4, results.size());
assertEquals(stilton2.getObject(), results.get(1));
assertEquals(bob2.getObject(), results.get(2));
assertEquals(bob1.getObject(), results.get(3));
// serialize session and rulebase
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
// dynamic add pkgs
kpkgs = loadKnowledgePackages("../test_Dynamic1_2.drl");
kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
InternalFactHandle stilton3 = (InternalFactHandle) session.insert(new Cheese("stilton", 40));
InternalFactHandle brie3 = (InternalFactHandle) session.insert(new Cheese("brie", 40));
InternalFactHandle bob3 = (InternalFactHandle) session.insert(new Person("bob", 40));
InternalFactHandle bob4 = (InternalFactHandle) session.insert(new Person("bob", 40));
InternalFactHandle addr1 = (InternalFactHandle) session.insert(new Address("bangalore"));
InternalFactHandle addr2 = (InternalFactHandle) session.insert(new Address("India"));
session.fireAllRules();
assertEquals(9, results.size());
assertEquals(stilton3.getObject(), results.get(4));
assertEquals(bob4.getObject(), results.get(5));
assertEquals(bob3.getObject(), results.get(6));
assertEquals(addr2.getObject(), results.get(7));
assertEquals(addr1.getObject(), results.get(8));
// serialize session and rulebase
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
InternalFactHandle stilton4 = (InternalFactHandle) session.insert(new Cheese("stilton", 50));
InternalFactHandle brie4 = (InternalFactHandle) session.insert(new Cheese("brie", 50));
InternalFactHandle bob5 = (InternalFactHandle) session.insert(new Person("bob", 50));
InternalFactHandle bob6 = (InternalFactHandle) session.insert(new Person("bob", 50));
InternalFactHandle addr3 = (InternalFactHandle) session.insert(new Address("Tripura"));
InternalFactHandle addr4 = (InternalFactHandle) session.insert(new Address("Agartala"));
session.fireAllRules();
assertEquals(14, results.size());
assertEquals(stilton4.getObject(), results.get(9));
assertEquals(bob6.getObject(), results.get(10));
assertEquals(bob5.getObject(), results.get(11));
assertEquals(addr4.getObject(), results.get(12));
assertEquals(addr3.getObject(), results.get(13));
// serialize session and rulebase
kBase = (InternalKnowledgeBase) SerializationHelper.serializeObject(kBase);
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
session.dispose();
}
use of org.drools.core.impl.InternalKnowledgeBase in project drools by kiegroup.
the class MarshallingTest method testSerializeWorkingMemoryAndRuleBase2.
@Test
public void testSerializeWorkingMemoryAndRuleBase2() throws Exception {
Collection<KiePackage> kpkgs = loadKnowledgePackages("../test_Serializable.drl");
InternalKnowledgeBase kBase = (InternalKnowledgeBase) getKnowledgeBase();
// serialise a hashmap with the RuleBase as a key
Map map = new HashMap();
map.put("x", kBase);
map = SerializationHelper.serializeObject(map);
kBase = (InternalKnowledgeBase) map.get("x");
KieSession session = kBase.newKieSession();
// serialise the working memory before population
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
kBase.addPackages(kpkgs);
session.setGlobal("list", new ArrayList());
final Person bob = new Person("bob");
session.insert(bob);
((InternalWorkingMemory) session).flushPropagations();
org.kie.api.definition.rule.Rule[] rules = (org.kie.api.definition.rule.Rule[]) kBase.getPackage("org.drools.compiler.test").getRules().toArray(new org.kie.api.definition.rule.Rule[0]);
assertEquals(4, rules.length);
assertEquals("match Person 1", rules[0].getName());
assertEquals("match Person 2", rules[1].getName());
assertEquals("match Person 3", rules[2].getName());
assertEquals("match Integer", rules[3].getName());
assertEquals(1, session.getObjects().size());
assertEquals(bob, IteratorToList.convert(session.getObjects().iterator()).get(0));
assertEquals(2, ((InternalAgenda) session.getAgenda()).agendaSize());
session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, true);
session.fireAllRules();
final List list = (List) session.getGlobal("list");
assertEquals(3, list.size());
// because of agenda-groups
assertEquals(new Integer(4), list.get(0));
assertEquals(2, session.getObjects().size());
assertTrue(IteratorToList.convert(session.getObjects().iterator()).contains(bob));
assertTrue(IteratorToList.convert(session.getObjects().iterator()).contains(new Person("help")));
}
Aggregations