use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.
the class RuleParserTest method testRuleParseLhsWithStringQuotes.
@Test
public void testRuleParseLhsWithStringQuotes() throws Exception {
final String text = "rule X when Person( location==\"atlanta\\\"\") 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(0);
assertEquals("location==\"atlanta\\\"\"", constr.getText());
}
use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.
the class RuleParserTest method testInOperator.
@Test
public void testInOperator() throws Exception {
final RuleDescr rule = (RuleDescr) parseResource("rule", "in_operator_test.drl");
assertNotNull(rule);
assertEqualsIgnoreWhitespace("consequence();", (String) rule.getConsequence());
assertEquals("simple_rule", rule.getName());
assertEquals(2, rule.getLhs().getDescrs().size());
// The first pattern, with 2 restrictions on a single field (plus a
// connective)
PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get(0);
assertEquals("Person", pattern.getObjectType());
assertEquals(1, pattern.getConstraint().getDescrs().size());
ExprConstraintDescr fld = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
assertEquals("age > 30 && < 40", fld.getExpression());
// the second col, with 2 fields, the first with 2 restrictions, the
// second field with one
pattern = (PatternDescr) rule.getLhs().getDescrs().get(1);
assertEquals("Vehicle", pattern.getObjectType());
assertEquals(2, pattern.getConstraint().getDescrs().size());
fld = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
assertEquals("type in ( \"sedan\", \"wagon\" )", fld.getExpression());
// now the second field
fld = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(1);
assertEquals("age < 3", fld.getExpression());
}
use of org.drools.compiler.lang.descr.ExprConstraintDescr 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());
}
use of org.drools.compiler.lang.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());
}
use of org.drools.compiler.lang.descr.ExprConstraintDescr in project drools by kiegroup.
the class RuleParserTest method testInlineEval.
@Test
public void testInlineEval() throws Exception {
final String text = "rule \"inline eval\"\n" + "when\n" + " Person( eval( name.startsWith(\"b\") && name.finishesWith(\"b\")) )\n" + "then\n" + "end";
PatternDescr pattern = (PatternDescr) ((RuleDescr) parse("rule", text)).getLhs().getDescrs().get(0);
assertEquals("Person", pattern.getObjectType());
ExprConstraintDescr constr = (ExprConstraintDescr) pattern.getConstraint().getDescrs().get(0);
assertEquals("eval( name.startsWith(\"b\") && name.finishesWith(\"b\"))", constr.getText());
}
Aggregations