Search in sources :

Example 11 with FactField

use of org.kie.api.definition.type.FactField in project drools by kiegroup.

the class TraitRegistry method bind.

private BitSet bind(String trait, String traitable) throws UnsupportedOperationException {
    ClassDefinition traitDef = getTrait(trait);
    if (traitDef == null) {
        throw new UnsupportedOperationException(" Unable to apply trait " + trait + " to class " + traitable + " : not a trait ");
    }
    ClassDefinition traitableDef = getTraitable(traitable);
    if (traitableDef == null) {
        throw new UnsupportedOperationException(" Unable to apply trait " + trait + " to class " + traitable + " : not a traitable ");
    }
    int j = 0;
    BitSet bitmask = new BitSet(traitDef.getFields().size());
    for (FactField field : traitDef.getFields()) {
        String alias = ((FieldDefinition) field).resolveAlias();
        FieldDefinition concreteField = traitableDef.getFieldByAlias(alias);
        if (concreteField != null) {
            if (!traitableDef.isFullTraiting() && !concreteField.getType().isAssignableFrom(field.getType())) {
                throw new UnsupportedOperationException(" Unable to apply trait " + trait + " to class " + traitable + " :" + " trait field " + field.getName() + ":" + ((FieldDefinition) field).getTypeName() + " is incompatible with" + " concrete hard field " + concreteField.getName() + ":" + concreteField.getTypeName() + ". Consider enabling logical traiting" + " mode using @Traitable( logical = true )");
            }
            bitmask.set(j);
        }
        j++;
    }
    return bitmask;
}
Also used : FactField(org.kie.api.definition.type.FactField) FieldDefinition(org.drools.core.factmodel.FieldDefinition) BitSet(java.util.BitSet) ClassDefinition(org.drools.core.factmodel.ClassDefinition)

Example 12 with FactField

use of org.kie.api.definition.type.FactField in project drools by kiegroup.

the class DefaultBeanClassBuilder method initializeDynamicTypeStructures.

/**
 * Initializes the trait map and dynamic property map to empty values
 * @param mv
 * @param classDef
 */
protected void initializeDynamicTypeStructures(MethodVisitor mv, ClassDefinition classDef) {
    if (classDef.isFullTraiting()) {
        mv.visitVarInsn(ALOAD, 0);
        mv.visitTypeInsn(NEW, Type.getInternalName(TraitFieldTMSImpl.class));
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(TraitFieldTMSImpl.class), "<init>", "()V");
        mv.visitFieldInsn(PUTFIELD, BuildUtils.getInternalType(classDef.getClassName()), TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class));
        for (FactField hardField : classDef.getFields()) {
            FieldDefinition fld = (FieldDefinition) hardField;
            mv.visitVarInsn(ALOAD, 0);
            mv.visitFieldInsn(GETFIELD, BuildUtils.getInternalType(classDef.getClassName()), TraitableBean.FIELDTMS_FIELD_NAME, Type.getDescriptor(TraitFieldTMS.class));
            mv.visitLdcInsn(Type.getType(BuildUtils.getTypeDescriptor(classDef.getClassName())));
            mv.visitLdcInsn(fld.resolveAlias());
            if (BuildUtils.isPrimitive(fld.getTypeName())) {
                // mv.visitFieldInsn( GETSTATIC, BuildUtils.getInternalType( BuildUtils.box( fld.getTypeName() ) ), "TYPE", Type.getDescriptor( Class.class ) );
                mv.visitLdcInsn(Type.getType(BuildUtils.getTypeDescriptor(BuildUtils.box(fld.getTypeName()))));
            } else {
                mv.visitLdcInsn(Type.getType(BuildUtils.getTypeDescriptor(fld.getTypeName())));
            }
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKEVIRTUAL, BuildUtils.getInternalType(classDef.getClassName()), BuildUtils.getterName(fld.getName(), fld.getTypeName()), "()" + BuildUtils.getTypeDescriptor(fld.getTypeName()));
            if (BuildUtils.isPrimitive(fld.getTypeName())) {
                mv.visitMethodInsn(INVOKESTATIC, BuildUtils.getInternalType(BuildUtils.box(fld.getTypeName())), "valueOf", "(" + BuildUtils.getTypeDescriptor(fld.getTypeName()) + ")" + BuildUtils.getTypeDescriptor(BuildUtils.box(fld.getTypeName())));
            }
            if (fld.getInitExpr() != null) {
                mv.visitLdcInsn(fld.getInitExpr());
            } else {
                mv.visitInsn(ACONST_NULL);
            }
            mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(TraitFieldTMS.class), "registerField", Type.getMethodDescriptor(Type.VOID_TYPE, new Type[] { Type.getType(Class.class), Type.getType(String.class), Type.getType(Class.class), Type.getType(Object.class), Type.getType(String.class) }));
        }
    }
}
Also used : TraitFieldTMSImpl(org.drools.core.factmodel.traits.TraitFieldTMSImpl) FactField(org.kie.api.definition.type.FactField) Type(org.mvel2.asm.Type) TraitFieldTMS(org.drools.core.factmodel.traits.TraitFieldTMS)

Example 13 with FactField

use of org.kie.api.definition.type.FactField in project drools by kiegroup.

the class TypeDeclarationFactory method compareTypeDeclarations.

protected int compareTypeDeclarations(TypeDeclaration oldDeclaration, TypeDeclaration newDeclaration) throws IncompatibleClassChangeError {
    // different formats -> incompatible
    if (!oldDeclaration.getFormat().equals(newDeclaration.getFormat())) {
        throw new IncompatibleClassChangeError("Type Declaration " + newDeclaration.getTypeName() + " has a different" + " format that its previous definition: " + newDeclaration.getFormat() + "!=" + oldDeclaration.getFormat());
    }
    // different superclasses -> Incompatible (TODO: check for hierarchy)
    if (!oldDeclaration.getTypeClassDef().getSuperClass().equals(newDeclaration.getTypeClassDef().getSuperClass())) {
        if (oldDeclaration.getNature() == TypeDeclaration.Nature.DEFINITION && newDeclaration.getNature() == TypeDeclaration.Nature.DECLARATION && Object.class.getName().equals(newDeclaration.getTypeClassDef().getSuperClass())) {
        // actually do nothing. The new declaration just recalls the previous definition, probably to extend it.
        } else {
            throw new IncompatibleClassChangeError("Type Declaration " + newDeclaration.getTypeName() + " has a different" + " superclass that its previous definition: " + newDeclaration.getTypeClassDef().getSuperClass() + " != " + oldDeclaration.getTypeClassDef().getSuperClass());
        }
    }
    // different duration -> Incompatible
    if (!nullSafeEqualityComparison(oldDeclaration.getDurationAttribute(), newDeclaration.getDurationAttribute())) {
        throw new IncompatibleClassChangeError("Type Declaration " + newDeclaration.getTypeName() + " has a different" + " duration: " + newDeclaration.getDurationAttribute() + " != " + oldDeclaration.getDurationAttribute());
    }
    // //different masks -> incompatible
    if (newDeclaration.getNature().equals(TypeDeclaration.Nature.DEFINITION)) {
        if (oldDeclaration.getSetMask() != newDeclaration.getSetMask()) {
            throw new IncompatibleClassChangeError("Type Declaration " + newDeclaration.getTypeName() + " is incompatible with" + " the previous definition: " + newDeclaration + " != " + oldDeclaration);
        }
    }
    // TODO: further comparison?
    // Field comparison
    List<FactField> oldFields = oldDeclaration.getTypeClassDef().getFields();
    Map<String, FactField> newFieldsMap = new HashMap<String, FactField>();
    for (FactField factField : newDeclaration.getTypeClassDef().getFields()) {
        newFieldsMap.put(factField.getName(), factField);
    }
    // each of the fields in the old definition that are also present in the
    // new definition must have the same type. If not -> Incompatible
    boolean allFieldsInOldDeclarationAreStillPresent = true;
    for (FactField oldFactField : oldFields) {
        FactField newFactField = newFieldsMap.get(oldFactField.getName());
        if (newFactField != null) {
            // we can't use newFactField.getType() since it throws a NPE at this point.
            String newFactType = ((FieldDefinition) newFactField).getTypeName();
            if (!newFactType.equals(((FieldDefinition) oldFactField).getTypeName())) {
                throw new IncompatibleClassChangeError("Type Declaration " + newDeclaration.getTypeName() + "." + newFactField.getName() + " has a different" + " type that its previous definition: " + newFactType + " != " + oldFactField.getType().getCanonicalName());
            }
        } else {
            allFieldsInOldDeclarationAreStillPresent = false;
        }
    }
    // If the old declaration has less fields than the new declaration, oldDefinition < newDefinition
    if (oldFields.size() < newFieldsMap.size()) {
        return -1;
    }
    // If the old declaration has more fields than the new declaration, oldDefinition > newDefinition
    if (oldFields.size() > newFieldsMap.size()) {
        return 1;
    }
    // they are incompatible
    if (allFieldsInOldDeclarationAreStillPresent) {
        return 0;
    }
    // fields in the old declaration are present in the new declaration.
    throw new IncompatibleClassChangeError(newDeclaration.getTypeName() + " introduces" + " fields that are not present in its previous version.");
}
Also used : FactField(org.kie.api.definition.type.FactField) HashMap(java.util.HashMap) FieldDefinition(org.drools.core.factmodel.FieldDefinition)

Example 14 with FactField

use of org.kie.api.definition.type.FactField in project drools by kiegroup.

the class DynamicFact method entrySet.

public Set entrySet() {
    Set set = new HashSet();
    List<FactField> list = this.typeDef.getFields();
    for (FactField factField : list) {
        final FactField ff = factField;
        Map.Entry<String, Object> ent = new Map.Entry<String, Object>() {

            public String getKey() {
                return ff.getName();
            }

            public Object getValue() {
                return typeDef.get(bean, ff.getName());
            }

            public Object setValue(Object o) {
                typeDef.set(bean, ff.getName(), o);
                return o;
            }
        };
    }
    return set;
}
Also used : FactField(org.kie.api.definition.type.FactField) HashSet(java.util.HashSet) Set(java.util.Set) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

FactField (org.kie.api.definition.type.FactField)14 Test (org.junit.Test)7 FactType (org.kie.api.definition.type.FactType)6 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)4 FieldDefinition (org.drools.core.factmodel.FieldDefinition)3 BitSet (java.util.BitSet)2 ClassDefinition (org.drools.core.factmodel.ClassDefinition)2 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)2 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)2 TypeDeclaration (org.drools.core.rule.TypeDeclaration)2 KiePackage (org.kie.api.definition.KiePackage)2 Type (org.mvel2.asm.Type)2 Externalizable (java.io.Externalizable)1 IOException (java.io.IOException)1 ObjectInput (java.io.ObjectInput)1 ObjectOutput (java.io.ObjectOutput)1 StringReader (java.io.StringReader)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1