Search in sources :

Example 41 with AndDescr

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

the class DRL6StrictParser method lhsAnd.

/**
 * lhsAnd := LEFT_PAREN AND lhsUnary+ RIGHT_PAREN
 *         | lhsUnary (AND lhsUnary)*
 *
 * @param ce
 * @throws org.antlr.runtime.RecognitionException
 */
private BaseDescr lhsAnd(final CEDescrBuilder<?, ?> ce, boolean allowOr) throws RecognitionException {
    BaseDescr result = null;
    if (input.LA(1) == DRL6Lexer.LEFT_PAREN && helper.validateLT(2, DroolsSoftKeywords.AND)) {
        // prefixed AND
        CEDescrBuilder<?, AndDescr> and = null;
        if (state.backtracking == 0) {
            and = ce.and();
            result = ce.getDescr();
            helper.start(and, CEDescrBuilder.class, null);
        }
        try {
            match(input, DRL6Lexer.LEFT_PAREN, null, null, DroolsEditorType.SYMBOL);
            if (state.failed)
                return null;
            match(input, DRL6Lexer.ID, DroolsSoftKeywords.AND, null, DroolsEditorType.KEYWORD);
            if (state.failed)
                return null;
            if (state.backtracking == 0) {
                helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION_AND_OR);
            }
            while (input.LA(1) != DRL6Lexer.RIGHT_PAREN) {
                lhsUnary(and, allowOr);
                if (state.failed)
                    return null;
            }
            match(input, DRL6Lexer.RIGHT_PAREN, null, null, DroolsEditorType.SYMBOL);
            if (state.failed)
                return null;
        } finally {
            if (state.backtracking == 0) {
                helper.end(CEDescrBuilder.class, and);
            }
        }
    } else {
        // infix AND
        // create an AND anyway, since if it is not an AND we remove it later
        CEDescrBuilder<?, AndDescr> and = null;
        if (state.backtracking == 0) {
            and = ce.and();
            result = and.getDescr();
            helper.start(and, CEDescrBuilder.class, null);
        }
        try {
            lhsUnary(and, allowOr);
            if (state.failed)
                return null;
            if (helper.validateIdentifierKey(DroolsSoftKeywords.AND) || input.LA(1) == DRL6Lexer.DOUBLE_AMPER) {
                while (helper.validateIdentifierKey(DroolsSoftKeywords.AND) || input.LA(1) == DRL6Lexer.DOUBLE_AMPER) {
                    if (input.LA(1) == DRL6Lexer.DOUBLE_AMPER) {
                        match(input, DRL6Lexer.DOUBLE_AMPER, null, null, DroolsEditorType.SYMBOL);
                    } else {
                        match(input, DRL6Lexer.ID, DroolsSoftKeywords.AND, null, DroolsEditorType.KEYWORD);
                    }
                    if (state.failed)
                        return null;
                    if (state.backtracking == 0) {
                        helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION_AND_OR);
                    }
                    lhsUnary(and, allowOr);
                    if (state.failed)
                        return null;
                }
            } else {
                if (state.backtracking == 0 && and.getDescr().getDescrs().size() < 2) {
                    // if no AND present, then remove it and add children to parent
                    ((ConditionalElementDescr) ce.getDescr()).getDescrs().remove(and.getDescr());
                    for (BaseDescr base : and.getDescr().getDescrs()) {
                        ((ConditionalElementDescr) ce.getDescr()).addDescr(base);
                    }
                    result = ce.getDescr();
                }
            }
        } finally {
            if (state.backtracking == 0) {
                helper.end(CEDescrBuilder.class, and);
            }
        }
    }
    return result;
}
Also used : AndDescr(org.drools.drl.ast.descr.AndDescr) AnnotatedBaseDescr(org.drools.drl.ast.descr.AnnotatedBaseDescr) BaseDescr(org.drools.drl.ast.descr.BaseDescr) ConditionalElementDescr(org.drools.drl.ast.descr.ConditionalElementDescr)

Example 42 with AndDescr

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

the class ModelGenerator method getExtendedLhs.

private static AndDescr getExtendedLhs(PackageDescr packageDescr, RuleDescr ruleDescr) {
    if (ruleDescr.getParentName() == null) {
        return ruleDescr.getLhs();
    }
    RuleDescr parent = packageDescr.getRules().stream().filter(r -> r.getName().equals(ruleDescr.getParentName())).findFirst().orElseThrow(() -> new RuntimeException("Rule " + ruleDescr.getName() + " extends an unknown rule " + ruleDescr.getParentName()));
    AndDescr extendedLhs = new AndDescr();
    getExtendedLhs(packageDescr, parent).getDescrs().forEach(extendedLhs::addDescr);
    ruleDescr.getLhs().getDescrs().forEach(extendedLhs::addDescr);
    return extendedLhs;
}
Also used : AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr)

Example 43 with AndDescr

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

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

the class KiePMMLDescrLhsFactoryTest method declareConstraintNotIn.

@Test
public void declareConstraintNotIn() {
    List<Object> values = Arrays.asList("3", "8.5");
    String patternType = "INPUT2";
    KiePMMLDescrLhsFactory.factory(lhsBuilder).declareConstraintNotIn(patternType, values);
    final List<BaseDescr> descrs = lhsBuilder.getDescr().getDescrs();
    assertNotNull(descrs);
    assertEquals(1, descrs.size());
    assertTrue(descrs.get(0) instanceof NotDescr);
    NotDescr notDescr = (NotDescr) descrs.get(0);
    assertEquals(1, notDescr.getDescrs().size());
    assertTrue(notDescr.getDescrs().get(0) instanceof PatternDescr);
    PatternDescr patternDescr = (PatternDescr) notDescr.getDescrs().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());
    String expected = "value in (3, 8.5)";
    assertEquals(expected, exprConstraintDescr.getExpression());
}
Also used : NotDescr(org.drools.drl.ast.descr.NotDescr) 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)

Example 45 with AndDescr

use of org.drools.drl.ast.descr.AndDescr 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)

Aggregations

AndDescr (org.drools.drl.ast.descr.AndDescr)88 Test (org.junit.Test)61 PatternDescr (org.drools.drl.ast.descr.PatternDescr)60 RuleDescr (org.drools.drl.ast.descr.RuleDescr)54 PackageDescr (org.drools.drl.ast.descr.PackageDescr)39 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)34 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)18 BaseDescr (org.drools.drl.ast.descr.BaseDescr)18 Cheese (org.drools.mvel.compiler.Cheese)13 ConditionalElementDescr (org.drools.drl.ast.descr.ConditionalElementDescr)11 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)10 OrDescr (org.drools.drl.ast.descr.OrDescr)10 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)9 BindingDescr (org.drools.drl.ast.descr.BindingDescr)8 NotDescr (org.drools.drl.ast.descr.NotDescr)8 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7 QueryDescr (org.drools.drl.ast.descr.QueryDescr)6 RuleDescrBuilder (org.drools.drl.ast.dsl.RuleDescrBuilder)5 FieldConstraintDescr (org.drools.drl.ast.descr.FieldConstraintDescr)4 LiteralRestrictionDescr (org.drools.drl.ast.descr.LiteralRestrictionDescr)4