use of org.kie.api.definition.type.Role in project drools by kiegroup.
the class PatternBuilder method isEvent.
private boolean isEvent(RuleBuildContext context, Class<?> userProvidedClass) {
TypeDeclaration typeDeclaration = getTypeDeclaration(context, userProvidedClass);
if (typeDeclaration != null) {
return typeDeclaration.getRole() == Role.Type.EVENT;
}
Role role = userProvidedClass.getAnnotation(Role.class);
return role != null && role.value() == Role.Type.EVENT;
}
use of org.kie.api.definition.type.Role in project drools by kiegroup.
the class AnnotationsTest method annotationTest.
@Test
public void annotationTest() {
String drl = "package org.drools.compiler.test;\n " + "" + "import org.kie.api.definition.type.Position; \n " + "import " + AnnotationsTest.class.getCanonicalName() + "; \n" + "import " + AnnotationsTest.Annot.class.getCanonicalName() + "; \n" + "" + "declare AnnotatedBean \n" + " @Deprecated \n" + "" + " @Annot( intProp=7 " + " ,typeProp=String.class " + " ,strProp=\"hello world\" " + " ,enumProp=AnnPropEnum.THREE " + " ,dblArrProp={1.0,2.0} " + " ,typeArrProp={String.class, AnnotationsTest.class} " + " ,strArrProp={\"x1\",\"x2\"} " + " ,enumArrProp={AnnPropEnum.ONE, AnnPropEnum.THREE} " + " ) \n " + " \n " + " @role(event) \n " + " " + " age : int \n" + " name : String @key @Position(0) @Deprecated \n" + " end \n " + " " + " \n\n" + " " + "declare SecondBean \n " + " @NonexistingAnnotation" + " \n" + " field : String @Annot \n" + "end \n";
KieBase kbase = loadKnowledgeBaseFromString(drl);
Class clazz = kbase.getFactType("org.drools.compiler.test", "AnnotatedBean").getFactClass();
assertNotNull(clazz);
try {
Field fld = clazz.getDeclaredField("name");
assertEquals(3, fld.getAnnotations().length);
assertNotNull(fld.getAnnotation(Deprecated.class));
assertNotNull(fld.getAnnotation(Position.class));
assertNotNull(fld.getAnnotation(Key.class));
Position pos = fld.getAnnotation(Position.class);
assertEquals(0, pos.value());
} catch (NoSuchFieldException nsfe) {
fail("field name has not been generated correctly : " + nsfe.getMessage());
}
Annotation[] anns = clazz.getAnnotations();
assertEquals(3, anns.length);
assertNotNull(clazz.getAnnotation(Deprecated.class));
assertNotNull(clazz.getAnnotation(Annot.class));
assertNotNull(clazz.getAnnotation(Role.class));
Annot ann = (Annot) clazz.getAnnotation(Annot.class);
assertEquals(7, ann.intProp());
assertEquals(String.class, ann.typeProp());
assertEquals("hello world", ann.strProp());
assertEquals(AnnPropEnum.THREE, ann.enumProp());
assertArrayEquals(new double[] { 1.0, 2.0 }, ann.dblArrProp(), 1e-16);
assertArrayEquals(new Class[] { String.class, AnnotationsTest.class }, ann.typeArrProp());
assertArrayEquals(new String[] { "x1", "x2" }, ann.strArrProp());
assertArrayEquals(new AnnPropEnum[] { AnnPropEnum.ONE, AnnPropEnum.THREE }, ann.enumArrProp());
Class clazz2 = kbase.getFactType("org.drools.compiler.test", "SecondBean").getFactClass();
assertNotNull(clazz2);
Annotation[] anns2 = clazz2.getAnnotations();
assertEquals(0, anns2.length);
Annot ann2 = null;
try {
Field fld2 = clazz2.getDeclaredField("field");
assertEquals(1, fld2.getAnnotations().length);
assertNotNull(fld2.getAnnotation(Annot.class));
ann2 = fld2.getAnnotation(Annot.class);
} catch (NoSuchFieldException nsfe) {
fail("field name has not been generated correctly : " + nsfe.getMessage());
}
assertNotNull(ann2);
assertEquals(0, ann2.intProp());
assertEquals("foo", ann2.strProp());
assertEquals(AnnPropEnum.ONE, ann2.enumProp());
assertArrayEquals(new double[] { 0.4, 0.5 }, ann2.dblArrProp(), 1e-16);
assertArrayEquals(new String[] { "a", "b", "c" }, ann2.strArrProp());
assertArrayEquals(new AnnPropEnum[] { AnnPropEnum.TWO, AnnPropEnum.THREE }, ann2.enumArrProp());
}
use of org.kie.api.definition.type.Role in project drools by kiegroup.
the class TypeDeclarationFactory method processAnnotations.
public static void processAnnotations(AbstractClassTypeDeclarationDescr typeDescr, TypeDeclaration type) {
Role role = typeDescr.getTypedAnnotation(Role.class);
if (role != null) {
type.setRole(role.value());
}
TypeSafe typeSafe = typeDescr.getTypedAnnotation(TypeSafe.class);
if (typeSafe != null) {
type.setTypesafe(typeSafe.value());
}
if (typeDescr instanceof EnumDeclarationDescr) {
type.setKind(TypeDeclaration.Kind.ENUM);
} else if (typeDescr instanceof TypeDeclarationDescr && ((TypeDeclarationDescr) typeDescr).isTrait()) {
type.setKind(TypeDeclaration.Kind.TRAIT);
}
type.setDynamic(typeDescr.hasAnnotation(PropertyChangeSupport.class));
}
use of org.kie.api.definition.type.Role in project drools by kiegroup.
the class KieModuleMetaDataTest method testKieModuleMetaDataInMemory.
private void testKieModuleMetaDataInMemory(boolean useTypeDeclaration) throws Exception {
KieServices ks = KieServices.Factory.get();
ReleaseId dependency = ks.newReleaseId("org.drools", "drools-core", "5.5.0.Final");
ReleaseId releaseId = ks.newReleaseId("org.kie", "metadata-test", "1.0-SNAPSHOT");
InternalKieModule kieModule = createKieJarWithClass(ks, releaseId, useTypeDeclaration, 2, 7, dependency);
KieModuleMetaData kieModuleMetaData = KieModuleMetaData.Factory.newKieModuleMetaData(kieModule);
checkDroolsCoreDep(kieModuleMetaData);
Collection<String> testClasses = kieModuleMetaData.getClasses("org.kie.test");
assertEquals(1, testClasses.size());
assertEquals("Bean", testClasses.iterator().next());
Class<?> beanClass = kieModuleMetaData.getClass("org.kie.test", "Bean");
assertNotNull(beanClass.getMethod("getValue"));
TypeMetaInfo beanTypeInfo = kieModuleMetaData.getTypeMetaInfo(beanClass);
assertNotNull(beanTypeInfo);
assertTrue(beanTypeInfo.isEvent());
Role role = beanClass.getAnnotation(Role.class);
assertNotNull(role);
assertEquals(Role.Type.EVENT, role.value());
assertEquals(useTypeDeclaration, beanTypeInfo.isDeclaredType());
}
Aggregations