use of org.kie.pmml.api.models.OutputField in project drools by kiegroup.
the class KiePMMLModelFactoryUtils method init.
/**
* Initialize the given <code>ClassOrInterfaceDeclaration</code> with all the <b>common</b> code needed to
* generate a <code>KiePMMLModel</code>
* @param compilationDTO
* @param modelTemplate
*/
public static void init(final CompilationDTO<? extends Model> compilationDTO, final ClassOrInterfaceDeclaration modelTemplate) {
final ConstructorDeclaration constructorDeclaration = modelTemplate.getDefaultConstructor().orElseThrow(() -> new KiePMMLInternalException(String.format(MISSING_DEFAULT_CONSTRUCTOR, modelTemplate.getName())));
final String name = compilationDTO.getModelName();
final String generatedClassName = compilationDTO.getSimpleClassName();
final List<MiningField> miningFields = compilationDTO.getKieMiningFields();
final List<OutputField> outputFields = compilationDTO.getKieOutputFields();
final List<TargetField> targetFields = compilationDTO.getKieTargetFields();
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();
}
final PMML_MODEL pmmlModelEnum = compilationDTO.getPMML_MODEL();
final NameExpr pmmlMODELExpression = new NameExpr(pmmlModelEnum.getClass().getName() + "." + pmmlModelEnum.name());
String targetFieldName = compilationDTO.getTargetFieldName();
final Expression targetFieldExpression;
if (targetFieldName != null) {
targetFieldExpression = new StringLiteralExpr(targetFieldName);
} else {
targetFieldExpression = new NullLiteralExpr();
}
setKiePMMLModelConstructor(generatedClassName, constructorDeclaration, name, miningFields, outputFields, targetFields);
addTransformationsInClassOrInterfaceDeclaration(modelTemplate, compilationDTO.getTransformationDictionary(), compilationDTO.getLocalTransformations());
final BlockStmt body = constructorDeclaration.getBody();
CommonCodegenUtils.setAssignExpressionValue(body, "pmmlMODEL", pmmlMODELExpression);
CommonCodegenUtils.setAssignExpressionValue(body, "miningFunction", miningFunctionExpression);
CommonCodegenUtils.setAssignExpressionValue(body, "targetField", targetFieldExpression);
addGetCreatedKiePMMLMiningFieldsMethod(modelTemplate, compilationDTO.getMiningSchema().getMiningFields(), compilationDTO.getFields());
MethodCallExpr getCreatedKiePMMLMiningFieldsExpr = new MethodCallExpr();
getCreatedKiePMMLMiningFieldsExpr.setScope(new ThisExpr());
getCreatedKiePMMLMiningFieldsExpr.setName(GET_CREATED_KIEPMMLMININGFIELDS);
CommonCodegenUtils.setAssignExpressionValue(body, "kiePMMLMiningFields", getCreatedKiePMMLMiningFieldsExpr);
if (compilationDTO.getOutput() != null) {
addGetCreatedKiePMMLOutputFieldsMethod(modelTemplate, compilationDTO.getOutput().getOutputFields());
MethodCallExpr getCreatedKiePMMLOutputFieldsExpr = new MethodCallExpr();
getCreatedKiePMMLOutputFieldsExpr.setScope(new ThisExpr());
getCreatedKiePMMLOutputFieldsExpr.setName(GET_CREATED_KIEPMMLOUTPUTFIELDS);
CommonCodegenUtils.setAssignExpressionValue(body, "kiePMMLOutputFields", getCreatedKiePMMLOutputFieldsExpr);
}
}
use of org.kie.pmml.api.models.OutputField in project drools by kiegroup.
the class KiePMMLModelFactoryUtilsTest method setKiePMMLModelConstructor.
@Test
public void setKiePMMLModelConstructor() {
String generatedClassName = "generatedClassName";
String name = "newName";
List<MiningField> miningFields = IntStream.range(0, 3).mapToObj(i -> ModelUtils.convertToKieMiningField(getRandomMiningField(), getRandomDataField())).collect(Collectors.toList());
List<OutputField> outputFields = IntStream.range(0, 2).mapToObj(i -> ModelUtils.convertToKieOutputField(getRandomOutputField(), getRandomDataField())).collect(Collectors.toList());
List<TargetField> targetFields = IntStream.range(0, 2).mapToObj(i -> ModelUtils.convertToKieTargetField(getRandomTarget())).collect(Collectors.toList());
KiePMMLModelFactoryUtils.setKiePMMLModelConstructor(generatedClassName, constructorDeclaration, name, miningFields, outputFields, targetFields);
commonVerifySuperInvocation(generatedClassName, name);
List<MethodCallExpr> retrieved = getMethodCallExprList(constructorDeclaration.getBody(), miningFields.size(), "miningFields", "add");
MethodCallExpr addMethodCall = retrieved.get(0);
NodeList<Expression> arguments = addMethodCall.getArguments();
commonVerifyMiningFieldsObjectCreation(arguments, miningFields);
retrieved = getMethodCallExprList(constructorDeclaration.getBody(), outputFields.size(), "outputFields", "add");
addMethodCall = retrieved.get(0);
arguments = addMethodCall.getArguments();
commonVerifyOutputFieldsObjectCreation(arguments, outputFields);
retrieved = getMethodCallExprList(constructorDeclaration.getBody(), outputFields.size(), "kiePMMLTargets", "add");
addMethodCall = retrieved.get(0);
arguments = addMethodCall.getArguments();
commonVerifyKiePMMLTargetFieldsMethodCallExpr(arguments, targetFields);
}
use of org.kie.pmml.api.models.OutputField in project drools by kiegroup.
the class KiePMMLModelFactoryUtilsTest method commonVerifyOutputFieldsObjectCreation.
private void commonVerifyOutputFieldsObjectCreation(List<Expression> toVerify, List<OutputField> outputFields) {
toVerify.forEach(argument -> {
assertTrue(argument instanceof ObjectCreationExpr);
ObjectCreationExpr objCrt = (ObjectCreationExpr) argument;
assertEquals(OutputField.class.getCanonicalName(), objCrt.getType().asString());
Optional<OutputField> outputFieldOpt = outputFields.stream().filter(outputField -> outputField.getName().equals(objCrt.getArgument(0).asStringLiteralExpr().asString())).findFirst();
assertTrue(outputFieldOpt.isPresent());
OutputField outputField = outputFieldOpt.get();
String expected = OP_TYPE.class.getCanonicalName() + "." + outputField.getOpType();
assertEquals(expected, objCrt.getArgument(1).asNameExpr().toString());
expected = DATA_TYPE.class.getCanonicalName() + "." + outputField.getDataType();
assertEquals(expected, objCrt.getArgument(2).asNameExpr().toString());
expected = outputField.getTargetField();
assertEquals(expected, objCrt.getArgument(3).asStringLiteralExpr().asString());
expected = RESULT_FEATURE.class.getCanonicalName() + "." + outputField.getResultFeature();
assertEquals(expected, objCrt.getArgument(4).asNameExpr().toString());
MethodCallExpr allowedValuesMethod = objCrt.getArgument(5).asMethodCallExpr();
IntStream.range(0, 3).forEach(i -> {
String exp = outputField.getAllowedValues().get(i);
assertEquals(exp, allowedValuesMethod.getArgument(i).asStringLiteralExpr().asString());
});
});
}
use of org.kie.pmml.api.models.OutputField in project drools by kiegroup.
the class KiePMMLModelFactoryUtilsTest method getOutputFieldsObjectCreations.
@Test
public void getOutputFieldsObjectCreations() {
List<OutputField> outputFields = IntStream.range(0, 2).mapToObj(i -> ModelUtils.convertToKieOutputField(getRandomOutputField(), getRandomDataField())).collect(Collectors.toList());
List retrieved = KiePMMLModelFactoryUtils.getOutputFieldsObjectCreations(outputFields);
commonVerifyOutputFieldsObjectCreation(retrieved, outputFields);
}
use of org.kie.pmml.api.models.OutputField in project kogito-runtimes by kiegroup.
the class PMMLOASResultFactoryTest method getPMMLOASResultNoMiningFieldsNoOutputFields.
@Test
void getPMMLOASResultNoMiningFieldsNoOutputFields() {
final List<MiningField> miningFields = Collections.emptyList();
final List<OutputField> outputFields = Collections.emptyList();
final KiePMMLModel kiePMMLModel = getKiePMMLModelInternal(miningFields, outputFields);
final PMMLOASResult retrieved = PMMLOASResultFactory.getPMMLOASResult(kiePMMLModel);
assertNotNull(retrieved);
final ObjectNode jsonNodes = retrieved.jsonSchemaNode();
assertNotNull(jsonNodes);
assertFalse(jsonNodes.isEmpty());
assertNotNull(jsonNodes.get(DEFINITIONS));
final JsonNode definitionsNode = jsonNodes.get(DEFINITIONS);
assertFalse(definitionsNode.isEmpty());
commonValidateInputSet(definitionsNode.get(INPUT_SET), miningFields);
assertNull(definitionsNode.get(RESULT_SET));
assertNotNull(definitionsNode.get(OUTPUT_SET));
assertNull(definitionsNode.get(OUTPUT_SET).get(RESULT_VARIABLES));
}
Aggregations