Search in sources :

Example 36 with InternalFactHandle

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

the class TraitHelper method asTrait.

private <T, K> T asTrait(K core, TraitableBean inner, Class<T> trait, boolean needsProxy, boolean hasTrait, boolean needsUpdate, TraitFactory builder, boolean logical, Activation activation) throws LogicalTypeInconsistencyException {
    T thing;
    if (needsProxy) {
        thing = (T) inner;
        inner.addTrait(trait.getName(), (Thing<K>) core);
    } else if (hasTrait) {
        thing = (T) inner.getTrait(trait.getName());
    } else {
        thing = (T) builder.getProxy(inner, trait, logical);
    }
    if (needsUpdate) {
        InternalFactHandle h = (InternalFactHandle) lookupFactHandle(core);
        if (h == null) {
            h = lookupHandleForWrapper(core);
        }
        if (h == null) {
            h = (InternalFactHandle) this.workingMemory.insert(core, false, activation.getRule(), activation.getTuple().getTupleSink());
        }
        if (!h.isTraitOrTraitable()) {
            throw new IllegalStateException("A traited working memory element is being used with a default fact handle. " + "Please verify that its class was declared as @Traitable : " + core.getClass().getName());
        }
        this.update(h, inner, activation);
    }
    return thing;
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 37 with InternalFactHandle

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

the class TraitHelper method update.

public void update(final FactHandle handle, final Object newObject, final Activation activation) {
    InternalFactHandle h = (InternalFactHandle) handle;
    h.getEntryPoint().update(h, newObject, onlyTraitBitSetMask(), newObject.getClass(), activation);
}
Also used : InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 38 with InternalFactHandle

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

the class TraitHelper method updateCore.

private <T> void updateCore(TraitableBean inner, Object core, Class<T> trait, boolean logical, Activation activation) {
    FactHandle handle = lookupFactHandle(inner);
    InternalFactHandle h = (InternalFactHandle) handle;
    if (handle != null) {
        TraitFieldTMS fieldTMS = inner._getFieldTMS();
        BitMask mask = fieldTMS == null ? onlyTraitBitSetMask() : fieldTMS.getModificationMask();
        Object o = h.getObject();
        NamedEntryPoint nep = (NamedEntryPoint) h.getEntryPoint();
        PropagationContext propagationContext = nep.getPctxFactory().createPropagationContext(nep.getInternalWorkingMemory().getNextPropagationIdCounter(), PropagationContext.Type.MODIFICATION, activation.getRule(), activation.getTuple().getTupleSink(), h, nep.getEntryPoint(), mask, core.getClass(), null);
        nep.update(h, o, o, nep.getObjectTypeConfigurationRegistry().getObjectTypeConf(nep.getEntryPoint(), o), propagationContext);
    } else {
        handle = this.workingMemory.insert(inner, false, activation.getRule(), activation.getTuple().getTupleSink());
    }
}
Also used : TraitFieldTMS(org.drools.core.factmodel.traits.TraitFieldTMS) PropagationContext(org.drools.core.spi.PropagationContext) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) BitMask(org.drools.core.util.bitmask.BitMask) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) InternalFactHandle(org.drools.core.common.InternalFactHandle)

Example 39 with InternalFactHandle

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

the class TraitHelper method shed.

public <T, K, X extends TraitableBean> Thing<K> shed(TraitableBean<K, X> core, Class<T> trait, Activation activation) {
    if (trait.isAssignableFrom(core.getClass())) {
        Collection<Thing<K>> removedTypes = core.removeTrait(trait.getName());
        if (!removedTypes.isEmpty()) {
            reassignNodes(core, removedTypes);
            update(getFactHandle(core), onlyTraitBitSetMask(), core.getClass(), activation);
        // updateTraits( core, Long.MIN_VALUE, null, core.getClass(), null, ((TraitableBean) core).getMostSpecificTraits()  );
        }
        if (core instanceof Thing) {
            return (Thing<K>) core;
        } else {
            return null;
        }
    } else {
        Collection<Thing<K>> removedTypes;
        Thing<K> thing = core.getTrait(Thing.class.getName());
        if (trait == Thing.class) {
            removedTypes = new ArrayList<Thing<K>>(core._getTraitMap().values());
            for (Thing t : removedTypes) {
                if (!((TraitType) t)._isVirtual()) {
                    delete(getFactHandle(t), activation);
                }
            }
            core._getTraitMap().clear();
            core._setTraitMap(null);
            return thing;
        } else if (core.hasTrait(trait.getName())) {
            removedTypes = core.removeTrait(trait.getName());
        } else {
            HierarchyEncoder hier = this.workingMemory.getKnowledgeBase().getConfiguration().getComponentFactory().getTraitRegistry().getHierarchy();
            BitSet code = hier.getCode(trait.getName());
            removedTypes = core.removeTrait(code);
        }
        removedTypes = new ArrayList<Thing<K>>(removedTypes);
        reassignNodes(core, removedTypes);
        for (Thing t : removedTypes) {
            if (!((TraitType) t)._isVirtual()) {
                InternalFactHandle handle = (InternalFactHandle) getFactHandle(t);
                if (handle.getEqualityKey() != null && handle.getEqualityKey().getLogicalFactHandle() == handle) {
                    entryPoint.getTruthMaintenanceSystem().delete(handle);
                } else {
                    delete(getFactHandle(t), activation);
                }
            }
        }
        if (!core.hasTraits()) {
            don(activation, core, Thing.class, false);
        } else if (!removedTypes.isEmpty()) {
            update(getFactHandle(core), onlyTraitBitSetMask(), core.getClass(), activation);
        // updateTraits( core, Long.MIN_VALUE, null, core.getClass(), null, ((TraitableBean) core).getMostSpecificTraits()  );
        }
        return thing;
    }
}
Also used : HierarchyEncoder(org.drools.core.util.HierarchyEncoder) BitSet(java.util.BitSet) InternalFactHandle(org.drools.core.common.InternalFactHandle) Thing(org.drools.core.factmodel.traits.Thing)

Example 40 with InternalFactHandle

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

the class TraitHelper method deleteWMAssertedTraitProxies.

public void deleteWMAssertedTraitProxies(InternalFactHandle handle, RuleImpl rule, TerminalNode terminalNode) {
    TraitableBean traitableBean = (TraitableBean) handle.getObject();
    if (traitableBean.hasTraits()) {
        PriorityQueue<TraitProxy> removedTypes = new PriorityQueue<TraitProxy>(traitableBean._getTraitMap().values().size());
        removedTypes.addAll(traitableBean._getTraitMap().values());
        while (!removedTypes.isEmpty()) {
            TraitProxy proxy = removedTypes.poll();
            if (!proxy._isVirtual()) {
                InternalFactHandle proxyHandle = (InternalFactHandle) getFactHandle(proxy);
                if (proxyHandle.getEqualityKey() == null || proxyHandle.getEqualityKey().getLogicalFactHandle() != proxyHandle) {
                    entryPoint.delete(proxyHandle, rule, terminalNode);
                }
            }
        }
    }
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) PriorityQueue(java.util.PriorityQueue) InternalFactHandle(org.drools.core.common.InternalFactHandle)

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