Search in sources :

Example 16 with KiePMMLFieldOperatorValue

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

the class KiePMMLDescrLhsFactoryTest method declareNestedConstraintAnd.

@Test
public void declareNestedConstraintAnd() {
    String temperatureField = "TEMPERATURE";
    String humidityField = "HUMIDITY";
    final List<KiePMMLFieldOperatorValue> nestedKiePMMLFieldOperatorValues = Arrays.asList(new KiePMMLFieldOperatorValue(humidityField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.LESS_THAN, 56)), null), new KiePMMLFieldOperatorValue(humidityField, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.GREATER_THAN, 91)), null));
    List<KiePMMLFieldOperatorValue> kiePMMLOperatorValues = Collections.singletonList(new KiePMMLFieldOperatorValue(temperatureField, BOOLEAN_OPERATOR.AND, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.LESS_THAN, 35)), nestedKiePMMLFieldOperatorValues));
    KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintsAndOr(kiePMMLOperatorValues, lhsBuilder.and());
    assertNotNull(lhsBuilder.getDescr());
    assertEquals(1, lhsBuilder.getDescr().getDescrs().size());
    assertTrue(lhsBuilder.getDescr().getDescrs().get(0) instanceof AndDescr);
    AndDescr baseAndDescr = (AndDescr) lhsBuilder.getDescr().getDescrs().get(0);
    final List<BaseDescr> descrs = baseAndDescr.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());
    // Nested KiePMMLFieldOperatorValues
    assertTrue(descrs.get(1) instanceof AndDescr);
    AndDescr nestedAndDescr = (AndDescr) descrs.get(1);
    assertEquals(2, nestedAndDescr.getDescrs().size());
    final List<BaseDescr> nestedDescrs = nestedAndDescr.getDescrs();
    // First nested KiePMMLFieldOperatorValue
    assertTrue(nestedDescrs.get(0) instanceof PatternDescr);
    patternDescr = (PatternDescr) nestedDescrs.get(0);
    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 < 56";
    assertEquals(expected, exprConstraintDescr.getExpression());
    // Second nested KiePMMLFieldOperatorValue
    assertTrue(nestedDescrs.get(1) instanceof PatternDescr);
    patternDescr = (PatternDescr) nestedDescrs.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 > 91";
    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) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 17 with KiePMMLFieldOperatorValue

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

the class KiePMMLDescrRulesFactoryTest method declareRule.

@Test
public void declareRule() {
    String name = "NAME";
    String statusToSet = "STATUS_TO_SET";
    String patternType = "TEMPERATURE";
    String agendaGroup = "agendaGroup";
    String activationGroup = "activationGroup";
    List<KiePMMLFieldOperatorValue> orConstraints = Arrays.asList(new KiePMMLFieldOperatorValue(patternType, BOOLEAN_OPERATOR.OR, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.LESS_THAN, 35)), null), new KiePMMLFieldOperatorValue(patternType, BOOLEAN_OPERATOR.AND, Collections.singletonList(new KiePMMLOperatorValue(OPERATOR.GREATER_THAN, 85)), null));
    KiePMMLDroolsRule rule = KiePMMLDroolsRule.builder(name, statusToSet, Collections.emptyList()).withAgendaGroup(agendaGroup).withActivationGroup(activationGroup).withOrConstraints(orConstraints).build();
    KiePMMLDescrRulesFactory.factory(builder).declareRule(rule);
    assertNotNull(builder.getDescr().getRules());
    assertEquals(1, builder.getDescr().getRules().size());
    final RuleDescr retrieved = builder.getDescr().getRules().get(0);
    assertEquals(name, retrieved.getName());
    assertEquals(2, retrieved.getAttributes().size());
    assertTrue(retrieved.getAttributes().containsKey("agenda-group"));
    assertEquals(agendaGroup, retrieved.getAttributes().get("agenda-group").getValue());
    assertTrue(retrieved.getAttributes().containsKey("activation-group"));
    assertEquals(activationGroup, retrieved.getAttributes().get("activation-group").getValue());
}
Also used : KiePMMLOperatorValue(org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue) RuleDescr(org.drools.drl.ast.descr.RuleDescr) KiePMMLDroolsRule(org.kie.pmml.models.drools.ast.KiePMMLDroolsRule) KiePMMLFieldOperatorValue(org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue) Test(org.junit.Test)

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