Search in sources :

Example 31 with AndDescr

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

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

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

the class RuleParserTest method testAccumulateMultiPattern.

@Test
public void testAccumulateMultiPattern() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "accumulate_multi_pattern.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(1, rule.getLhs().getDescrs().size());
    final PatternDescr outPattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    final AccumulateDescr accum = (AccumulateDescr) outPattern.getSource();
    assertEqualsIgnoreWhitespace("$counter", outPattern.getIdentifier());
    assertEqualsIgnoreWhitespace("int x = 0 ;", accum.getInitCode());
    assertEqualsIgnoreWhitespace("x++;", accum.getActionCode());
    assertEqualsIgnoreWhitespace("new Integer(x)", accum.getResultCode());
    final AndDescr and = (AndDescr) accum.getInput();
    assertEquals(2, and.getDescrs().size());
    final PatternDescr person = (PatternDescr) and.getDescrs().get(0);
    final PatternDescr cheese = (PatternDescr) and.getDescrs().get(1);
    assertEquals("Person", person.getObjectType());
    assertEquals("Cheese", cheese.getObjectType());
}
Also used : 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) AccumulateDescr(org.drools.drl.ast.descr.AccumulateDescr) Test(org.junit.Test)

Example 34 with AndDescr

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

the class QueryHandler method end.

public Object end(final String uri, final String localName, final ExtensibleXmlParser parser) throws SAXException {
    final Element element = parser.endElementBuilder();
    final QueryDescr queryDescr = (QueryDescr) parser.getCurrent();
    final AndDescr lhs = queryDescr.getLhs();
    if (lhs == null || lhs.getDescrs().isEmpty()) {
        throw new SAXParseException("<query> requires a LHS", parser.getLocator());
    }
    ((PackageDescr) parser.getData()).addRule(queryDescr);
    return queryDescr;
}
Also used : QueryDescr(org.drools.drl.ast.descr.QueryDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) SAXParseException(org.xml.sax.SAXParseException) Element(org.w3c.dom.Element) PackageDescr(org.drools.drl.ast.descr.PackageDescr)

Example 35 with AndDescr

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

the class AndHandler method start.

public Object start(final String uri, final String localName, final Attributes attrs, final ExtensibleXmlParser parser) throws SAXException {
    parser.startElementBuilder(localName, attrs);
    final AndDescr andDescr = new AndDescr();
    return andDescr;
}
Also used : AndDescr(org.drools.drl.ast.descr.AndDescr)

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