@Test public void testLineNumberIncludingCommentsInRHS() throws Exception { PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "test_CommentLineNumbersInConsequence.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); final String rhs = (String) ((RuleDescr) pkg.getRules().get( 0 )).getConsequence(); String expected = "\\s*//woot$\\s*first$\\s*$\\s*//$\\s*$\\s*/\\* lala$\\s*$\\s*\\*/$\\s*second$\\s*"; assertTrue( Pattern.compile( expected, Pattern.DOTALL | Pattern.MULTILINE ).matcher( rhs ).matches() ); }
@Test public void testFunctionImport2() throws Exception { PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "test_FunctionImport.drl" ); assertEquals( 2, pkg.getFunctionImports().size() ); assertEquals( "abd.def.x", ((FunctionImportDescr) pkg.getFunctionImports().get( 0 )).getTarget() ); assertFalse( ((FunctionImportDescr) pkg.getFunctionImports().get( 0 )).getStartCharacter() == -1 ); assertFalse( ((FunctionImportDescr) pkg.getFunctionImports().get( 0 )).getEndCharacter() == -1 ); assertEquals( "qed.wah.*", ((FunctionImportDescr) pkg.getFunctionImports().get( 1 )).getTarget() ); assertFalse( ((FunctionImportDescr) pkg.getFunctionImports().get( 1 )).getStartCharacter() == -1 ); assertFalse( ((FunctionImportDescr) pkg.getFunctionImports().get( 1 )).getEndCharacter() == -1 ); }
@Test public void testTypeWithMetaData() throws Exception { PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "type_with_meta.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); final List<TypeDeclarationDescr> declarations = pkg.getTypeDeclarations(); assertEquals( 3, declarations.size() ); }
@Test public void testKeywordCollisions() throws Exception { PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "eol_funny_business.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertEquals( 1, pkg.getRules().size() ); }
@Test public void testPackage() throws Exception { final String source = "package foo.bar.baz"; final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6); final PackageDescr pkg = parser.parse( new StringReader( source ) ); assertFalse( parser.hasErrors() ); assertEquals( "foo.bar.baz", pkg.getName() ); }
@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 testFrom() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "from.drl" ); assertFalse( parser.getErrorMessages().toString(), parser.hasErrors() ); assertNotNull( rule ); assertEquals( "using_from", rule.getName() ); assertEquals( 9, rule.getLhs().getDescrs().size() ); }
@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() ); }
@Test public void testSimpleFunctionCallWithFrom() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "test_SimpleFunctionCallWithFrom.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); final PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get( 0 ); final FromDescr from = (FromDescr) pattern.getSource(); final MVELExprDescr func = (MVELExprDescr) from.getDataSource(); assertEquals( "doIt( foo,bar,42,\"hello\",[ a : \"b\", \"something\" : 42, \"a\" : foo, x : [x:y]],\"end\", [a, \"b\", 42] )", func.getExpression() ); }
@Test public void testSimpleAccessorWithFrom() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "test_SimpleAccessorWithFrom.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); final PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get( 0 ); final FromDescr from = (FromDescr) pattern.getSource(); final MVELExprDescr accessor = (MVELExprDescr) from.getDataSource(); assertEquals( "something.doIt", accessor.getExpression() ); }
@Test public void testSimpleAccessorAndArgWithFrom() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "test_SimpleAccessorArgWithFrom.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); final PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get( 0 ); final FromDescr from = (FromDescr) pattern.getSource(); final MVELExprDescr accessor = (MVELExprDescr) from.getDataSource(); assertEquals( "something.doIt[\"key\"]", accessor.getExpression() ); }
@Test public void testComplexChainedAcessor() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "test_ComplexChainedCallWithFrom.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); final PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get( 0 ); final FromDescr from = (FromDescr) pattern.getSource(); final MVELExprDescr accessor = (MVELExprDescr) from.getDataSource(); assertEquals( "doIt1( foo,bar,42,\"hello\",[ a : \"b\"], [a, \"b\", 42] ).doIt2(bar, [a, \"b\", 42]).field[\"key\"]", accessor.getExpression() ); }
@Test public void testAlmostEmptyRule() throws Exception { PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "almost_empty_rule.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertNotNull( pkg ); RuleDescr rule = pkg.getRules().get( 0 ); assertEquals( "almost_empty", rule.getName() ); assertNotNull( rule.getLhs() ); assertEquals( "", ((String) rule.getConsequence()).trim() ); }
@Test public void testQuotedStringNameRule() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "quoted_string_name_rule.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertNotNull( rule ); assertEquals( "quoted string name", rule.getName() ); assertNotNull( rule.getLhs() ); assertEquals( "", ((String) rule.getConsequence()).trim() ); }
@Test public void testExpanderLineSpread() throws Exception { final DrlParser parser = new DrlParser(LanguageLevelOption.DRL6); final PackageDescr pkg = parser.parse( this.getReader( "expander_spread_lines.dslr" ), this.getReader( "complex.dsl" ) ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); final RuleDescr rule = (RuleDescr) pkg.getRules().get( 0 ); assertEquals( 1, rule.getLhs().getDescrs().size() ); final OrDescr or = (OrDescr) rule.getLhs().getDescrs().get( 0 ); assertEquals( 2, or.getDescrs().size() ); assertNotNull( (String) rule.getConsequence() ); }
@Test public void testRuleFlowGroup() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "ruleflowgroup.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertNotNull( rule ); assertEquals( "rule1", rule.getName() ); final AttributeDescr att = (AttributeDescr) rule.getAttributes().get( "ruleflow-group" ); assertEquals( "a group", att.getValue() ); assertEquals( "ruleflow-group", att.getName() ); }
@Test public void testNoLoop() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "no-loop.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertNotNull( rule ); assertEquals( "rule1", rule.getName() ); final AttributeDescr att = (AttributeDescr) rule.getAttributes().get( "no-loop" ); assertEquals( "false", att.getValue() ); assertEquals( "no-loop", att.getName() ); }
@Test public void testAutofocus() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "autofocus.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertNotNull( rule ); assertEquals( "rule1", rule.getName() ); final AttributeDescr att = (AttributeDescr) rule.getAttributes().get( "auto-focus" ); assertEquals( "true", att.getValue() ); assertEquals( "auto-focus", att.getName() ); }