Search in sources :

Example 11 with PatternDescr

use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.

the class RuleParserTest method testSoundsLike.

@Test
public void testSoundsLike() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "soundslike_operator.drl");
    RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    PatternDescr pat = (PatternDescr) rule.getLhs().getDescrs().get(0);
    pat.getConstraint();
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Test(org.junit.Test)

Example 12 with PatternDescr

use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.

the class RuleParserTest method testFromWithTernaryFollowedByQuery.

@Test
public void testFromWithTernaryFollowedByQuery() throws Exception {
    // the 'from' expression requires a ";" to disambiguate the "?"
    // prefix for queries from the ternary operator "? :"
    final String text = "rule X when Cheese() from (isFull ? $cheesery : $market) ?person( \"Mark\", 42; ) then end";
    RuleDescr rule = (RuleDescr) parse("rule", text);
    assertFalse(parser.getErrors().toString(), parser.hasErrors());
    PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("Cheese", pattern.getObjectType());
    assertEquals("from (isFull ? $cheesery : $market)", pattern.getSource().getText());
    assertFalse(pattern.isQuery());
    pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
    assertEquals("person", pattern.getObjectType());
    assertTrue(pattern.isQuery());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Test(org.junit.Test)

Example 13 with PatternDescr

use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.

the class RuleParserTest method testOrCE.

@Test
public void testOrCE() throws Exception {
    final PackageDescr pkg = (PackageDescr) parseResource("compilationUnit", "or_ce.drl");
    assertEquals(1, pkg.getRules().size());
    final RuleDescr rule = (RuleDescr) pkg.getRules().get(0);
    assertEquals(2, rule.getLhs().getDescrs().size());
    final PatternDescr person = (PatternDescr) rule.getLhs().getDescrs().get(0);
    assertEquals("Person", person.getObjectType());
    assertEquals("$p", person.getIdentifier());
    final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get(1);
    assertEquals(2, or.getDescrs().size());
    final PatternDescr cheese1 = (PatternDescr) or.getDescrs().get(0);
    assertEquals("Cheese", cheese1.getObjectType());
    assertEquals("$c", cheese1.getIdentifier());
    final PatternDescr cheese2 = (PatternDescr) or.getDescrs().get(1);
    assertEquals("Cheese", cheese2.getObjectType());
    assertNull(cheese2.getIdentifier());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) OrDescr(org.drools.compiler.lang.descr.OrDescr) Test(org.junit.Test)

Example 14 with PatternDescr

use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.

the class RuleParserTest method testIsQuery.

@Test
public void testIsQuery() throws Exception {
    final String text = "rule X when ?person( \"Mark\", 42; ) then end";
    PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
    assertTrue(pattern.isQuery());
    assertEquals(2, pattern.getDescrs().size());
    ExprConstraintDescr fcd = (ExprConstraintDescr) pattern.getDescrs().get(0);
    assertEquals("\"Mark\"", fcd.getExpression());
    assertEquals(0, fcd.getPosition());
    assertEquals(ExprConstraintDescr.Type.POSITIONAL, fcd.getType());
    fcd = (ExprConstraintDescr) pattern.getDescrs().get(1);
    assertEquals("42", fcd.getExpression());
    assertEquals(1, fcd.getPosition());
    assertEquals(ExprConstraintDescr.Type.POSITIONAL, fcd.getType());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) Test(org.junit.Test)

Example 15 with PatternDescr

use of org.drools.compiler.lang.descr.PatternDescr in project drools by kiegroup.

the class RuleParserTest method testBindingCompositeWithMethods.

@Test
public void testBindingCompositeWithMethods() throws Exception {
    final String text = "rule X when Person( $name : name.toUpperCase() == \"Bob\" || $loc : location[0].city == \"Montreal\" ) then end";
    PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
    assertEquals("Person", pattern.getObjectType());
    assertFalse(pattern.isUnification());
    // assertEquals( 2,
    // pattern.getDescrs().size() );
    // BindingDescr bindingDescr = pattern.getDescrs().get( 0 );
    // assertEquals( "$name",
    // bindingDescr.getVariable() );
    // assertEquals( "name.toUpperCase()",
    // bindingDescr.getExpression() );
    // assertFalse( bindingDescr.isUnification() );
    // 
    // bindingDescr = pattern.getDescrs().get( 1 );
    // assertEquals( "$loc",
    // bindingDescr.getVariable() );
    // assertEquals( "location[0].city",
    // bindingDescr.getExpression() );
    // assertFalse( bindingDescr.isUnification() );
    // embedded bindings are extracted at compile time
    List<?> constraints = pattern.getDescrs();
    assertEquals(1, constraints.size());
    assertEquals("$name : name.toUpperCase() == \"Bob\" || $loc : location[0].city == \"Montreal\"", ((ExprConstraintDescr) constraints.get(0)).getExpression());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) Test(org.junit.Test)

Aggregations

PatternDescr (org.drools.compiler.lang.descr.PatternDescr)150 Test (org.junit.Test)122 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)103 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)84 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)56 AndDescr (org.drools.compiler.lang.descr.AndDescr)52 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)19 Cheese (org.drools.compiler.Cheese)14 FromDescr (org.drools.compiler.lang.descr.FromDescr)14 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)11 NotDescr (org.drools.compiler.lang.descr.NotDescr)11 OrDescr (org.drools.compiler.lang.descr.OrDescr)11 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)11 Element (org.w3c.dom.Element)9 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)8 InputStreamReader (java.io.InputStreamReader)7 List (java.util.List)7 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)7 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)7 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7