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());
}
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[]);
}
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);
}
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;
}
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;
}
Aggregations