Search in sources :

Example 31 with VariableDeclarationExpr

use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.

the class ModelGenerator method addVariable.

private static void addVariable(BlockStmt ruleBlock, DeclarationSpec declaration, RuleContext context, boolean domainClass) {
    if (declaration.getDeclarationClass() == null) {
        context.addCompilationError(new UnknownDeclarationError(declaration.getBindingId()));
        return;
    }
    Type declType = classToReferenceType(declaration);
    ClassOrInterfaceType varType = toClassOrInterfaceType(Variable.class);
    varType.setTypeArguments(declType);
    VariableDeclarationExpr var_ = new VariableDeclarationExpr(varType, context.getVar(declaration.getBindingId()), Modifier.finalModifier());
    MethodCallExpr declarationOfCall = createDslTopLevelMethod(DECLARATION_OF_CALL);
    declarationOfCall.addArgument(new ClassExpr(declaration.getBoxedType()));
    if (domainClass) {
        String domainClassSourceName = asJavaSourceName(declaration.getDeclarationClass());
        declarationOfCall.addArgument(DOMAIN_CLASSESS_METADATA_FILE_NAME + context.getPackageModel().getPackageUUID() + "." + domainClassSourceName + DOMAIN_CLASS_METADATA_INSTANCE);
    }
    declarationOfCall.addArgument(toStringLiteral(declaration.getVariableName().orElse(declaration.getBindingId())));
    declaration.getDeclarationSource().ifPresent(declarationOfCall::addArgument);
    declaration.getEntryPoint().ifPresent(ep -> {
        MethodCallExpr entryPointCall = createDslTopLevelMethod(ENTRY_POINT_CALL);
        entryPointCall.addArgument(toStringLiteral(ep));
        declarationOfCall.addArgument(entryPointCall);
    });
    for (BehaviorDescr behaviorDescr : declaration.getBehaviors()) {
        MethodCallExpr windowCall = createDslTopLevelMethod(WINDOW_CALL);
        if (Behavior.BehaviorType.TIME_WINDOW.matches(behaviorDescr.getSubType())) {
            windowCall.addArgument("org.drools.model.Window.Type.TIME");
            windowCall.addArgument("" + TimeUtils.parseTimeString(behaviorDescr.getParameters().get(0)));
        }
        if (Behavior.BehaviorType.LENGTH_WINDOW.matches(behaviorDescr.getSubType())) {
            windowCall.addArgument("org.drools.model.Window.Type.LENGTH");
            windowCall.addArgument("" + Integer.valueOf(behaviorDescr.getParameters().get(0)));
        }
        declarationOfCall.addArgument(windowCall);
    }
    AssignExpr var_assign = new AssignExpr(var_, declarationOfCall, AssignExpr.Operator.ASSIGN);
    if (!DrlxParseUtil.hasDuplicateExpr(ruleBlock, var_assign)) {
        ruleBlock.addStatement(var_assign);
    }
}
Also used : ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) DrlxParseUtil.classToReferenceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.classToReferenceType) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) BehaviorDescr(org.drools.drl.ast.descr.BehaviorDescr) ClassExpr(com.github.javaparser.ast.expr.ClassExpr) UnknownDeclarationError(org.drools.modelcompiler.builder.errors.UnknownDeclarationError) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) AssignExpr(com.github.javaparser.ast.expr.AssignExpr)

Example 32 with VariableDeclarationExpr

use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.

the class QueryGenerator method processQuery.

public static void processQuery(PackageModel packageModel, QueryDescr queryDescr) {
    String queryDefVariableName = toQueryDef(queryDescr.getName());
    RuleContext context = packageModel.getQueryDefWithType().get(queryDefVariableName).getContext();
    context.addGlobalDeclarations();
    context.setDialectFromAttributes(queryDescr.getAttributes().values());
    new ModelGeneratorVisitor(context, packageModel).visit(queryDescr.getLhs());
    if (context.getRuleUnitDescr() != null) {
        Map<String, Class<?>> queryBindings = new HashMap<>();
        for (DeclarationSpec declr : context.getAllDeclarations()) {
            if (!declr.isGlobal() && !declr.getBindingId().startsWith(GENERATED_VARIABLE_PREFIX)) {
                queryBindings.put(declr.getBindingId(), declr.getDeclarationClass());
            }
        }
        QueryModel queryModel = new QueryModel(queryDescr.getName(), queryDescr.getNamespace(), queryDescr.getParameters(), queryBindings);
        packageModel.addQueryInRuleUnit(context.getRuleUnitDescr(), queryModel);
    }
    final Type queryType = toClassOrInterfaceType(Query.class);
    MethodDeclaration queryMethod = new MethodDeclaration(NodeList.nodeList(Modifier.privateModifier()), queryType, QUERY_METHOD_PREFIX + toId(queryDescr.getName()));
    BlockStmt queryBody = new BlockStmt();
    ModelGenerator.createVariables(queryBody, packageModel, context);
    queryMethod.setBody(queryBody);
    String queryBuildVarName = toId(queryDescr.getName()) + "_build";
    VariableDeclarationExpr queryBuildVar = new VariableDeclarationExpr(queryType, queryBuildVarName);
    MethodCallExpr buildCall = new MethodCallExpr(new NameExpr(queryDefVariableName), BUILD_CALL);
    context.getExpressions().forEach(buildCall::addArgument);
    AssignExpr queryBuildAssign = new AssignExpr(queryBuildVar, buildCall, AssignExpr.Operator.ASSIGN);
    queryBody.addStatement(queryBuildAssign);
    queryBody.addStatement(new ReturnStmt(queryBuildVarName));
    packageModel.putQueryMethod(queryMethod);
    RuleUnitDescription ruleUnitDescr = context.getRuleUnitDescr();
    if (ruleUnitDescr != null) {
        packageModel.putRuleUnit(ruleUnitDescr.getSimpleName());
    }
}
Also used : VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) RuleUnitDescription(org.kie.internal.ruleunit.RuleUnitDescription) HashMap(java.util.HashMap) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) QueryModel(org.drools.modelcompiler.builder.QueryModel) AssignExpr(com.github.javaparser.ast.expr.AssignExpr) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Type(com.github.javaparser.ast.type.Type) DrlxParseUtil.toClassOrInterfaceType(org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 33 with VariableDeclarationExpr

use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.

the class BoxedParameters method getBoxedParametersWithUnboxedAssignment.

// Types in the executable model are promoted to boxed to type check the Java DSL.
// We add such promoted types as _<PARAMETER_NAME> (with the underscore prefix)
// and then we downcast to the original unboxed type in the body of the function (methodBody)
public NodeList<Parameter> getBoxedParametersWithUnboxedAssignment(Collection<String> declarationUsedInRHS, BlockStmt methodBody) {
    NodeList<Parameter> parameters = NodeList.nodeList();
    for (String parameterName : declarationUsedInRHS) {
        DeclarationSpec declaration = context.getDeclarationByIdWithException(parameterName);
        Parameter boxedParameter;
        Type boxedType = declaration.getBoxedType();
        if (declaration.isBoxed()) {
            String boxedParameterName = "_" + parameterName;
            boxedParameter = new Parameter(boxedType, boxedParameterName);
            Expression unboxedTypeDowncast = new VariableDeclarationExpr(new VariableDeclarator(declaration.getRawType(), parameterName, new NameExpr(boxedParameterName)));
            methodBody.addStatement(0, unboxedTypeDowncast);
        } else {
            boxedParameter = new Parameter(boxedType, parameterName);
        }
        parameters.add(boxedParameter);
    }
    return parameters;
}
Also used : Type(com.github.javaparser.ast.type.Type) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Expression(com.github.javaparser.ast.expr.Expression) NameExpr(com.github.javaparser.ast.expr.NameExpr) Parameter(com.github.javaparser.ast.body.Parameter) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 34 with VariableDeclarationExpr

use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.

the class CommonCodegenUtils method createMap.

private static void createMap(final BlockStmt body, final String mapName, final List<String> mapTypes, final Class<? extends Map> mapClass) {
    final VariableDeclarator mapDeclarator = new VariableDeclarator(getTypedClassOrInterfaceTypeByTypeNames(Map.class.getName(), mapTypes), mapName);
    final ObjectCreationExpr mapInitializer = new ObjectCreationExpr();
    mapInitializer.setType(getTypedClassOrInterfaceTypeByTypeNames(mapClass.getName(), mapTypes));
    mapDeclarator.setInitializer(mapInitializer);
    final VariableDeclarationExpr mapDeclarationExpr = new VariableDeclarationExpr(mapDeclarator);
    body.addStatement(mapDeclarationExpr);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Example 35 with VariableDeclarationExpr

use of com.github.javaparser.ast.expr.VariableDeclarationExpr in project drools by kiegroup.

the class KiePMMLNodeFactoryTest method populateEvaluateNodeWithScoreDistributions.

@Test
public void populateEvaluateNodeWithScoreDistributions() {
    final BlockStmt toPopulate = new BlockStmt();
    final VariableDeclarator variableDeclarator = new VariableDeclarator();
    variableDeclarator.setType("List");
    variableDeclarator.setName(SCORE_DISTRIBUTIONS);
    toPopulate.addStatement(new VariableDeclarationExpr(variableDeclarator));
    assertFalse(variableDeclarator.getInitializer().isPresent());
    // Without probability
    List<ScoreDistribution> scoreDistributions = getRandomPMMLScoreDistributions(false);
    KiePMMLNodeFactory.populateEvaluateNodeWithScoreDistributions(toPopulate, scoreDistributions);
    commonVerifyEvaluateNodeWithScoreDistributions(variableDeclarator, scoreDistributions);
    // With probability
    scoreDistributions = getRandomPMMLScoreDistributions(true);
    KiePMMLNodeFactory.populateEvaluateNodeWithScoreDistributions(toPopulate, scoreDistributions);
    commonVerifyEvaluateNodeWithScoreDistributions(variableDeclarator, scoreDistributions);
}
Also used : ScoreDistribution(org.dmg.pmml.ScoreDistribution) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Test(org.junit.Test)

Aggregations

VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)38 Test (org.junit.Test)17 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)16 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)12 Expression (com.github.javaparser.ast.expr.Expression)11 AssignExpr (com.github.javaparser.ast.expr.AssignExpr)9 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)8 NameExpr (com.github.javaparser.ast.expr.NameExpr)7 ExpressionStmt (com.github.javaparser.ast.stmt.ExpressionStmt)7 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)5 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)4 Parameter (com.github.javaparser.ast.body.Parameter)4 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)4 Type (com.github.javaparser.ast.type.Type)4 Node (com.github.javaparser.ast.Node)3 CastExpr (com.github.javaparser.ast.expr.CastExpr)3 LambdaExpr (com.github.javaparser.ast.expr.LambdaExpr)3 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)3 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)3 ClassExpr (com.github.javaparser.ast.expr.ClassExpr)2