Search in sources :

Example 71 with ExprConstraintDescr

use of org.drools.drl.ast.descr.ExprConstraintDescr in project drools by kiegroup.

the class PatternBuilder method lookAheadFieldsOfIdentifier.

public static Collection<String> lookAheadFieldsOfIdentifier(RuleDescr ruleDescr, PatternDescr patternDescr) {
    String identifier = patternDescr.getIdentifier();
    if (identifier == null) {
        return Collections.emptyList();
    }
    Collection<String> props = new HashSet<>();
    for (PatternDescr pattern : ruleDescr.getLhs().getAllPatternDescr()) {
        if (pattern == patternDescr) {
            continue;
        }
        if (pattern != null) {
            for (BaseDescr expr : pattern.getDescrs()) {
                if (expr instanceof ExprConstraintDescr) {
                    String text = expr.getText();
                    int pos = text.indexOf(identifier + ".");
                    if (pos == 0 || (pos > 0 && !Character.isJavaIdentifierPart(text.charAt(pos - 1)))) {
                        String prop = StringUtils.extractFirstIdentifier(text, pos + identifier.length() + 1);
                        String propFromGetter = ClassUtils.getter2property(prop);
                        props.add(propFromGetter != null ? propFromGetter : StringUtils.lcFirst(prop));
                    }
                }
            }
            if (isPassThroughPattern(pattern, identifier)) {
                props.addAll(collectProps(pattern));
            }
        }
    }
    return props;
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) HashSet(java.util.HashSet)

Example 72 with ExprConstraintDescr

use of org.drools.drl.ast.descr.ExprConstraintDescr in project drools by kiegroup.

the class PatternDescrBuilderImpl method constraint.

public PatternDescrBuilder<P> constraint(String constraint, boolean positional) {
    ExprConstraintDescr constr = new ExprConstraintDescr(constraint);
    constr.setType(positional ? ExprConstraintDescr.Type.POSITIONAL : ExprConstraintDescr.Type.NAMED);
    constr.setPosition(descr.getConstraint().getDescrs().size());
    constr.setResource(descr.getResource());
    descr.addConstraint(constr);
    return this;
}
Also used : ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr)

Example 73 with ExprConstraintDescr

use of org.drools.drl.ast.descr.ExprConstraintDescr in project drools by kiegroup.

the class KiePMMLDescrLhsFactoryTest method declareConstraintAnd.

@Test
public void declareConstraintAnd() {
    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.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());
    // 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) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 74 with ExprConstraintDescr

use of org.drools.drl.ast.descr.ExprConstraintDescr 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 75 with ExprConstraintDescr

use of org.drools.drl.ast.descr.ExprConstraintDescr in project drools by kiegroup.

the class KiePMMLDescrLhsFactoryTest method commonDeclarePatternWithConstraint.

@Test
public void commonDeclarePatternWithConstraint() {
    String patternType = "TEMPERATURE";
    String constraintsString = "value < 35";
    final CEDescrBuilder<CEDescrBuilder<CEDescrBuilder<RuleDescrBuilder, AndDescr>, NotDescr>, ExistsDescr> existsBuilder = lhsBuilder.not().exists();
    KiePMMLDescrLhsFactory.factory(lhsBuilder).commonDeclarePatternWithConstraint(existsBuilder, patternType, constraintsString);
    assertNotNull(existsBuilder.getDescr());
    final List<BaseDescr> descrs = existsBuilder.getDescr().getDescrs();
    assertNotNull(descrs);
    assertEquals(1, descrs.size());
    assertTrue(descrs.get(0) instanceof PatternDescr);
    PatternDescr patternDescr = (PatternDescr) descrs.get(0);
    assertEquals(patternType, 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());
    assertEquals(constraintsString, exprConstraintDescr.getExpression());
}
Also used : CEDescrBuilder(org.drools.drl.ast.dsl.CEDescrBuilder) RuleDescrBuilder(org.drools.drl.ast.dsl.RuleDescrBuilder) ExistsDescr(org.drools.drl.ast.descr.ExistsDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Aggregations

ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)75 PatternDescr (org.drools.drl.ast.descr.PatternDescr)65 Test (org.junit.Test)56 RuleDescr (org.drools.drl.ast.descr.RuleDescr)37 AndDescr (org.drools.drl.ast.descr.AndDescr)34 PackageDescr (org.drools.drl.ast.descr.PackageDescr)23 BaseDescr (org.drools.drl.ast.descr.BaseDescr)11 BindingDescr (org.drools.drl.ast.descr.BindingDescr)10 Cheese (org.drools.mvel.compiler.Cheese)9 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)8 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)5 NotDescr (org.drools.drl.ast.descr.NotDescr)5 Declaration (org.drools.core.rule.Declaration)4 KiePMMLFieldOperatorValue (org.kie.pmml.models.drools.ast.KiePMMLFieldOperatorValue)4 KiePMMLOperatorValue (org.kie.pmml.models.drools.tuples.KiePMMLOperatorValue)4 Element (org.w3c.dom.Element)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)3 PredicateConstraint (org.drools.core.rule.PredicateConstraint)3