use of org.kie.pmml.models.drools.ast.KiePMMLDroolsRule in project drools by kiegroup.
the class KiePMMLDescrRhsFactoryTest method declareDefaultThenWithoutResult.
@Test
public void declareDefaultThenWithoutResult() {
String name = "NAME";
String statusToSet = "STATUS_TO_SET";
KiePMMLDroolsRule rule = KiePMMLDroolsRule.builder(name, statusToSet, Collections.emptyList()).build();
KiePMMLDescrRhsFactory.factory(ruleBuilder).declareDefaultThen(rule);
assertNotNull(ruleBuilder.getDescr().getConsequence());
String expectedConsequence = String.format(UPDATE_STATUS_HOLDER_STATUS, statusToSet);
assertTrue(ruleBuilder.getDescr().getConsequence().toString().contains(expectedConsequence));
assertNotNull(ruleBuilder.getDescr().getNamedConsequences());
assertTrue(ruleBuilder.getDescr().getNamedConsequences().isEmpty());
}
use of org.kie.pmml.models.drools.ast.KiePMMLDroolsRule in project drools by kiegroup.
the class KiePMMLDescrRhsFactoryTest method commonDeclareThen.
@Test
public void commonDeclareThen() {
String ruleName = "RULENAME";
String statusToSet = "STATUSTOSET";
String outputFieldName = "OUTPUTFIELDNAME";
Object result = "RESULT";
OutputField outputField = new OutputField();
outputField.setName(FieldName.create(outputFieldName));
outputField.setResultFeature(ResultFeature.PREDICTED_VALUE);
List<OutputField> outputFields = Collections.singletonList(outputField);
KiePMMLDroolsRule.Builder builder = KiePMMLDroolsRule.builder(ruleName, statusToSet, outputFields);
KiePMMLDroolsRule rule = builder.build();
StringJoiner joiner = new StringJoiner("");
KiePMMLDescrRhsFactory.factory(ruleBuilder).commonDeclareThen(rule, joiner);
String retrieved = joiner.toString();
assertTrue(retrieved.isEmpty());
//
ResultCode resultCode = ResultCode.OK;
builder = builder.withResultCode(resultCode);
rule = builder.build();
joiner = new StringJoiner("");
KiePMMLDescrRhsFactory.factory(ruleBuilder).commonDeclareThen(rule, joiner);
retrieved = joiner.toString();
String expected = String.format(SET_PMML4_RESULT_CODE, resultCode);
assertTrue(retrieved.contains(expected));
//
builder = builder.withResult(result);
rule = builder.build();
joiner = new StringJoiner("");
KiePMMLDescrRhsFactory.factory(ruleBuilder).commonDeclareThen(rule, joiner);
retrieved = joiner.toString();
expected = String.format(ADD_PMML4_RESULT_VARIABLE, result);
assertTrue(retrieved.contains(expected));
expected = String.format(ADD_PMML4_OUTPUT_FIELD, outputFieldName, result);
assertTrue(retrieved.contains(expected));
//
String focusedAgendaGroup = "FOCUSEDAGENDAGROUP";
builder = builder.withFocusedAgendaGroup(focusedAgendaGroup);
rule = builder.build();
joiner = new StringJoiner("");
KiePMMLDescrRhsFactory.factory(ruleBuilder).commonDeclareThen(rule, joiner);
retrieved = joiner.toString();
expected = String.format(FOCUS_AGENDA_GROUP, focusedAgendaGroup);
assertTrue(retrieved.contains(expected));
//
}
use of org.kie.pmml.models.drools.ast.KiePMMLDroolsRule in project drools by kiegroup.
the class KiePMMLDescrRhsFactoryTest method declareRhsWithIfBreak.
@Test
public void declareRhsWithIfBreak() {
String name = "NAME";
String statusToSet = "STATUS_TO_SET";
String ifBreakField = "ifBreakField";
String ifBreakOperator = "ifBreakOperator";
Object ifBreakValue = "ifBreakValue";
KiePMMLDroolsRule rule = KiePMMLDroolsRule.builder(name, statusToSet, Collections.emptyList()).withIfBreak(ifBreakField, ifBreakOperator, ifBreakValue).build();
KiePMMLDescrRhsFactory.factory(ruleBuilder).declareRhs(rule);
assertNotNull(ruleBuilder.getDescr().getConsequence());
String expectedConsequence = String.format(UPDATE_STATUS_HOLDER_STATUS, statusToSet);
assertTrue(ruleBuilder.getDescr().getConsequence().toString().contains(expectedConsequence));
assertNotNull(ruleBuilder.getDescr().getNamedConsequences());
assertEquals(1, ruleBuilder.getDescr().getNamedConsequences().size());
assertNotNull(ruleBuilder.getDescr().getNamedConsequences().get(BREAK_LABEL));
}
use of org.kie.pmml.models.drools.ast.KiePMMLDroolsRule in project drools by kiegroup.
the class KiePMMLDescrRhsFactoryTest method declareIfThenWithoutResult.
@Test
public void declareIfThenWithoutResult() {
String name = "NAME";
String statusToSet = "STATUS_TO_SET";
KiePMMLDroolsRule rule = KiePMMLDroolsRule.builder(name, statusToSet, Collections.emptyList()).build();
KiePMMLDescrRhsFactory.factory(ruleBuilder).declareIfThen(rule);
assertNotNull(ruleBuilder.getDescr().getConsequence());
String expectedConsequence = String.format(UPDATE_STATUS_HOLDER_STATUS, statusToSet);
assertEquals(expectedConsequence, ruleBuilder.getDescr().getConsequence());
assertNotNull(ruleBuilder.getDescr().getNamedConsequences());
assertEquals(1, ruleBuilder.getDescr().getNamedConsequences().size());
assertNotNull(ruleBuilder.getDescr().getNamedConsequences().get(BREAK_LABEL));
expectedConsequence = String.format(UPDATE_STATUS_HOLDER_STATUS, DONE);
assertTrue(expectedConsequence, ruleBuilder.getDescr().getNamedConsequences().get(BREAK_LABEL).toString().contains(expectedConsequence));
}
use of org.kie.pmml.models.drools.ast.KiePMMLDroolsRule in project drools by kiegroup.
the class KiePMMLCompoundPredicateASTFactoryTest method declareRuleFromCompoundPredicateSurrogateNotFinalLeaf.
@Test
public void declareRuleFromCompoundPredicateSurrogateNotFinalLeaf() {
final Map<String, KiePMMLOriginalTypeGeneratedType> fieldTypeMap = new HashMap<>();
final List<SimplePredicate> predicates = getSimplePredicates(fieldTypeMap);
String parentPath = "_will play";
String currentRule = "_will play_will play";
String result = "RESULT";
CompoundPredicate compoundPredicate = new CompoundPredicate();
compoundPredicate.setBooleanOperator(CompoundPredicate.BooleanOperator.SURROGATE);
predicates.forEach(compoundPredicate::addPredicates);
final List<KiePMMLDroolsRule> rules = new ArrayList<>();
PredicateASTFactoryData predicateASTFactoryData = getPredicateASTFactoryData(compoundPredicate, Collections.emptyList(), rules, parentPath, currentRule, fieldTypeMap);
KiePMMLCompoundPredicateASTFactory.factory(predicateASTFactoryData).declareRuleFromCompoundPredicate(result, false);
// For each "surrogate" predicate two rules -"TRUE" and "FALSE" - are generated; one more rule is generated for the Compound predicate itself
int expectedRules = (predicates.size() * 2) + 1;
assertEquals(expectedRules, rules.size());
String agendaActivationGroup = String.format(SURROGATE_GROUP_PATTERN, currentRule);
for (KiePMMLDroolsRule retrieved : rules) {
String ruleName = retrieved.getName();
if (ruleName.contains("_surrogate_")) {
String[] ruleNameParts = ruleName.split("_surrogate_");
String generatedTypePart = ruleNameParts[1];
boolean isTrueRule = generatedTypePart.endsWith("_TRUE");
final String generatedType = generatedTypePart.replace("_TRUE", "").replace("_FALSE", "");
final Optional<String> fieldName = fieldTypeMap.entrySet().stream().filter(entry -> generatedType.equals(entry.getValue().getGeneratedType())).map(Map.Entry::getKey).findFirst();
if (fieldName.isPresent()) {
SimplePredicate mappedPredicate = predicates.stream().filter(pred -> fieldName.get().equals(pred.getField().getValue())).findFirst().orElse(null);
assertNotNull(mappedPredicate);
assertNull(retrieved.getStatusConstraint());
assertEquals(agendaActivationGroup, retrieved.getActivationGroup());
assertEquals(agendaActivationGroup, retrieved.getAgendaGroup());
// Those are not in a final leaf node
if (isTrueRule) {
assertEquals(currentRule, retrieved.getStatusToSet());
assertNull(retrieved.getResult());
assertNull(retrieved.getResultCode());
} else {
assertEquals(parentPath, retrieved.getStatusToSet());
assertNull(retrieved.getResult());
assertNull(retrieved.getResultCode());
}
}
} else {
assertNotNull(retrieved.getStatusConstraint());
assertEquals(String.format(STATUS_PATTERN, parentPath), retrieved.getStatusConstraint());
assertEquals(agendaActivationGroup, retrieved.getFocusedAgendaGroup());
assertNull(retrieved.getStatusToSet());
assertNull(retrieved.getResult());
assertNull(retrieved.getResultCode());
}
}
}
Aggregations