Search in sources :

Example 1 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.stream.readInt();
    int id = context.stream.readInt();
    long recency = context.stream.readLong();
    long startTimeStamp = 0;
    long duration = 0;
    boolean expired = false;
    long activationsCount = 0;
    if (type == 2) {
        startTimeStamp = context.stream.readLong();
        duration = context.stream.readLong();
        expired = context.stream.readBoolean();
        activationsCount = context.stream.readLong();
    }
    int strategyIndex = context.stream.readInt();
    Object object = null;
    ObjectMarshallingStrategy strategy = null;
    // This is the old way of de/serializing strategy objects
    if (strategyIndex >= 0) {
        strategy = context.resolverStrategyFactory.getStrategy(strategyIndex);
    } else // This is the new way
    if (strategyIndex == -2) {
        String strategyClassName = context.stream.readUTF();
        if (!StringUtils.isEmpty(strategyClassName)) {
            strategy = context.resolverStrategyFactory.getStrategyObject(strategyClassName);
            if (strategy == null) {
                throw new IllegalStateException("No strategy of type " + strategyClassName + " available.");
            }
        }
    }
    // If either way retrieves a strategy, use it
    if (strategy != null) {
        object = strategy.read(context.stream);
    }
    EntryPoint entryPoint = null;
    if (context.readBoolean()) {
        String entryPointId = context.readUTF();
        if (entryPointId != null && !entryPointId.equals("")) {
            entryPoint = ((RuleRuntime) context.wm).getEntryPoint(entryPointId);
        }
    }
    EntryPointId confEP;
    if (entryPoint != null) {
        confEP = ((NamedEntryPoint) entryPoint).getEntryPoint();
    } else {
        confEP = context.wm.getEntryPoint();
    }
    ObjectTypeConf typeConf = context.wm.getObjectTypeConfigurationRegistry().getObjectTypeConf(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.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.core.common.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)

Example 2 with DefaultFactHandle

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

the class ProtobufInputMarshaller method readFactHandle.

public static InternalFactHandle readFactHandle(MarshallerReaderContext context, EntryPoint entryPoint, FactHandle _handle) throws IOException, ClassNotFoundException {
    Object object = null;
    ObjectMarshallingStrategy strategy = null;
    if (_handle.hasStrategyIndex()) {
        strategy = context.usedStrategies.get(_handle.getStrategyIndex());
        object = strategy.unmarshal(context.strategyContexts.get(strategy), context, _handle.getObject().toByteArray(), (context.kBase == null) ? null : context.kBase.getRootClassLoader());
    }
    EntryPointId confEP;
    if (entryPoint != null) {
        confEP = ((NamedEntryPoint) entryPoint).getEntryPoint();
    } else {
        confEP = context.wm.getEntryPoint();
    }
    ObjectTypeConf typeConf = context.wm.getObjectTypeConfigurationRegistry().getObjectTypeConf(confEP, object);
    InternalFactHandle handle = null;
    switch(_handle.getType()) {
        case FACT:
            {
                handle = new DefaultFactHandle(_handle.getId(), object, _handle.getRecency(), (WorkingMemoryEntryPoint) entryPoint, typeConf != null && typeConf.isTrait());
                break;
            }
        case QUERY:
            {
                handle = new QueryElementFactHandle(object, _handle.getId(), _handle.getRecency());
                break;
            }
        case EVENT:
            {
                handle = new EventFactHandle(_handle.getId(), object, _handle.getRecency(), _handle.getTimestamp(), _handle.getDuration(), (WorkingMemoryEntryPoint) entryPoint, typeConf != null && typeConf.isTrait());
                ((EventFactHandle) handle).setExpired(_handle.getIsExpired());
                ((EventFactHandle) handle).setOtnCount(_handle.getOtnCount());
                // ((EventFactHandle) handle).setActivationsCount( _handle.getActivationsCount() );
                break;
            }
        default:
            {
                throw new IllegalStateException("Unable to marshal FactHandle, as type does not exist:" + _handle.getType());
            }
    }
    return handle;
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) EntryPointId(org.drools.core.rule.EntryPointId) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) QueryElementFactHandle(org.drools.core.common.QueryElementFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 3 with DefaultFactHandle

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

the class StatefulSessionTest method testDisconnectedFactHandle.

@Test
public void testDisconnectedFactHandle() {
    final KieBase kbase = getKnowledgeBase();
    final KieSession ksession = createKnowledgeSession(kbase);
    final DefaultFactHandle helloHandle = (DefaultFactHandle) ksession.insert("hello");
    final DefaultFactHandle goodbyeHandle = (DefaultFactHandle) ksession.insert("goodbye");
    FactHandle key = DefaultFactHandle.createFromExternalFormat(helloHandle.toExternalForm());
    assertEquals("hello", ksession.getObject(key));
    key = DefaultFactHandle.createFromExternalFormat(goodbyeHandle.toExternalForm());
    assertEquals("goodbye", ksession.getObject(key));
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 4 with DefaultFactHandle

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

the class Misc2Test method testDynamicAddRule.

@Test
public void testDynamicAddRule() {
    // DROOLS-17
    String str = "import " + Misc2Test.A.class.getCanonicalName() + "\n" + "rule r1 when\n" + "    $a : A( f1 == 1 )\n" + "then\n" + "end\n" + "\n" + "rule r2 when\n" + "    $a : A( f2 == 1 )\n" + "then\n" + "end\n" + "\n" + "rule r3 when\n" + "    $a : A( f3 == 1 )" + "then\n" + "end";
    String str2 = "import " + Misc2Test.A.class.getCanonicalName() + "\n" + "rule r4 when\n" + "    $a : A( f2 == 1, f4 == 1 )" + "then\n" + "end";
    InternalKnowledgeBase kbase = (InternalKnowledgeBase) loadKnowledgeBaseFromString(str);
    KieSession ksession = kbase.newKieSession();
    FactHandle fh = ksession.insert(new A(1, 1, 1, 1));
    ksession.fireAllRules();
    kbase.addPackages(loadKnowledgePackagesFromString(str2));
    ksession.fireAllRules();
    // this second insert forces the regeneration of the otnIds
    ksession.insert(new A(2, 2, 2, 2));
    LeftTuple leftTuple = ((DefaultFactHandle) fh).getFirstLeftTuple();
    ObjectTypeNode.Id letTupleOtnId = leftTuple.getInputOtnId();
    leftTuple = leftTuple.getHandleNext();
    while (leftTuple != null) {
        assertTrue(letTupleOtnId.before(leftTuple.getInputOtnId()));
        letTupleOtnId = leftTuple.getInputOtnId();
        leftTuple = leftTuple.getHandleNext();
    }
}
Also used : DefaultFactHandle(org.drools.core.common.DefaultFactHandle) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) LeftTuple(org.drools.core.reteoo.LeftTuple) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 5 with DefaultFactHandle

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

the class DroolsAbstractPMMLTest method reportWMObjects.

public String reportWMObjects(KieSession session) {
    PriorityQueue<String> queue = new PriorityQueue<String>();
    for (FactHandle fh : session.getFactHandles()) {
        Object o;
        if (fh instanceof EventFactHandle) {
            EventFactHandle efh = (EventFactHandle) fh;
            queue.add("\t " + efh.getStartTimestamp() + "\t" + efh.getObject().toString() + "\n");
        } else {
            o = ((DefaultFactHandle) fh).getObject();
            queue.add("\t " + o.toString() + " >> " + System.identityHashCode(o.getClass()) + "\n");
        }
    }
    String ans = " ---------------- WM " + session.getObjects().size() + " --------------\n";
    while (!queue.isEmpty()) ans += queue.poll();
    ans += " ---------------- END WM -----------\n";
    return ans;
}
Also used : FactHandle(org.kie.api.runtime.rule.FactHandle) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) EventFactHandle(org.drools.core.common.EventFactHandle) PriorityQueue(java.util.PriorityQueue)

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