Search in sources :

Example 46 with MethodCallExpr

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

the class ModelGenerator method addVariable.

private static void addVariable(KnowledgeBuilderImpl kbuilder, BlockStmt ruleBlock, DeclarationSpec decl) {
    if (decl.getDeclarationClass() == null) {
        kbuilder.addBuilderResult(new UnknownDeclarationError(decl.getBindingId()));
        return;
    }
    Type declType = classToReferenceType(decl.getDeclarationClass());
    ClassOrInterfaceType varType = JavaParser.parseClassOrInterfaceType(Variable.class.getCanonicalName());
    varType.setTypeArguments(declType);
    VariableDeclarationExpr var_ = new VariableDeclarationExpr(varType, toVar(decl.getBindingId()), Modifier.FINAL);
    MethodCallExpr declarationOfCall = new MethodCallExpr(null, DECLARATION_OF_CALL);
    declarationOfCall.addArgument(new ClassExpr(decl.getType()));
    declarationOfCall.addArgument(new StringLiteralExpr(decl.getVariableName().orElse(decl.getBindingId())));
    decl.getDeclarationSource().ifPresent(declarationOfCall::addArgument);
    decl.getEntryPoint().ifPresent(ep -> {
        MethodCallExpr entryPointCall = new MethodCallExpr(null, "entryPoint");
        entryPointCall.addArgument(new StringLiteralExpr(ep));
        declarationOfCall.addArgument(entryPointCall);
    });
    for (BehaviorDescr behaviorDescr : decl.getBehaviors()) {
        MethodCallExpr windowCall = new MethodCallExpr(null, "window");
        if (Behavior.BehaviorType.TIME_WINDOW.matches(behaviorDescr.getSubType())) {
            windowCall.addArgument("Window.Type.TIME");
            windowCall.addArgument("" + TimeUtils.parseTimeString(behaviorDescr.getParameters().get(0)));
        }
        if (Behavior.BehaviorType.LENGTH_WINDOW.matches(behaviorDescr.getSubType())) {
            windowCall.addArgument("Window.Type.LENGTH");
            windowCall.addArgument("" + Integer.valueOf(behaviorDescr.getParameters().get(0)));
        }
        declarationOfCall.addArgument(windowCall);
    }
    AssignExpr var_assign = new AssignExpr(var_, declarationOfCall, 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) Variable(org.drools.model.Variable) BehaviorDescr(org.drools.compiler.lang.descr.BehaviorDescr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) UnknownDeclarationError(org.drools.modelcompiler.builder.errors.UnknownDeclarationError) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr)

Example 47 with MethodCallExpr

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

the class ConstraintParser method compareBigDecimal.

public static Expression compareBigDecimal(BinaryExpr.Operator operator, TypedExpression left, TypedExpression right) {
    left.setExpression(convertExpressionToBigDecimal(left));
    right.setExpression(convertExpressionToBigDecimal(right));
    final MethodCallExpr methodCallExpr = new MethodCallExpr(left.getExpression(), "compareTo");
    methodCallExpr.addArgument(right.getExpression());
    return new BinaryExpr(methodCallExpr, new IntegerLiteralExpr(0), operator);
}
Also used : IntegerLiteralExpr(org.drools.javaparser.ast.expr.IntegerLiteralExpr) BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 48 with MethodCallExpr

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

the class ConstraintParser method handleSpecialComparisonCases.

private static Expression handleSpecialComparisonCases(BinaryExpr.Operator operator, TypedExpression left, TypedExpression right) {
    if (isComparisonOperator(operator)) {
        MethodCallExpr compareMethod = null;
        if (left.getType() == String.class && right.getType() == String.class) {
            compareMethod = new MethodCallExpr(null, "org.drools.modelcompiler.util.EvaluationUtil.compareStringsAsNumbers");
        } else if (Comparable.class.isAssignableFrom(left.getType()) && Comparable.class.isAssignableFrom(right.getType())) {
            compareMethod = new MethodCallExpr(null, "org.drools.modelcompiler.util.EvaluationUtil.compare");
        }
        if (compareMethod != null) {
            compareMethod.addArgument(left.getExpression());
            compareMethod.addArgument(right.getExpression());
            compareMethod.addArgument(new StringLiteralExpr(operator.asString()));
            return compareMethod;
        }
    }
    if (isAnyOperandBigDecimal(left, right) && (isComparisonOperator(operator))) {
        return compareBigDecimal(operator, left, right);
    }
    return new BinaryExpr(left.getExpression(), right.getExpression(), operator);
}
Also used : BinaryExpr(org.drools.javaparser.ast.expr.BinaryExpr) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 49 with MethodCallExpr

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

the class QueryGenerator method processQueryDef.

public static void processQueryDef(KnowledgeBuilderImpl kbuilder, TypeResolver typeResolver, PackageModel packageModel, QueryDescr queryDescr, boolean isPattern) {
    RuleContext context = new RuleContext(kbuilder, packageModel, queryDescr, typeResolver, isPattern);
    String queryName = queryDescr.getName();
    final String queryDefVariableName = toQueryDef(queryName);
    context.setQueryName(Optional.of(queryDefVariableName));
    parseQueryParameters(context, packageModel, queryDescr);
    ClassOrInterfaceType queryDefType = getQueryType(context.getQueryParameters());
    MethodCallExpr queryCall = new MethodCallExpr(null, QUERY_CALL);
    if (!queryDescr.getNamespace().isEmpty()) {
        queryCall.addArgument(new StringLiteralExpr(queryDescr.getNamespace()));
    }
    queryCall.addArgument(new StringLiteralExpr(queryName));
    for (QueryParameter qp : context.getQueryParameters()) {
        queryCall.addArgument(new ClassExpr(JavaParser.parseType(qp.type.getCanonicalName())));
        queryCall.addArgument(new StringLiteralExpr(qp.name));
    }
    packageModel.getQueryDefWithType().put(queryDefVariableName, new QueryDefWithType(queryDefType, queryCall, context));
}
Also used : StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) ClassOrInterfaceType(org.drools.javaparser.ast.type.ClassOrInterfaceType) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 50 with MethodCallExpr

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

the class FlowExpressionBuilder method buildReactOn.

private MethodCallExpr buildReactOn(DrlxParseSuccess drlxParseResult, MethodCallExpr exprDSL) {
    if (!drlxParseResult.getReactOnProperties().isEmpty()) {
        exprDSL = new MethodCallExpr(exprDSL, "reactOn");
        drlxParseResult.getReactOnProperties().stream().map(StringLiteralExpr::new).forEach(exprDSL::addArgument);
    }
    if (!drlxParseResult.getWatchedProperties().isEmpty()) {
        exprDSL = new MethodCallExpr(exprDSL, "watch");
        drlxParseResult.getWatchedProperties().stream().map(StringLiteralExpr::new).forEach(exprDSL::addArgument);
    }
    return exprDSL;
}
Also used : MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Aggregations

MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)75 NameExpr (org.drools.javaparser.ast.expr.NameExpr)36 Expression (org.drools.javaparser.ast.expr.Expression)32 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)26 ArrayList (java.util.ArrayList)16 List (java.util.List)15 ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)15 TypedExpression (org.drools.modelcompiler.builder.generator.TypedExpression)14 FieldAccessExpr (org.drools.javaparser.ast.expr.FieldAccessExpr)13 PackageModel (org.drools.modelcompiler.builder.PackageModel)13 Parameter (org.drools.javaparser.ast.body.Parameter)11 LambdaExpr (org.drools.javaparser.ast.expr.LambdaExpr)11 ClassOrInterfaceType (org.drools.javaparser.ast.type.ClassOrInterfaceType)11 Collection (java.util.Collection)10 Optional (java.util.Optional)10 Type (org.drools.javaparser.ast.type.Type)10 RuleContext (org.drools.modelcompiler.builder.generator.RuleContext)10 BinaryExpr (org.drools.javaparser.ast.expr.BinaryExpr)9 DeclarationSpec (org.drools.modelcompiler.builder.generator.DeclarationSpec)9 JavaParser (org.drools.javaparser.JavaParser)8