use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project drools by kiegroup.
the class QueryDefImplGenerator method generate.
public CompilationUnit generate() {
CompilationUnit cu = new CompilationUnit("org.drools.model.impl");
cu.setImports(nodeList(parseImport("import org.drools.model.Argument;"), parseImport(String.format("import org.drools.model.Query%dDef;", arity)), parseImport("import org.drools.model.Variable;"), parseImport("import org.drools.model.view.QueryCallViewItem;"), parseImport("import org.drools.model.view.QueryCallViewItemImpl;"), parseImport("import static org.drools.model.FlowDSL.declarationOf;"), parseImport("import static org.drools.model.impl.RuleBuilder.DEFAULT_PACKAGE;")));
ClassOrInterfaceDeclaration clazz = classDeclaration(cu);
nameClassConstructor(clazz);
packageNameClassConstructor(clazz);
nameClassArgConstructor(clazz);
pkgNameClassArgConstructor(clazz);
callMethod(clazz);
getArgumentsMethod(clazz);
getters(clazz);
generateEquals(clazz);
return cu;
}
use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project drools by kiegroup.
the class QueryDefImplGenerator method generateEquals.
private void generateEquals(ClassOrInterfaceDeclaration clazz) {
String template = " @Override\n" + " public boolean isEqualTo( ModelComponent other ) {\n" + " if ( this == other ) return true;\n" + " if ( !(other instanceof DEF_IMPL_TYPE) ) return false;\n" + "\n" + " DEF_IMPL_TYPE that = (DEF_IMPL_TYPE) other;\n" + "\n" + " return EQUALS_CALL;\n" + " }";
BodyDeclaration<?> parse = parseBodyDeclaration(template);
Expression andExpr = new BooleanLiteralExpr(true);
for (int i : rangeArity().toArray()) {
String argWithIndex = argIndex(i);
Expression e = parseExpression(String.format("ModelComponent.areEqualInModel( %s, that.%s )", argWithIndex, argWithIndex));
andExpr = new BinaryExpr(andExpr, e, BinaryExpr.Operator.AND);
}
final Expression finalAndExpr = andExpr;
parse.findAll(ClassOrInterfaceType.class, n -> n.toString().equals("DEF_IMPL_TYPE")).forEach(s -> s.replace(parseType("Query" + arity + "DefImpl")));
parse.findAll(NameExpr.class, n -> n.toString().equals("EQUALS_CALL")).forEach(s -> s.replace(finalAndExpr));
clazz.addMember(parse);
}
use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project drools by kiegroup.
the class QueryDefGenerator method generate.
public CompilationUnit generate() {
CompilationUnit cu = new CompilationUnit("org.drools.model");
cu.setImports(nodeList(parseImport("import org.drools.model.view.QueryCallViewItem;")));
ClassOrInterfaceDeclaration clazz = classDeclaration(cu);
callMethod(clazz);
callMethodInterface(clazz);
getters(clazz);
return cu;
}
use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project drools by kiegroup.
the class PackageModel method buildRulesField.
private MethodCallExpr buildRulesField(RuleSourceResult results, int index) {
CompilationUnit cu = new CompilationUnit();
results.withClass(cu);
cu.setPackageDeclaration(name);
cu.getImports().add(new ImportDeclaration(new Name(Arrays.class.getCanonicalName()), false, false));
cu.getImports().add(new ImportDeclaration(new Name(List.class.getCanonicalName()), false, false));
cu.getImports().add(new ImportDeclaration(new Name(Rule.class.getCanonicalName()), false, false));
String currentRulesMethodClassName = rulesFileName + "Rules" + index;
ClassOrInterfaceDeclaration rulesClass = cu.addClass(currentRulesMethodClassName);
rulesClass.addImplementedType(RulesSupplier.class);
return buildRulesField(rulesClass);
}
use of com.github.javaparser.ast.body.ClassOrInterfaceDeclaration in project drools by kiegroup.
the class PackageModel method getRulesSource.
public RuleSourceResult getRulesSource() {
boolean hasRuleUnit = !ruleUnits.isEmpty();
CompilationUnit cu = new CompilationUnit();
cu.setPackageDeclaration(name);
manageImportForCompilationUnit(cu);
ClassOrInterfaceDeclaration rulesClass = cu.addClass(rulesFileName);
rulesClass.addImplementedType(Model.class.getCanonicalName());
if (hasRuleUnit) {
rulesClass.addModifier(Modifier.Keyword.ABSTRACT);
}
BodyDeclaration<?> dateFormatter = parseBodyDeclaration("public final static java.time.format.DateTimeFormatter " + DATE_TIME_FORMATTER_FIELD + " = new java.time.format.DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern(org.drools.core.util.DateUtils.getDateFormatMask()).toFormatter(java.util.Locale.ENGLISH);\n");
rulesClass.addMember(dateFormatter);
BodyDeclaration<?> getNameMethod = parseBodyDeclaration(" @Override\n" + " public String getName() {\n" + " return \"" + name + "\";\n" + " }\n");
rulesClass.addMember(getNameMethod);
String entryPointsBuilder = entryPoints.isEmpty() ? "java.util.Collections.emptyList()" : "java.util.Arrays.asList(D.entryPoint(\"" + entryPoints.stream().collect(joining("\"), D.entryPoint(\"")) + "\"))";
BodyDeclaration<?> getEntryPointsMethod = parseBodyDeclaration(" @Override\n" + " public java.util.List<org.drools.model.EntryPoint> getEntryPoints() {\n" + " return " + entryPointsBuilder + ";\n" + " }\n");
rulesClass.addMember(getEntryPointsMethod);
BodyDeclaration<?> getGlobalsMethod = parseBodyDeclaration(" @Override\n" + " public java.util.List<org.drools.model.Global> getGlobals() {\n" + " return globals;\n" + " }\n");
rulesClass.addMember(getGlobalsMethod);
BodyDeclaration<?> getTypeMetaDataMethod = parseBodyDeclaration(" @Override\n" + " public java.util.List<org.drools.model.TypeMetaData> getTypeMetaDatas() {\n" + " return typeMetaDatas;\n" + " }\n");
rulesClass.addMember(getTypeMetaDataMethod);
for (Map.Entry<String, TypedExpression> dateField : dateFields.entrySet()) {
FieldDeclaration f = rulesClass.addField(dateField.getValue().getJPType(), dateField.getKey(), publicModifier().getKeyword(), staticModifier().getKeyword(), finalModifier().getKeyword());
f.getVariables().get(0).setInitializer(dateField.getValue().getExpression());
}
for (Map.Entry<String, MethodCallExpr> windowReference : windowReferences.entrySet()) {
FieldDeclaration f = rulesClass.addField(WINDOW_REFERENCE_TYPE, windowReference.getKey(), publicModifier().getKeyword(), staticModifier().getKeyword(), finalModifier().getKeyword());
f.getVariables().get(0).setInitializer(windowReference.getValue());
}
for (Map.Entry<String, Class<?>> g : getGlobals().entrySet()) {
addGlobalField(rulesClass, getName(), g.getKey(), g.getValue());
}
for (Map.Entry<String, QueryGenerator.QueryDefWithType> queryDef : queryDefWithType.entrySet()) {
FieldDeclaration field = rulesClass.addField(queryDef.getValue().getQueryType(), queryDef.getKey(), publicModifier().getKeyword(), staticModifier().getKeyword(), finalModifier().getKeyword());
field.getVariables().get(0).setInitializer(queryDef.getValue().getMethodCallExpr());
}
// instance initializer block.
// add to `rules` list the result of invoking each method for rule
InitializerDeclaration rulesListInitializer = new InitializerDeclaration();
BlockStmt rulesListInitializerBody = new BlockStmt();
rulesListInitializer.setBody(rulesListInitializerBody);
buildArtifactsDeclaration(getGlobals().keySet(), rulesClass, rulesListInitializerBody, "org.drools.model.Global", "globals", true);
if (!typeMetaDataExpressions.isEmpty()) {
BodyDeclaration<?> typeMetaDatasList = parseBodyDeclaration("java.util.List<org.drools.model.TypeMetaData> typeMetaDatas = new java.util.ArrayList<>();");
rulesClass.addMember(typeMetaDatasList);
for (Expression expr : typeMetaDataExpressions) {
addInitStatement(rulesListInitializerBody, expr, "typeMetaDatas");
}
} else {
BodyDeclaration<?> typeMetaDatasList = parseBodyDeclaration("java.util.List<org.drools.model.TypeMetaData> typeMetaDatas = java.util.Collections.emptyList();");
rulesClass.addMember(typeMetaDatasList);
}
functions.forEach(rulesClass::addMember);
RuleSourceResult results = new RuleSourceResult(cu);
if (hasRuleUnit) {
ruleMethods.keySet().forEach(unitName -> {
String className = rulesFileName + "_" + unitName;
ClassOrInterfaceDeclaration unitClass = createClass(className, results);
unitClass.addExtendedType(rulesFileName);
InitializerDeclaration unitInitializer = new InitializerDeclaration();
BlockStmt unitInitializerBody = new BlockStmt();
unitInitializer.setBody(unitInitializerBody);
generateRulesInUnit(unitName, unitInitializerBody, results, unitClass);
Set<QueryModel> queries = queriesByRuleUnit.get(unitName);
Collection<String> queryNames = queries == null ? Collections.emptyList() : queries.stream().map(QueryModel::getName).map(name -> QUERY_METHOD_PREFIX + name).collect(toList());
Collection<MethodDeclaration> queryImpls = queryNames.stream().map(queryMethods::get).collect(toList());
generateQueriesInUnit(unitClass, unitInitializerBody, queryNames, queryImpls);
if (!unitInitializerBody.getStatements().isEmpty()) {
unitClass.addMember(unitInitializer);
}
});
} else {
generateRulesInUnit(DEFAULT_RULE_UNIT, rulesListInitializerBody, results, rulesClass);
generateQueriesInUnit(rulesClass, rulesListInitializerBody, queryMethods.keySet(), queryMethods.values());
}
if (!rulesListInitializerBody.getStatements().isEmpty()) {
rulesClass.addMember(rulesListInitializer);
}
return results;
}
Aggregations