Search in sources :

Example 36 with PatternDescr

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

the class RuleParserTest method testRuleOldSyntax2.

@Test
public void testRuleOldSyntax2() throws Exception {
    final String source = "rule \"Test\" when ( $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());
    PatternDescr patternDescr = (PatternDescr) rule.getLhs().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 : 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 37 with PatternDescr

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

the class RuleParserTest method testBasicBinding.

// @Test public void testExpanderUnExpandableErrorLines() throws Exception {
// 
// //stubb expander
// final ExpanderResolver res = new ExpanderResolver() {
// public Expander get(String name,
// String config) {
// return new Expander() {
// public String expand(String scope,
// String pattern) {
// if ( pattern.startsWith( "Good" ) ) {
// return pattern;
// } else {
// throw new IllegalArgumentException( "whoops" );
// }
// 
// }
// };
// }
// };
// 
// final DRLParser parser = parseResource( "expander_line_errors.dslr" );
// parser.setExpanderResolver( res );
// parser.compilationUnit();
// assertTrue( parser.hasErrors() );
// 
// final List messages = parser.getErrorMessages();
// assertEquals( messages.size(),
// parser.getErrors().size() );
// 
// assertEquals( 4,
// parser.getErrors().size() );
// assertEquals( ExpanderException.class,
// parser.getErrors().get( 0 ).getClass() );
// assertEquals( 8,
// ((RecognitionException) parser.getErrors().get( 0 )).line );
// assertEquals( 10,
// ((RecognitionException) parser.getErrors().get( 1 )).line );
// assertEquals( 12,
// ((RecognitionException) parser.getErrors().get( 2 )).line );
// assertEquals( 13,
// ((RecognitionException) parser.getErrors().get( 3 )).line );
// 
// final PackageDescr pack = parser.getPackageDescr();
// assertNotNull( pack );
// 
// final ExpanderException ex = (ExpanderException) parser.getErrors().get(
// 0 );
// assertTrue( ex.getMessage().indexOf( "whoops" ) > -1 );
// 
// }
@Test
public void testBasicBinding() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "basic_binding.drl");
    final RuleDescr ruleDescr = (RuleDescr) pkg.getRules().get(0);
    final AndDescr lhs = ruleDescr.getLhs();
    assertEquals(1, lhs.getDescrs().size());
    final PatternDescr cheese = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Cheese", cheese.getObjectType());
    assertEquals(1, cheese.getConstraint().getDescrs().size());
    final ExprConstraintDescr fieldBinding = (ExprConstraintDescr) cheese.getDescrs().get(0);
    assertEquals("$type:type", fieldBinding.getExpression());
}
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) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 38 with PatternDescr

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

the class RuleParserTest method testRuleParseLhs3.

@Test
public void testRuleParseLhs3() throws Exception {
    final String text = "rule X when (or\nnot Person()\n(and Cheese()\nMeat()\nWine())) then end";
    AndDescr pattern = ((RuleDescr) parse("rule", text)).getLhs();
    assertEquals(1, pattern.getDescrs().size());
    OrDescr or = (OrDescr) pattern.getDescrs().get(0);
    assertEquals(2, or.getDescrs().size());
    NotDescr not = (NotDescr) or.getDescrs().get(0);
    AndDescr and = (AndDescr) or.getDescrs().get(1);
    assertEquals(1, not.getDescrs().size());
    PatternDescr person = (PatternDescr) not.getDescrs().get(0);
    assertEquals("Person", person.getObjectType());
    assertEquals(3, and.getDescrs().size());
    PatternDescr cheese = (PatternDescr) and.getDescrs().get(0);
    assertEquals("Cheese", cheese.getObjectType());
    PatternDescr meat = (PatternDescr) and.getDescrs().get(1);
    assertEquals("Meat", meat.getObjectType());
    PatternDescr wine = (PatternDescr) and.getDescrs().get(2);
    assertEquals("Wine", wine.getObjectType());
}
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) OrDescr(org.drools.drl.ast.descr.OrDescr) Test(org.junit.Test)

Example 39 with PatternDescr

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

the class RuleParserTest method testMultipleRules.

@Test
public void testMultipleRules() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "multiple_rules.drl");
    final List<RuleDescr> rules = pkg.getRules();
    assertEquals(2, rules.size());
    final RuleDescr rule0 = rules.get(0);
    assertEquals("Like Stilton", rule0.getName());
    final RuleDescr rule1 = rules.get(1);
    assertEquals("Like Cheddar", rule1.getName());
    // checkout the first rule
    AndDescr lhs = rule1.getLhs();
    assertNotNull(lhs);
    assertEquals(1, lhs.getDescrs().size());
    assertEqualsIgnoreWhitespace("System.out.println(\"I like \" + t);", (String) rule0.getConsequence());
    // Check first pattern
    PatternDescr first = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Cheese", first.getObjectType());
    // checkout the second rule
    lhs = rule1.getLhs();
    assertNotNull(lhs);
    assertEquals(1, lhs.getDescrs().size());
    assertEqualsIgnoreWhitespace("System.out.println(\"I like \" + t);", (String) rule1.getConsequence());
    // Check first pattern
    first = (PatternDescr) lhs.getDescrs().get(0);
    assertEquals("Cheese", first.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) Test(org.junit.Test)

Example 40 with PatternDescr

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

the class RuleParserTest method testEntryPoint2.

@Test
public void testEntryPoint2() throws Exception {
    final String text = "rule X when StockTick( symbol==\"ACME\") from entry-point \"StreamA\" then end";
    PackageDescr pkg = (PackageDescr) parse("compilationUnit", text);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    RuleDescr rule = pkg.getRules().get(0);
    PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals(1, pattern.getDescrs().size());
    ExprConstraintDescr fcd = (ExprConstraintDescr) pattern.getDescrs().get(0);
    assertEquals("symbol==\"ACME\"", fcd.getExpression());
    assertNotNull(pattern.getSource());
    EntryPointDescr entry = (EntryPointDescr) pattern.getSource();
    assertEquals("StreamA", entry.getEntryId());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) EntryPointDescr(org.drools.drl.ast.descr.EntryPointDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) 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