Search in sources :

Example 1 with AnnotationDefinition

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

the class AnnotationsTest method testCollectAnnotationsParsingAndBuilding.

@Test
public void testCollectAnnotationsParsingAndBuilding() throws Exception {
    final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6);
    final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
    kBuilder.addPackage(new PackageDescr("org.drools"));
    String ruleDrl = "package org.drools.compiler; " + " " + "dialect 'mvel' " + " " + "import java.util.Collection; " + "import " + Annot.class.getCanonicalName() + "; " + " " + "rule \"test collect with annotation\" " + "    when " + "        Collection() from collect ( " + "            String() @Annot " + "        ) " + "    then " + "end " + "";
    final PackageDescr pkgDescr = parser.parse(new StringReader(ruleDrl));
    // just checking there is no parsing errors
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    kBuilder.addPackage(pkgDescr);
    assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
    final RuleImpl rule = kBuilder.getPackage(pkgDescr.getName()).getRule("test collect with annotation");
    List<? extends RuleConditionElement> nested = ((Pattern) rule.getLhs().getChildren().get(0)).getSource().getNestedElements();
    assertEquals(1, nested.size());
    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) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) DrlParser(org.drools.compiler.compiler.DrlParser) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 2 with AnnotationDefinition

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

the class AnnotationsTest method testTypedSimpleArrays.

@Test
public void testTypedSimpleArrays() {
    String drl = "package org.drools.test; " + "import " + Simple.class.getName().replace("$", ".") + "; " + "rule Foo " + "when " + "  String() @Simple( numbers = { 1, 2, 3 } ) " + "then " + "end ";
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    Pattern p = ((Pattern) ((RuleImpl) helper.build().getRule("org.drools.test", "Foo")).getLhs().getChildren().get(0));
    Map<String, AnnotationDefinition> defs = p.getAnnotations();
    assertEquals(1, defs.size());
    AnnotationDefinition simple = defs.get(Simple.class.getName().replace("$", "."));
    assertNotNull(simple);
    Object val = simple.getPropertyValue("numbers");
    assertTrue(val instanceof int[]);
}
Also used : Pattern(org.drools.core.rule.Pattern) AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) KieHelper(org.kie.internal.utils.KieHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 3 with AnnotationDefinition

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

the class AnnotationsTest method testAnnotationWithQualifiandClass.

@Test
public void testAnnotationWithQualifiandClass() {
    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 ";
    KieHelper helper = new KieHelper();
    helper.addContent(drl, ResourceType.DRL);
    Pattern p = ((Pattern) ((RuleImpl) helper.build().getRule("org.drools.test", "Foo")).getLhs().getChildren().get(0));
    AnnotationDefinition adef = p.getAnnotations().get(Outer.class.getName().replace("$", "."));
    assertEquals(String.class, adef.getPropertyValue("klass"));
    assertEquals(Arrays.asList(new Class[] { 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) KieHelper(org.kie.internal.utils.KieHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 4 with AnnotationDefinition

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

the class StandaloneTraitFactory method extendAsProperTrait.

private Class<T> extendAsProperTrait(Class<T> trait) {
    String extName = trait.getName() + TraitFactory.SUFFIX;
    if (!classLoader.isClassInUse(extName)) {
        try {
            ClassDefinition extDef = new ClassDefinition(extName);
            extDef.setSuperClass(Object.class.getName());
            AnnotationDefinition annot = new AnnotationDefinition(Trait.class.getName());
            extDef.addAnnotation(annot);
            String[] supers = new String[] { Thing.class.getName(), trait.getName() };
            extDef.setInterfaces(supers);
            byte[] ext = getComponentFactory().getClassBuilderFactory().getTraitBuilder().buildClass(extDef, classLoader);
            Class<?> klass = registerAndLoadTypeDefinition(extName, ext);
            ClassDefinition tDef = buildClassDefinition(trait, trait);
            tDef.setDefinedClass(klass);
            getTraitRegistry().addTrait(tDef);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    try {
        return (Class<T>) Class.forName(extName, false, classLoader);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) Trait(org.drools.core.factmodel.traits.Trait) ClassDefinition(org.drools.core.factmodel.ClassDefinition) IOException(java.io.IOException) LogicalTypeInconsistencyException(org.drools.core.factmodel.traits.LogicalTypeInconsistencyException)

Example 5 with AnnotationDefinition

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

the class StandaloneTraitFactory method extendAsProperTrait.

private Class<T> extendAsProperTrait(Class<T> trait) {
    String extName = trait.getName() + SUFFIX;
    if (!classLoader.isClassInUse(extName)) {
        try {
            ClassDefinition extDef = new ClassDefinition(extName);
            extDef.setSuperClass(Object.class.getName());
            AnnotationDefinition annot = new AnnotationDefinition(Trait.class.getName());
            extDef.addAnnotation(annot);
            String[] supers = new String[] { Thing.class.getName(), trait.getName() };
            extDef.setInterfaces(supers);
            byte[] ext = new TraitClassBuilderImpl().buildClass(extDef, classLoader);
            Class<?> klass = registerAndLoadTypeDefinition(extName, ext);
            ClassDefinition tDef = buildClassDefinition(trait, trait);
            tDef.setDefinedClass(klass);
            getTraitRegistry().addTrait(tDef);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    try {
        return (Class<T>) Class.forName(extName, false, classLoader);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return null;
}
Also used : AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) TraitClassBuilderImpl(org.drools.traits.core.factmodel.TraitClassBuilderImpl) Trait(org.drools.core.factmodel.traits.Trait) ClassDefinition(org.drools.core.factmodel.ClassDefinition) IOException(java.io.IOException) LogicalTypeInconsistencyException(org.drools.traits.core.factmodel.LogicalTypeInconsistencyException)

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