Search in sources :

Example 6 with AnnotationDefinition

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

the class DefaultBeanClassBuilder method buildClassAnnotations.

protected void buildClassAnnotations(ClassDefinition classDef, ClassVisitor cw) {
    for (AnnotationDefinition ad : classDef.getAnnotations()) {
        AnnotationVisitor av = cw.visitAnnotation("L" + BuildUtils.getInternalType(ad.getName()) + ";", true);
        addAnnotationAttribute(ad, av);
        av.visitEnd();
    }
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) AnnotationVisitor(org.mvel2.asm.AnnotationVisitor)

Example 7 with AnnotationDefinition

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

the class DefaultBeanClassBuilder method addAnnotationAttribute.

public static void addAnnotationAttribute(AnnotationDefinition ad, AnnotationVisitor av) {
    for (String key : ad.getValues().keySet()) {
        AnnotationDefinition.AnnotationPropertyVal apv = ad.getValues().get(key);
        switch(apv.getValType()) {
            case STRINGARRAY:
                AnnotationVisitor subAv = av.visitArray(apv.getProperty());
                Object[] array = (Object[]) apv.getValue();
                for (Object o : array) {
                    subAv.visit(null, o);
                }
                subAv.visitEnd();
                break;
            case PRIMARRAY:
                av.visit(apv.getProperty(), apv.getValue());
                break;
            case ENUMARRAY:
                AnnotationVisitor subEnav = av.visitArray(apv.getProperty());
                Enum[] enArray = (Enum[]) apv.getValue();
                String aenumType = "L" + BuildUtils.getInternalType(enArray[0].getClass().getName()) + ";";
                for (Enum enumer : enArray) {
                    subEnav.visitEnum(null, aenumType, enumer.name());
                }
                subEnav.visitEnd();
                break;
            case CLASSARRAY:
                AnnotationVisitor subKlav = av.visitArray(apv.getProperty());
                Class[] klarray = (Class[]) apv.getValue();
                for (Class klass : klarray) {
                    subKlav.visit(null, Type.getType("L" + BuildUtils.getInternalType(klass.getName()) + ";"));
                }
                subKlav.visitEnd();
                break;
            case ENUMERATION:
                String enumType = "L" + BuildUtils.getInternalType(apv.getType().getName()) + ";";
                av.visitEnum(apv.getProperty(), enumType, ((Enum) apv.getValue()).name());
                break;
            case KLASS:
                String klassName = BuildUtils.getInternalType(((Class) apv.getValue()).getName());
                av.visit(apv.getProperty(), Type.getType("L" + klassName + ";"));
                break;
            case PRIMITIVE:
                av.visit(apv.getProperty(), apv.getValue());
                break;
            case STRING:
                av.visit(apv.getProperty(), apv.getValue());
                break;
        }
    }
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) AnnotationVisitor(org.mvel2.asm.AnnotationVisitor) ReactiveObject(org.drools.core.phreak.ReactiveObject)

Example 8 with AnnotationDefinition

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

the class AnnotationsOnPatternTest method testCollectAnnotationsParsingAndBuilding.

@Test
public void testCollectAnnotationsParsingAndBuilding() {
    final String packageName = "org.drools.compiler.integrationtests";
    final String drl = "package " + packageName + "; " + " " + "dialect 'mvel' " + " " + "import java.util.Collection; " + "import " + Inner.class.getCanonicalName() + "; " + " " + "rule \"test collect with annotation\" " + "    when " + "        Collection() from collect ( " + "            String() @Inner " + "        ) " + "    then " + "end " + "";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("annotations-test", kieBaseTestConfiguration, drl);
    final RuleImpl rule = (RuleImpl) kbase.getRule(packageName, "test collect with annotation");
    final List<? extends RuleConditionElement> nested = ((Pattern) rule.getLhs().getChildren().get(0)).getSource().getNestedElements();
    assertEquals(1, nested.size());
    final Map<String, AnnotationDefinition> annotations = ((Pattern) nested.get(0)).getAnnotations();
    assertEquals(1, annotations.size());
    assertNotNull(annotations.keySet().iterator().next());
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) Pattern(org.drools.core.rule.Pattern) KieBase(org.kie.api.KieBase) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 9 with AnnotationDefinition

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

the class ModelGenerator method ruleMetaAttributes.

/**
 * Build a list of method calls, representing each needed {@link org.drools.model.impl.RuleBuilder#metadata(String, Object)}
 * starting from a drools-compiler {@link RuleDescr}.<br/>
 * Based on {@link org.drools.modelcompiler.KiePackagesBuilder#setRuleMetaAttributes(Rule, RuleImpl)} the reserved annotation keywords are:
 * Propagation, All, Direct.
 */
private static List<MethodCallExpr> ruleMetaAttributes(RuleContext context, RuleDescr ruleDescr) {
    List<MethodCallExpr> ruleMetaAttributes = new ArrayList<>();
    for (String metaAttr : ruleDescr.getAnnotationNames()) {
        MethodCallExpr metaAttributeCall = new MethodCallExpr(METADATA_CALL);
        metaAttributeCall.addArgument(toStringLiteral(metaAttr));
        AnnotationDescr ad = ruleDescr.getAnnotation(metaAttr);
        String adFqn = ad.getFullyQualifiedName();
        if ("Propagation".equals(metaAttr)) {
            // legacy case, as explained in the javadoc annotation above, ref. DROOLS-5685
            metaAttributeCall.addArgument(parseExpression(org.kie.api.definition.rule.Propagation.Type.class.getCanonicalName() + "." + ad.getSingleValueAsString()));
        } else if (adFqn != null) {
            AnnotationDefinition annotationDefinition;
            try {
                annotationDefinition = AnnotationDefinition.build(context.getTypeResolver().resolveType(adFqn), ad.getValueMap(), context.getTypeResolver());
            } catch (NoSuchMethodException | ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
            if (annotationDefinition.getValues().size() == 1 && annotationDefinition.getValues().containsKey(AnnotationDescr.VALUE)) {
                Object annValue = annotationDefinition.getPropertyValue(AnnotationDescr.VALUE);
                metaAttributeCall.addArgument(toStringLiteral(annValue.toString()));
            } else {
                Map<String, Object> map = new HashMap<>(annotationDefinition.getValues().size());
                for (String key : annotationDefinition.getValues().keySet()) {
                    map.put(key, annotationDefinition.getPropertyValue(key));
                }
                metaAttributeCall.addArgument(objectAsJPExpression(map));
            }
        } else {
            if (ad.hasValue()) {
                if (ad.getValueMap().size() == 1) {
                    metaAttributeCall.addArgument(annotationSingleValueExpression(ad));
                } else {
                    metaAttributeCall.addArgument(objectAsJPExpression(ad.getValueMap()));
                }
            } else {
                metaAttributeCall.addArgument(new NullLiteralExpr());
            }
        }
        ruleMetaAttributes.add(metaAttributeCall);
    }
    return ruleMetaAttributes;
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) ArrayList(java.util.ArrayList) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Map(java.util.Map) HashMap(java.util.HashMap) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 10 with AnnotationDefinition

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

the class PatternBuilder method processMetadataAnnotations.

protected void processMetadataAnnotations(PatternDescr patternDescr, Pattern pattern, TypeResolver typeResolver) {
    for (AnnotationDescr ann : patternDescr.getAnnotations()) {
        String annFQN = ann.getFullyQualifiedName();
        if (!Watch.class.getCanonicalName().equals(annFQN)) {
            AnnotationDefinition def = buildAnnotationDef(ann, typeResolver);
            pattern.getAnnotations().put(annFQN, def);
        }
    }
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr)

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