Search in sources :

Example 1 with HierarchyEncoder

use of org.drools.core.util.HierarchyEncoder 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 2 with HierarchyEncoder

use of org.drools.core.util.HierarchyEncoder in project drools by kiegroup.

the class AbstractTraitFactory method getProxy.

public T getProxy(K core, Class<?> trait, boolean logical) throws LogicalTypeInconsistencyException {
    String traitName = trait.getName();
    if (core.hasTrait(traitName)) {
        return (T) core.getTrait(traitName);
    }
    String key = getKey(core.getClass(), trait);
    Constructor<T> konst;
    synchronized (this) {
        konst = factoryCache.get(key);
        if (konst == null) {
            konst = cacheConstructor(key, core, trait);
        }
    }
    T proxy = null;
    HierarchyEncoder hier = getHierarchyEncoder();
    try {
        switch(mode) {
            case MAP:
                proxy = konst.newInstance(core, core._getDynamicProperties(), hier.getCode(trait.getName()), hier.getBottom(), logical);
                break;
            case TRIPLES:
                proxy = konst.newInstance(core, getTripleStore(), getTripleFactory(), hier.getCode(trait.getName()), hier.getBottom(), logical);
                break;
            default:
                throw new RuntimeException(" This should not happen : unexpected property wrapping method " + mode);
        }
        return proxy;
    } catch (InstantiationException | InvocationTargetException | IllegalAccessException e) {
        e.printStackTrace();
    }
    throw new LogicalTypeInconsistencyException("Could not apply trait " + trait + " to object " + core, trait, core.getClass());
}
Also used : HierarchyEncoder(org.drools.core.util.HierarchyEncoder) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

HierarchyEncoder (org.drools.core.util.HierarchyEncoder)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 BitSet (java.util.BitSet)1 InternalFactHandle (org.drools.core.common.InternalFactHandle)1 Thing (org.drools.core.factmodel.traits.Thing)1