use of org.kie.pmml.api.enums.BOOLEAN_OPERATOR in project drools by kiegroup.
the class KiePMMLCompoundPredicateFactory method getCompoundPredicateVariableDeclaration.
static BlockStmt getCompoundPredicateVariableDeclaration(final String variableName, final CompoundPredicate compoundPredicate, final List<Field<?>> fields) {
final MethodDeclaration methodDeclaration = COMPOUND_PREDICATE_TEMPLATE.getMethodsByName(GETKIEPMMLCOMPOUNDPREDICATE).get(0).clone();
final BlockStmt compoundPredicateBody = methodDeclaration.getBody().orElseThrow(() -> new KiePMMLException(String.format(MISSING_BODY_TEMPLATE, methodDeclaration)));
final VariableDeclarator variableDeclarator = getVariableDeclarator(compoundPredicateBody, COMPOUND_PREDICATE).orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_IN_BODY, COMPOUND_PREDICATE, compoundPredicateBody)));
variableDeclarator.setName(variableName);
final BlockStmt toReturn = new BlockStmt();
int counter = 0;
final NodeList<Expression> arguments = new NodeList<>();
for (Predicate predicate : compoundPredicate.getPredicates()) {
String nestedVariableName = String.format(VARIABLE_NAME_TEMPLATE, variableName, counter);
arguments.add(new NameExpr(nestedVariableName));
BlockStmt toAdd = getKiePMMLPredicate(nestedVariableName, predicate, fields);
toAdd.getStatements().forEach(toReturn::addStatement);
counter++;
}
final BOOLEAN_OPERATOR booleanOperator = BOOLEAN_OPERATOR.byName(compoundPredicate.getBooleanOperator().value());
final NameExpr booleanOperatorExpr = new NameExpr(BOOLEAN_OPERATOR.class.getName() + "." + booleanOperator.name());
final MethodCallExpr initializer = variableDeclarator.getInitializer().orElseThrow(() -> new KiePMMLException(String.format(MISSING_VARIABLE_INITIALIZER_TEMPLATE, COMPOUND_PREDICATE, compoundPredicateBody))).asMethodCallExpr();
final MethodCallExpr builder = getChainedMethodCallExprFrom("builder", initializer);
builder.setArgument(1, booleanOperatorExpr);
getChainedMethodCallExprFrom("asList", initializer).setArguments(arguments);
compoundPredicateBody.getStatements().forEach(toReturn::addStatement);
return toReturn;
}
use of org.kie.pmml.api.enums.BOOLEAN_OPERATOR in project drools by kiegroup.
the class KiePMMLCompoundPredicateInstanceFactory method getKiePMMLCompoundPredicate.
static KiePMMLCompoundPredicate getKiePMMLCompoundPredicate(final CompoundPredicate compoundPredicate, final List<Field<?>> fields) {
final BOOLEAN_OPERATOR booleanOperator = BOOLEAN_OPERATOR.byName(compoundPredicate.getBooleanOperator().value());
final List<KiePMMLPredicate> kiePMMLPredicates = compoundPredicate.hasPredicates() ? getKiePMMLPredicates(compoundPredicate.getPredicates(), fields) : Collections.emptyList();
return KiePMMLCompoundPredicate.builder(getKiePMMLExtensions(compoundPredicate.getExtensions()), booleanOperator).withKiePMMLPredicates(kiePMMLPredicates).build();
}
use of org.kie.pmml.api.enums.BOOLEAN_OPERATOR in project drools by kiegroup.
the class KiePMMLScorecardModelCharacteristicASTFactoryTest method declareRulesFromCharacteristics.
@Test
public void declareRulesFromCharacteristics() {
Characteristics characteristics = scorecardModel.getCharacteristics();
String parentPath = "_will";
List<KiePMMLDroolsRule> retrieved = getKiePMMLScorecardModelCharacteristicASTFactory().declareRulesFromCharacteristics(characteristics, parentPath, null);
final List<Characteristic> characteristicList = characteristics.getCharacteristics();
List<Attribute> attributes = new ArrayList<>();
AtomicInteger counter = new AtomicInteger(0);
for (int i = 0; i < characteristicList.size(); i++) {
Characteristic characteristic = characteristicList.get(i);
attributes.addAll(characteristic.getAttributes());
for (int j = 0; j < characteristic.getAttributes().size(); j++) {
Attribute attribute = characteristic.getAttributes().get(j);
KiePMMLDroolsRule rule = retrieved.get(counter.incrementAndGet());
int expectedOperatorValuesSize = 1;
Integer expectedAndConstraints = null;
Integer expectedInConstraints = null;
BOOLEAN_OPERATOR expectedOperator = BOOLEAN_OPERATOR.AND;
if (attribute.getPredicate() instanceof SimplePredicate) {
expectedAndConstraints = 1;
}
if (attribute.getPredicate() instanceof CompoundPredicate) {
expectedOperatorValuesSize = ((CompoundPredicate) attribute.getPredicate()).getPredicates().size();
expectedAndConstraints = 1;
}
if (attribute.getPredicate() instanceof SimpleSetPredicate) {
expectedInConstraints = 1;
}
boolean isLastCharacteristic = (i == characteristicList.size() - 1);
String statusToSet = isLastCharacteristic ? DONE : String.format(PATH_PATTERN, parentPath, characteristicList.get(i + 1).getName());
commonValidateRule(rule, attribute, statusToSet, parentPath + "_" + characteristic.getName(), j, isLastCharacteristic, expectedAndConstraints, expectedInConstraints, expectedOperator, null, expectedOperatorValuesSize);
}
}
assertEquals(attributes.size() + 1, retrieved.size());
}
Aggregations