use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class MarshallingTest method testSerializable.
@Test
public void testSerializable() throws Exception {
Collection<KiePackage> kpkgs = loadKnowledgePackages("test_Serializable.drl");
KiePackage kpkg = kpkgs.iterator().next();
kpkg = SerializationHelper.serializeObject(kpkg);
InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBase();
kbase.addPackages(Collections.singleton(kpkg));
final org.kie.api.definition.rule.Rule[] rules = kbase.getKiePackages().iterator().next().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());
KieSession ksession = kbase.newKieSession();
ksession.setGlobal("list", new ArrayList());
final Person bob = new Person("bob");
ksession.insert(bob);
ksession = getSerialisedStatefulKnowledgeSession(ksession, true);
assertEquals(1, ksession.getFactCount());
assertEquals(bob, ksession.getObjects().iterator().next());
assertEquals(2, ((InternalAgenda) ksession.getAgenda()).getAgendaGroupsManager().agendaSize());
ksession.fireAllRules();
List list = (List) ksession.getGlobal("list");
assertEquals(3, list.size());
// because of agenda-groups
assertEquals(new Integer(4), list.get(0));
// need to create a new collection or otherwise the collection will be identity based
List<?> objects = new ArrayList<Object>(ksession.getObjects());
assertEquals(2, objects.size());
assertTrue(objects.contains(bob));
assertTrue(objects.contains(new Person("help")));
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class MarshallingTest method testSerializeAdd_newRuleNotFiredForNewData.
/*
* Here I am inserting data points(e.g. Cheese) with 'stilton' / 'brie' as type value.
* Initially I had only 1 rule ('like stilton') for data points(e.g. Cheese) having type as 'stilton'.
*
* Later added new rule ('like brie') for data points(e.g. Cheese) having type as 'brie'.
*
* Result: new rule is not getting fired for new data points having type as 'brie'.
* Only for old data points having type as 'brie' the new rule got fired.
*/
@Test
public void testSerializeAdd_newRuleNotFiredForNewData() 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);
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();
// serialize session
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
// dynamically add a new package
kpkgs = loadKnowledgePackages("test_Dynamic1_1.drl");
kBase.addPackages(SerializationHelper.serializeObject(kpkgs));
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
InternalFactHandle stilton2 = (InternalFactHandle) session.insert(new Cheese("stilton", 20));
InternalFactHandle brie2 = (InternalFactHandle) session.insert(new Cheese("brie", 20));
InternalFactHandle brie3 = (InternalFactHandle) session.insert(new Cheese("brie", 30));
session.fireAllRules();
assertEquals(5, results.size());
assertEquals(stilton2.getObject(), results.get(1));
assertTrue(results.contains(brie1.getObject()));
assertTrue(results.contains(brie3.getObject()));
assertTrue(results.contains(brie3.getObject()));
session = getSerialisedStatefulKnowledgeSession(session, kBase, true);
session.dispose();
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class FactHandleMarshallingTest method backwardsCompatibleEventFactHandleTest.
@Test
public void backwardsCompatibleEventFactHandleTest() throws IOException, ClassNotFoundException {
InternalKnowledgeBase kBase = createKnowledgeBase();
StatefulKnowledgeSessionImpl wm = createWorkingMemory(kBase);
InternalFactHandle factHandle = createEventFactHandle(wm, kBase);
// marshall/serialize workItem
byte[] byteArray;
{
ObjectMarshallingStrategy[] strats = new ObjectMarshallingStrategy[] { MarshallerFactory.newSerializeMarshallingStrategy(), new MarshallerProviderImpl().newIdentityMarshallingStrategy() };
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ProtobufMarshallerWriteContext outContext = new ProtobufMarshallerWriteContext(baos, null, null, null, new ObjectMarshallingStrategyStoreImpl(strats), true, true, null);
OldOutputMarshallerMethods.writeFactHandle_v1(outContext, (ObjectOutputStream) outContext, outContext.getObjectMarshallingStrategyStore(), 2, factHandle);
outContext.close();
byteArray = baos.toByteArray();
}
// unmarshall/deserialize workItem
InternalFactHandle newFactHandle;
{
// Only put serialization strategy in
ObjectMarshallingStrategy[] newStrats = new ObjectMarshallingStrategy[] { MarshallerFactory.newSerializeMarshallingStrategy() };
ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
ProtobufMarshallerReaderContext inContext = new ProtobufMarshallerReaderContext(bais, null, null, new ObjectMarshallingStrategyStoreImpl(newStrats), Collections.EMPTY_MAP, true, true, null);
inContext.setWorkingMemory(wm);
newFactHandle = InputMarshaller.readFactHandle(inContext);
inContext.close();
}
assertTrue("Serialized FactHandle not the same as the original.", compareInstances(factHandle, newFactHandle));
}
use of org.drools.kiesession.rulebase.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);
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.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class MarshallingTest method testScheduledActivation.
@Test
@Ignore("This test is suspicious to say the least...")
public void testScheduledActivation() {
InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
InternalKnowledgePackage impl = CoreComponentFactory.get().createKnowledgePackage("test");
BuildContext buildContext = new BuildContext(knowledgeBase, Collections.emptyList());
// simple rule that fires after 10 seconds
final RuleImpl rule = new RuleImpl("test-rule");
new RuleTerminalNode(1, new MockTupleSource(2, buildContext), rule, rule.getLhs(), 0, buildContext);
final List<String> fired = new ArrayList<String>();
rule.setConsequence(new Consequence() {
public void evaluate(KnowledgeHelper knowledgeHelper, ReteEvaluator reteEvaluator) throws Exception {
fired.add("a");
}
public String getName() {
return "default";
}
});
rule.setTimer(new DurationTimer(10000));
rule.setPackage("test");
impl.addRule(rule);
knowledgeBase.addPackages(Collections.singleton(impl));
SessionConfiguration config = SessionConfiguration.newInstance();
config.setClockType(ClockType.PSEUDO_CLOCK);
KieSession ksession = knowledgeBase.newKieSession(config, KieServices.get().newEnvironment());
PseudoClockScheduler scheduler = (PseudoClockScheduler) ksession.<SessionClock>getSessionClock();
Marshaller marshaller = MarshallerFactory.newMarshaller(knowledgeBase);
ksession.insert("cheese");
assertTrue(fired.isEmpty());
// marshall, then unmarshall session
readWrite(knowledgeBase, ksession, config);
// the activations should fire after 10 seconds
assertTrue(fired.isEmpty());
scheduler.advanceTime(12, TimeUnit.SECONDS);
assertFalse(fired.isEmpty());
}
Aggregations