Search in sources :

Example 41 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.

the class KiePMMLAttributeFactory method getAttributeVariableDeclaration.

static BlockStmt getAttributeVariableDeclaration(final String variableName, final Attribute attribute, final List<Field<?>> fields) {
    final MethodDeclaration methodDeclaration = ATTRIBUTE_TEMPLATE.getMethodsByName(GETKIEPMMLATTRIBUTE).get(0).clone();
    final BlockStmt attributeBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(attributeBody, ATTRIBUTE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, ATTRIBUTE, attributeBody)));
    variableDeclarator.setName(variableName);
    final BlockStmt toReturn = new BlockStmt();
    String predicateVariableName = String.format("%s_Predicate", variableName);
    BlockStmt toAdd = getKiePMMLPredicate(predicateVariableName, attribute.getPredicate(), fields);
    toAdd.getStatements().forEach(toReturn::addStatement);
    final Expression complexPartialScoreExpression;
    if (attribute.getComplexPartialScore() != null) {
        String complexPartialScoreVariableName = String.format("%s_ComplexPartialScore", variableName);
        toAdd = getComplexPartialScoreVariableDeclaration(complexPartialScoreVariableName, attribute.getComplexPartialScore());
        toAdd.getStatements().forEach(toReturn::addStatement);
        complexPartialScoreExpression = new NameExpr(complexPartialScoreVariableName);
    } else {
        complexPartialScoreExpression = new NullLiteralExpr();
    }
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, ATTRIBUTE, attributeBody))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
    builder.setArgument(0, new StringLiteralExpr(variableName));
    builder.setArgument(2, new NameExpr(predicateVariableName));
    getChainedMethodCallExprFrom("withPartialScore", initializer).setArgument(0, getExpressionForObject(attribute.getPartialScore()));
    getChainedMethodCallExprFrom("withComplexPartialScore", initializer).setArgument(0, complexPartialScoreExpression);
    attributeBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 42 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.

the class CompiledFEELSupport method compiledErrorUnaryTest.

public static DirectCompilerResult compiledErrorUnaryTest(String msg) {
    LambdaExpr initializer = new LambdaExpr();
    initializer.setEnclosingParameters(true);
    initializer.addParameter(new Parameter(new UnknownType(), "feelExprCtx"));
    initializer.addParameter(new Parameter(new UnknownType(), "left"));
    Statement lambdaBody = new BlockStmt(new NodeList<>(new ExpressionStmt(compiledErrorExpression(msg)), new ReturnStmt(new BooleanLiteralExpr(false))));
    initializer.setBody(lambdaBody);
    String constantName = "UT_EMPTY";
    VariableDeclarator vd = new VariableDeclarator(parseClassOrInterfaceType(UnaryTest.class.getCanonicalName()), constantName);
    vd.setInitializer(initializer);
    FieldDeclaration fd = new FieldDeclaration();
    fd.setModifier(Modifier.publicModifier().getKeyword(), true);
    fd.setModifier(Modifier.staticModifier().getKeyword(), true);
    fd.setModifier(Modifier.finalModifier().getKeyword(), true);
    fd.addVariable(vd);
    fd.setJavadocComment(" FEEL unary test: - ");
    MethodCallExpr list = new MethodCallExpr(compiledFeelSemanticMappingsFQN(), "list", new NodeList<>(new NameExpr(constantName)));
    DirectCompilerResult directCompilerResult = DirectCompilerResult.of(list, BuiltInType.LIST);
    directCompilerResult.addFieldDeclaration(fd);
    return directCompilerResult;
}
Also used : Statement(com.github.javaparser.ast.stmt.Statement) LambdaExpr(com.github.javaparser.ast.expr.LambdaExpr) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) ExpressionStmt(com.github.javaparser.ast.stmt.ExpressionStmt) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) UnknownType(com.github.javaparser.ast.type.UnknownType) BooleanLiteralExpr(com.github.javaparser.ast.expr.BooleanLiteralExpr) Parameter(com.github.javaparser.ast.body.Parameter) ReturnStmt(com.github.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 43 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.

the class KiePMMLModelFactoryUtils method initStaticGetter.

/**
 * Populate the given <code>ClassOrInterfaceDeclaration</code>' <b>staticGetter</b> with the <b>common</b>
 * parameters needed to
 * instantiate a <code>KiePMMLModel</code>
 * @param compilationDTO
 * @param modelTemplate
 * @return
 */
public static void initStaticGetter(final CompilationDTO<? extends Model> compilationDTO, final ClassOrInterfaceDeclaration modelTemplate) {
    final MethodDeclaration staticGetterMethod = modelTemplate.getMethodsByName(GET_MODEL).get(0);
    final BlockStmt staticGetterBody = staticGetterMethod.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, staticGetterMethod)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(staticGetterBody, TO_RETURN).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TO_RETURN, staticGetterBody)));
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TO_RETURN, staticGetterBody))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
    final String name = compilationDTO.getModelName();
    final Expression miningFunctionExpression;
    if (compilationDTO.getMINING_FUNCTION() != null) {
        MINING_FUNCTION miningFunction = compilationDTO.getMINING_FUNCTION();
        miningFunctionExpression = new NameExpr(miningFunction.getClass().getName() + "." + miningFunction.name());
    } else {
        miningFunctionExpression = new NullLiteralExpr();
    }
    builder.setArgument(0, new StringLiteralExpr(name));
    builder.setArgument(1, miningFunctionExpression);
    String targetFieldName = compilationDTO.getTargetFieldName();
    final Expression targetFieldExpression;
    if (targetFieldName != null) {
        targetFieldExpression = new StringLiteralExpr(targetFieldName);
    } else {
        targetFieldExpression = new NullLiteralExpr();
    }
    getChainedMethodCallExprFrom("withTargetField", initializer).setArgument(0, targetFieldExpression);
    // 
    populateGetCreatedMiningFieldsMethod(modelTemplate, compilationDTO.getKieMiningFields());
    populateGetCreatedOutputFieldsMethod(modelTemplate, compilationDTO.getKieOutputFields());
    populateGetCreatedKiePMMLMiningFieldsMethod(modelTemplate, compilationDTO.getMiningSchema().getMiningFields(), compilationDTO.getFields());
    if (compilationDTO.getOutput() != null) {
        populateGetCreatedKiePMMLOutputFieldsMethod(modelTemplate, compilationDTO.getOutput().getOutputFields());
    }
    if (compilationDTO.getKieTargetFields() != null) {
        populateGetCreatedKiePMMLTargetsMethod(modelTemplate, compilationDTO.getKieTargetFields());
    }
    populateGetCreatedTransformationDictionaryMethod(modelTemplate, compilationDTO.getTransformationDictionary());
    populateGetCreatedLocalTransformationsMethod(modelTemplate, compilationDTO.getLocalTransformations());
}
Also used : NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) MINING_FUNCTION(org.kie.pmml.api.enums.MINING_FUNCTION) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) CommonCodegenUtils.addListPopulationByMethodCallExpr(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.addListPopulationByMethodCallExpr) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 44 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.

the class KiePMMLDerivedFieldFactory method getDerivedFieldVariableDeclaration.

static BlockStmt getDerivedFieldVariableDeclaration(final String variableName, final DerivedField derivedField) {
    final MethodDeclaration methodDeclaration = DERIVED_FIELD_TEMPLATE.getMethodsByName(GETKIEPMMLDERIVEDFIELD).get(0).clone();
    final BlockStmt derivedFieldBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(derivedFieldBody, DERIVED_FIELD).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, DERIVED_FIELD, derivedFieldBody)));
    variableDeclarator.setName(variableName);
    final BlockStmt toReturn = new BlockStmt();
    String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, 0);
    BlockStmt toAdd = getKiePMMLExpressionBlockStmt(nestedVariableName, derivedField.getExpression());
    toAdd.getStatements().forEach(toReturn::addStatement);
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, DERIVED_FIELD, derivedFieldBody))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
    final Expression dataTypeExpression = getExpressionForDataType(derivedField.getDataType());
    final Expression opTypeExpression = getExpressionForOpType(derivedField.getOpType());
    builder.setArgument(0, new StringLiteralExpr(derivedField.getName().getValue()));
    builder.setArgument(2, dataTypeExpression);
    builder.setArgument(3, opTypeExpression);
    builder.setArgument(4, new NameExpr(nestedVariableName));
    getChainedMethodCallExprFrom("withDisplayName", initializer).setArgument(0, getExpressionForObject(derivedField.getDisplayName()));
    derivedFieldBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) KiePMMLExpressionFactory.getKiePMMLExpressionBlockStmt(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLExpressionFactory.getKiePMMLExpressionBlockStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) NameExpr(com.github.javaparser.ast.expr.NameExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 45 with VariableDeclarator

use of com.github.javaparser.ast.body.VariableDeclarator in project drools by kiegroup.

the class KiePMMLFieldRefFactory method getFieldRefVariableDeclaration.

static BlockStmt getFieldRefVariableDeclaration(final String variableName, final FieldRef fieldRef) {
    final MethodDeclaration methodDeclaration = FIELDREF_TEMPLATE.getMethodsByName(GETKIEPMMLFIELDREF).get(0).clone();
    final BlockStmt toReturn = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(toReturn, FIELD_REF).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, FIELD_REF, toReturn)));
    variableDeclarator.setName(variableName);
    final ObjectCreationExpr objectCreationExpr = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, FIELD_REF, toReturn))).asObjectCreationExpr();
    final StringLiteralExpr nameExpr = new StringLiteralExpr(fieldRef.getField().getValue());
    final Expression mapMissingToExpr = getExpressionForObject(fieldRef.getMapMissingTo());
    objectCreationExpr.getArguments().set(0, nameExpr);
    objectCreationExpr.getArguments().set(2, mapMissingToExpr);
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Expression(com.github.javaparser.ast.expr.Expression) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator)

Aggregations

VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)110 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)50 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)44 Expression (com.github.javaparser.ast.expr.Expression)43 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)41 CommonCodegenUtils.getVariableDeclarator (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)39 NameExpr (com.github.javaparser.ast.expr.NameExpr)33 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)32 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)30 Test (org.junit.Test)25 ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)24 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)18 VariableDeclarationExpr (com.github.javaparser.ast.expr.VariableDeclarationExpr)17 CompilationUnit (com.github.javaparser.ast.CompilationUnit)16 NodeList (com.github.javaparser.ast.NodeList)16 ResolvedType (com.github.javaparser.resolution.types.ResolvedType)14 FieldDeclaration (com.github.javaparser.ast.body.FieldDeclaration)13 ReflectionTypeSolver (com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver)13 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)11 Parameter (com.github.javaparser.ast.body.Parameter)9