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