Search in sources :

Example 16 with Thing

use of org.drools.core.factmodel.traits.Thing in project drools by kiegroup.

the class TraitHelper method updateManyTraits.

private void updateManyTraits(Object object, BitMask mask, Collection<Thing> originators, Class<?> modifiedClass, Collection<Thing> traits, Activation activation) {
    for (Thing t : traits) {
        if (!originators.contains(t)) {
            InternalFactHandle h = (InternalFactHandle) lookupFactHandle(t);
            if (h != null) {
                NamedEntryPoint nep = (NamedEntryPoint) h.getEntryPoint();
                PropagationContext propagationContext = nep.getPctxFactory().createPropagationContext(nep.getInternalWorkingMemory().getNextPropagationIdCounter(), PropagationContext.Type.MODIFICATION, activation != null ? activation.getRule() : null, activation != null ? activation.getTuple().getTupleSink() : null, h, nep.getEntryPoint(), mask, modifiedClass, null);
                nep.update(h, t, t, nep.getObjectTypeConfigurationRegistry().getObjectTypeConf(nep.getEntryPoint(), t), propagationContext);
            }
        }
    }
}
Also used : PropagationContext(org.drools.core.spi.PropagationContext) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) InternalFactHandle(org.drools.core.common.InternalFactHandle) Thing(org.drools.core.factmodel.traits.Thing)

Example 17 with Thing

use of org.drools.core.factmodel.traits.Thing in project drools by kiegroup.

the class TraitHelper method applyManyTraits.

protected <T, K> T applyManyTraits(Activation activation, K core, Collection<Class<? extends Thing>> traits, Object value, boolean logical, Mode... modes) throws LogicalTypeInconsistencyException {
    // Precondition : traits is not empty, checked by don
    TraitFactory builder = TraitFactory.getTraitBuilderForKnowledgeBase(entryPoint.getKnowledgeBase());
    TraitableBean inner = makeTraitable(core, builder, logical, activation);
    Collection<Thing> mostSpecificTraits = inner.getMostSpecificTraits();
    boolean newTraitsAdded = false;
    T firstThing = null;
    List<Thing> things = new ArrayList<Thing>(traits.size());
    checkStaticTypeCode(inner);
    for (Class<?> trait : traits) {
        boolean needsProxy = trait.isAssignableFrom(inner.getClass());
        boolean hasTrait = inner.hasTrait(trait.getName());
        boolean needsUpdate = needsProxy || core != inner;
        if (!hasTrait) {
            T thing = (T) asTrait(core, inner, trait, needsProxy, hasTrait, needsUpdate, builder, logical, activation);
            configureTrait(thing, value);
            things.add((Thing) thing);
            if (!newTraitsAdded && trait != Thing.class) {
                firstThing = thing;
                newTraitsAdded = true;
            }
        }
    }
    for (Thing t : things) {
        doInsertTrait(activation, t, core, logical, modes);
    }
    if (newTraitsAdded) {
        if (mostSpecificTraits != null) {
            updateCore(inner, core, null, logical, activation);
            if (!mostSpecificTraits.isEmpty()) {
                updateManyTraits(inner, onlyTraitBitSetMask(), things, core.getClass(), mostSpecificTraits, activation);
            }
        }
    }
    return firstThing;
}
Also used : ArrayList(java.util.ArrayList) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) Thing(org.drools.core.factmodel.traits.Thing) TraitFactory(org.drools.core.factmodel.traits.TraitFactory)

Example 18 with Thing

use of org.drools.core.factmodel.traits.Thing in project drools by kiegroup.

the class TraitHelperImpl method getTraitBoundary.

protected <K> Collection<Thing> getTraitBoundary(TraitableBean<K, ?> inner, boolean needsProxy, boolean hasTrait, Class trait) {
    boolean refresh = !needsProxy && !hasTrait && Thing.class != trait;
    if (!refresh) {
        return null;
    }
    if (inner._getTraitMap() == null || inner instanceof Thing)
        return Collections.EMPTY_LIST;
    if (inner._getTraitMap().isEmpty())
        return null;
    Collection<Thing> ts = new ArrayList<Thing>();
    for (Thing t : inner._getTraitMap().values()) {
        if (t instanceof TraitProxyImpl) {
            if (((TraitProxyImpl) t).hasOtns()) {
                ts.add(t);
            }
        }
    }
    return ts;
}
Also used : TraitProxyImpl(org.drools.traits.core.factmodel.TraitProxyImpl) ArrayList(java.util.ArrayList) Thing(org.drools.core.factmodel.traits.Thing)

Example 19 with Thing

use of org.drools.core.factmodel.traits.Thing in project drools by kiegroup.

the class TraitHelperImpl 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);
            FactHandle factHandle = getFactHandle(core);
            update(factHandle, 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 = ((TraitRuntimeComponentFactory) RuntimeComponentFactory.get()).getTraitRegistry(this.workingMemory.getKnowledgeBase()).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) {
                    TruthMaintenanceSystemFactory.get().getOrCreateTruthMaintenanceSystem(entryPoint).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 : InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) HierarchyEncoder(org.drools.traits.core.factmodel.HierarchyEncoder) BitSet(java.util.BitSet) TraitRuntimeComponentFactory(org.drools.traits.core.reteoo.TraitRuntimeComponentFactory) InternalFactHandle(org.drools.core.common.InternalFactHandle) Thing(org.drools.core.factmodel.traits.Thing)

Example 20 with Thing

use of org.drools.core.factmodel.traits.Thing in project drools by kiegroup.

the class DefaultBeanClassBuilder method buildTraitMap.

/**
 * A traitable class is a special class with support for dynamic properties and types.
 *
 * This method builds the trait map, containing the references to the proxies
 * for each trait carried by an object at a given time.
 *
 * @param cw
 * @param classDef
 */
protected void buildTraitMap(ClassWriter cw, ClassDefinition classDef) {
    FieldVisitor fv = cw.visitField(ACC_PRIVATE, TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class), "Ljava/util/Map<Ljava/lang/String;Lorg/drools/core/factmodel/traits/Thing;>;", null);
    fv.visitEnd();
    MethodVisitor mv;
    mv = cw.visitMethod(ACC_PUBLIC, "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}), "()Ljava/util/Map<Ljava/lang/String;Lorg/drools/factmodel/traits/Thing;>;", null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "_setTraitMap", Type.getMethodDescriptor(Type.getType(void.class), new Type[] { Type.getType(Map.class) }), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitFieldInsn(PUTFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "addTrait", Type.getMethodDescriptor(Type.getType(void.class), new Type[] { Type.getType(String.class), Type.getType(Thing.class) }), "(Ljava/lang/String;Lorg/drools/core/factmodel/traits/Thing;)V", null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitVarInsn(ALOAD, 2);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "put", Type.getMethodDescriptor(Type.getType(Object.class), new Type[] { Type.getType(Object.class), Type.getType(Object.class) }));
    mv.visitInsn(POP);
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "getTrait", Type.getMethodDescriptor(Type.getType(Thing.class), new Type[] { Type.getType(String.class) }), Type.getMethodDescriptor(Type.getType(Thing.class), new Type[] { Type.getType(String.class) }), null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "get", Type.getMethodDescriptor(Type.getType(Object.class), new Type[] { Type.getType(Object.class) }));
    mv.visitTypeInsn(CHECKCAST, Type.getInternalName(Thing.class));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "hasTrait", Type.getMethodDescriptor(Type.getType(boolean.class), new Type[] { Type.getType(String.class) }), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
    Label l0 = new Label();
    mv.visitJumpInsn(IFNONNULL, l0);
    mv.visitInsn(ICONST_0);
    mv.visitInsn(IRETURN);
    mv.visitLabel(l0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "containsKey", Type.getMethodDescriptor(Type.getType(boolean.class), new Type[] { Type.getType(Object.class) }));
    mv.visitInsn(IRETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "hasTraits", Type.getMethodDescriptor(Type.getType(boolean.class), new Type[] {}), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    Label l5 = new Label();
    mv.visitJumpInsn(IFNULL, l5);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "isEmpty", Type.getMethodDescriptor(Type.BOOLEAN_TYPE, new Type[] {}));
    mv.visitJumpInsn(IFNE, l5);
    mv.visitInsn(ICONST_1);
    Label l4 = new Label();
    mv.visitJumpInsn(GOTO, l4);
    mv.visitLabel(l5);
    mv.visitInsn(ICONST_0);
    mv.visitLabel(l4);
    mv.visitInsn(IRETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "removeTrait", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(String.class) }), Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(String.class) }), null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
    mv.visitTypeInsn(CHECKCAST, TraitTypeMapConstants.TYPE_NAME);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, TraitTypeMapConstants.TYPE_NAME, "removeCascade", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(String.class) }));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "removeTrait", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(BitSet.class) }), Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(BitSet.class) }), null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
    mv.visitTypeInsn(CHECKCAST, TraitTypeMapConstants.TYPE_NAME);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, TraitTypeMapConstants.TYPE_NAME, "removeCascade", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(BitSet.class) }));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "getTraits", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] {}), "()Ljava/util/Collection<Ljava/lang/String;>;", null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getName()), "_getTraitMap", Type.getMethodDescriptor(Type.getType(Map.class), new Type[] {}));
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(Map.class), "keySet", Type.getMethodDescriptor(Type.getType(Set.class), new Type[] {}));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "_setBottomTypeCode", Type.getMethodDescriptor(Type.getType(void.class), new Type[] { Type.getType(BitSet.class) }), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    mv.visitTypeInsn(CHECKCAST, TraitTypeMapConstants.TYPE_NAME);
    mv.visitVarInsn(ALOAD, 1);
    mv.visitMethodInsn(INVOKEVIRTUAL, TraitTypeMapConstants.TYPE_NAME, "setBottomCode", Type.getMethodDescriptor(Type.getType(void.class), new Type[] { Type.getType(BitSet.class) }));
    mv.visitInsn(RETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "getMostSpecificTraits", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] {}), "()Ljava/util/Collection<Lorg/drools/core/factmodel/traits/Thing;>;", null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    Label l99 = new Label();
    mv.visitJumpInsn(IFNULL, l99);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    mv.visitTypeInsn(CHECKCAST, TraitTypeMapConstants.TYPE_NAME);
    mv.visitMethodInsn(INVOKEVIRTUAL, TraitTypeMapConstants.TYPE_NAME, "getMostSpecificTraits", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] {}));
    mv.visitInsn(ARETURN);
    mv.visitLabel(l99);
    mv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Collections.class), "emptySet", Type.getMethodDescriptor(Type.getType(Set.class), new Type[] {}));
    mv.visitMethodInsn(INVOKESTATIC, Type.getInternalName(Collections.class), "unmodifiableCollection", Type.getMethodDescriptor(Type.getType(Collection.class), new Type[] { Type.getType(Collection.class) }));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
    mv = cw.visitMethod(ACC_PUBLIC, "getCurrentTypeCode", Type.getMethodDescriptor(Type.getType(BitSet.class), new Type[] {}), null, null);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    Label l3 = new Label();
    mv.visitJumpInsn(IFNONNULL, l3);
    mv.visitInsn(ACONST_NULL);
    mv.visitInsn(ARETURN);
    mv.visitLabel(l3);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getName()), TraitableBean.TRAITSET_FIELD_NAME, Type.getDescriptor(Map.class));
    mv.visitTypeInsn(CHECKCAST, TraitTypeMapConstants.TYPE_NAME);
    mv.visitMethodInsn(INVOKEVIRTUAL, TraitTypeMapConstants.TYPE_NAME, "getCurrentTypeCode", Type.getMethodDescriptor(Type.getType(BitSet.class), new Type[] {}));
    mv.visitInsn(ARETURN);
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : Type(org.mvel2.asm.Type) Label(org.mvel2.asm.Label) BitSet(java.util.BitSet) Collection(java.util.Collection) Collections(java.util.Collections) FieldVisitor(org.mvel2.asm.FieldVisitor) Map(java.util.Map) Thing(org.drools.core.factmodel.traits.Thing) MethodVisitor(org.mvel2.asm.MethodVisitor)

Aggregations

Thing (org.drools.core.factmodel.traits.Thing)23 BitSet (java.util.BitSet)7 ArrayList (java.util.ArrayList)6 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)6 Test (org.junit.Test)6 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 Collection (java.util.Collection)3 Map (java.util.Map)3 TraitFactory (org.drools.core.factmodel.traits.TraitFactory)3 TraitProxy (org.drools.core.factmodel.traits.TraitProxy)3 TraitTypeMap (org.drools.core.factmodel.traits.TraitTypeMap)3 TraitFactoryImpl (org.drools.traits.core.factmodel.TraitFactoryImpl)3 TraitProxyImpl (org.drools.traits.core.factmodel.TraitProxyImpl)3 FieldVisitor (org.mvel2.asm.FieldVisitor)3 Label (org.mvel2.asm.Label)3 MethodVisitor (org.mvel2.asm.MethodVisitor)3 Type (org.mvel2.asm.Type)3 Collections (java.util.Collections)2 List (java.util.List)2 TraitType (org.drools.core.factmodel.traits.TraitType)2