use of org.drools.compiler.lang.descr.TypeDeclarationDescr in project drools by kiegroup.
the class TypeDeclarationNameResolver method qualifyNames.
private void qualifyNames(List<TypeDefinition> unresolvedTypes, PackageDescr packageDescr, TypeResolver typeResolver) {
for (TypeDeclarationDescr declarationDescr : packageDescr.getTypeDeclarations()) {
qualifyNames(declarationDescr, packageDescr, unresolvedTypes, typeResolver);
discoverHierarchyForRedeclarations(declarationDescr, packageDescr, typeResolver);
}
for (EnumDeclarationDescr enumDeclarationDescr : packageDescr.getEnumDeclarations()) {
qualifyNames(enumDeclarationDescr, packageDescr, unresolvedTypes, typeResolver);
}
}
use of org.drools.compiler.lang.descr.TypeDeclarationDescr in project drools by kiegroup.
the class RuleParserTest method testTypeWithMetaData.
@Test
public void testTypeWithMetaData() throws Exception {
PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "type_with_meta.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
final List<TypeDeclarationDescr> declarations = pkg.getTypeDeclarations();
assertEquals(3, declarations.size());
}
use of org.drools.compiler.lang.descr.TypeDeclarationDescr in project drools by kiegroup.
the class RuleParserTest method testTypeDeclarationWithFields.
@Test
public void testTypeDeclarationWithFields() throws Exception {
final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "declare_type_with_fields.drl");
assertFalse(parser.getErrors().toString(), parser.hasErrors());
List<TypeDeclarationDescr> td = pkg.getTypeDeclarations();
assertEquals(3, td.size());
TypeDeclarationDescr d = td.get(0);
assertEquals("SomeFact", d.getTypeName());
assertEquals(2, d.getFields().size());
assertTrue(d.getFields().containsKey("name"));
assertTrue(d.getFields().containsKey("age"));
TypeFieldDescr f = d.getFields().get("name");
assertEquals("String", f.getPattern().getObjectType());
f = d.getFields().get("age");
assertEquals("Integer", f.getPattern().getObjectType());
d = td.get(1);
assertEquals("AnotherFact", d.getTypeName());
TypeDeclarationDescr type = td.get(2);
assertEquals("Person", type.getTypeName());
assertEquals("fact", type.getAnnotation("role").getValue());
assertEquals("\"Models a person\"", type.getAnnotation("doc").getValue("descr"));
assertEquals("\"Bob\"", type.getAnnotation("doc").getValue("author"));
assertEquals("Calendar.getInstance().getDate()", type.getAnnotation("doc").getValue("date"));
assertEquals(2, type.getFields().size());
TypeFieldDescr field = type.getFields().get("name");
assertEquals("name", field.getFieldName());
assertEquals("String", field.getPattern().getObjectType());
assertEquals("\"John Doe\"", field.getInitExpr());
assertEquals("50", field.getAnnotation("length").getValue("max"));
assertNotNull(field.getAnnotation("key"));
field = type.getFields().get("age");
assertEquals("age", field.getFieldName());
assertEquals("int", field.getPattern().getObjectType());
assertEquals("-1", field.getInitExpr());
assertEquals("0", field.getAnnotation("ranged").getValue("min"));
assertEquals("150", field.getAnnotation("ranged").getValue("max"));
assertEquals("-1", field.getAnnotation("ranged").getValue("unknown"));
}
use of org.drools.compiler.lang.descr.TypeDeclarationDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testTypeDeclarationNewBean.
@Test
public void testTypeDeclarationNewBean() throws Exception {
PackageDescr pkgDescr = new PackageDescr("org.drools.compiler.test");
TypeDeclarationDescr typeDescr = new TypeDeclarationDescr("NewBean");
TypeFieldDescr f1 = new TypeFieldDescr("name", new PatternDescr("String"));
TypeFieldDescr f2 = new TypeFieldDescr("age", new PatternDescr("int"));
typeDescr.addField(f1);
typeDescr.addField(f2);
pkgDescr.addTypeDeclaration(typeDescr);
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
builder.addPackage(pkgDescr);
InternalKnowledgePackage pkg = builder.getPackage(pkgDescr.getName());
assertEquals(1, pkg.getTypeDeclarations().size());
TypeDeclaration type = pkg.getTypeDeclaration("NewBean");
assertEquals("NewBean", type.getTypeName());
assertEquals(Role.Type.FACT, type.getRole());
assertEquals("org.drools.compiler.test.NewBean", type.getTypeClass().getName());
assertFalse(builder.hasErrors());
InternalKnowledgePackage bp = builder.getPackage(pkgDescr.getName());
Class newBean = bp.getPackageClassLoader().loadClass("org.drools.compiler.test.NewBean");
assertNotNull(newBean);
}
use of org.drools.compiler.lang.descr.TypeDeclarationDescr in project drools by kiegroup.
the class POJOGenerator method toClassDeclaration.
/**
* @param packageDescr
*/
public static ClassOrInterfaceDeclaration toClassDeclaration(TypeDeclarationDescr typeDeclaration, PackageDescr packageDescr) {
EnumSet<Modifier> classModifiers = EnumSet.of(Modifier.PUBLIC);
String generatedClassName = typeDeclaration.getTypeName();
ClassOrInterfaceDeclaration generatedClass = new ClassOrInterfaceDeclaration(classModifiers, false, generatedClassName);
generatedClass.addImplementedType(GeneratedFact.class.getName());
// Ref: {@link org.drools.core.factmodel.DefaultBeanClassBuilder} by default always receive is Serializable.
generatedClass.addImplementedType(Serializable.class.getName());
boolean hasSuper = typeDeclaration.getSuperTypeName() != null;
if (hasSuper) {
generatedClass.addExtendedType(typeDeclaration.getSuperTypeName());
}
List<AnnotationDescr> softAnnotations = new ArrayList<>();
for (AnnotationDescr ann : typeDeclaration.getAnnotations()) {
final String annFqn = Optional.ofNullable(ann.getFullyQualifiedName()).orElse(Optional.ofNullable(predefinedClassLevelAnnotation.get(ann.getName())).map(Class::getCanonicalName).orElse(null));
if (annFqn != null) {
NormalAnnotationExpr annExpr = generatedClass.addAndGetAnnotation(annFqn);
ann.getValueMap().forEach((k, v) -> annExpr.addPair(k, getAnnotationValue(annFqn, k, v.toString())));
} else {
softAnnotations.add(ann);
}
}
if (softAnnotations.size() > 0) {
String softAnnDictionary = softAnnotations.stream().map(a -> "<dt>" + a.getName() + "</dt><dd>" + a.getValuesAsString() + "</dd>").collect(Collectors.joining());
JavadocComment generatedClassJavadoc = new JavadocComment("<dl>" + softAnnDictionary + "</dl>");
generatedClass.setJavadocComment(generatedClassJavadoc);
}
// No-args ctor
generatedClass.addConstructor(Modifier.PUBLIC);
List<Statement> equalsFieldStatement = new ArrayList<>();
List<Statement> hashCodeFieldStatement = new ArrayList<>();
List<String> toStringFieldStatement = new ArrayList<>();
List<TypeFieldDescr> keyFields = new ArrayList<>();
Collection<TypeFieldDescr> inheritedFields = findInheritedDeclaredFields(typeDeclaration, packageDescr);
Collection<TypeFieldDescr> typeFields = typeDeclaration.getFields().values();
if (!inheritedFields.isEmpty() || !typeDeclaration.getFields().isEmpty()) {
ConstructorDeclaration fullArgumentsCtor = generatedClass.addConstructor(Modifier.PUBLIC);
NodeList<Statement> ctorFieldStatement = NodeList.nodeList();
MethodCallExpr superCall = new MethodCallExpr(null, "super");
for (TypeFieldDescr typeFieldDescr : inheritedFields) {
String fieldName = typeFieldDescr.getFieldName();
addCtorArg(fullArgumentsCtor, typeFieldDescr.getPattern().getObjectType(), fieldName);
superCall.addArgument(fieldName);
if (typeFieldDescr.getAnnotation("key") != null) {
keyFields.add(typeFieldDescr);
}
}
ctorFieldStatement.add(new ExpressionStmt(superCall));
int position = inheritedFields.size();
for (TypeFieldDescr typeFieldDescr : typeFields) {
String fieldName = typeFieldDescr.getFieldName();
Type returnType = addCtorArg(fullArgumentsCtor, typeFieldDescr.getPattern().getObjectType(), fieldName);
FieldDeclaration field = generatedClass.addField(returnType, fieldName, Modifier.PRIVATE);
field.createSetter();
field.addAndGetAnnotation(Position.class.getName()).addPair("value", "" + position++);
MethodDeclaration getter = field.createGetter();
equalsFieldStatement.add(generateEqualsForField(getter, fieldName));
hashCodeFieldStatement.addAll(generateHashCodeForField(getter, fieldName));
ctorFieldStatement.add(replaceFieldName(parseStatement("this.__fieldName = __fieldName;"), fieldName));
toStringFieldStatement.add(format("+ {0}+{1}", quote(fieldName + "="), fieldName));
if (typeFieldDescr.getAnnotation("key") != null) {
keyFields.add(typeFieldDescr);
}
}
fullArgumentsCtor.setBody(new BlockStmt(ctorFieldStatement));
if (!keyFields.isEmpty() && keyFields.size() != inheritedFields.size() + typeFields.size()) {
ConstructorDeclaration keyArgumentsCtor = generatedClass.addConstructor(Modifier.PUBLIC);
NodeList<Statement> ctorKeyFieldStatement = NodeList.nodeList();
MethodCallExpr keySuperCall = new MethodCallExpr(null, "super");
ctorKeyFieldStatement.add(new ExpressionStmt(keySuperCall));
for (TypeFieldDescr typeFieldDescr : keyFields) {
String fieldName = typeFieldDescr.getFieldName();
addCtorArg(keyArgumentsCtor, typeFieldDescr.getPattern().getObjectType(), fieldName);
if (typeDeclaration.getFields().get(fieldName) != null) {
ctorKeyFieldStatement.add(replaceFieldName(parseStatement("this.__fieldName = __fieldName;"), fieldName));
} else {
keySuperCall.addArgument(fieldName);
}
}
keyArgumentsCtor.setBody(new BlockStmt(ctorKeyFieldStatement));
}
if (hasSuper) {
generatedClass.addMember(generateEqualsMethod(generatedClassName, equalsFieldStatement));
generatedClass.addMember(generateHashCodeMethod(hashCodeFieldStatement));
}
}
generatedClass.addMember(generateToStringMethod(generatedClassName, toStringFieldStatement));
return generatedClass;
}
Aggregations