Search in sources :

Example 11 with NotDescr

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

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

the class NotHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final Element element = parser.endElementBuilder();
    final NotDescr notDescr = (NotDescr) parser.getCurrent();
    if ((notDescr.getDescrs().size() != 1) && (notDescr.getDescrs().get(0).getClass() != PatternDescr.class)) {
        throw new SAXParseException("<not> can only have a single <pattern...> as a child element", parser.getLocator());
    }
    final ConditionalElementDescr parentDescr = (ConditionalElementDescr) parser.getParent();
    parentDescr.addDescr(notDescr);
    return null;
}
Also used : NotDescr(org.drools.drl.ast.descr.NotDescr) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) ConditionalElementDescr(org.drools.drl.ast.descr.ConditionalElementDescr)

Example 13 with NotDescr

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

the class RuleParserTest method testNotWithConstraint.

@Test
public void testNotWithConstraint() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "not_with_constraint.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(2, rule.getLhs().getDescrs().size());
    PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    final ExprConstraintDescr fieldBinding = (ExprConstraintDescr) pattern.getDescrs().get(0);
    assertEquals("$likes:like", fieldBinding.getExpression());
    final NotDescr not = (NotDescr) rule.getLhs().getDescrs().get(1);
    pattern = (PatternDescr) not.getDescrs().get(0);
    final ExprConstraintDescr fld = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
    assertEquals("type == $likes", fld.getExpression());
}
Also used : NotDescr(org.drools.drl.ast.descr.NotDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 14 with NotDescr

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

the class RuleParserTest method testNotExistWithBrackets.

@Test
public void testNotExistWithBrackets() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "not_exist_with_brackets.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertNotNull(rule);
    assertEquals("simple_rule", rule.getName());
    final AndDescr lhs = rule.getLhs();
    assertEquals(2, lhs.getDescrs().size());
    final NotDescr not = (NotDescr) lhs.getDescrs().get(0);
    assertEquals(1, not.getDescrs().size());
    final PatternDescr pattern = (PatternDescr) not.getDescrs().get(0);
    assertEquals("Cheese", pattern.getObjectType());
    final ExistsDescr ex = (ExistsDescr) lhs.getDescrs().get(1);
    assertEquals(1, ex.getDescrs().size());
    final PatternDescr exPattern = (PatternDescr) ex.getDescrs().get(0);
    assertEquals("Foo", exPattern.getObjectType());
}
Also used : NotDescr(org.drools.drl.ast.descr.NotDescr) ExistsDescr(org.drools.drl.ast.descr.ExistsDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 15 with NotDescr

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

the class RuleParserTest method testNotNode.

@Test
public void testNotNode() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "rule_not.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertNotNull(rule);
    assertEquals("simple_rule", rule.getName());
    final AndDescr lhs = rule.getLhs();
    assertEquals(1, lhs.getDescrs().size());
    final NotDescr not = (NotDescr) lhs.getDescrs().get(0);
    assertEquals(1, not.getDescrs().size());
    final PatternDescr pattern = (PatternDescr) not.getDescrs().get(0);
    assertEquals("Cheese", pattern.getObjectType());
    assertEquals(1, pattern.getConstraint().getDescrs().size());
    final AndDescr and = (AndDescr) pattern.getConstraint();
    final ExprConstraintDescr fld = (ExprConstraintDescr) and.getDescrs().get(0);
    assertEquals("type == \"stilton\"", fld.getExpression());
}
Also used : NotDescr(org.drools.drl.ast.descr.NotDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Aggregations

NotDescr (org.drools.drl.ast.descr.NotDescr)18 PatternDescr (org.drools.drl.ast.descr.PatternDescr)14 Test (org.junit.Test)14 RuleDescr (org.drools.drl.ast.descr.RuleDescr)10 AndDescr (org.drools.drl.ast.descr.AndDescr)8 PackageDescr (org.drools.drl.ast.descr.PackageDescr)7 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)5 ExistsDescr (org.drools.drl.ast.descr.ExistsDescr)4 OrDescr (org.drools.drl.ast.descr.OrDescr)4 GroupElement (org.drools.core.rule.GroupElement)2 Pattern (org.drools.core.rule.Pattern)2 BaseDescr (org.drools.drl.ast.descr.BaseDescr)2 ConditionalElementDescr (org.drools.drl.ast.descr.ConditionalElementDescr)2 FromDescr (org.drools.drl.ast.descr.FromDescr)2 InputStreamReader (java.io.InputStreamReader)1 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)1 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)1 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)1 Forall (org.drools.core.rule.Forall)1 ForallDescr (org.drools.drl.ast.descr.ForallDescr)1