Search in sources :

Example 81 with ObjectCreationExpr

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

the class KiePMMLRegressionTableFactory method populateWithGroupedCategoricalPredictorMap.

/**
 * Populate the given <b>body</b> with the creation of a <code>Map</code> for the given <b>categoricalPredictors</b>
 * @param categoricalPredictors
 * @param toPopulate
 * @param categoricalPredictorMapName
 * @return
 */
static void populateWithGroupedCategoricalPredictorMap(final List<CategoricalPredictor> categoricalPredictors, final BlockStmt toPopulate, final String categoricalPredictorMapName) {
    final VariableDeclarator categoricalMapDeclarator = new VariableDeclarator(getTypedClassOrInterfaceTypeByTypeNames(Map.class.getName(), Arrays.asList(String.class.getSimpleName(), Double.class.getSimpleName())), categoricalPredictorMapName);
    final ObjectCreationExpr categoricalMapInitializer = new ObjectCreationExpr();
    categoricalMapInitializer.setType(getTypedClassOrInterfaceTypeByTypeNames(HashMap.class.getName(), Arrays.asList(String.class.getSimpleName(), Double.class.getSimpleName())));
    categoricalMapDeclarator.setInitializer(categoricalMapInitializer);
    final VariableDeclarationExpr categoricalMapDeclarationExpr = new VariableDeclarationExpr(categoricalMapDeclarator);
    toPopulate.addStatement(categoricalMapDeclarationExpr);
    final Map<String, Expression> mapExpressions = new LinkedHashMap<>();
    categoricalPredictors.forEach(categoricalPredictor -> mapExpressions.put(categoricalPredictor.getValue().toString(), getExpressionForObject(categoricalPredictor.getCoefficient().doubleValue())));
    addMapPopulationExpressions(mapExpressions, toPopulate, categoricalPredictorMapName);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) VariableDeclarationExpr(com.github.javaparser.ast.expr.VariableDeclarationExpr) Expression(com.github.javaparser.ast.expr.Expression) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) CommonCodegenUtils.getVariableDeclarator(org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator) LinkedHashMap(java.util.LinkedHashMap)

Example 82 with ObjectCreationExpr

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

the class KiePMMLScorecardModelFactory method setConstructor.

static void setConstructor(final ScorecardCompilationDTO compilationDTO, final ClassOrInterfaceDeclaration modelTemplate, final String fullCharacteristicsClassName) {
    KiePMMLModelFactoryUtils.init(compilationDTO, modelTemplate);
    final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
    final BlockStmt body = constructorDeclaration.getBody();
    final ExplicitConstructorInvocationStmt superStatement = CommonCodegenUtils.getExplicitConstructorInvocationStmt(body).orElseThrow(() -> new KiePMMLException(String.format(MISSING_CONSTRUCTOR_IN_BODY, body)));
    ClassOrInterfaceType characteristicsClass = parseClassOrInterfaceType(fullCharacteristicsClassName);
    ObjectCreationExpr characteristicsReference = new ObjectCreationExpr();
    characteristicsReference.setType(characteristicsClass);
    superStatement.setArgument(2, characteristicsReference);
    superStatement.setArgument(3, getExpressionForObject(compilationDTO.getInitialScore()));
    superStatement.setArgument(4, getExpressionForObject(compilationDTO.isUseReasonCodes()));
    REASONCODE_ALGORITHM reasoncodeAlgorithm = compilationDTO.getREASONCODE_ALGORITHM();
    NameExpr reasonCodeExpr = new NameExpr(REASONCODE_ALGORITHM.class.getName() + "." + reasoncodeAlgorithm.name());
    superStatement.setArgument(5, reasonCodeExpr);
    superStatement.setArgument(6, getExpressionForObject(compilationDTO.getBaselineScore()));
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) BlockStmt(com.github.javaparser.ast.stmt.BlockStmt) REASONCODE_ALGORITHM(org.kie.pmml.api.enums.REASONCODE_ALGORITHM) NameExpr(com.github.javaparser.ast.expr.NameExpr) KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLInternalException(org.kie.pmml.api.exceptions.KiePMMLInternalException) ExplicitConstructorInvocationStmt(com.github.javaparser.ast.stmt.ExplicitConstructorInvocationStmt) StaticJavaParser.parseClassOrInterfaceType(com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType) ClassOrInterfaceType(com.github.javaparser.ast.type.ClassOrInterfaceType)

Example 83 with ObjectCreationExpr

use of com.github.javaparser.ast.expr.ObjectCreationExpr in project checker-framework by typetools.

the class WholeProgramInferenceJavaParserStorage method createWrappersForClass.

/**
 * The first two arugments are a javac tree and a JavaParser node representing the same class.
 * This method creates wrappers around all the classes, fields, and methods in that class, and
 * stores those wrappers in {@code sourceAnnos}.
 *
 * @param javacClass javac tree for class
 * @param javaParserClass JavaParser node corresponding to the same class as {@code javacClass}
 * @param sourceAnnos compilation unit wrapper to add new wrappers to
 */
private void createWrappersForClass(ClassTree javacClass, TypeDeclaration<?> javaParserClass, CompilationUnitAnnos sourceAnnos) {
    JointJavacJavaParserVisitor visitor = new DefaultJointVisitor() {

        @Override
        public void processClass(ClassTree javacTree, ClassOrInterfaceDeclaration javaParserNode) {
            addClass(javacTree);
        }

        @Override
        public void processClass(ClassTree javacTree, EnumDeclaration javaParserNode) {
            addClass(javacTree);
        }

        @Override
        public void processClass(ClassTree javacTree, RecordDeclaration javaParserNode) {
            addClass(javacTree);
        }

        @Override
        public void processNewClass(NewClassTree javacTree, ObjectCreationExpr javaParserNode) {
            if (javacTree.getClassBody() != null) {
                addClass(javacTree.getClassBody());
            }
        }

        /**
         * Creates a wrapper around the class for {@code tree} and stores it in {@code
         * sourceAnnos}.
         *
         * @param tree tree to add
         */
        private void addClass(ClassTree tree) {
            TypeElement classElt = TreeUtils.elementFromDeclaration(tree);
            String className = ElementUtils.getBinaryName(classElt);
            ClassOrInterfaceAnnos typeWrapper = new ClassOrInterfaceAnnos();
            if (!classToAnnos.containsKey(className)) {
                classToAnnos.put(className, typeWrapper);
            }
            sourceAnnos.types.add(typeWrapper);
        }

        @Override
        public void processMethod(MethodTree javacTree, MethodDeclaration javaParserNode) {
            addCallableDeclaration(javacTree, javaParserNode);
        }

        @Override
        public void processMethod(MethodTree javacTree, ConstructorDeclaration javaParserNode) {
            addCallableDeclaration(javacTree, javaParserNode);
        }

        /**
         * Creates a wrapper around {@code javacTree} with the corresponding declaration {@code
         * javaParserNode} and stores it in {@code sourceAnnos}.
         *
         * @param javacTree javac tree for declaration to add
         * @param javaParserNode JavaParser node for the same class as {@code javacTree}
         */
        private void addCallableDeclaration(MethodTree javacTree, CallableDeclaration<?> javaParserNode) {
            ExecutableElement elt = TreeUtils.elementFromDeclaration(javacTree);
            String className = ElementUtils.getEnclosingClassName(elt);
            ClassOrInterfaceAnnos enclosingClass = classToAnnos.get(className);
            String executableSignature = JVMNames.getJVMMethodSignature(javacTree);
            if (!enclosingClass.callableDeclarations.containsKey(executableSignature)) {
                enclosingClass.callableDeclarations.put(executableSignature, new CallableDeclarationAnnos(javaParserNode));
            }
        }

        @Override
        public void processVariable(VariableTree javacTree, EnumConstantDeclaration javaParserNode) {
            VariableElement elt = TreeUtils.elementFromDeclaration(javacTree);
            if (!elt.getKind().isField()) {
                throw new BugInCF(elt + " is not a field but a " + elt.getKind());
            }
            String enclosingClassName = ElementUtils.getEnclosingClassName(elt);
            ClassOrInterfaceAnnos enclosingClass = classToAnnos.get(enclosingClassName);
            String fieldName = javacTree.getName().toString();
            enclosingClass.enumConstants.add(fieldName);
            // Ensure that if an enum constant defines a class, that class gets registered properly.
            // See e.g. https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9.1 for
            // the specification of an enum constant, which does permit it to define an anonymous
            // class.
            NewClassTree constructor = (NewClassTree) javacTree.getInitializer();
            if (constructor.getClassBody() != null) {
                addClass(constructor.getClassBody());
            }
        }

        @Override
        public void processVariable(VariableTree javacTree, VariableDeclarator javaParserNode) {
            // below call to TreeUtils.elementFromDeclaration causes a crash.
            if (TreeUtils.elementFromTree(javacTree) == null) {
                return;
            }
            VariableElement elt = TreeUtils.elementFromDeclaration(javacTree);
            if (!elt.getKind().isField()) {
                return;
            }
            String enclosingClassName = ElementUtils.getEnclosingClassName(elt);
            ClassOrInterfaceAnnos enclosingClass = classToAnnos.get(enclosingClassName);
            String fieldName = javacTree.getName().toString();
            if (!enclosingClass.fields.containsKey(fieldName)) {
                enclosingClass.fields.put(fieldName, new FieldAnnos(javaParserNode));
            }
        }
    };
    visitor.visitClass(javacClass, javaParserClass);
}
Also used : ObjectCreationExpr(com.github.javaparser.ast.expr.ObjectCreationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodTree(com.sun.source.tree.MethodTree) TypeElement(javax.lang.model.element.TypeElement) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) ExecutableElement(javax.lang.model.element.ExecutableElement) NewClassTree(com.sun.source.tree.NewClassTree) ClassTree(com.sun.source.tree.ClassTree) VariableTree(com.sun.source.tree.VariableTree) NewClassTree(com.sun.source.tree.NewClassTree) VariableElement(javax.lang.model.element.VariableElement) BugInCF(org.checkerframework.javacutil.BugInCF) EnumDeclaration(com.github.javaparser.ast.body.EnumDeclaration) VariableDeclarator(com.github.javaparser.ast.body.VariableDeclarator) RecordDeclaration(com.github.javaparser.ast.body.RecordDeclaration) EnumConstantDeclaration(com.github.javaparser.ast.body.EnumConstantDeclaration) JointJavacJavaParserVisitor(org.checkerframework.framework.ajava.JointJavacJavaParserVisitor) ConstructorDeclaration(com.github.javaparser.ast.body.ConstructorDeclaration) CallableDeclaration(com.github.javaparser.ast.body.CallableDeclaration) DefaultJointVisitor(org.checkerframework.framework.ajava.DefaultJointVisitor)

Aggregations

ObjectCreationExpr (com.github.javaparser.ast.expr.ObjectCreationExpr)83 Expression (com.github.javaparser.ast.expr.Expression)59 BlockStmt (com.github.javaparser.ast.stmt.BlockStmt)44 NameExpr (com.github.javaparser.ast.expr.NameExpr)41 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)38 ClassOrInterfaceType (com.github.javaparser.ast.type.ClassOrInterfaceType)31 MethodCallExpr (com.github.javaparser.ast.expr.MethodCallExpr)30 StringLiteralExpr (com.github.javaparser.ast.expr.StringLiteralExpr)30 NodeList (com.github.javaparser.ast.NodeList)24 VariableDeclarator (com.github.javaparser.ast.body.VariableDeclarator)24 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)21 CommonCodegenUtils.getVariableDeclarator (org.kie.pmml.compiler.commons.utils.CommonCodegenUtils.getVariableDeclarator)19 NullLiteralExpr (com.github.javaparser.ast.expr.NullLiteralExpr)14 List (java.util.List)13 ReturnStmt (com.github.javaparser.ast.stmt.ReturnStmt)12 Type (com.github.javaparser.ast.type.Type)12 Test (org.junit.Test)12 StaticJavaParser.parseClassOrInterfaceType (com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType)11 DrlxParseUtil.toClassOrInterfaceType (org.drools.modelcompiler.builder.generator.DrlxParseUtil.toClassOrInterfaceType)11 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)10