Refine search
@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 testSimpleRule() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", (String) rule.getConsequence() );
@Test public void testConsequenceWithDeclaration() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "declaration-in-consequence.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); assertNotNull( rule ); assertEquals( "myrule", rule.getName() ); final String expected = "int i = 0; i = 1; i / 1; i == 1; i(i); i = 'i'; i.i.i; i\\i; i<i; i>i; i=\"i\"; ++i;" + "i++; --i; i--; i += i; i -= i; i *= i; i /= i;" + "int i = 5;" + "for(int j; j<i; ++j) {" + "System.out.println(j);}" + "Object o = new String(\"Hello\");" + "String s = (String) o;"; assertEqualsIgnoreWhitespace( expected, (String) rule.getConsequence() ); assertTrue( ((String) rule.getConsequence()).indexOf( "++" ) > 0 ); assertTrue( ((String) rule.getConsequence()).indexOf( "--" ) > 0 ); assertTrue( ((String) rule.getConsequence()).indexOf( "+=" ) > 0 ); assertTrue( ((String) rule.getConsequence()).indexOf( "==" ) > 0 ); // System.out.println(( String ) rule.getConsequence()); // note, need to assert that "i++" is preserved as is, no extra spaces. }
@Test public void testTernaryExpression() throws Exception { PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "ternary_expression.drl" ); final RuleDescr rule = (RuleDescr) pkg.getRules().get( 0 ); assertEquals( 1, pkg.getRules().size() ); assertEqualsIgnoreWhitespace( "if (speed > speedLimit ? true : false;) pullEmOver();", (String) rule.getConsequence() ); }
@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 testEscapedStrings() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "escaped-string.drl" ); assertNotNull( rule ); assertEquals( "test_Quotes", rule.getName() ); final String expected = "String s = \"\\\"\\n\\t\\\\\";"; assertEqualsIgnoreWhitespace( expected, (String) rule.getConsequence() ); }
@Test public void testFixExitPointsReferences() { String consequence = try { JavaExprAnalyzer analyzer = new JavaExprAnalyzer(); JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock( (String) ruleDescr.getConsequence(), new BoundIdentifiers( new HashMap<String, Class<?>>(), null ) );
@Test public void testFixExitPointsReferences() { String consequence = try { JavaExprAnalyzer analyzer = new JavaExprAnalyzer(); JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock( (String) ruleDescr.getConsequence(), new BoundIdentifiers( new HashMap<String, Class<?>>(), null ) );
@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 testEmptyRule() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "empty_rule.drl" ); assertNotNull( rule ); assertEquals( "empty", rule.getName() ); assertNotNull( rule.getLhs() ); assertNotNull( rule.getConsequence() ); }
@Test public void testFixEntryPointsReferences() { String consequence = ruleDescr.setConsequence( consequence ); JavaExprAnalyzer analyzer = new JavaExprAnalyzer(); JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock( (String) ruleDescr.getConsequence(), new BoundIdentifiers( new HashMap<String, Class<?>>(), null ) );
@Test public void testFixEntryPointsReferences() { String consequence = ruleDescr.setConsequence( consequence ); JavaExprAnalyzer analyzer = new JavaExprAnalyzer(); JavaAnalysisResult analysis = (JavaAnalysisResult) analyzer.analyzeBlock( (String) ruleDescr.getConsequence(), new BoundIdentifiers( new HashMap<String, Class<?>>(), null ) );
@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 testFunctionWithArrays() throws Exception { PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "function_arrays.drl" ); assertEquals( "foo", pkg.getName() ); assertEquals( 1, pkg.getRules().size() ); final RuleDescr rule = (RuleDescr) pkg.getRules().get( 0 ); assertEqualsIgnoreWhitespace( "yourFunction(new String[3] {\"a\",\"b\",\"c\"});", (String) rule.getConsequence() ); final FunctionDescr func = (FunctionDescr) pkg.getFunctions().get( 0 ); assertEquals( "String[]", func.getReturnType() ); assertEquals( "args[]", func.getParameterNames().get( 0 ) ); assertEquals( "String", func.getParameterTypes().get( 0 ) ); }
@Test public void testRuleMetadata() throws Exception { final PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "Rule_with_Metadata.drl" ); assertFalse( parser.getErrors().toString(), parser.hasErrors() ); // @fooAttribute(barValue) // @fooAtt2(barVal2) RuleDescr rule = pkg.getRules().get( 0 ); assertTrue( rule.getAnnotationNames().contains( "fooMeta1" ) ); assertEquals( "barVal1", rule.getAnnotation( "fooMeta1" ).getValue() ); assertTrue( rule.getAnnotationNames().contains( "fooMeta2" ) ); assertEquals( "barVal2", rule.getAnnotation( "fooMeta2" ).getValue() ); assertEqualsIgnoreWhitespace( "System.out.println(\"Consequence\");", (String) rule.getConsequence() ); }
@Test public void testWithEval() throws Exception { final PackageDescr pkg = (PackageDescr) parseResource( "compilationUnit", "with_eval.drl" ); assertEquals( 1, pkg.getRules().size() ); final RuleDescr rule = (RuleDescr) pkg.getRules().get( 0 ); assertEquals( 3, rule.getLhs().getDescrs().size() ); PatternDescr pattern = (PatternDescr) rule.getLhs().getDescrs().get( 0 ); assertEquals( "Foo", pattern.getObjectType() ); pattern = (PatternDescr) rule.getLhs().getDescrs().get( 1 ); assertEquals( "Bar", pattern.getObjectType() ); final EvalDescr eval = (EvalDescr) rule.getLhs().getDescrs().get( 2 ); assertEqualsIgnoreWhitespace( "abc(\"foo\")", (String) eval.getContent() ); assertEqualsIgnoreWhitespace( "Kapow", (String) rule.getConsequence() ); }
@Test public void testDurationExpression() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "rule_duration_expression.drl" ); assertEquals( "simple_rule", rule.getName() ); assertEqualsIgnoreWhitespace( "bar();", (String) rule.getConsequence() ); final Map<String, AttributeDescr> attrs = rule.getAttributes(); assertEquals( 2, attrs.size() ); AttributeDescr at = (AttributeDescr) attrs.get( "duration" ); assertEquals( "duration", at.getName() ); assertEquals( "1h30m", at.getValue() ); at = (AttributeDescr) attrs.get( "lock-on-active" ); assertEquals( "lock-on-active", at.getName() ); assertEquals( "true", at.getValue() ); }
@Test public void testCalendars() throws Exception { final RuleDescr rule = (RuleDescr) parseResource( "rule", "rule_calendars_attribute.drl" ); assertEquals( "simple_rule", rule.getName() ); assertEqualsIgnoreWhitespace( "bar();", (String) rule.getConsequence() ); final Map<String, AttributeDescr> attrs = rule.getAttributes(); assertEquals( 2, attrs.size() ); AttributeDescr at = (AttributeDescr) attrs.get( "calendars" ); assertEquals( "calendars", at.getName() ); assertEquals( "[ \"cal1\" ]", at.getValue() ); at = (AttributeDescr) attrs.get( "lock-on-active" ); assertEquals( "lock-on-active", at.getName() ); assertEquals( "true", at.getValue() ); }