Search in sources :

Example 6 with PatternDescr

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

the class RuleParserTest method testInfinityLiteral.

@Test
public void testInfinityLiteral() throws Exception {
    final String text = "rule \"infinity\"\n" + "when\n" + "    StockTick( this after[-*,*] $another )\n" + "then\n" + "end";
    PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
    assertEquals("StockTick", pattern.getObjectType());
    ExprConstraintDescr constr = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
    assertEquals("this after[-*,*] $another", constr.getText());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 7 with PatternDescr

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

the class RuleParserTest method testSimpleMethodCallWithFrom.

@Test
public void testSimpleMethodCallWithFrom() throws Exception {
    final RuleDescr rule = (RuleDescr) parseResource("rule", "test_SimpleMethodCallWithFrom.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    final PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    final FromDescr from = (FromDescr) pattern.getSource();
    final MVELExprDescr method = (MVELExprDescr) from.getDataSource();
    assertEquals("something.doIt( foo,bar,42,\"hello\",[ a : \"b\", \"something\" : 42, \"a\" : foo, x : [x:y]],\"end\", [a, \"b\", 42] )", method.getExpression());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) FromDescr(org.drools.drl.ast.descr.FromDescr) MVELExprDescr(org.drools.drl.ast.descr.MVELExprDescr) Test(org.junit.Test)

Example 8 with PatternDescr

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

the class RuleParserTest method testAndOrRules.

/**
 * Test that explicit "&&", "||" works as expected
 */
@Test
public void testAndOrRules() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "and_or_rule.drl");
    assertNotNull(pkg);
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals("simple_rule", rule.getName());
    // we will have 3 children under the main And node
    final AndDescr and = rule.getLhs();
    assertEquals(3, and.getDescrs().size());
    PatternDescr left = (PatternDescr) and.getDescrs().get(0);
    PatternDescr right = (PatternDescr) and.getDescrs().get(1);
    assertEquals("Person", left.getObjectType());
    assertEquals("Cheese", right.getObjectType());
    assertEquals(1, left.getConstraint().getDescrs().size());
    ExprConstraintDescr fld = (ExprConstraintDescr) left.getConstraint().getDescrs().get(0);
    assertEquals("name == \"mark\"", fld.getExpression());
    assertEquals(1, right.getConstraint().getDescrs().size());
    fld = (ExprConstraintDescr) right.getConstraint().getDescrs().get(0);
    assertEquals("type == \"stilton\"", fld.getExpression());
    // now the "||" part
    final OrDescr or = (OrDescr) and.getDescrs().get(2);
    assertEquals(2, or.getDescrs().size());
    left = (PatternDescr) or.getDescrs().get(0);
    right = (PatternDescr) or.getDescrs().get(1);
    assertEquals("Person", left.getObjectType());
    assertEquals("Cheese", right.getObjectType());
    assertEquals(1, left.getConstraint().getDescrs().size());
    fld = (ExprConstraintDescr) left.getConstraint().getDescrs().get(0);
    assertEquals("name == \"mark\"", fld.getExpression());
    assertEquals(1, right.getConstraint().getDescrs().size());
    fld = (ExprConstraintDescr) right.getConstraint().getDescrs().get(0);
    assertEquals("type == \"stilton\"", fld.getExpression());
    assertEqualsIgnoreWhitespace("System.out.println( \"Mark and Michael\" );", (String) rule.getConsequence());
}
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) OrDescr(org.drools.drl.ast.descr.OrDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 9 with PatternDescr

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

the class RuleParserTest method testRuleOldSyntax1.

@Test
public void testRuleOldSyntax1() throws Exception {
    final String source = "rule \"Test\" when ( not $r :LiteralRestriction( operator == Operator.EQUAL ) ) then end";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", source);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals("Test", rule.getName());
    assertEquals(1, rule.getLhs().getDescrs().size());
    assertEquals(1, ((NotDescr) rule.getLhs().getDescrs().get(0)).getDescrs().size());
    NotDescr notDescr = (NotDescr) rule.getLhs().getDescrs().get(0);
    PatternDescr patternDescr = (PatternDescr) notDescr.getDescrs().get(0);
    assertEquals("$r", patternDescr.getIdentifier());
    assertEquals(1, patternDescr.getDescrs().size());
    ExprConstraintDescr fieldConstraintDescr = (ExprConstraintDescr) patternDescr.getDescrs().get(0);
    assertEquals("operator == Operator.EQUAL", fieldConstraintDescr.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 10 with PatternDescr

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

the class RuleParserTest method testRuleExtends.

@Test
public void testRuleExtends() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "Rule_with_Extends.drl");
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    RuleDescr rule = pkg.getRules().get(0);
    assertTrue(rule.getParentName() != null);
    assertEquals("rule1", rule.getParentName());
    AndDescr lhs = rule.getLhs();
    assertNotNull(lhs);
    assertEquals(1, lhs.getDescrs().size());
    PatternDescr pattern = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("foo", pattern.getObjectType());
    assertEquals("$foo", pattern.getIdentifier());
}
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) Test(org.junit.Test)

Aggregations

PatternDescr (org.drools.drl.ast.descr.PatternDescr)162 Test (org.junit.Test)131 RuleDescr (org.drools.drl.ast.descr.RuleDescr)103 PackageDescr (org.drools.drl.ast.descr.PackageDescr)84 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)65 AndDescr (org.drools.drl.ast.descr.AndDescr)61 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)18 AccumulateDescr (org.drools.drl.ast.descr.AccumulateDescr)18 FromDescr (org.drools.drl.ast.descr.FromDescr)16 BaseDescr (org.drools.drl.ast.descr.BaseDescr)15 NotDescr (org.drools.drl.ast.descr.NotDescr)14 Cheese (org.drools.mvel.compiler.Cheese)14 OrDescr (org.drools.drl.ast.descr.OrDescr)13 BindingDescr (org.drools.drl.ast.descr.BindingDescr)11 Element (org.w3c.dom.Element)9 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)8 InputStreamReader (java.io.InputStreamReader)7 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)7 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)7 MVELExprDescr (org.drools.drl.ast.descr.MVELExprDescr)7