Search in sources :

Example 11 with InternalFactHandle

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

the class ProtobufInputMarshaller method readTruthMaintenanceSystem.

public static void readTruthMaintenanceSystem(MarshallerReaderContext context, EntryPoint wmep, ProtobufMessages.EntryPoint _ep, List<PropagationContext> pctxs) throws IOException, ClassNotFoundException {
    TruthMaintenanceSystem tms = ((NamedEntryPoint) wmep).getTruthMaintenanceSystem();
    // if 0, then the OTC was not serialized (older versions of drools)
    boolean wasOTCSerialized = _ep.getOtcCount() > 0;
    Set<String> tmsEnabled = new HashSet<String>();
    for (ObjectTypeConfiguration _otc : _ep.getOtcList()) {
        if (_otc.getTmsEnabled()) {
            tmsEnabled.add(_otc.getType());
        }
    }
    ProtobufMessages.TruthMaintenanceSystem _tms = _ep.getTms();
    for (ProtobufMessages.EqualityKey _key : _tms.getKeyList()) {
        InternalFactHandle handle = (InternalFactHandle) context.handles.get(_key.getHandleId());
        // ObjectTypeConf state is not marshalled, so it needs to be re-determined
        ObjectTypeConf typeConf = context.wm.getObjectTypeConfigurationRegistry().getObjectTypeConf(((NamedEntryPoint) handle.getEntryPoint()).getEntryPoint(), handle.getObject());
        if (!typeConf.isTMSEnabled() && (!wasOTCSerialized || tmsEnabled.contains(typeConf.getTypeName()))) {
            typeConf.enableTMS();
        }
        EqualityKey key = new EqualityKey(handle, _key.getStatus());
        handle.setEqualityKey(key);
        if (key.getStatus() == EqualityKey.JUSTIFIED) {
            // not yet added to the object stores
            ((NamedEntryPoint) handle.getEntryPoint()).getObjectStore().addHandle(handle, handle.getObject());
            // add handle to object type node
            assertHandleIntoOTN(context, context.wm, handle, pctxs);
        }
        for (Integer factHandleId : _key.getOtherHandleList()) {
            handle = (InternalFactHandle) context.handles.get(factHandleId.intValue());
            key.addFactHandle(handle);
            handle.setEqualityKey(key);
        }
        tms.put(key);
        context.filter.fireRNEAs(context.wm);
        readBeliefSet(context, tms, key, _key);
    }
}
Also used : TruthMaintenanceSystem(org.drools.core.common.TruthMaintenanceSystem) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ObjectTypeConfiguration(org.drools.core.marshalling.impl.ProtobufMessages.ObjectTypeConfiguration) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) EqualityKey(org.drools.core.common.EqualityKey) InternalFactHandle(org.drools.core.common.InternalFactHandle) HashSet(java.util.HashSet)

Example 12 with InternalFactHandle

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

the class ProtobufInputMarshaller method readFactHandles.

public static void readFactHandles(MarshallerReaderContext context, org.drools.core.marshalling.impl.ProtobufMessages.EntryPoint _ep, ObjectStore objectStore, List<PropagationContext> pctxs) throws IOException, ClassNotFoundException {
    InternalWorkingMemory wm = context.wm;
    EntryPoint entryPoint = ((StatefulKnowledgeSessionImpl) context.wm).getEntryPointMap().get(_ep.getEntryPointId());
    // load the handles
    for (ProtobufMessages.FactHandle _handle : _ep.getHandleList()) {
        InternalFactHandle handle = readFactHandle(context, entryPoint, _handle);
        context.handles.put(handle.getId(), handle);
        if (!_handle.getIsJustified()) {
            // BeliefSystem handles the Object type
            if (handle.getObject() != null) {
                objectStore.addHandle(handle, handle.getObject());
            }
            // add handle to object type node
            assertHandleIntoOTN(context, wm, handle, pctxs);
        }
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) FactHandle(org.drools.core.marshalling.impl.ProtobufMessages.FactHandle) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 13 with InternalFactHandle

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

the class ProtobufInputMarshaller method readBeliefSet.

private static void readBeliefSet(MarshallerReaderContext context, TruthMaintenanceSystem tms, EqualityKey key, ProtobufMessages.EqualityKey _key) throws IOException, ClassNotFoundException {
    if (_key.hasBeliefSet()) {
        ProtobufMessages.BeliefSet _beliefSet = _key.getBeliefSet();
        InternalFactHandle handle = (InternalFactHandle) context.handles.get(_key.getHandleId());
        // phreak might serialize empty belief sets, so he have to handle it during deserialization
        if (_beliefSet.getLogicalDependencyCount() > 0) {
            for (ProtobufMessages.LogicalDependency _logicalDependency : _beliefSet.getLogicalDependencyList()) {
                ProtobufMessages.Activation _activation = _logicalDependency.getActivation();
                Activation activation = (Activation) context.filter.getTuplesCache().get(PersisterHelper.createActivationKey(_activation.getPackageName(), _activation.getRuleName(), _activation.getTuple())).getContextObject();
                Object object = null;
                ObjectMarshallingStrategy strategy = null;
                if (_logicalDependency.hasObjectStrategyIndex()) {
                    strategy = context.usedStrategies.get(_logicalDependency.getObjectStrategyIndex());
                    object = strategy.unmarshal(context.strategyContexts.get(strategy), context, _logicalDependency.getObject().toByteArray(), (context.kBase == null) ? null : context.kBase.getRootClassLoader());
                }
                Object value = null;
                if (_logicalDependency.hasValueStrategyIndex()) {
                    strategy = context.usedStrategies.get(_logicalDependency.getValueStrategyIndex());
                    value = strategy.unmarshal(context.strategyContexts.get(strategy), context, _logicalDependency.getValue().toByteArray(), (context.kBase == null) ? null : context.kBase.getRootClassLoader());
                }
                ObjectTypeConf typeConf = context.wm.getObjectTypeConfigurationRegistry().getObjectTypeConf(((NamedEntryPoint) handle.getEntryPoint()).getEntryPoint(), handle.getObject());
                tms.readLogicalDependency(handle, object, value, activation, activation.getPropagationContext(), activation.getRule(), typeConf);
            }
        } else {
            handle.getEqualityKey().setBeliefSet(tms.getBeliefSystem().newBeliefSet(handle));
        }
    }
}
Also used : ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) ObjectMarshallingStrategy(org.kie.api.marshalling.ObjectMarshallingStrategy) Activation(org.drools.core.spi.Activation) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 14 with InternalFactHandle

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

the class CursoredDataSource method bind.

@Override
public void bind(RuleUnit unit, WorkingMemoryEntryPoint ep) {
    setWorkingMemory(ep.getInternalWorkingMemory());
    PropagationList propagationList = propagationsMap.get(unit.getUnitIdentity());
    if (propagationList != null) {
        flush(ep, propagationList.takeAll());
    } else {
        Iterator<InternalFactHandle> fhs = objectStore.iterateFactHandles();
        while (fhs.hasNext()) {
            new Insert((DataSourceFactHandle) fhs.next()).execute(ep);
        }
        propagationsMap.put(unit.getUnitIdentity(), new SynchronizedPropagationList(((WorkingMemoryEntryPoint) ep).getInternalWorkingMemory()));
    }
    currentUnit = unit.getUnitIdentity();
    currentEntryPoint = ep;
}
Also used : SynchronizedPropagationList(org.drools.core.phreak.SynchronizedPropagationList) SynchronizedPropagationList(org.drools.core.phreak.SynchronizedPropagationList) PropagationList(org.drools.core.phreak.PropagationList) InternalFactHandle(org.drools.core.common.InternalFactHandle) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) InternalWorkingMemoryEntryPoint(org.drools.core.common.InternalWorkingMemoryEntryPoint)

Example 15 with InternalFactHandle

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

the class StatelessKnowledgeSessionImpl method executeWithResults.

public List executeWithResults(Iterable objects, ObjectFilter filter) {
    List list = new ArrayList();
    StatefulKnowledgeSession ksession = newWorkingMemory();
    try {
        for (Object object : objects) {
            ksession.insert(object);
        }
        ksession.fireAllRules();
        for (FactHandle fh : ksession.getFactHandles(filter)) {
            list.add(((InternalFactHandle) fh).getObject());
        }
    } finally {
        dispose(ksession);
    }
    return list;
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

InternalFactHandle (org.drools.core.common.InternalFactHandle)182 Test (org.junit.Test)89 KieSession (org.kie.api.runtime.KieSession)66 ArrayList (java.util.ArrayList)63 List (java.util.List)41 KieBase (org.kie.api.KieBase)31 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)29 LeftTuple (org.drools.core.reteoo.LeftTuple)27 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)21 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)21 FactHandle (org.kie.api.runtime.rule.FactHandle)21 RightTuple (org.drools.core.reteoo.RightTuple)20 Declaration (org.drools.core.rule.Declaration)20 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)16 StockTickInterface (org.drools.compiler.StockTickInterface)15 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)15 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)15 StockTick (org.drools.compiler.StockTick)14 Cheese (org.drools.core.test.model.Cheese)14 KieBaseConfiguration (org.kie.api.KieBaseConfiguration)14