use of org.dmg.pmml.CompoundPredicate in project drools by kiegroup.
the class KiePMMLCompoundPredicateInstanceFactoryTest method getKiePMMLCompoundPredicate.
@Test
public void getKiePMMLCompoundPredicate() {
List<Field<?>> fields = IntStream.range(0, 3).mapToObj(i -> getRandomDataField()).collect(Collectors.toList());
final CompoundPredicate toConvert = getRandomCompoundPredicate(fields);
final KiePMMLCompoundPredicate retrieved = KiePMMLCompoundPredicateInstanceFactory.getKiePMMLCompoundPredicate(toConvert, fields);
commonVerifyKKiePMMLCompoundPredicate(retrieved, toConvert);
}
use of org.dmg.pmml.CompoundPredicate in project drools by kiegroup.
the class KiePMMLPredicateInstanceFactoryTest method getKiePMMLPredicate.
@Test
public void getKiePMMLPredicate() {
List<Field<?>> fields = IntStream.range(0, 3).mapToObj(i -> getRandomDataField()).collect(Collectors.toList());
SimplePredicate simplePredicate1 = getRandomSimplePredicate((DataField) fields.get(0));
KiePMMLPredicate retrieved = KiePMMLPredicateInstanceFactory.getKiePMMLPredicate(simplePredicate1, fields);
commonVerifyKiePMMLPredicate(retrieved, simplePredicate1);
SimpleSetPredicate simpleSetPredicate = getRandomSimpleSetPredicate((DataField) fields.get(2));
retrieved = KiePMMLPredicateInstanceFactory.getKiePMMLPredicate(simpleSetPredicate, fields);
commonVerifyKiePMMLPredicate(retrieved, simpleSetPredicate);
final CompoundPredicate compoundPredicate = getRandomCompoundPredicate(fields);
retrieved = KiePMMLPredicateInstanceFactory.getKiePMMLPredicate(compoundPredicate, fields);
commonVerifyKiePMMLPredicate(retrieved, compoundPredicate);
False falsePredicate = new False();
retrieved = KiePMMLPredicateInstanceFactory.getKiePMMLPredicate(falsePredicate, fields);
commonVerifyKiePMMLPredicate(retrieved, falsePredicate);
True truePredicate = new True();
retrieved = KiePMMLPredicateInstanceFactory.getKiePMMLPredicate(truePredicate, fields);
commonVerifyKiePMMLPredicate(retrieved, truePredicate);
}
use of org.dmg.pmml.CompoundPredicate 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.dmg.pmml.CompoundPredicate in project drools by kiegroup.
the class KiePMMLCompoundPredicateASTFactory method getBuilderForCompoundPredicateAndOrXor.
/**
* Method to be invoked when <b>compoundPredicate.getBooleanOperator()</b> is <code>AND</code>, <code>OR</code> or
* <XOR>XOR</XOR>. Throws exception otherwise
* @param statusToSet
*/
private KiePMMLDroolsRule.Builder getBuilderForCompoundPredicateAndOrXor(final String statusToSet) {
logger.trace("getBuilderForCompoundPredicateAndOrXor {}", statusToSet);
CompoundPredicate compoundPredicate = (CompoundPredicate) predicateASTFactoryData.getPredicate();
if (!CompoundPredicate.BooleanOperator.AND.equals(compoundPredicate.getBooleanOperator()) && !CompoundPredicate.BooleanOperator.OR.equals((compoundPredicate.getBooleanOperator())) && !CompoundPredicate.BooleanOperator.XOR.equals((compoundPredicate.getBooleanOperator()))) {
throw new KiePMMLException(String.format("getBuilderForCompoundPredicateAndOrXor invoked with %s CompoundPredicate", compoundPredicate.getBooleanOperator()));
}
String statusConstraint = StringUtils.isEmpty(predicateASTFactoryData.getParentPath()) ? KiePMMLAbstractModelASTFactory.STATUS_NULL : String.format(STATUS_PATTERN, predicateASTFactoryData.getParentPath());
List<KiePMMLFieldOperatorValue> constraints;
KiePMMLDroolsRule.Builder toReturn = KiePMMLDroolsRule.builder(predicateASTFactoryData.getCurrentRule(), statusToSet, predicateASTFactoryData.getOutputFields()).withStatusConstraint(statusConstraint);
switch(compoundPredicate.getBooleanOperator()) {
case AND:
constraints = getConstraintEntriesFromAndOrCompoundPredicate(compoundPredicate, predicateASTFactoryData.getFieldTypeMap());
toReturn = toReturn.withAndConstraints(constraints);
break;
case OR:
constraints = getConstraintEntriesFromAndOrCompoundPredicate(compoundPredicate, predicateASTFactoryData.getFieldTypeMap());
toReturn = toReturn.withOrConstraints(constraints);
break;
case XOR:
constraints = getConstraintEntriesFromXOrCompoundPredicate(compoundPredicate, predicateASTFactoryData.getFieldTypeMap());
toReturn = toReturn.withXorConstraints(constraints);
break;
default:
throw new IllegalStateException(String.format("CompoundPredicate.booleanOperator should never be %s at this point", compoundPredicate.getBooleanOperator()));
}
return toReturn;
}
use of org.dmg.pmml.CompoundPredicate in project drools by kiegroup.
the class KiePMMLCompoundPredicateASTFactory method declareRuleFromCompoundPredicateSurrogate.
/**
* Method to be invoked when <b>compoundPredicate.getBooleanOperator()</b> is <code>SURROGATE</code>.
* Throws exception otherwise
* @param agendaActivationGroup
* @param statusToSet
*/
private void declareRuleFromCompoundPredicateSurrogate(final String agendaActivationGroup, final String statusToSet) {
logger.trace("declareRuleFromCompoundPredicateSurrogate {} {}", agendaActivationGroup, statusToSet);
CompoundPredicate compoundPredicate = (CompoundPredicate) predicateASTFactoryData.getPredicate();
if (!CompoundPredicate.BooleanOperator.SURROGATE.equals(compoundPredicate.getBooleanOperator())) {
throw new KiePMMLException(String.format("declareRuleFromCompoundPredicateSurrogate invoked with %s CompoundPredicate", compoundPredicate.getBooleanOperator()));
}
KiePMMLDroolsRule.Builder builder = KiePMMLDroolsRule.builder(predicateASTFactoryData.getCurrentRule(), null, predicateASTFactoryData.getOutputFields()).withStatusConstraint(String.format(STATUS_PATTERN, predicateASTFactoryData.getParentPath())).withFocusedAgendaGroup(agendaActivationGroup);
predicateASTFactoryData.getRules().add(builder.build());
}
Aggregations