use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.
the class KiePMMLClusteringModelFactory method clusteringFieldCreationExprFrom.
private static ObjectCreationExpr clusteringFieldCreationExprFrom(ClusteringField clusteringField) {
double fieldWeight = clusteringField.getFieldWeight() == null ? 1.0 : clusteringField.getFieldWeight().doubleValue();
boolean isCenterField = clusteringField.getCenterField() == null || clusteringField.getCenterField() == ClusteringField.CenterField.TRUE;
NodeList<Expression> arguments = new NodeList<>();
arguments.add(literalExprFrom(clusteringField.getField().getValue()));
arguments.add(new DoubleLiteralExpr(fieldWeight));
arguments.add(new BooleanLiteralExpr(isCenterField));
arguments.add(clusteringField.getCompareFunction() == null ? new NullLiteralExpr() : literalExprFrom(compareFunctionFrom(clusteringField.getCompareFunction())));
arguments.add(new NullLiteralExpr());
return new ObjectCreationExpr(null, new ClassOrInterfaceType(null, KiePMMLClusteringField.class.getCanonicalName()), arguments);
}
use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.
the class KiePMMLSegmentFactoryTest method setConstructorNoInterpreted.
@Test
public void setConstructorNoInterpreted() {
ConstructorDeclaration constructorDeclaration = MODEL_TEMPLATE.getDefaultConstructor().get();
String segmentName = "SEGMENTNAME";
String generatedClassName = "GENERATEDCLASSNAME";
String kiePMMLModelClass = "KIEPMMLMODELCLASS";
double weight = 12.22;
KiePMMLSegmentFactory.setConstructor(segmentName, generatedClassName, constructorDeclaration, kiePMMLModelClass, false, weight);
Map<Integer, Expression> superInvocationExpressionsMap = new HashMap<>();
superInvocationExpressionsMap.put(0, new NameExpr(String.format("\"%s\"", segmentName)));
ClassOrInterfaceType classOrInterfaceType = parseClassOrInterfaceType(kiePMMLModelClass);
ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
objectCreationExpr.setType(classOrInterfaceType);
superInvocationExpressionsMap.put(3, new NameExpr(objectCreationExpr.toString()));
Map<String, Expression> assignExpressionMap = new HashMap<>();
assignExpressionMap.put("weight", new DoubleLiteralExpr(weight));
assignExpressionMap.put("id", new StringLiteralExpr(segmentName));
assertTrue(commonEvaluateConstructor(constructorDeclaration, generatedClassName, superInvocationExpressionsMap, assignExpressionMap));
}
use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.
the class KiePMMLSegmentFactoryTest method setConstructorInterpreted.
@Test
public void setConstructorInterpreted() throws IOException {
ConstructorDeclaration constructorDeclaration = MODEL_TEMPLATE.getDefaultConstructor().get();
String segmentName = "SEGMENTNAME";
String generatedClassName = "GENERATEDCLASSNAME";
String kiePMMLModelClass = "KIEPMMLMODELCLASS";
double weight = 12.22;
KiePMMLSegmentFactory.setConstructor(segmentName, generatedClassName, constructorDeclaration, kiePMMLModelClass, true, weight);
Map<Integer, Expression> superInvocationExpressionsMap = new HashMap<>();
superInvocationExpressionsMap.put(0, new NameExpr(String.format("\"%s\"", segmentName)));
ClassOrInterfaceType classOrInterfaceType = parseClassOrInterfaceType(kiePMMLModelClass);
ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
objectCreationExpr.setType(classOrInterfaceType);
superInvocationExpressionsMap.put(3, new NameExpr(objectCreationExpr.toString()));
Map<String, Expression> assignExpressionMap = new HashMap<>();
assignExpressionMap.put("weight", new DoubleLiteralExpr(weight));
assignExpressionMap.put("id", new StringLiteralExpr(segmentName));
String text = getFileContent(TEST_01_SOURCE);
BlockStmt expected = JavaParserUtils.parseConstructorBlock(text);
assertTrue(JavaParserUtils.equalsNode(expected, constructorDeclaration.getBody()));
}
use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.
the class KiePMMLMiningModelFactory method setConstructor.
static void setConstructor(final MiningModelCompilationDTO compilationDTO, final ClassOrInterfaceDeclaration modelTemplate) {
KiePMMLModelFactoryUtils.init(compilationDTO, modelTemplate);
final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
final BlockStmt body = constructorDeclaration.getBody();
ClassOrInterfaceType kiePMMLSegmentationClass = parseClassOrInterfaceType(compilationDTO.getSegmentationCanonicalClassName());
ObjectCreationExpr objectCreationExpr = new ObjectCreationExpr();
objectCreationExpr.setType(kiePMMLSegmentationClass);
CommonCodegenUtils.setAssignExpressionValue(body, "segmentation", objectCreationExpr);
}
use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.
the class KiePMMLRegressionTableFactory method getNumericPredictorExpression.
/**
* Create a <b>NumericPredictor</b> <code>CastExpr</code>
* @param numericPredictor
* @return
*/
static CastExpr getNumericPredictorExpression(final NumericPredictor numericPredictor) {
boolean withExponent = !Objects.equals(1, numericPredictor.getExponent());
final String lambdaExpressionMethodName = withExponent ? "evaluateNumericWithExponent" : "evaluateNumericWithoutExponent";
final String parameterName = "input";
final MethodCallExpr lambdaMethodCallExpr = new MethodCallExpr();
lambdaMethodCallExpr.setName(lambdaExpressionMethodName);
lambdaMethodCallExpr.setScope(new NameExpr(KiePMMLRegressionTable.class.getSimpleName()));
final NodeList<Expression> arguments = new NodeList<>();
arguments.add(0, new NameExpr(parameterName));
arguments.add(1, getExpressionForObject(numericPredictor.getCoefficient().doubleValue()));
if (withExponent) {
arguments.add(2, getExpressionForObject(numericPredictor.getExponent().doubleValue()));
}
lambdaMethodCallExpr.setArguments(arguments);
final ExpressionStmt lambdaExpressionStmt = new ExpressionStmt(lambdaMethodCallExpr);
final LambdaExpr lambdaExpr = new LambdaExpr();
final Parameter lambdaParameter = new Parameter(new UnknownType(), parameterName);
lambdaExpr.setParameters(NodeList.nodeList(lambdaParameter));
lambdaExpr.setBody(lambdaExpressionStmt);
final String doubleClassName = Double.class.getSimpleName();
final ClassOrInterfaceType serializableFunctionType = getTypedClassOrInterfaceTypeByTypeNames(SerializableFunction.class.getCanonicalName(), Arrays.asList(doubleClassName, doubleClassName));
final CastExpr toReturn = new CastExpr();
toReturn.setType(serializableFunctionType);
toReturn.setExpression(lambdaExpr);
return toReturn;
}
Aggregations