Search in sources :

Example 1 with DrlxParseUtil

use of org.drools.modelcompiler.builder.generator.DrlxParseUtil in project drools by kiegroup.

the class PackageModel method generateRulesInUnit.

private void generateRulesInUnit(String ruleUnitName, BlockStmt rulesListInitializerBody, RuleSourceResult results, ClassOrInterfaceDeclaration rulesClass) {
    results.withModel(name + "." + ruleUnitName, name + "." + rulesClass.getNameAsString());
    Collection<MethodDeclaration> ruleMethodsInUnit = ofNullable(ruleMethods.get(ruleUnitName)).map(Map::values).orElse(null);
    if (ruleMethodsInUnit == null || ruleMethodsInUnit.isEmpty()) {
        BodyDeclaration<?> getQueriesMethod = parseBodyDeclaration("    @Override\n" + "    public java.util.List<org.drools.model.Rule> getRules() {\n" + "        return java.util.Collections.emptyList();\n" + "    }\n");
        rulesClass.addMember(getQueriesMethod);
        return;
    }
    if (!ruleUnitName.equals(DEFAULT_RULE_UNIT)) {
        BodyDeclaration<?> modelNameMethod = parseBodyDeclaration("    @Override\n" + "    public String getName() {\n" + "        return super.getName() + \"." + ruleUnitName + "\";\n" + "    }\n");
        rulesClass.addMember(modelNameMethod);
        BodyDeclaration<?> modelPackageNameMethod = parseBodyDeclaration("    @Override\n" + "    public String getPackageName() {\n" + "        return super.getName();\n" + "    }\n");
        rulesClass.addMember(modelPackageNameMethod);
    }
    createAndAddGetRulesMethod(rulesClass);
    int ruleCount = ruleMethodsInUnit.size();
    boolean requiresMultipleRulesLists = ruleCount >= RULES_DECLARATION_PER_CLASS - 1;
    boolean parallelRulesLoad = ruleCount >= (RULES_DECLARATION_PER_CLASS * 3 - 1);
    MethodCallExpr parallelRulesGetter = null;
    MethodCallExpr rules = buildRulesField(rulesClass);
    if (requiresMultipleRulesLists) {
        rulesClass.addImplementedType(RulesSupplier.class);
        if (parallelRulesLoad) {
            parallelRulesGetter = new MethodCallExpr(new NameExpr(RulesSupplier.class.getCanonicalName()), "getRules");
            parallelRulesGetter.addArgument(new ThisExpr());
            rulesListInitializerBody.addStatement(new AssignExpr(new NameExpr("this.rules"), parallelRulesGetter, AssignExpr.Operator.ASSIGN));
        } else {
            MethodCallExpr add = new MethodCallExpr(new NameExpr("rules"), "addAll");
            add.addArgument("getRulesList()");
            rulesListInitializerBody.addStatement(add);
        }
    }
    ruleMethodsInUnit.parallelStream().forEach(DrlxParseUtil::transformDrlNameExprToNameExpr);
    int maxLength = ruleMethodsInUnit.parallelStream().map(MethodDeclaration::toString).mapToInt(String::length).max().orElse(1);
    int rulesPerClass = oneClassPerRule ? 1 : Math.max(50000 / maxLength, 1);
    // each method per Drlx parser result
    int count = -1;
    Map<Integer, ClassOrInterfaceDeclaration> splitted = new LinkedHashMap<>();
    for (MethodDeclaration ruleMethod : ruleMethodsInUnit) {
        String methodName = ruleMethod.getNameAsString();
        ClassOrInterfaceDeclaration rulesMethodClass = splitted.computeIfAbsent(++count / rulesPerClass, i -> {
            String className = rulesClass.getNameAsString() + (oneClassPerRule ? "_" + methodName : "RuleMethods" + i);
            return createClass(className, results);
        });
        rulesMethodClass.addMember(ruleMethod);
        if (count % RULES_DECLARATION_PER_CLASS == RULES_DECLARATION_PER_CLASS - 1) {
            int index = count / RULES_DECLARATION_PER_CLASS;
            rules = buildRulesField(results, index);
            ObjectCreationExpr newObject = new ObjectCreationExpr(null, toClassOrInterfaceType(rulesFileName + "Rules" + index), NodeList.nodeList());
            if (parallelRulesLoad) {
                parallelRulesGetter.addArgument(newObject);
            } else {
                MethodCallExpr add = new MethodCallExpr(new NameExpr("rules"), "addAll");
                add.addArgument(new MethodCallExpr(newObject, "getRulesList"));
                rulesListInitializerBody.addStatement(add);
            }
        }
        // manage in main class init block:
        rules.addArgument(new MethodCallExpr(new NameExpr(rulesMethodClass.getNameAsString()), methodName));
    }
    BodyDeclaration<?> rulesList = requiresMultipleRulesLists ? parseBodyDeclaration("java.util.List<org.drools.model.Rule> rules = new java.util.ArrayList<>(" + ruleCount + ");") : parseBodyDeclaration("java.util.List<org.drools.model.Rule> rules = getRulesList();");
    rulesClass.addMember(rulesList);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) NameExpr(com.github.javaparser.ast.expr.NameExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) LinkedHashMap(java.util.LinkedHashMap) RulesSupplier(org.drools.model.RulesSupplier) DrlxParseUtil(org.drools.modelcompiler.builder.generator.DrlxParseUtil) ThisExpr(com.github.javaparser.ast.expr.ThisExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)1 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 NameExpr (com.github.javaparser.ast.expr.NameExpr)1 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)1 ThisExpr (com.github.javaparser.ast.expr.ThisExpr)1 LinkedHashMap (java.util.LinkedHashMap)1 RulesSupplier (org.drools.model.RulesSupplier)1 DrlxParseUtil (org.drools.modelcompiler.builder.generator.DrlxParseUtil)1