private Object parseResource( final String parserRuleName, final String name ) throws Exception { final Reader reader = new InputStreamReader( getClass().getResourceAsStream( name ) ); final StringBuilder text = new StringBuilder(); final char[] buf = new char[1024]; int len = 0; while ( (len = reader.read( buf )) >= 0 ) { text.append( buf, 0, len ); } return parse( parserRuleName, text.toString() ); }
@Test public void testPackage_OneSegment() throws Exception { final String packageName = (String) parse( "packageStatement", "package foo" ); assertEquals( "foo", packageName ); }
@Test public void testPackage_MultipleSegments() throws Exception { final String packageName = (String) parse( "packageStatement", "package foo.bar.baz;" ); assertEquals( "foo.bar.baz", packageName ); }
@Test public void testRuleWithoutEnd() throws Exception { String source = "rule \"Invalid customer id\" \n" + " when \n" + " o: Order( ) \n" + " then \n" + " System.err.println(\"Invalid customer id found!\"); \n"; parse( "compilationUnit", source ); assertTrue( parser.hasErrors() ); }
@Test public void testCheckOrDescr() throws Exception { final String text = "rule X when Person( eval( age == 25 ) || ( eval( name.equals( \"bob\" ) ) && eval( age == 30 ) ) ) then end"; PatternDescr pattern = (PatternDescr) ((RuleDescr) parse( "rule", text )).getLhs().getDescrs().get( 0 ); assertEquals( 1, pattern.getDescrs().size() ); assertEquals( pattern.getConstraint().getClass(), AndDescr.class ); assertEquals( ExprConstraintDescr.class, pattern.getConstraint().getDescrs().get( 0 ).getClass() ); }
@Test public void testConstraintOrConnective() throws Exception { final String text = "rule X when Person( age < 42 || location==\"atlanta\") then end"; PatternDescr pattern = (PatternDescr) ((RuleDescr) parse( "rule", text )).getLhs().getDescrs().get( 0 ); assertEquals( 1, pattern.getDescrs().size() ); ExprConstraintDescr fcd = (ExprConstraintDescr) pattern.getDescrs().get( 0 ); assertEquals( "age < 42 || location==\"atlanta\"", fcd.getExpression() ); }
@Test public void testConstraintAndConnective() throws Exception { final String text = "rule X when Person( age < 42 && location==\"atlanta\") then end"; PatternDescr pattern = (PatternDescr) ((RuleDescr) parse( "rule", text )).getLhs().getDescrs().get( 0 ); assertEquals( 1, pattern.getDescrs().size() ); ExprConstraintDescr fcd = (ExprConstraintDescr) pattern.getDescrs().get( 0 ); assertEquals( "age < 42 && location==\"atlanta\"", fcd.getExpression() ); }
@Test public void testPredicate2() throws Exception { // predicates are also prefixed by the eval keyword final RuleDescr rule = (RuleDescr) parse( "rule", "rule X when Foo(eval( $var.equals(\"xyz\") )) then end" ); final PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get( 0 ); final List< ? > constraints = pattern.getConstraint().getDescrs(); assertEquals( 1, constraints.size() ); final ExprConstraintDescr predicate = (ExprConstraintDescr) constraints.get( 0 ); assertEquals( "eval( $var.equals(\"xyz\") )", predicate.getExpression() ); }
@Test public void testMemberof() throws Exception { final String text = "rule X when Country( $cities : city )\nPerson( city memberOf $cities )\n then end"; AndDescr descrs = ((RuleDescr) parse( "rule", text )).getLhs(); assertEquals( 2, descrs.getDescrs().size() ); PatternDescr pat = (PatternDescr) descrs.getDescrs().get( 1 ); ExprConstraintDescr fieldConstr = (ExprConstraintDescr) pat.getConstraint().getDescrs().get( 0 ); assertEquals( "city memberOf $cities", fieldConstr.getExpression() ); }
@Test public void testRestrictions() throws Exception { final String text = "rule X when Foo( bar > 1 || == 1 ) then end\n"; AndDescr descrs = (AndDescr) ((RuleDescr) parse( "rule", text )).getLhs(); assertEquals( 1, descrs.getDescrs().size() ); PatternDescr pat = (PatternDescr) descrs.getDescrs().get( 0 ); ExprConstraintDescr fieldConstr = (ExprConstraintDescr) pat.getConstraint().getDescrs().get( 0 ); assertEquals( "bar > 1 || == 1", fieldConstr.getExpression() ); }
@Test public void testDialect() throws Exception { final String source = "dialect 'mvel'"; PackageDescr pkg = (PackageDescr) parse( "compilationUnit", source ); AttributeDescr attr = (AttributeDescr) pkg.getAttributes().get( 0 ); assertEquals( "dialect", attr.getName() ); assertEquals( "mvel", attr.getValue() ); }
@Test public void testNotMemberof() throws Exception { final String text = "rule X when Country( $cities : city )\nPerson( city not memberOf $cities ) then end\n"; AndDescr descrs = ((RuleDescr) parse( "rule", text )).getLhs(); assertEquals( 2, descrs.getDescrs().size() ); PatternDescr pat = (PatternDescr) descrs.getDescrs().get( 1 ); ExprConstraintDescr fieldConstr = (ExprConstraintDescr) pat.getConstraint().getDescrs().get( 0 ); assertEquals( "city not memberOf $cities", fieldConstr.getExpression() ); }
@Test public void testDialect2() throws Exception { final String source = "dialect \"mvel\""; PackageDescr pkg = (PackageDescr) parse( "compilationUnit", source ); AttributeDescr attr = pkg.getAttributes().get( 0 ); assertEquals( "dialect", attr.getName() ); assertEquals( "mvel", attr.getValue() ); }
@Test public void testRuleParseLhs() throws Exception { final String text = "rule X when Person(age < 42, location==\"atlanta\") \nor\nPerson(name==\"bob\") then end"; RuleDescr rule = (RuleDescr) parse( "rule", text ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertNotNull( rule ); AndDescr lhs = rule.getLhs(); assertEquals( 1, lhs.getDescrs().size() ); assertEquals( 2, ((OrDescr) lhs.getDescrs().get( 0 )).getDescrs().size() ); }
@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() ); }
@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() ); }
@Test public void testRuleTwoLines() throws Exception { final String text = "rule \"another test\" salience 10 when eval( true ) then System.out.println(1);\n end"; RuleDescr rule = (RuleDescr) parse( "rule", text ); assertEquals( "another test", rule.getName() ); assertEquals( "System.out.println(1);\n ", rule.getConsequence() ); }
@Test public void testRuleSingleLine() throws Exception { final String text = "rule \"another test\" salience 10 when eval( true ) then System.out.println(1); end"; RuleDescr rule = (RuleDescr) parse( "rule", text ); assertEquals( "another test", rule.getName() ); assertEquals( "System.out.println(1); ", rule.getConsequence() ); }
@Test public void testCompatibleRestriction() throws Exception { String source = "package com.sample rule test when Test( ( text == null || text2 matches \"\" ) ) then end"; PackageDescr pkg = (PackageDescr) parse( "compilationUnit", source ); assertEquals( "com.sample", pkg.getName() ); RuleDescr rule = (RuleDescr) pkg.getRules().get( 0 ); assertEquals( "test", rule.getName() ); ExprConstraintDescr expr = (ExprConstraintDescr) ((PatternDescr) rule.getLhs().getDescrs().get( 0 )).getDescrs().get( 0 ); assertEquals( "( text == null || text2 matches \"\" )", expr.getText() ); }
@Test public void testMultiValueAnnotationsBackwardCompatibility() throws Exception { // multiple values with no keys are parsed as a single value final String text = "rule X @ann1( val1, val2 ) @ann2( \"val1\", \"val2\" ) when then end"; RuleDescr rule = (RuleDescr) parse( "rule", text ); AnnotationDescr ann = rule.getAnnotation( "ann1" ); assertNotNull( ann ); assertEquals( "val1, val2", ann.getValue() ); ann = rule.getAnnotation( "ann2" ); assertNotNull( ann ); assertEquals( "\"val1\", \"val2\"", ann.getValue() ); }