Search in sources :

Example 6 with KiePMMLFieldOperatorValue

use of org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue in project drools by kiegroup.

the class KiePMMLDescrLhsFactoryTest method declareConstraintsXor.

@Test
public void declareConstraintsXor() {
    String temperatureField = "TEMPERATURE";
    final List<KiePMMLFieldOperatorValue> xorConstraints = Arrays.asList(new KiePMMLFieldOperatorValue(temperatureField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.LESS_THAN, 35)), null), new KiePMMLFieldOperatorValue(temperatureField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.GREATER_THAN, 85)), null));
    KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintsXor(xorConstraints);
    assertNotNull(lhsBuilder.getDescr());
    assertNotNull(lhsBuilder.getDescr().getDescrs());
    assertEquals(1, lhsBuilder.getDescr().getDescrs().size());
    assertTrue(lhsBuilder.getDescr().getDescrs().get(0) instanceof AndDescr);
    AndDescr rootAndDescr = (AndDescr) lhsBuilder.getDescr().getDescrs().get(0);
    assertEquals(2, rootAndDescr.getDescrs().size());
    assertTrue(rootAndDescr.getDescrs().get(0) instanceof NotDescr);
    assertTrue(rootAndDescr.getDescrs().get(1) instanceof ExistsDescr);
    // "Not" construct
    NotDescr notDescr = (NotDescr) rootAndDescr.getDescrs().get(0);
    assertEquals(1, notDescr.getDescrs().size());
    assertTrue(notDescr.getDescrs().get(0) instanceof AndDescr);
    AndDescr notAndDescr = (AndDescr) notDescr.getDescrs().get(0);
    assertTrue(notAndDescr.getDescrs().get(0) instanceof PatternDescr);
    assertTrue(notAndDescr.getDescrs().get(1) instanceof PatternDescr);
    PatternDescr patternDescr = (PatternDescr) notAndDescr.getDescrs().get(0);
    assertEquals(temperatureField, patternDescr.getObjectType());
    assertNull(patternDescr.getIdentifier());
    assertTrue(patternDescr.getConstraint() instanceof AndDescr);
    AndDescr andDescr = (AndDescr) patternDescr.getConstraint();
    assertEquals(1, andDescr.getDescrs().size());
    assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
    ExprConstraintDescr exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
    assertFalse(exprConstraintDescr.isNegated());
    assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
    String expected = "value < 35";
    assertEquals(expected, exprConstraintDescr.getExpression());
    patternDescr = (PatternDescr) notAndDescr.getDescrs().get(1);
    assertEquals(temperatureField, patternDescr.getObjectType());
    assertNull(patternDescr.getIdentifier());
    assertTrue(patternDescr.getConstraint() instanceof AndDescr);
    andDescr = (AndDescr) patternDescr.getConstraint();
    assertEquals(1, andDescr.getDescrs().size());
    assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
    exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
    assertFalse(exprConstraintDescr.isNegated());
    assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
    expected = "value > 85";
    assertEquals(expected, exprConstraintDescr.getExpression());
    // "Exists" construct
    ExistsDescr existsDescr = (ExistsDescr) rootAndDescr.getDescrs().get(1);
    assertEquals(1, existsDescr.getDescrs().size());
    assertTrue(existsDescr.getDescrs().get(0) instanceof OrDescr);
    OrDescr existsOrDescr = (OrDescr) existsDescr.getDescrs().get(0);
    assertEquals(2, existsOrDescr.getDescrs().size());
    assertTrue(existsOrDescr.getDescrs().get(0) instanceof PatternDescr);
    assertTrue(existsOrDescr.getDescrs().get(1) instanceof OrDescr);
    patternDescr = (PatternDescr) existsOrDescr.getDescrs().get(0);
    assertEquals(temperatureField, patternDescr.getObjectType());
    assertNull(patternDescr.getIdentifier());
    assertTrue(patternDescr.getConstraint() instanceof AndDescr);
    andDescr = (AndDescr) patternDescr.getConstraint();
    assertEquals(1, andDescr.getDescrs().size());
    assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
    exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
    assertFalse(exprConstraintDescr.isNegated());
    assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
    expected = "value < 35";
    assertEquals(expected, exprConstraintDescr.getExpression());
    OrDescr nestedOrDescr = (OrDescr) existsOrDescr.getDescrs().get(1);
    assertEquals(1, nestedOrDescr.getDescrs().size());
    assertTrue(nestedOrDescr.getDescrs().get(0) instanceof PatternDescr);
    patternDescr = (PatternDescr) nestedOrDescr.getDescrs().get(0);
    assertEquals(temperatureField, patternDescr.getObjectType());
    assertNull(patternDescr.getIdentifier());
    assertTrue(patternDescr.getConstraint() instanceof AndDescr);
    andDescr = (AndDescr) patternDescr.getConstraint();
    assertEquals(1, andDescr.getDescrs().size());
    assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
    exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
    assertFalse(exprConstraintDescr.isNegated());
    assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
    expected = "value > 85";
    assertEquals(expected, exprConstraintDescr.getExpression());
}
Also used : NotDescr(org.drools.drl.ast.descr.NotDescr) KiePMMLOperatorValue(org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue) ExistsDescr(org.drools.drl.ast.descr.ExistsDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) OrDescr(org.drools.drl.ast.descr.OrDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 7 with KiePMMLFieldOperatorValue

use of org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue in project drools by kiegroup.

the class KiePMMLDescrLhsFactoryTest method declareConstraintsOr.

@Test
public void declareConstraintsOr() {
    String temperatureField = "TEMPERATURE";
    String humidityField = "HUMIDITY";
    List<KiePMMLFieldOperatorValue> kiePMMLOperatorValues = Arrays.asList(new KiePMMLFieldOperatorValue(temperatureField, BOOLEAN_OPERATOR.AND, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.LESS_THAN, 35)), null), new KiePMMLFieldOperatorValue(humidityField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.GREATER_THAN, 85)), null));
    KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintsAndOr(kiePMMLOperatorValues, lhsBuilder.or());
    assertNotNull(lhsBuilder.getDescr());
    assertEquals(1, lhsBuilder.getDescr().getDescrs().size());
    assertTrue(lhsBuilder.getDescr().getDescrs().get(0) instanceof OrDescr);
    OrDescr baseOrDescr = (OrDescr) lhsBuilder.getDescr().getDescrs().get(0);
    final List<BaseDescr> descrs = baseOrDescr.getDescrs();
    assertNotNull(descrs);
    assertEquals(2, descrs.size());
    // First KiePMMLFieldOperatorValue
    assertTrue(descrs.get(0) instanceof PatternDescr);
    PatternDescr patternDescr = (PatternDescr) descrs.get(0);
    assertEquals(temperatureField, patternDescr.getObjectType());
    assertNull(patternDescr.getIdentifier());
    assertTrue(patternDescr.getConstraint() instanceof AndDescr);
    AndDescr andDescr = (AndDescr) patternDescr.getConstraint();
    assertEquals(1, andDescr.getDescrs().size());
    assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
    ExprConstraintDescr exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
    assertFalse(exprConstraintDescr.isNegated());
    assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
    String expected = "value < 35";
    assertEquals(expected, exprConstraintDescr.getExpression());
    // Second KiePMMLFieldOperatorValue
    assertTrue(descrs.get(1) instanceof PatternDescr);
    patternDescr = (PatternDescr) descrs.get(1);
    assertEquals(humidityField, patternDescr.getObjectType());
    assertNull(patternDescr.getIdentifier());
    assertTrue(patternDescr.getConstraint() instanceof AndDescr);
    andDescr = (AndDescr) patternDescr.getConstraint();
    assertEquals(1, andDescr.getDescrs().size());
    assertTrue(andDescr.getDescrs().get(0) instanceof ExprConstraintDescr);
    exprConstraintDescr = (ExprConstraintDescr) andDescr.getDescrs().get(0);
    assertFalse(exprConstraintDescr.isNegated());
    assertEquals(ExprConstraintDescr.Type.NAMED, exprConstraintDescr.getType());
    expected = "value > 85";
    assertEquals(expected, exprConstraintDescr.getExpression());
}
Also used : KiePMMLOperatorValue(org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) BaseDescr(org.drools.drl.ast.descr.BaseDescr) OrDescr(org.drools.drl.ast.descr.OrDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 8 with KiePMMLFieldOperatorValue

use of org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue in project drools by kiegroup.

the class KiePMMLDescrFactoryTest method getBaseDescr.

@Test
public void getBaseDescr() {
    List<KiePMMLDroolsType> types = new ArrayList<>();
    types.add(KiePMMLDescrTestUtils.getDroolsType());
    types.add(KiePMMLDescrTestUtils.getDottedDroolsType());
    List<KiePMMLFieldOperatorValue> orConstraints = Arrays.asList(new KiePMMLFieldOperatorValue(PATTERN_TYPE, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.LESS_THAN, 35)), null), new KiePMMLFieldOperatorValue(PATTERN_TYPE, BOOLEAN_OPERATOR.AND, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.GREATER_THAN, 85)), null));
    KiePMMLDroolsRule rule = KiePMMLDroolsRule.builder(RULE_NAME, STATUS_TO_SET, Collections.emptyList()).withOrConstraints(orConstraints).build();
    List<KiePMMLDroolsRule> rules = new ArrayList<>();
    rules.add(rule);
    KiePMMLDroolsAST drooledAST = new KiePMMLDroolsAST(types, rules);
    PackageDescr packageDescr = KiePMMLDescrFactory.getBaseDescr(drooledAST, PACKAGE_NAME);
    assertEquals(PACKAGE_NAME, packageDescr.getName());
    checkImports(packageDescr.getImports());
    checkGlobals(packageDescr.getGlobals());
    checkRules(packageDescr.getRules());
}
Also used : KiePMMLDroolsAST(org.kie.pmml.models.drools.ast.KiePMMLDroolsAST) KiePMMLOperatorValue(org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue) ArrayList(java.util.ArrayList) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) KiePMMLDroolsType(org.kie.pmml.models.drools.ast.KiePMMLDroolsType) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 9 with KiePMMLFieldOperatorValue

use of org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue 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;
}
Also used : KiePMMLException(org.kie.pmml.api.exceptions.KiePMMLException) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) CompoundPredicate(org.dmg.pmml.CompoundPredicate) KiePMMLASTFactoryUtils.getConstraintEntriesFromAndOrCompoundPredicate(org.kie.pmml.models.drools.utils.KiePMMLASTFactoryUtils.getConstraintEntriesFromAndOrCompoundPredicate) KiePMMLASTFactoryUtils.getConstraintEntriesFromXOrCompoundPredicate(org.kie.pmml.models.drools.utils.KiePMMLASTFactoryUtils.getConstraintEntriesFromXOrCompoundPredicate) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue)

Example 10 with KiePMMLFieldOperatorValue

use of org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue in project drools by kiegroup.

the class KiePMMLSimplePredicateASTFactory method getBuilderForSimplePredicate.

/**
 * This method will create a <b>rule</b> that, in the RHS,
 * 1) update the status (used for flowing between rules)
 * 2) add <i>outputfields</i> to result variables
 * 3) eventually set the value to accumulate
 * <p>
 * rule "_ResidenceStateScore_1"
 * when
 * $statusHolder : KiePMMLStatusHolder( status == "_ResidenceStateScore" )
 * <p>
 * RESIDENCESTATE( value == "KN" )
 * then
 * <p>
 * $statusHolder.setStatus("_ResidenceStateScore_1");
 * $statusHolder.accumulate("10.0");
 * update($statusHolder);
 * <p>
 * end
 * <p>
 * end
 * @param statusToSet
 * @return
 */
protected KiePMMLDroolsRule.Builder getBuilderForSimplePredicate(final String statusToSet) {
    logger.trace("getBuilderForSimplePredicate {}", statusToSet);
    String statusConstraint = StringUtils.isEmpty(predicateASTFactoryData.getParentPath()) ? KiePMMLAbstractModelASTFactory.STATUS_NULL : String.format(STATUS_PATTERN, predicateASTFactoryData.getParentPath());
    String key = predicateASTFactoryData.getFieldTypeMap().get(((SimplePredicate) predicateASTFactoryData.getPredicate()).getField().getValue()).getGeneratedType();
    OPERATOR operator = OPERATOR.byName(((SimplePredicate) predicateASTFactoryData.getPredicate()).getOperator().value());
    Object value = KiePMMLASTFactoryUtils.getCorrectlyFormattedObject(((SimplePredicate) predicateASTFactoryData.getPredicate()), predicateASTFactoryData.getFieldTypeMap());
    List<KiePMMLFieldOperatorValue> andConstraints = Collections.singletonList(new KiePMMLFieldOperatorValue(key, BOOLEAN_OPERATOR.AND, Collections.singletonList(new KiePMMLOperatorValue(operator, value)), null));
    return KiePMMLDroolsRule.builder(predicateASTFactoryData.getCurrentRule(), statusToSet, predicateASTFactoryData.getOutputFields()).withStatusConstraint(statusConstraint).withAndConstraints(andConstraints);
}
Also used : KiePMMLOperatorValue(org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue) BOOLEAN_OPERATOR(org.kie.pmml.api.enums.BOOLEAN_OPERATOR) OPERATOR(org.kie.pmml.api.enums.OPERATOR) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) SimplePredicate(org.dmg.pmml.SimplePredicate)

Aggregations

KiePMMLFieldOperatorValue (org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue)17 Test (org.junit.Test)14 KiePMMLOperatorValue (org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue)11 ArrayList (java.util.ArrayList)8 SimplePredicate (org.dmg.pmml.SimplePredicate)8 HashMap (java.util.HashMap)7 KiePMMLDroolsRule (org.kie.pmml.models.drools.ast.KiePMMLDroolsRule)7 KiePMMLOriginalTypeGeneratedType (org.kie.pmml.models.drools.tuples.KiePMMLOriginalTypeGeneratedType)7 CompoundPredicate (org.dmg.pmml.CompoundPredicate)4 AndDescr (org.drools.drl.ast.descr.AndDescr)4 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)4 PatternDescr (org.drools.drl.ast.descr.PatternDescr)4 BOOLEAN_OPERATOR (org.kie.pmml.api.enums.BOOLEAN_OPERATOR)4 OPERATOR (org.kie.pmml.api.enums.OPERATOR)4 KiePMMLException (org.kie.pmml.api.exceptions.KiePMMLException)4 KiePMMLASTTestUtils.getPredicateASTFactoryData (org.kie.pmml.models.drools.utils.KiePMMLASTTestUtils.getPredicateASTFactoryData)4 List (java.util.List)3 Map (java.util.Map)3 Consumer (java.util.function.Consumer)3 Collectors (java.util.stream.Collectors)3