Search in sources :

Example 16 with AnnotationDefinition

use of org.drools.core.factmodel.AnnotationDefinition in project drools by kiegroup.

the class ClassDefinitionFactory method sortFields.

private static List<FieldDefinition> sortFields(Map<String, TypeFieldDescr> fields, TypeResolver typeResolver, KnowledgeBuilderImpl kbuilder) {
    List<FieldDefinition> fieldDefs = new ArrayList<>(fields.size());
    int maxDeclaredPos = 0;
    BitSet occupiedPositions = new BitSet(fields.size());
    for (TypeFieldDescr field : fields.values()) {
        GenericTypeDefinition genericType = field.getPattern().getGenericType().map(type -> TypeDeclarationUtils.toBuildableType(type, kbuilder != null ? kbuilder.getRootClassLoader() : null));
        FieldDefinition fieldDef = new FieldDefinition(field.getFieldName(), genericType);
        fieldDefs.add(fieldDef);
        if (field.hasOverride()) {
            fieldDef.setOverriding(field.getOverriding().getPattern().getObjectType());
        }
        fieldDef.setInherited(field.isInherited());
        fieldDef.setRecursive(field.isRecursive());
        fieldDef.setInitExpr(TypeDeclarationUtils.rewriteInitExprWithImports(field.getInitExpr(), typeResolver));
        if (field.getIndex() >= 0) {
            int pos = field.getIndex();
            occupiedPositions.set(pos);
            maxDeclaredPos = Math.max(maxDeclaredPos, pos);
            fieldDef.addMetaData("position", pos);
        } else {
            Position position = getTypedAnnotation(field, Position.class);
            if (position != null) {
                int pos = position.value();
                field.setIndex(pos);
                occupiedPositions.set(pos);
                maxDeclaredPos = Math.max(maxDeclaredPos, pos);
                fieldDef.addMetaData("position", pos);
            }
        }
        if (field.hasAnnotation(Key.class)) {
            fieldDef.setKey(true);
            fieldDef.addMetaData("key", null);
        }
        for (AnnotationDescr annotationDescr : field.getAnnotations()) {
            if (annotationDescr.getFullyQualifiedName() == null) {
                if (annotationDescr.isStrict()) {
                    kbuilder.addBuilderResult(new TypeDeclarationError(field, "Unknown annotation @" + annotationDescr.getName() + " on field " + field.getFieldName()));
                } else {
                    // Annotation is custom metadata
                    fieldDef.addMetaData(annotationDescr.getName(), annotationDescr.getSingleValue());
                    continue;
                }
            }
            Annotation annotation = AnnotationFactory.buildAnnotation(typeResolver, annotationDescr);
            if (annotation != null) {
                try {
                    AnnotationDefinition annotationDefinition = AnnotationDefinition.build(annotation.annotationType(), field.getAnnotation(annotationDescr.getFullyQualifiedName()).getValueMap(), typeResolver);
                    fieldDef.addAnnotation(annotationDefinition);
                } catch (Exception e) {
                    kbuilder.addBuilderResult(new TypeDeclarationError(field, "Annotated field " + field.getFieldName() + "  - undefined property in @annotation " + annotationDescr.getName() + ": " + e.getMessage() + ";"));
                }
            } else {
                if (annotationDescr.isStrict()) {
                    kbuilder.addBuilderResult(new TypeDeclarationError(field, "Unknown annotation @" + annotationDescr.getName() + " on field " + field.getFieldName()));
                }
            }
        }
        fieldDef.setDeclIndex(field.getIndex());
    }
    int curr = 0;
    for (FieldDefinition fieldDef : fieldDefs) {
        if (fieldDef.getDeclIndex() < 0) {
            int freePos = occupiedPositions.nextClearBit(0);
            if (freePos < maxDeclaredPos) {
                occupiedPositions.set(freePos);
            } else {
                freePos = maxDeclaredPos + 1;
            }
            fieldDef.setPriority(freePos * 256 + curr++);
        } else {
            fieldDef.setPriority(fieldDef.getDeclIndex() * 256 + curr++);
        }
    }
    Collections.sort(fieldDefs);
    return fieldDefs;
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) Position(org.kie.api.definition.type.Position) FieldDefinition(org.drools.core.factmodel.FieldDefinition) ArrayList(java.util.ArrayList) BitSet(java.util.BitSet) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr) AnnotationFactory.getTypedAnnotation(org.drools.compiler.rule.builder.util.AnnotationFactory.getTypedAnnotation) Annotation(java.lang.annotation.Annotation) IOException(java.io.IOException) TypeDeclarationError(org.drools.compiler.compiler.TypeDeclarationError) TypeFieldDescr(org.drools.drl.ast.descr.TypeFieldDescr) GenericTypeDefinition(org.kie.internal.definition.GenericTypeDefinition)

Example 17 with AnnotationDefinition

use of org.drools.core.factmodel.AnnotationDefinition in project drools by kiegroup.

the class RuleBuilder method buildMetaAttributes.

public static void buildMetaAttributes(final RuleBuildContext context) {
    RuleImpl rule = context.getRule();
    for (String metaAttr : context.getRuleDescr().getAnnotationNames()) {
        AnnotationDescr ad = context.getRuleDescr().getAnnotation(metaAttr);
        String adFqn = ad.getFullyQualifiedName();
        if (adFqn != null) {
            AnnotationDefinition annotationDefinition;
            try {
                annotationDefinition = AnnotationDefinition.build(context.getDialect().getTypeResolver().resolveType(adFqn), ad.getValueMap(), context.getDialect().getTypeResolver());
            } catch (NoSuchMethodException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
            if (annotationDefinition.getValues().size() == 1 && annotationDefinition.getValues().containsKey(AnnotationDescr.VALUE)) {
                rule.addMetaAttribute(metaAttr, annotationDefinition.getPropertyValue(AnnotationDescr.VALUE));
            } else {
                Map<String, Object> map = new HashMap<>(annotationDefinition.getValues().size());
                for (String key : annotationDefinition.getValues().keySet()) {
                    map.put(key, annotationDefinition.getPropertyValue(key));
                }
                rule.addMetaAttribute(metaAttr, map);
            }
        } else {
            if (ad.hasValue()) {
                if (ad.getValueMap().size() == 1) {
                    rule.addMetaAttribute(metaAttr, resolveValue(ad.getSingleValueAsString()));
                } else {
                    rule.addMetaAttribute(metaAttr, ad.getValueMap());
                }
            } else {
                rule.addMetaAttribute(metaAttr, null);
            }
        }
    }
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) HashMap(java.util.HashMap) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr)

Example 18 with AnnotationDefinition

use of org.drools.core.factmodel.AnnotationDefinition in project drools by kiegroup.

the class AnnotationsOnPatternTest method testAnnotationWithQualifiandClass.

@Test
public void testAnnotationWithQualifiandClass() {
    final String drl = "package org.drools.test; " + "import " + Outer.class.getName().replace("$", ".") + "; " + "rule Foo " + "when " + "  String() @Outer( klass = String.class, klasses = { String.class, Integer.class } ) " + "then " + "end ";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("annotations-test", kieBaseTestConfiguration, drl);
    final Pattern p = ((Pattern) ((RuleImpl) kbase.getRule("org.drools.test", "Foo")).getLhs().getChildren().get(0));
    final AnnotationDefinition adef = p.getAnnotations().get(Outer.class.getName().replace("$", "."));
    assertEquals(String.class, adef.getPropertyValue("klass"));
    assertEquals(Arrays.asList(String.class, Integer.class), Arrays.asList((Class[]) adef.getPropertyValue("klasses")));
    assertNotNull(adef);
}
Also used : Pattern(org.drools.core.rule.Pattern) AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) KieBase(org.kie.api.KieBase) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 19 with AnnotationDefinition

use of org.drools.core.factmodel.AnnotationDefinition in project drools by kiegroup.

the class AnnotationsOnPatternTest method testTypedSimpleArrays.

@Test
public void testTypedSimpleArrays() {
    final String drl = "package org.drools.test; " + "import " + AnnotationsTest.Simple.class.getName().replace("$", ".") + "; " + "rule Foo " + "when " + "  String() @Simple( numbers = { 1, 2, 3 } ) " + "then " + "end ";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("annotations-test", kieBaseTestConfiguration, drl);
    final Pattern p = ((Pattern) ((RuleImpl) kbase.getRule("org.drools.test", "Foo")).getLhs().getChildren().get(0));
    final Map<String, AnnotationDefinition> defs = p.getAnnotations();
    assertEquals(1, defs.size());
    final AnnotationDefinition simple = defs.get(AnnotationsTest.Simple.class.getName().replace("$", "."));
    assertNotNull(simple);
    final Object val = simple.getPropertyValue("numbers");
    assertTrue(val instanceof int[]);
}
Also used : Pattern(org.drools.core.rule.Pattern) AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) KieBase(org.kie.api.KieBase) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 20 with AnnotationDefinition

use of org.drools.core.factmodel.AnnotationDefinition in project drools by kiegroup.

the class AnnotationsOnPatternTest method testNestedAnnotations.

@Test
public void testNestedAnnotations() {
    final String drl = "package org.drools.test; " + "import " + Outer.class.getName().replace("$", ".") + "; " + "import " + Inner.class.getName().replace("$", ".") + "; " + "rule Foo " + "when " + "  String() @Outer( value = @Inner( text = \"world\" ) ) " + "then " + "end ";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("annotations-test", kieBaseTestConfiguration, drl);
    final Pattern p = ((Pattern) ((RuleImpl) kbase.getRule("org.drools.test", "Foo")).getLhs().getChildren().get(0));
    final Map<String, AnnotationDefinition> defs = p.getAnnotations();
    assertEquals(1, defs.size());
    final AnnotationDefinition outer = defs.get(Outer.class.getName().replace("$", "."));
    assertNotNull(outer);
    final Object val = outer.getPropertyValue("value");
    assertNotNull(val);
    assertTrue(val instanceof AnnotationDefinition);
    final AnnotationDefinition inner = (AnnotationDefinition) val;
    assertEquals("world", inner.getPropertyValue("text"));
}
Also used : Pattern(org.drools.core.rule.Pattern) AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) KieBase(org.kie.api.KieBase) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Aggregations

AnnotationDefinition (org.drools.core.factmodel.AnnotationDefinition)23 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)11 Pattern (org.drools.core.rule.Pattern)10 Test (org.junit.Test)10 AnnotationDescr (org.drools.drl.ast.descr.AnnotationDescr)5 KieBase (org.kie.api.KieBase)5 AnnotationVisitor (org.mvel2.asm.AnnotationVisitor)5 KieHelper (org.kie.internal.utils.KieHelper)4 IOException (java.io.IOException)3 TypeDeclarationError (org.drools.compiler.compiler.TypeDeclarationError)3 FieldDefinition (org.drools.core.factmodel.FieldDefinition)3 Trait (org.drools.core.factmodel.traits.Trait)3 Serializable (java.io.Serializable)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 ClassDefinition (org.drools.core.factmodel.ClassDefinition)2 GeneratedFact (org.drools.core.factmodel.GeneratedFact)2 ClassWriter (org.mvel2.asm.ClassWriter)2 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1