Search in sources :

Example 26 with ExprConstraintDescr

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

the class RuleParserTest method testRuleParseLhsWithStringQuotes2.

@Test
public void testRuleParseLhsWithStringQuotes2() throws Exception {
    final String text = "rule X when Cheese( $x: type, type == \"s\\tti\\\"lto\\nn\" ) then end\n";
    RuleDescr rule = (RuleDescr) parse("rule", text);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    assertNotNull(rule);
    AndDescr lhs = rule.getLhs();
    ExprConstraintDescr constr = (ExprConstraintDescr) ((PatternDescr) lhs.getDescrs().get(0)).getDescrs().get(1);
    assertEquals("type == \"s\\tti\\\"lto\\nn\"", constr.getText());
}
Also used : 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)

Example 27 with ExprConstraintDescr

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

the class RuleParserTest method testSimpleQuery.

@Test
public void testSimpleQuery() throws Exception {
    final QueryDescr query = (QueryDescr) parseResource("query", "simple_query.drl");
    assertNotNull(query);
    assertEquals("simple_query", query.getName());
    final AndDescr lhs = query.getLhs();
    assertNotNull(lhs);
    assertEquals(3, lhs.getDescrs().size());
    // Check first pattern
    final PatternDescr first = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("foo3", first.getIdentifier());
    assertEquals("Bar", first.getObjectType());
    assertEquals(1, first.getConstraint().getDescrs().size());
    AndDescr and = (AndDescr) first.getConstraint();
    ExprConstraintDescr fld = (ExprConstraintDescr) and.getDescrs().get(0);
    assertNotNull(fld);
    assertEquals("a==3", fld.getExpression());
    // Check second pattern
    final PatternDescr second = (PatternDescr) lhs.getDescrs().get(1);
    assertEquals("foo4", second.getIdentifier());
    assertEquals("Bar", second.getObjectType());
    assertEquals(1, second.getDescrs().size());
    // check it has field bindings.
    final ExprConstraintDescr bindingDescr = (ExprConstraintDescr) second.getDescrs().get(0);
    assertEquals("a4:a==4", bindingDescr.getExpression());
}
Also used : QueryDescr(org.drools.drl.ast.descr.QueryDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 28 with ExprConstraintDescr

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

the class FieldConstraintHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final Element element = parser.endElementBuilder();
    final ConnectiveDescr c = (ConnectiveDescr) parser.getCurrent();
    Object p = parser.getParent();
    if (p instanceof PatternDescr) {
        StringBuilder sb = new StringBuilder();
        c.buildExpression(sb);
        ExprConstraintDescr expr = new ExprConstraintDescr();
        expr.setExpression(sb.toString());
        final PatternDescr patternDescr = (PatternDescr) parser.getParent();
        patternDescr.addConstraint(expr);
    } else if (p instanceof ConnectiveDescr) {
        ((ConnectiveDescr) p).add(c);
    }
    return c;
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) Element(org.w3c.dom.Element) ConnectiveDescr(org.drools.drl.ast.descr.ConnectiveDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr)

Example 29 with ExprConstraintDescr

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

the class ExprConstraintHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final Element element = parser.endElementBuilder();
    final String expression = ((org.w3c.dom.Text) element.getChildNodes().item(0)).getWholeText();
    emptyContentCheck(localName, expression, parser);
    PatternDescr p = (PatternDescr) parser.getParent();
    ExprConstraintDescr descr = new ExprConstraintDescr(expression);
    p.getConstraint().addDescr(descr);
    return descr;
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) Element(org.w3c.dom.Element) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr)

Example 30 with ExprConstraintDescr

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

the class PatternBuilder method collectProps.

private static Collection<String> collectProps(PatternDescr pattern) {
    Collection<String> props = new HashSet<>();
    for (BaseDescr expr : pattern.getDescrs()) {
        if (expr instanceof ExprConstraintDescr) {
            String text = expr.getText();
            String prop = StringUtils.extractFirstIdentifier(text, 0);
            String propFromGetter = ClassUtils.getter2property(prop);
            props.add(propFromGetter != null ? propFromGetter : StringUtils.lcFirst(prop));
        }
    }
    return props;
}
Also used : BaseDescr(org.drools.drl.ast.descr.BaseDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) HashSet(java.util.HashSet)

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