Search in sources :

Example 76 with InternalKnowledgeBase

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")));
}
Also used : ArrayList(java.util.ArrayList) InternalAgenda(org.drools.core.common.InternalAgenda) KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) IteratorToList(org.drools.mvel.integrationtests.IteratorToList) List(java.util.List) ArrayList(java.util.ArrayList) Person(org.drools.mvel.compiler.Person) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 77 with InternalKnowledgeBase

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();
}
Also used : KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) IteratorToList(org.drools.mvel.integrationtests.IteratorToList) List(java.util.List) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) Cheese(org.drools.mvel.compiler.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Example 78 with InternalKnowledgeBase

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));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InternalFactHandle(org.drools.core.common.InternalFactHandle) ObjectOutputStream(java.io.ObjectOutputStream) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) ObjectMarshallingStrategyStoreImpl(org.drools.serialization.protobuf.marshalling.ObjectMarshallingStrategyStoreImpl) Test(org.junit.Test)

Example 79 with InternalKnowledgeBase

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();
    }
}
Also used : KiePackage(org.kie.api.definition.KiePackage) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) IOException(java.io.IOException) Test(org.junit.Test)

Example 80 with InternalKnowledgeBase

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());
}
Also used : ReteEvaluator(org.drools.core.common.ReteEvaluator) Marshaller(org.kie.api.marshalling.Marshaller) ArrayList(java.util.ArrayList) Consequence(org.drools.core.spi.Consequence) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) IOException(java.io.IOException) PseudoClockScheduler(org.drools.core.time.impl.PseudoClockScheduler) DurationTimer(org.drools.core.time.impl.DurationTimer) MockTupleSource(org.drools.core.reteoo.MockTupleSource) BuildContext(org.drools.core.reteoo.builder.BuildContext) KnowledgeHelper(org.drools.core.spi.KnowledgeHelper) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) SessionConfiguration(org.drools.core.SessionConfiguration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)268 Test (org.junit.Test)213 KieSession (org.kie.api.runtime.KieSession)136 ArrayList (java.util.ArrayList)99 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)98 KiePackage (org.kie.api.definition.KiePackage)82 List (java.util.List)55 CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)39 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)34 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)33 InternalFactHandle (org.drools.core.common.InternalFactHandle)30 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)24 Cheese (org.drools.mvel.compiler.Cheese)24 FactType (org.kie.api.definition.type.FactType)23 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)22 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)21 BetaMemory (org.drools.core.reteoo.BetaMemory)18 PackageDescr (org.drools.drl.ast.descr.PackageDescr)18 Resource (org.kie.api.io.Resource)18 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)17