Search in sources :

Example 1 with ATTRIBUTE_CALL

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

the class ModelGenerator method ruleAttributes.

/**
 * Build a list of method calls, representing each needed {@link org.drools.model.impl.RuleBuilder#attribute(org.drools.model.Rule.Attribute, Object)}
 * starting from a drools-compiler {@link RuleDescr}.
 * The tuple represent the Rule Attribute expressed in JavParser form, and the attribute value expressed in JavaParser form.
 */
private static List<MethodCallExpr> ruleAttributes(RuleContext context, RuleDescr ruleDescr) {
    final List<MethodCallExpr> ruleAttributes = new ArrayList<>();
    final Set<Entry<String, AttributeDescr>> excludingDialect = ruleDescr.getAttributes().entrySet().stream().filter(r -> !r.getKey().equals("dialect")).collect(Collectors.toSet());
    for (Entry<String, AttributeDescr> as : excludingDialect) {
        MethodCallExpr attributeCall = new MethodCallExpr(null, ATTRIBUTE_CALL);
        attributeCall.addArgument(attributesMap.get(as.getKey()));
        String value = as.getValue().getValue().trim();
        switch(as.getKey()) {
            case "salience":
                try {
                    Integer.parseInt(value);
                    attributeCall.addArgument(value);
                } catch (NumberFormatException nfe) {
                    addDynamicAttributeArgument(context, attributeCall, value, int.class);
                }
                break;
            case "enabled":
                if (value.equalsIgnoreCase("true") || value.equalsIgnoreCase("false")) {
                    attributeCall.addArgument(value.toLowerCase());
                } else {
                    addDynamicAttributeArgument(context, attributeCall, value, boolean.class);
                }
                break;
            case "no-loop":
            case "auto-focus":
            case "lock-on-active":
                attributeCall.addArgument(value);
                break;
            case "agenda-group":
            case "activation-group":
            case "ruleflow-group":
            case "duration":
                attributeCall.addArgument(toStringLiteral(value));
                break;
            case "timer":
                if (validateTimer(value)) {
                    attributeCall.addArgument(toStringLiteral(value));
                } else {
                    context.addCompilationError(new InvalidExpressionErrorResult(value));
                }
                break;
            case "calendars":
                if (value.startsWith("[")) {
                    value = value.substring(1, value.length() - 1).trim();
                }
                Expression arrayExpr = parseExpression("new String[] { " + value + " }");
                attributeCall.addArgument(arrayExpr);
                break;
            case "date-effective":
            case "date-expires":
                attributeCall.addArgument(parseExpression(String.format("java.util.GregorianCalendar.from(java.time.LocalDate.parse(\"%s\", " + DATE_TIME_FORMATTER_FIELD + ").atStartOfDay(java.time.ZoneId.systemDefault()))", as.getValue().getValue())));
                break;
            default:
                throw new UnsupportedOperationException("Unhandled case for rule attribute: " + as.getKey());
        }
        ruleAttributes.add(attributeCall);
    }
    return ruleAttributes;
}
Also used : ClassExpr(com.github.javaparser.ast.expr.ClassExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) AnnotationDescr(org.drools.drl.ast.descr.AnnotationDescr) TypeResolver(org.drools.core.addon.TypeResolver) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) BehaviorDescr(org.drools.drl.ast.descr.BehaviorDescr) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) ExpressionTyperContext(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyperContext) ClassUtil.asJavaSourceName(org.drools.modelcompiler.util.ClassUtil.asJavaSourceName) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) ParseExpressionErrorResult(org.drools.modelcompiler.builder.errors.ParseExpressionErrorResult) Type(com.github.javaparser.ast.type.Type) DrlxParseUtil.generateLambdaWithoutParameters(org.drools.modelcompiler.builder.generator.DrlxParseUtil.generateLambdaWithoutParameters) Map(java.util.Map) Expression(com.github.javaparser.ast.expr.Expression) BUILD_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.BUILD_CALL) DEFAULT_RULE_UNIT(org.drools.kiesession.session.StatefulKnowledgeSessionImpl.DEFAULT_RULE_UNIT) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) PackageModel(org.drools.modelcompiler.builder.PackageModel) NodeList(com.github.javaparser.ast.NodeList) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) Set(java.util.Set) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) Collectors(java.util.stream.Collectors) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) TimerUtil.validateTimer(org.drools.modelcompiler.util.TimerUtil.validateTimer) Modifier(com.github.javaparser.ast.Modifier) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) CoreComponentsBuilder(org.drools.core.base.CoreComponentsBuilder) METADATA_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.METADATA_CALL) List(java.util.List) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) AnnotationDefinition(org.drools.core.factmodel.AnnotationDefinition) Entry(java.util.Map.Entry) Optional(java.util.Optional) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) ATTRIBUTE_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.ATTRIBUTE_CALL) DrlxParseUtil.toStringLiteral(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toStringLiteral) DOMAIN_CLASS_METADATA_INSTANCE(org.drools.modelcompiler.builder.PackageModel.DOMAIN_CLASS_METADATA_INSTANCE) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) HashMap(java.util.HashMap) Behavior(org.drools.core.rule.Behavior) ENTRY_POINT_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.ENTRY_POINT_CALL) QueryDescr(org.drools.drl.ast.descr.QueryDescr) RuleUnitDescription(org.kie.internal.ruleunit.RuleUnitDescription) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) WINDOW_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.WINDOW_CALL) DslMethodNames.createDslTopLevelMethod(org.drools.modelcompiler.builder.generator.DslMethodNames.createDslTopLevelMethod) AndDescr(org.drools.drl.ast.descr.AndDescr) DATE_TIME_FORMATTER_FIELD(org.drools.modelcompiler.builder.PackageModel.DATE_TIME_FORMATTER_FIELD) Variable(org.drools.model.Variable) PackageDescr(org.drools.drl.ast.descr.PackageDescr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) SUPPLY_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.SUPPLY_CALL) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) DECLARATION_OF_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.DECLARATION_OF_CALL) DOMAIN_CLASSESS_METADATA_FILE_NAME(org.drools.modelcompiler.builder.PackageModel.DOMAIN_CLASSESS_METADATA_FILE_NAME) StringUtil.toId(org.drools.modelcompiler.util.StringUtil.toId) TimeUtils(org.drools.core.time.TimeUtils) UnknownDeclarationError(org.drools.modelcompiler.builder.errors.UnknownDeclarationError) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) DrlxParseUtil.classToReferenceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.classToReferenceType) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Rule(org.drools.model.Rule) RULE_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.RULE_CALL) UNIT_CALL(org.drools.modelcompiler.builder.generator.DslMethodNames.UNIT_CALL) ArrayList(java.util.ArrayList) Entry(java.util.Map.Entry) InvalidExpressionErrorResult(org.drools.modelcompiler.builder.errors.InvalidExpressionErrorResult) Expression(com.github.javaparser.ast.expr.Expression) StaticJavaParser.parseExpression(com.github.javaparser.StaticJavaParser.parseExpression) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Aggregations

StaticJavaParser.parseExpression (com.github.javaparser.StaticJavaParser.parseExpression)1 Modifier (com.github.javaparser.ast.Modifier)1 NodeList (com.github.javaparser.ast.NodeList)1 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)1 ClassExpr (com.github.javaparser.ast.expr.ClassExpr)1 Expression (com.github.javaparser.ast.expr.Expression)1 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)1 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)1 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)1 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)1 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)1 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)1 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)1 Type (com.github.javaparser.ast.type.Type)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1