Search in sources :

Example 11 with StringLiteralExpr

use of org.drools.javaparser.ast.expr.StringLiteralExpr in project drools by kiegroup.

the class ToTypedExpressionTest method pointFreeTest.

@Test
public void pointFreeTest() {
    final PointFreeExpr expression = new PointFreeExpr(null, new NameExpr("name"), NodeList.nodeList(new StringLiteralExpr("[A-Z]")), new SimpleName("matches"), false, null, null, null, null);
    TypedExpressionResult typedExpressionResult = new ExpressionTyper(ruleContext, Person.class, null, true).toTypedExpression(expression);
    final TypedExpression actual = typedExpressionResult.getTypedExpression().get();
    final TypedExpression expected = typedResult("eval(org.drools.model.operators.MatchesOperator.INSTANCE, _this.getName(), \"[A-Z]\")", String.class);
    assertEquals(expected, actual);
}
Also used : SimpleName(org.drools.javaparser.ast.expr.SimpleName) PointFreeExpr(org.drools.javaparser.ast.drlx.expr.PointFreeExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) ExpressionTyper(org.drools.modelcompiler.builder.generator.expressiontyper.ExpressionTyper) TypedExpressionResult(org.drools.modelcompiler.builder.generator.expressiontyper.TypedExpressionResult) Person(org.drools.modelcompiler.domain.Person) Test(org.junit.Test)

Example 12 with StringLiteralExpr

use of org.drools.javaparser.ast.expr.StringLiteralExpr in project drools by kiegroup.

the class Consequence method rewriteRHS.

private boolean rewriteRHS(BlockStmt ruleBlock, BlockStmt rhs) {
    AtomicBoolean requireDrools = new AtomicBoolean(false);
    List<MethodCallExpr> methodCallExprs = rhs.findAll(MethodCallExpr.class);
    List<MethodCallExpr> updateExprs = new ArrayList<>();
    for (MethodCallExpr methodCallExpr : methodCallExprs) {
        if (isDroolsMethod(methodCallExpr)) {
            if (!methodCallExpr.getScope().isPresent()) {
                methodCallExpr.setScope(new NameExpr("drools"));
            }
            if (knowledgeHelperMethods.contains(methodCallExpr.getNameAsString())) {
                methodCallExpr.setScope(asKnoledgeHelperExpression);
            } else if (methodCallExpr.getNameAsString().equals("update")) {
                updateExprs.add(methodCallExpr);
            } else if (methodCallExpr.getNameAsString().equals("retract")) {
                methodCallExpr.setName(new SimpleName("delete"));
            }
            requireDrools.set(true);
        }
    }
    for (MethodCallExpr updateExpr : updateExprs) {
        Expression argExpr = updateExpr.getArgument(0);
        if (argExpr instanceof NameExpr) {
            String updatedVar = ((NameExpr) argExpr).getNameAsString();
            Class<?> updatedClass = context.getDeclarationById(updatedVar).map(DeclarationSpec::getDeclarationClass).orElseThrow(RuntimeException::new);
            MethodCallExpr bitMaskCreation = new MethodCallExpr(new NameExpr(BitMask.class.getCanonicalName()), "getPatternMask");
            bitMaskCreation.addArgument(new ClassExpr(JavaParser.parseClassOrInterfaceType(updatedClass.getCanonicalName())));
            methodCallExprs.subList(0, methodCallExprs.indexOf(updateExpr)).stream().filter(mce -> mce.getScope().isPresent() && hasScope(mce, updatedVar)).map(mce -> ClassUtils.setter2property(mce.getNameAsString())).filter(Objects::nonNull).distinct().forEach(s -> bitMaskCreation.addArgument(new StringLiteralExpr(s)));
            VariableDeclarationExpr bitMaskVar = new VariableDeclarationExpr(BITMASK_TYPE, "mask_" + updatedVar, Modifier.FINAL);
            AssignExpr bitMaskAssign = new AssignExpr(bitMaskVar, bitMaskCreation, AssignExpr.Operator.ASSIGN);
            ruleBlock.addStatement(bitMaskAssign);
            updateExpr.addArgument("mask_" + updatedVar);
        }
    }
    return requireDrools.get();
}
Also used : BitMask(org.drools.model.BitMask) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Expression(org.drools.javaparser.ast.expr.Expression) DrlxParseUtil.isNameExprWithName(org.drools.modelcompiler.builder.generator.DrlxParseUtil.isNameExprWithName) DroolsImpl(org.drools.modelcompiler.consequence.DroolsImpl) JavaParser(org.drools.javaparser.JavaParser) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) BREAKING_CALL(org.drools.modelcompiler.builder.generator.visitor.NamedConsequenceVisitor.BREAKING_CALL) LambdaExpr(org.drools.javaparser.ast.expr.LambdaExpr) JavaParser.parseExpression(org.drools.javaparser.JavaParser.parseExpression) Statement(org.drools.javaparser.ast.stmt.Statement) Collectors.toSet(java.util.stream.Collectors.toSet) PackageModel(org.drools.modelcompiler.builder.PackageModel) ObjectCreationExpr(org.drools.javaparser.ast.expr.ObjectCreationExpr) DrlxParseUtil.hasScope(org.drools.modelcompiler.builder.generator.DrlxParseUtil.hasScope) Parameter(org.drools.javaparser.ast.body.Parameter) ClassUtils(org.drools.core.util.ClassUtils) Collection(java.util.Collection) DrlxParseUtil.parseBlock(org.drools.modelcompiler.builder.generator.DrlxParseUtil.parseBlock) Set(java.util.Set) EmptyStmt(org.drools.javaparser.ast.stmt.EmptyStmt) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) NameExpr(org.drools.javaparser.ast.expr.NameExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) Objects(java.util.Objects) VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) UnknownType(org.drools.javaparser.ast.type.UnknownType) DrlxParseUtil.findAllChildrenRecursive(org.drools.modelcompiler.builder.generator.DrlxParseUtil.findAllChildrenRecursive) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) Modifier(org.drools.javaparser.ast.Modifier) SimpleName(org.drools.javaparser.ast.expr.SimpleName) StringUtils(org.drools.core.util.StringUtils) VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) SimpleName(org.drools.javaparser.ast.expr.SimpleName) ArrayList(java.util.ArrayList) NameExpr(org.drools.javaparser.ast.expr.NameExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Expression(org.drools.javaparser.ast.expr.Expression) JavaParser.parseExpression(org.drools.javaparser.JavaParser.parseExpression) Objects(java.util.Objects) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 13 with StringLiteralExpr

use of org.drools.javaparser.ast.expr.StringLiteralExpr in project drools by kiegroup.

the class ModelGenerator method addUnitData.

private static void addUnitData(String unitVar, Class<?> type, BlockStmt ruleBlock) {
    Type declType = classToReferenceType(type);
    ClassOrInterfaceType varType = JavaParser.parseClassOrInterfaceType(UnitData.class.getCanonicalName());
    varType.setTypeArguments(declType);
    VariableDeclarationExpr var_ = new VariableDeclarationExpr(varType, toVar(unitVar), Modifier.FINAL);
    MethodCallExpr unitDataCall = new MethodCallExpr(null, UNIT_DATA_CALL);
    unitDataCall.addArgument(new ClassExpr(declType));
    unitDataCall.addArgument(new StringLiteralExpr(unitVar));
    AssignExpr var_assign = new AssignExpr(var_, unitDataCall, AssignExpr.Operator.ASSIGN);
    ruleBlock.addStatement(var_assign);
}
Also used : ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) Type(org.drools.javaparser.ast.type.Type) DrlxParseUtil.classToReferenceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.classToReferenceType) VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) UnitData(org.drools.model.UnitData) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr)

Example 14 with StringLiteralExpr

use of org.drools.javaparser.ast.expr.StringLiteralExpr 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) {
    List<MethodCallExpr> ruleAttributes = new ArrayList<>();
    for (Entry<String, AttributeDescr> as : ruleDescr.getAttributes().entrySet()) {
        // dialect=mvel is not an attribute of DSL expr(), so we check it before.
        if (as.getKey().equals("dialect")) {
            if (as.getValue().getValue().equals("mvel")) {
                context.setRuleDialect(RuleDialect.MVEL);
            }
            continue;
        }
        MethodCallExpr attributeCall = new MethodCallExpr(null, ATTRIBUTE_CALL);
        attributeCall.addArgument(attributesMap.get(as.getKey()));
        switch(as.getKey()) {
            case "dialect":
                throw new RuntimeException("should not have reached this part of the code");
            case "no-loop":
            case "salience":
            case "enabled":
            case "auto-focus":
            case "lock-on-active":
                attributeCall.addArgument(parseExpression(as.getValue().getValue()));
                break;
            case "agenda-group":
            case "activation-group":
            case "ruleflow-group":
            case "duration":
            case "timer":
                attributeCall.addArgument(new StringLiteralExpr(as.getValue().getValue()));
                break;
            case "calendars":
                String value = as.getValue().getValue().trim();
                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("GregorianCalendar.from(LocalDate.parse(\"%s\", dateTimeFormatter).atStartOfDay(ZoneId.systemDefault()))", as.getValue().getValue())));
                break;
            default:
                throw new UnsupportedOperationException("Unhandled case for rule attribute: " + as.getKey());
        }
        ruleAttributes.add(attributeCall);
    }
    return ruleAttributes;
}
Also used : Expression(org.drools.javaparser.ast.expr.Expression) JavaParser.parseExpression(org.drools.javaparser.JavaParser.parseExpression) ArrayList(java.util.ArrayList) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 15 with StringLiteralExpr

use of org.drools.javaparser.ast.expr.StringLiteralExpr in project drools by kiegroup.

the class ModelGenerator method objectAsJPExpression.

private static Expression objectAsJPExpression(Object annValue) {
    if (annValue instanceof String) {
        StringLiteralExpr aStringLiteral = new StringLiteralExpr();
        // use the setter method in order for the string literal be properly escaped.
        aStringLiteral.setString(annValue.toString());
        return aStringLiteral;
    } else if (annValue instanceof Number) {
        return parseExpression(annValue.toString());
    } else if (annValue instanceof Map) {
        throw new UnsupportedOperationException("cannot define a canonical representation for a java.util.Map yet.");
    } else {
        throw new UnsupportedOperationException("I was unable to define a canonical String representation to give to JP yet about: " + annValue);
    }
}
Also used : StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)21 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)19 ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)6 Expression (org.drools.javaparser.ast.expr.Expression)6 NameExpr (org.drools.javaparser.ast.expr.NameExpr)5 ArrayList (java.util.ArrayList)4 AssignExpr (org.drools.javaparser.ast.expr.AssignExpr)4 BinaryExpr (org.drools.javaparser.ast.expr.BinaryExpr)4 ClassOrInterfaceType (org.drools.javaparser.ast.type.ClassOrInterfaceType)4 HashMap (java.util.HashMap)3 JavaParser.parseExpression (org.drools.javaparser.JavaParser.parseExpression)3 VariableDeclarationExpr (org.drools.javaparser.ast.expr.VariableDeclarationExpr)3 List (java.util.List)2 Map (java.util.Map)2 AnnotationDescr (org.drools.compiler.lang.descr.AnnotationDescr)2 MethodDeclaration (org.drools.javaparser.ast.body.MethodDeclaration)2 PointFreeExpr (org.drools.javaparser.ast.drlx.expr.PointFreeExpr)2 CastExpr (org.drools.javaparser.ast.expr.CastExpr)2 EnclosedExpr (org.drools.javaparser.ast.expr.EnclosedExpr)2 IntegerLiteralExpr (org.drools.javaparser.ast.expr.IntegerLiteralExpr)2