Search in sources :

Example 31 with VariableDeclarator

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

the class KiePMMLFactoryFactory method populateKiePmmlFields.

static void populateKiePmmlFields(final FieldDeclaration toPopulate, Map<String, Boolean> generatedClassesModelTypeMap) {
    final VariableDeclarator variable = toPopulate.getVariable(0);
    Set<Expression> methodCallExpressions = generatedClassesModelTypeMap.entrySet().stream().map(entry -> getInstantiationExpression(entry.getKey(), entry.getValue())).collect(Collectors.toSet());
    NodeList<Expression> expressions = NodeList.nodeList(methodCallExpressions);
    MethodCallExpr initializer = new MethodCallExpr(new NameExpr("Arrays"), "asList", expressions);
    variable.setInitializer(initializer);
}
Also used : NodeList(com.github.javaparser.ast.NodeList) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType) Logger(org.slf4j.Logger) JavaParserUtils.getFromFileName(org.kie.pmml.compiler.commons.utils.JavaParserUtils.getFromFileName) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) HashMap(java.util.HashMap) NameExpr(com.github.javaparser.ast.expr.NameExpr) Collectors(java.util.stream.Collectors) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) MAIN_CLASS_NOT_FOUND(org.kie.pmml.compiler.commons.utils.JavaParserUtils.MAIN_CLASS_NOT_FOUND) FieldDeclaration(com.github.javaparser.ast.body.FieldDeclaration) ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) Map(java.util.Map) GET_MODEL(org.kie.pmml.commons.Constants.GET_MODEL) Expression(com.github.javaparser.ast.expr.Expression) CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) Expression(com.github.javaparser.ast.expr.Expression) NameExpr(com.github.javaparser.ast.expr.NameExpr) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 32 with VariableDeclarator

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

the class KiePMMLTextIndexFactory method getTextIndexVariableDeclaration.

static BlockStmt getTextIndexVariableDeclaration(final String variableName, final TextIndex textIndex) {
    final MethodDeclaration methodDeclaration = TEXTINDEX_TEMPLATE.getMethodsByName(GETKIEPMMLTEXTINDEX).get(0).clone();
    final BlockStmt textIndexBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(textIndexBody, TEXTINDEX).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TEXTINDEX, textIndexBody)));
    variableDeclarator.setName(variableName);
    final BlockStmt toReturn = new BlockStmt();
    String expressionVariableName = String.format("%s_Expression", variableName);
    final BlockStmt expressionBlockStatement = getKiePMMLExpressionBlockStmt(expressionVariableName, textIndex.getExpression());
    expressionBlockStatement.getStatements().forEach(toReturn::addStatement);
    int counter = 0;
    final NodeList<Expression> arguments = new NodeList<>();
    if (textIndex.hasTextIndexNormalizations()) {
        for (TextIndexNormalization textIndexNormalization : textIndex.getTextIndexNormalizations()) {
            String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, counter);
            arguments.add(new NameExpr(nestedVariableName));
            BlockStmt toAdd = getTextIndexNormalizationVariableDeclaration(nestedVariableName, textIndexNormalization);
            toAdd.getStatements().forEach(toReturn::addStatement);
            counter++;
        }
    }
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TEXTINDEX, toReturn))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
    final StringLiteralExpr nameExpr = new StringLiteralExpr(textIndex.getTextField().getValue());
    final NameExpr expressionExpr = new NameExpr(expressionVariableName);
    builder.setArgument(0, nameExpr);
    builder.setArgument(2, expressionExpr);
    Expression localTermWeightsExpression;
    if (textIndex.getLocalTermWeights() != null) {
        final LOCAL_TERM_WEIGHTS localTermWeights = LOCAL_TERM_WEIGHTS.byName(textIndex.getLocalTermWeights().value());
        localTermWeightsExpression = new NameExpr(LOCAL_TERM_WEIGHTS.class.getName() + "." + localTermWeights.name());
    } else {
        localTermWeightsExpression = new NullLiteralExpr();
    }
    getChainedMethodCallExprFrom("withLocalTermWeights", initializer).setArgument(0, localTermWeightsExpression);
    getChainedMethodCallExprFrom("withIsCaseSensitive", initializer).setArgument(0, getExpressionForObject(textIndex.isCaseSensitive()));
    getChainedMethodCallExprFrom("withMaxLevenshteinDistance", initializer).setArgument(0, getExpressionForObject(textIndex.getMaxLevenshteinDistance()));
    Expression countHitsExpression;
    if (textIndex.getCountHits() != null) {
        final COUNT_HITS countHits = COUNT_HITS.byName(textIndex.getCountHits().value());
        countHitsExpression = new NameExpr(COUNT_HITS.class.getName() + "." + countHits.name());
    } else {
        countHitsExpression = new NullLiteralExpr();
    }
    getChainedMethodCallExprFrom("withCountHits", initializer).setArgument(0, countHitsExpression);
    Expression wordSeparatorCharacterREExpression;
    if (textIndex.getWordSeparatorCharacterRE() != null) {
        String wordSeparatorCharacterRE = StringEscapeUtils.escapeJava(textIndex.getWordSeparatorCharacterRE());
        wordSeparatorCharacterREExpression = new StringLiteralExpr(wordSeparatorCharacterRE);
    } else {
        wordSeparatorCharacterREExpression = new NullLiteralExpr();
    }
    getChainedMethodCallExprFrom("withWordSeparatorCharacterRE", initializer).setArgument(0, wordSeparatorCharacterREExpression);
    getChainedMethodCallExprFrom("withTokenize", initializer).setArgument(0, getExpressionForObject(textIndex.isTokenize()));
    getChainedMethodCallExprFrom("asList", initializer).setArguments(arguments);
    textIndexBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : COUNT_HITS(org.kie.pmml.api.enums.COUNT_HITS) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) KiePMMLExpressionFactory.getKiePMMLExpressionBlockStmt(org.kie.pmml.compiler.commons.codegenfactories.KiePMMLExpressionFactory.getKiePMMLExpressionBlockStmt) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NodeList(com.github.javaparser.ast.NodeList) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) LOCAL_TERM_WEIGHTS(org.kie.pmml.api.enums.LOCAL_TERM_WEIGHTS) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) NullLiteralExpr(com.github.javaparser.ast.expr.NullLiteralExpr) TextIndexNormalization(org.dmg.pmml.TextIndexNormalization) Expression(com.github.javaparser.ast.expr.Expression) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 33 with VariableDeclarator

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

the class KiePMMLSimpleSetPredicateFactory method getSimpleSetPredicateVariableDeclaration.

static BlockStmt getSimpleSetPredicateVariableDeclaration(final String variableName, final SimpleSetPredicate simpleSetPredicate) {
    final MethodDeclaration methodDeclaration = SIMPLESET_PREDICATE_TEMPLATE.getMethodsByName(GETKIEPMMLSIMPLESETPREDICATE).get(0).clone();
    final BlockStmt simpleSetPredicateBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(simpleSetPredicateBody, SIMPLESET_PREDICATE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, SIMPLESET_PREDICATE, simpleSetPredicateBody)));
    variableDeclarator.setName(variableName);
    final BlockStmt toReturn = new BlockStmt();
    final NodeList<Expression> arguments = new NodeList<>();
    List<Object> values = getObjectsFromArray(simpleSetPredicate.getArray());
    for (Object value : values) {
        arguments.add(getExpressionForObject(value));
    }
    final ARRAY_TYPE arrayType = ARRAY_TYPE.byName(simpleSetPredicate.getArray().getType().value());
    final NameExpr arrayTypeExpr = new NameExpr(ARRAY_TYPE.class.getName() + "." + arrayType.name());
    final IN_NOTIN inNotIn = IN_NOTIN.byName(simpleSetPredicate.getBooleanOperator().value());
    final NameExpr inNotInExpr = new NameExpr(IN_NOTIN.class.getName() + "." + inNotIn.name());
    final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, SIMPLESET_PREDICATE, simpleSetPredicateBody))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
    builder.setArgument(0, new StringLiteralExpr(simpleSetPredicate.getField().getValue()));
    builder.setArgument(2, arrayTypeExpr);
    builder.setArgument(3, inNotInExpr);
    getChainedMethodCallExprFrom("asList", initializer).setArguments(arguments);
    simpleSetPredicateBody.getStatements().forEach(toReturn::addStatement);
    return toReturn;
}
Also used : MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) NodeList(com.github.javaparser.ast.NodeList) ARRAY_TYPE(org.kie.pmml.api.enums.ARRAY_TYPE) NameExpr(com.github.javaparser.ast.expr.NameExpr) StringLiteralExpr(com.github.javaparser.ast.expr.StringLiteralExpr) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) Expression(com.github.javaparser.ast.expr.Expression) IN_NOTIN(org.kie.pmml.api.enums.IN_NOTIN) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) CommonCodegenUtils.getExpressionForObject(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getExpressionForObject) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 34 with VariableDeclarator

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

the class KiePMMLTargetValueFactory method getKiePMMLTargetValueVariableInitializer.

static MethodCallExpr getKiePMMLTargetValueVariableInitializer(final TargetValue targetValueField) {
    final MethodDeclaration methodDeclaration = TARGETVALUE_TEMPLATE.getMethodsByName(GETKIEPMMLTARGETVALUE).get(0).clone();
    final BlockStmt targetValueBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(targetValueBody, TARGETVALUE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TARGETVALUE, targetValueBody)));
    variableDeclarator.setName(targetValueField.getName());
    final ObjectCreationExpr targetValueFieldInstance = TargetValueFactory.getTargetValueVariableInitializer(targetValueField);
    final MethodCallExpr toReturn = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TARGETVALUE, targetValueBody))).asMethodCallExpr();
    final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", toReturn);
    final StringLiteralExpr nameExpr = new StringLiteralExpr(targetValueField.getName());
    builder.setArgument(0, nameExpr);
    builder.setArgument(2, targetValueFieldInstance);
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) 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) MethodCallExpr(com.github.javaparser.ast.expr.MethodCallExpr)

Example 35 with VariableDeclarator

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

the class TargetValueFactory method getTargetValueVariableInitializer.

static ObjectCreationExpr getTargetValueVariableInitializer(final TargetValue targetValueField) {
    final MethodDeclaration methodDeclaration = TARGETVALUE_TEMPLATE.getMethodsByName(GETTARGETVALUE).get(0).clone();
    final BlockStmt targetValueBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
    final VariableDeclarator variableDeclarator = getVariableDeclarator(targetValueBody, TARGETVALUE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, TARGETVALUE, targetValueBody)));
    final ObjectCreationExpr toReturn = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, TARGETVALUE, targetValueBody))).asObjectCreationExpr();
    toReturn.setArgument(0, getExpressionForObject(targetValueField.getValue()));
    toReturn.setArgument(1, getExpressionForObject(targetValueField.getDisplayValue()));
    toReturn.setArgument(2, getExpressionForObject(targetValueField.getPriorProbability()));
    toReturn.setArgument(3, getExpressionForObject(targetValueField.getDefaultValue()));
    return toReturn;
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) 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