Search in sources :

Example 96 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class ObjectMarshallingStrategyStoreTest method testMultipleObjectMarshallingStrategiesOfTheSameClassWithDifferentNames.

@Test
public void testMultipleObjectMarshallingStrategiesOfTheSameClassWithDifferentNames() throws IOException, ClassNotFoundException {
    Environment env = EnvironmentFactory.newEnvironment();
    final Thing entityOne = new Thing(1, "Object 1");
    final Thing entityTwo = new Thing(2, "Object 2");
    Collection srcItems = new ArrayList();
    srcItems.add(entityOne);
    srcItems.add(entityTwo);
    ObjectMarshallingStrategy[] strats = new ObjectMarshallingStrategy[] { new IdentityPlaceholderResolverStrategy("entityOne", new ObjectMarshallingStrategyAcceptor() {

        @Override
        public boolean accept(Object object) {
            return entityOne.equals(object);
        }
    }, Collections.singletonMap(entityOne.id, (Object) entityOne)), new IdentityPlaceholderResolverStrategy("entityTwo", new ObjectMarshallingStrategyAcceptor() {

        @Override
        public boolean accept(Object object) {
            return entityTwo.equals(object);
        }
    }, Collections.singletonMap(entityTwo.id, (Object) entityTwo)) };
    env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, strats);
    KieSessionConfiguration ksc = SessionConfiguration.newInstance();
    final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbconf);
    KieSession ks = kbase.newKieSession(ksc, env);
    ks.insert(entityOne);
    ks.insert(entityTwo);
    ProtobufMarshaller marshaller = (ProtobufMarshaller) MarshallerFactory.newMarshaller(kbase, strats);
    // Serialize object
    final byte[] b1;
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        marshaller.marshall(bos, ks, System.currentTimeMillis());
        b1 = bos.toByteArray();
        bos.close();
    }
    // Deserialize object
    StatefulKnowledgeSession ksession2;
    {
        ByteArrayInputStream bais = new ByteArrayInputStream(b1);
        try {
            ksession2 = marshaller.unmarshall(bais, ks.getSessionConfiguration(), ks.getEnvironment());
            Collection items = ksession2.getFactHandles();
            Assert.assertTrue(items.size() == 2);
            for (Object item : items) {
                FactHandle factHandle = (FactHandle) item;
                Assert.assertTrue(srcItems.contains(((DefaultFactHandle) factHandle).getObject()));
            }
        } catch (RuntimeException npe) {
            // Here ocurrs the bug that shows that NamedObjectMarshallingStrategies are required.
            Assert.fail("This error only happens if identity ObjectMarshallingStrategy use old name");
        } finally {
            bais.close();
        }
    }
}
Also used : ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) ByteArrayInputStream(java.io.ByteArrayInputStream) Environment(org.kie.api.runtime.Environment) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) ObjectMarshallingStrategyAcceptor(org.kie.api.marshalling.ObjectMarshallingStrategyAcceptor) Test(org.junit.Test)

Example 97 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class ObjectMarshallingStrategyStoreTest method testThrowErrorWhenExistMultipleMarshallingStrategiesWithSameName.

@Test
public void testThrowErrorWhenExistMultipleMarshallingStrategiesWithSameName() throws IOException, ClassNotFoundException {
    Environment env = EnvironmentFactory.newEnvironment();
    final Thing entityOne = new Thing(1, "Object 1");
    final Thing entityTwo = new Thing(2, "Object 2");
    Collection srcItems = new ArrayList();
    srcItems.add(entityOne);
    srcItems.add(entityTwo);
    ObjectMarshallingStrategy[] strats = new ObjectMarshallingStrategy[] { new IdentityPlaceholderResolverStrategy(new ObjectMarshallingStrategyAcceptor() {

        @Override
        public boolean accept(Object object) {
            return entityOne.equals(object);
        }
    }, Collections.singletonMap(entityOne.id, (Object) entityOne)), new IdentityPlaceholderResolverStrategy(new ObjectMarshallingStrategyAcceptor() {

        @Override
        public boolean accept(Object object) {
            return entityTwo.equals(object);
        }
    }, Collections.singletonMap(entityTwo.id, (Object) entityTwo)) };
    env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, strats);
    KieSessionConfiguration ksc = SessionConfiguration.newInstance();
    final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbconf);
    KieSession ks = kbase.newKieSession(ksc, env);
    ks.insert(entityOne);
    ks.insert(entityTwo);
    try {
        ProtobufMarshaller marshaller = (ProtobufMarshaller) MarshallerFactory.newMarshaller(kbase, strats);
        // Here ocurrs the bug that shows that NamedObjectMarshallingStrategies are required.
        Assert.fail("A runtime error must be thrown while found strategies with same name");
    } catch (RuntimeException re) {
        Assert.assertTrue(re.getMessage().contains("Multiple"));
    }
}
Also used : ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) ArrayList(java.util.ArrayList) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) Environment(org.kie.api.runtime.Environment) Collection(java.util.Collection) KieSession(org.kie.api.runtime.KieSession) KieSessionConfiguration(org.kie.api.runtime.KieSessionConfiguration) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) ObjectMarshallingStrategyAcceptor(org.kie.api.marshalling.ObjectMarshallingStrategyAcceptor) Test(org.junit.Test)

Example 98 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class NodeSegmentUnlinkingTest method testAllLinkedInWithJoinNodesOnly.

@Test
public void testAllLinkedInWithJoinNodesOnly() {
    setUp(JOIN_NODE);
    // make sure it created JoinNodes
    assertEquals(JoinNode.class, n3.getClass());
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    DefaultFactHandle f1 = (DefaultFactHandle) ksession.insert("test1");
    n3.assertObject(f1, context, ksession);
    BetaMemory bm = (BetaMemory) ksession.getNodeMemory(n3);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());
    n4.assertObject(f1, context, ksession);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());
    n5.assertObject(f1, context, ksession);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());
    n6.assertObject(f1, context, ksession);
    // only after all 4 nodes are populated, is the segment linked in
    assertTrue(bm.getSegmentMemory().isSegmentLinked());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 99 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class NodeSegmentUnlinkingTest method testAllLinkedInWithExistsNodesOnly.

@Test
public void testAllLinkedInWithExistsNodesOnly() {
    setUp(EXISTS_NODE);
    // make sure it created ExistsNodes
    assertEquals(ExistsNode.class, n3.getClass());
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    DefaultFactHandle f1 = (DefaultFactHandle) ksession.insert("test1");
    n3.assertObject(f1, context, ksession);
    BetaMemory bm = (BetaMemory) ksession.getNodeMemory(n3);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());
    n4.assertObject(f1, context, ksession);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());
    n5.assertObject(f1, context, ksession);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());
    n6.assertObject(f1, context, ksession);
    // only after all 4 nodes are populated, is the segment linked in
    assertTrue(bm.getSegmentMemory().isSegmentLinked());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 100 with KieBaseConfiguration

use of org.kie.api.KieBaseConfiguration in project drools by kiegroup.

the class NodeSegmentUnlinkingTest method testAllLinkedInWithNotNodesOnly.

@Test
public void testAllLinkedInWithNotNodesOnly() {
    setUp(NOT_NODE);
    // make sure it created NotNodes
    assertEquals(NotNode.class, n3.getClass());
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase(kconf);
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    BetaMemory bm = (BetaMemory) ksession.getNodeMemory(n3);
    createSegmentMemory(n3, ksession);
    // not nodes start off linked
    assertTrue(bm.getSegmentMemory().isSegmentLinked());
    // unlinked after first assertion
    DefaultFactHandle f1 = (DefaultFactHandle) ksession.insert("test1");
    n3.assertObject(f1, context, ksession);
    // this doesn't unlink on the assertObject, as the node's memory must be processed. So use the helper method the main network evaluator uses.
    PhreakNotNode.unlinkNotNodeOnRightInsert((NotNode) n3, bm, ksession);
    assertFalse(bm.getSegmentMemory().isSegmentLinked());
    n3.retractRightTuple(f1.getFirstRightTuple(), context, ksession);
    assertTrue(bm.getSegmentMemory().isSegmentLinked());
// assertFalse( bm.getSegmentMemory().isSigmentLinked() ); // check retraction unlinks again
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

KieBaseConfiguration (org.kie.api.KieBaseConfiguration)124 Test (org.junit.Test)94 KieSession (org.kie.api.runtime.KieSession)77 KieBase (org.kie.api.KieBase)69 ArrayList (java.util.ArrayList)54 List (java.util.List)38 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)25 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)23 FactHandle (org.kie.api.runtime.rule.FactHandle)21 EntryPoint (org.kie.api.runtime.rule.EntryPoint)18 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)16 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)16 InternalFactHandle (org.drools.core.common.InternalFactHandle)15 StockTick (org.drools.compiler.StockTick)11 KieContainer (org.kie.api.runtime.KieContainer)11 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)11 Match (org.kie.api.runtime.rule.Match)10 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)9 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)8 BuildContext (org.drools.core.reteoo.builder.BuildContext)7