Search in sources :

Example 81 with DefaultFactHandle

use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.

the class FactHandleTest method testEqualsObject.

/*
     * Class under test for boolean equals(Object)
     */
@Test
public void testEqualsObject() {
    final DefaultFactHandle f0 = new DefaultFactHandle(134, "cheese");
    final DefaultFactHandle f1 = new DefaultFactHandle(96, "cheese");
    final DefaultFactHandle f3 = new DefaultFactHandle(96, "cheese");
    assertFalse("f0 should not equal f1", f0.equals(f1));
    assertEquals(f1, f3);
    assertNotSame(f1, f3);
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) Test(org.junit.Test)

Example 82 with DefaultFactHandle

use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.

the class FactHandleTest method testInvalidate.

@Test
public void testInvalidate() {
    final DefaultFactHandle f0 = new DefaultFactHandle(134, "cheese");
    assertEquals(134, f0.getId());
    f0.invalidate();
    // invalidate no longer sets the id to -1
    assertEquals(134, f0.getId());
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) Test(org.junit.Test)

Example 83 with DefaultFactHandle

use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.

the class RightTupleListTest method testEmptyIterator.

@Test
public void testEmptyIterator() {
    final TupleList map = new TupleList();
    final Cheese stilton1 = new Cheese("stilton", 35);
    final InternalFactHandle h1 = new DefaultFactHandle(1, stilton1);
    assertNull(map.getFirst(new LeftTupleImpl(h1, null, true)));
}
Also used : TupleList(org.drools.core.util.index.TupleList) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) Cheese(org.drools.core.test.model.Cheese) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 84 with DefaultFactHandle

use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.

the class DefaultFactHandleFactoryTest method testNewFactHandle.

/*
     * Class under test for FactHandle newFactHandle()
     */
@Test
public void testNewFactHandle() {
    final ReteooFactHandleFactory factory = new ReteooFactHandleFactory();
    DefaultFactHandle handle = (DefaultFactHandle) factory.newFactHandle("cheese", null, null, null);
    assertEquals(1, handle.getId());
    assertEquals(1, handle.getRecency());
    // issue  new handle
    handle = (DefaultFactHandle) factory.newFactHandle("cheese", null, null, null);
    assertEquals(2, handle.getId());
    assertEquals(2, handle.getRecency());
    // issue  new handle, under a different reference so we  can destroy later
    final DefaultFactHandle handle2 = (DefaultFactHandle) factory.newFactHandle("cheese", null, null, null);
    assertEquals(3, handle2.getId());
    assertEquals(3, handle2.getRecency());
    // Check recency increasion works
    factory.increaseFactHandleRecency(handle);
    assertEquals(4, handle.getRecency());
    // issue new handle and make sure  recency is still inline
    handle = (DefaultFactHandle) factory.newFactHandle("cheese", null, null, null);
    assertEquals(4, handle.getId());
    assertEquals(5, handle.getRecency());
    // destroy handle
    factory.destroyFactHandle(handle2);
// @FIXME recycling is currently disabled
// // issue  new  fact handle and  make sure it  recycled the  id=2
// handle = (DefaultFactHandle) factory.newFactHandle( "cheese", false, null );
// assertEquals( 2,
// handle.getId() );
// assertEquals( 5,
// handle.getRecency() );
// 
// // issue new  handle  making  sure it correctly resumes  ids  and recency
// handle = (DefaultFactHandle) factory.newFactHandle( "cheese", false, null );
// assertEquals( 4,
// handle.getId() );
// assertEquals( 6,
// handle.getRecency() );
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) Test(org.junit.Test)

Example 85 with DefaultFactHandle

use of org.drools.core.common.DefaultFactHandle in project drools by kiegroup.

the class InputMarshaller method readFactHandle.

public static InternalFactHandle readFactHandle(MarshallerReaderContext context) throws IOException, ClassNotFoundException {
    int type = context.readInt();
    long id = context.readLong();
    long recency = context.readLong();
    long startTimeStamp = 0;
    long duration = 0;
    boolean expired = false;
    long activationsCount = 0;
    if (type == 2) {
        startTimeStamp = context.readLong();
        duration = context.readLong();
        expired = context.readBoolean();
        activationsCount = context.readLong();
    }
    int strategyIndex = context.readInt();
    ObjectMarshallingStrategy strategy = null;
    // This is the old way of de/serializing strategy objects
    if (strategyIndex >= 0) {
        strategy = context.getResolverStrategyFactory().getStrategy(strategyIndex);
    } else // This is the new way
    if (strategyIndex == -2) {
        String strategyClassName = context.readUTF();
        if (!StringUtils.isEmpty(strategyClassName)) {
            strategy = context.getResolverStrategyFactory().getStrategyObject(strategyClassName);
            if (strategy == null) {
                throw new IllegalStateException("No strategy of type " + strategyClassName + " available.");
            }
        }
    }
    // If either way retrieves a strategy, use it
    Object object = null;
    if (strategy != null) {
        object = strategy.read((ObjectInputStream) context);
    }
    EntryPoint entryPoint = null;
    if (context.readBoolean()) {
        String entryPointId = context.readUTF();
        if (entryPointId != null && !entryPointId.equals("")) {
            entryPoint = ((RuleRuntime) context.getWorkingMemory()).getEntryPoint(entryPointId);
        }
    }
    EntryPointId confEP;
    if (entryPoint != null) {
        confEP = ((NamedEntryPoint) entryPoint).getEntryPoint();
    } else {
        confEP = context.getWorkingMemory().getEntryPoint();
    }
    ObjectTypeConf typeConf = context.getWorkingMemory().getObjectTypeConfigurationRegistry().getOrCreateObjectTypeConf(confEP, object);
    InternalFactHandle handle = null;
    switch(type) {
        case 0:
            {
                handle = new DefaultFactHandle(id, object, recency, (WorkingMemoryEntryPoint) entryPoint, typeConf != null && typeConf.isTrait());
                break;
            }
        case 1:
            {
                handle = new QueryElementFactHandle(object, id, recency);
                break;
            }
        case 2:
            {
                handle = new EventFactHandle(id, object, recency, startTimeStamp, duration, (WorkingMemoryEntryPoint) entryPoint, typeConf != null && typeConf.isTrait());
                ((EventFactHandle) handle).setExpired(expired);
                ((EventFactHandle) handle).setActivationsCount(activationsCount);
                break;
            }
        default:
            {
                throw new IllegalStateException("Unable to marshal FactHandle, as type does not exist:" + type);
            }
    }
    return handle;
}
Also used : ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) EntryPointId(org.drools.core.rule.EntryPointId) QueryElementFactHandle(org.drools.core.common.QueryElementFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) InternalFactHandle(org.drools.core.common.InternalFactHandle) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

DefaultFactHandle (org.drools.core.common.DefaultFactHandle)92 Test (org.junit.Test)78 InternalFactHandle (org.drools.core.common.InternalFactHandle)30 FactHandle (org.kie.api.runtime.rule.FactHandle)29 Cheese (org.drools.core.test.model.Cheese)24 ArrayList (java.util.ArrayList)23 ClassObjectType (org.drools.core.base.ClassObjectType)22 KieSession (org.kie.api.runtime.KieSession)19 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)18 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)16 Declaration (org.drools.core.rule.Declaration)15 RightTuple (org.drools.core.reteoo.RightTuple)14 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)14 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)13 StatefulKnowledgeSessionImpl (org.drools.kiesession.session.StatefulKnowledgeSessionImpl)13 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)12 Pattern (org.drools.core.rule.Pattern)12 Tuple (org.drools.core.spi.Tuple)12 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)12 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)11