Search in sources :

Example 1 with Alias

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

the class ClassHierarchyManager method buildInheritedFieldDescrFromDefinition.

protected TypeFieldDescr buildInheritedFieldDescrFromDefinition(org.kie.api.definition.type.FactField fld, TypeDeclarationDescr typeDescr) {
    TypeFieldDescr inheritedFldDescr = new TypeFieldDescr();
    inheritedFldDescr.setFieldName(fld.getName());
    inheritedFldDescr.setResource(typeDescr.getResource());
    PatternDescr fldType = new PatternDescr();
    fldType.setObjectType(((FieldDefinition) fld).getTypeName());
    // also sets resource for PatternDescr fldType
    inheritedFldDescr.setPattern(fldType);
    if (fld.isKey()) {
        AnnotationDescr keyAnnotation = new AnnotationDescr(Key.class.getCanonicalName());
        keyAnnotation.setFullyQualifiedName(Key.class.getCanonicalName());
        keyAnnotation.setResource(typeDescr.getResource());
        inheritedFldDescr.addAnnotation(keyAnnotation);
    }
    inheritedFldDescr.setIndex(((FieldDefinition) fld).getDeclIndex());
    inheritedFldDescr.setInherited(true);
    String initExprOverride = ((FieldDefinition) fld).getInitExpr();
    int overrideCount = 0;
    // only @aliasing local fields may override defaults.
    for (TypeFieldDescr localField : typeDescr.getFields().values()) {
        Alias alias = localField.getTypedAnnotation(Alias.class);
        if (alias != null && fld.getName().equals(alias.value().replaceAll("\"", "")) && localField.getInitExpr() != null) {
            overrideCount++;
            initExprOverride = localField.getInitExpr();
        }
    }
    if (overrideCount > 1) {
        // however, only one is allowed
        initExprOverride = null;
    }
    inheritedFldDescr.setInitExpr(initExprOverride);
    return inheritedFldDescr;
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) Alias(org.drools.core.factmodel.traits.Alias) FieldDefinition(org.drools.core.factmodel.FieldDefinition) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) Key(org.kie.api.definition.type.Key)

Aggregations

AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)1 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)1 TypeFieldDescr (org.drools.compiler.lang.descr.TypeFieldDescr)1 FieldDefinition (org.drools.core.factmodel.FieldDefinition)1 Alias (org.drools.core.factmodel.traits.Alias)1 Key (org.kie.api.definition.type.Key)1