@Test public void testRegexp() throws Exception { this.expander.addDSLMapping(this.file.getMapping()); final Reader rules = new InputStreamReader(this.getClass().getResourceAsStream("test_expansion.dslr")); final String result = this.expander.expand(rules); }
@Test public void testAddDSLMapping() { this.expander.addDSLMapping(this.file.getMapping()); // should not raise any exception }
@Test public void testParseFile() { try { final Reader reader = new InputStreamReader( this.getClass().getResourceAsStream( this.filename ) ); this.file = new DSLTokenizedMappingFile(); final boolean parsingResult = this.file.parseAndLoad( reader ); reader.close(); assertTrue( this.file.getErrors().toString(), parsingResult ); assertTrue( this.file.getErrors().isEmpty() ); assertEquals( 31, this.file.getMapping().getEntries().size() ); } catch ( final IOException e ) { e.printStackTrace(); fail( "Should not raise exception " ); } }
@Test public void testParseFile() { try { final Reader reader = new InputStreamReader( this.getClass().getResourceAsStream( this.filename ) ); this.file = new DSLTokenizedMappingFile(); final boolean parsingResult = this.file.parseAndLoad( reader ); reader.close(); assertTrue( this.file.getErrors().toString(), parsingResult ); assertTrue( this.file.getErrors().isEmpty() ); assertEquals( 31, this.file.getMapping().getEntries().size() ); } catch ( final IOException e ) { e.printStackTrace(); fail( "Should not raise exception " ); } }
@Test public void testExpandParts() throws Exception { DSLMappingFile file = new DSLTokenizedMappingFile(); String dsl = "[when]foo=Foo()" + NL + "[then]bar {num}=baz({num});"; file.parseAndLoad(new StringReader(dsl)); assertEquals(0, file.getErrors().size()); DefaultExpander ex = new DefaultExpander(); ex.addDSLMapping(file.getMapping()); //System.err.println(ex.expand( "rule 'x' " + NL + " when " + NL + " foo " + NL + " then " + NL + " end" )); }
@Test public void testExpandKeyword() throws Exception { DSLMappingFile file = new DSLTokenizedMappingFile(); String dsl = "[keyword]key {param}=Foo( attr=={param} )"; file.parseAndLoad(new StringReader(dsl)); assertEquals(0, file.getErrors().size()); DefaultExpander ex = new DefaultExpander(); ex.addDSLMapping(file.getMapping()); String source = "rule x" + NL + "when" + NL + " key 1 " + NL + " key 2 " + NL + "then" + NL + "end"; String drl = ex.expand(source); System.out.println(drl); assertTrue(drl.contains("attr==1")); assertTrue(drl.contains("attr==2")); //System.err.println(ex.expand( "rule 'x' " + NL + " when " + NL + " foo " + NL + " then " + NL + " end" )); }
@Test public void testExpandDRLUsingInjectedExpander() throws Exception { String dsl = "[condition]Something=Something()" + NL + "[then]another=another();"; String drl = "rule 'foo' " + NL + " when " + NL + " Something " + NL + " then " + NL + " another " + NL + "end"; DefaultExpanderResolver resolver = new DefaultExpanderResolver(new StringReader(dsl)); final DSLMappingFile file = new DSLTokenizedMappingFile(); if ( file.parseAndLoad( new StringReader(dsl) ) ) { final Expander expander = new DefaultExpander(); expander.addDSLMapping( file.getMapping() ); resolver.addExpander("*", expander); } else { throw new RuntimeException( "Error parsing and loading DSL file." + file.getErrors() ); } DrlParser parser = new DrlParser(LanguageLevelOption.DRL5); String result = parser.getExpandedDRL( drl, resolver); Assertions.assertThat("rule 'foo' " + NL + " when " + NL + " Something() " + NL + " then " + NL + " another(); " + NL + "end") .isEqualToIgnoringWhitespace(result); }
@Test public void testParseFileWithBrackets() { String file = "[when]ATTRIBUTE \"{attr}\" IS IN [{list}]=Attribute( {attr} in ({list}) )"; try { final Reader reader = new StringReader( file ); this.file = new DSLTokenizedMappingFile(); final boolean parsingResult = this.file.parseAndLoad( reader ); reader.close(); assertTrue( this.file.getErrors().toString(), parsingResult ); assertTrue( this.file.getErrors().isEmpty() ); assertEquals( 1, this.file.getMapping().getEntries().size() ); DSLMappingEntry entry = (DSLMappingEntry) this.file.getMapping().getEntries().get( 0 ); assertEquals( DSLMappingEntry.CONDITION, entry.getSection() ); assertEquals( DSLMappingEntry.EMPTY_METADATA, entry.getMetaData() ); assertEquals( lookbehind + "ATTRIBUTE\\s+\"(.*?)\"\\s+IS\\s+IN\\s+[(.*?)](?=\\W|$)", entry.getKeyPattern().toString() ); //Attribute( {attr} in ({list}) ) assertEquals( "Attribute( {attr} in ({list}) )", entry.getValuePattern() ); } catch ( final IOException e ) { e.printStackTrace(); fail( "Should not raise exception " ); } }
@Test public void testParseFileWithEscapes() { String file = "[then]TEST=System.out.println(\"DO_SOMETHING\");" + NL + "" + "[when]code {code1} occurs and sum of all digit not equal \\( {code2} \\+ {code3} \\)=AAAA( cd1 == {code1}, cd2 != ( {code2} + {code3} ))" + NL + "" + "[when]code {code1} occurs=BBBB" + NL + ""; try { final Reader reader = new StringReader( file ); this.file = new DSLTokenizedMappingFile(); final boolean parsingResult = this.file.parseAndLoad( reader ); reader.close(); assertTrue( this.file.getErrors().toString(), parsingResult ); assertTrue( this.file.getErrors().isEmpty() ); final String LHS = "code 1041 occurs and sum of all digit not equal ( 1034 + 1035 )"; final String rule = "rule \"x\"" + NL + "when" + NL + "" + LHS + "" + NL + "then" + NL + "TEST" + NL + "end"; DefaultExpander de = new DefaultExpander(); de.addDSLMapping(this.file.getMapping()); final String ruleAfterExpansion = de.expand(rule); final String expected = "rule \"x\"" + NL + "when" + NL + "AAAA( cd1 == 1041, cd2 != ( 1034 + 1035 ))" + NL + "then" + NL + "System.out.println(\"DO_SOMETHING\");" + NL + "end"; assertEquals( expected, ruleAfterExpansion ); } catch ( final IOException e ) { e.printStackTrace(); fail( "Should not raise exception " ); } }
this.file.getMapping().getEntries().size() ); DSLMappingEntry entry = (DSLMappingEntry) this.file.getMapping().getEntries().get( 0 );
@Test(timeout = 1000) public void testExpandInfiniteLoop() throws Exception { // DROOLS-73 DSLMappingFile file = new DSLTokenizedMappingFile(); String dsl = "[when]Foo with {var} bars=Foo( bars == {var} )"; file.parseAndLoad(new StringReader(dsl)); assertEquals(0, file.getErrors().size()); DefaultExpander ex = new DefaultExpander(); ex.addDSLMapping(file.getMapping()); String source = "rule 'dsl rule'" + NL + "when" + NL + " Foo with {var} bars" + NL + "then" + NL + NL + "end"; ex.expand(source); assertFalse(ex.hasErrors()); }
@Test public void testParseFileWithEscaptedBrackets() { String file = "[when]ATTRIBUTE \"{attr}\" IS IN \\[{list}\\]=Attribute( {attr} in ({list}) )"; try { final Reader reader = new StringReader( file ); this.file = new DSLTokenizedMappingFile(); final boolean parsingResult = this.file.parseAndLoad( reader ); reader.close(); assertTrue( this.file.getErrors().toString(), parsingResult ); assertTrue( this.file.getErrors().isEmpty() ); assertEquals( 1, this.file.getMapping().getEntries().size() ); DSLMappingEntry entry = (DSLMappingEntry) this.file.getMapping().getEntries().get( 0 ); assertEquals( DSLMappingEntry.CONDITION, entry.getSection() ); assertEquals( DSLMappingEntry.EMPTY_METADATA, entry.getMetaData() ); assertEquals( lookbehind + "ATTRIBUTE\\s+\"(.*?)\"\\s+IS\\s+IN\\s+\\[(.*?)\\](?=\\W|$)", entry.getKeyPattern().toString() ); //Attribute( {attr} in ({list}) ) assertEquals( "Attribute( {attr} in ({list}) )", entry.getValuePattern() ); } catch ( final IOException e ) { e.printStackTrace(); fail( "Should not raise exception " ); } }
@Test public void testParseFileWithEscapes() { String file = "[then]TEST=System.out.println(\"DO_SOMETHING\");" + NL + "" + "[when]code {code1} occurs and sum of all digit not equal \\( {code2} \\+ {code3} \\)=AAAA( cd1 == {code1}, cd2 != ( {code2} + {code3} ))" + NL + "" + "[when]code {code1} occurs=BBBB" + NL + ""; try { final Reader reader = new StringReader( file ); this.file = new DSLTokenizedMappingFile(); final boolean parsingResult = this.file.parseAndLoad( reader ); reader.close(); assertTrue( this.file.getErrors().toString(), parsingResult ); assertTrue( this.file.getErrors().isEmpty() ); final String LHS = "code 1041 occurs and sum of all digit not equal ( 1034 + 1035 )"; final String rule = "rule \"x\"" + NL + "when" + NL + "" + LHS + "" + NL + "then" + NL + "TEST" + NL + "end"; DefaultExpander de = new DefaultExpander(); de.addDSLMapping( this.file.getMapping() ); final String ruleAfterExpansion = de.expand( rule ); final String expected = "rule \"x\"" + NL + "when" + NL + "AAAA( cd1 == 1041, cd2 != ( 1034 + 1035 ))" + NL + "then" + NL + "System.out.println(\"DO_SOMETHING\");" + NL + "end"; assertEquals( expected, ruleAfterExpansion ); } catch ( final IOException e ) { e.printStackTrace(); fail( "Should not raise exception " ); } }
@Test public void testParseFileWithEscaptedBrackets() { String file = "[when][]ATTRIBUTE \"{attr}\" IS IN \\[{list}\\]=Attribute( {attr} in ({list}) )"; try { final Reader reader = new StringReader( file ); this.file = new DSLTokenizedMappingFile(); final boolean parsingResult = this.file.parseAndLoad( reader ); reader.close(); assertTrue( this.file.getErrors().toString(), parsingResult ); assertTrue( this.file.getErrors().isEmpty() ); assertEquals( 1, this.file.getMapping().getEntries().size() ); DSLMappingEntry entry = (DSLMappingEntry) this.file.getMapping().getEntries().get( 0 ); assertEquals( DSLMappingEntry.CONDITION, entry.getSection() ); assertEquals( DSLMappingEntry.EMPTY_METADATA, entry.getMetaData() ); assertEquals( "ATTRIBUTE \"{attr}\" IS IN \\[{list}\\]", entry.getMappingKey() ); assertEquals( "Attribute( {attr} in ({list}) )", entry.getMappingValue() ); } catch ( final IOException e ) { e.printStackTrace(); fail( "Should not raise exception " ); } }
/** * Right now this test fails because there is no RHS for the rule. It connects the "then" and "end" to "thenend". */ @Test public void testNoRHS() { String file = "[then]TEST=System.out.println(\"DO_SOMETHING\");" + NL + "" + "[when]code {code1} occurs and sum of all digit not equal \\( {code2} \\+ {code3} \\)=AAAA( cd1 == {code1}, cd2 != ( {code2} + {code3} ))" + NL + "" + "[when]code {code1} occurs=BBBB" + NL + ""; try { final Reader reader = new StringReader( file ); this.file = new DSLTokenizedMappingFile(); final boolean parsingResult = this.file.parseAndLoad( reader ); reader.close(); assertTrue( this.file.getErrors().toString(), parsingResult ); assertTrue( this.file.getErrors().isEmpty() ); final String LHS = "code 1041 occurs and sum of all digit not equal ( 1034 + 1035 )"; final String rule = "rule \"x\"" + NL + "when" + NL + "" + LHS + "" + NL + "then" + NL + "end"; DefaultExpander de = new DefaultExpander(); de.addDSLMapping( this.file.getMapping() ); final String ruleAfterExpansion = de.expand( rule ); final String expected = "rule \"x\"" + NL + "when" + NL + "AAAA( cd1 == 1041, cd2 != ( 1034 + 1035 ))" + NL + "then" + NL + "end"; assertEquals( expected, ruleAfterExpansion ); } catch ( final IOException e ) { e.printStackTrace(); fail( "Should not raise exception " ); } }
this.file.getMapping().getEntries().size() ); DSLMappingEntry entry = (DSLMappingEntry) this.file.getMapping().getEntries().get( 0 );
@Test @Ignore public void testParseFileWithEscaptedEquals() { String file = "[when][]something:\\={value}=Attribute( something == \"{value}\" )"; try { final Reader reader = new StringReader( file ); this.file = new DSLTokenizedMappingFile(); final boolean parsingResult = this.file.parseAndLoad( reader ); reader.close(); assertTrue( this.file.getErrors().toString(), parsingResult ); assertTrue( this.file.getErrors().isEmpty() ); assertEquals( 1, this.file.getMapping().getEntries().size() ); DSLMappingEntry entry = (DSLMappingEntry) this.file.getMapping().getEntries().get( 0 ); assertEquals( DSLMappingEntry.CONDITION, entry.getSection() ); assertEquals( DSLMappingEntry.EMPTY_METADATA, entry.getMetaData() ); assertEquals( "something:={value}", entry.getMappingKey() ); assertEquals( "Attribute( something == \"{value}\" )", entry.getMappingValue() ); } catch ( final IOException e ) { e.printStackTrace(); fail( "Should not raise exception " ); } }
@Test public void testExpandFailure() throws Exception { DSLMappingFile file = new DSLTokenizedMappingFile(); String dsl = "[when]foo=Foo()" + NL + "[then]bar {num}=baz({num});"; file.parseAndLoad(new StringReader(dsl)); assertEquals(0, file.getErrors().size()); DefaultExpander ex = new DefaultExpander(); ex.addDSLMapping(file.getMapping()); String source = "rule 'q'" + NL + "agenda-group 'x'" + NL + "when" + NL + " foo " + NL + "then" + NL + " bar 42" + NL + "end"; String drl = ex.expand(source); assertFalse(ex.hasErrors()); ex = new DefaultExpander(); ex.addDSLMapping(file.getMapping()); source = "rule 'q' agenda-group 'x'" + NL + "when" + NL + " foos " + NL + "then" + NL + " bar 42" + NL + " end"; drl = ex.expand(source); //System.out.println( drl ); assertTrue(ex.hasErrors()); assertEquals(1, ex.getErrors().size()); //System.err.println(( (ExpanderException) ex.getErrors().get( 0 )).getMessage()); }
@Test public void testExpandWithKeywordClashes() throws Exception { DSLMappingFile file = new DSLTokenizedMappingFile(); String dsl = "[when]Invoke rule executor=ruleExec: RuleExecutor()" + NL + "[then]Execute rule \"{id}\"=ruleExec.ExecuteSubRule( new Long({id}));"; file.parseAndLoad(new StringReader(dsl)); assertEquals(0, file.getErrors().size()); DefaultExpander ex = new DefaultExpander(); ex.addDSLMapping(file.getMapping()); String source = "package something;" + NL + NL + "rule \"1\"" + NL + "when" + NL + " Invoke rule executor" + NL + "then" + NL + " Execute rule \"5\"" + NL + "end"; String expected = "package something;" + NL + NL + "rule \"1\"" + NL + "when" + NL + " ruleExec: RuleExecutor()" + NL + "then" + NL + " ruleExec.ExecuteSubRule( new Long(5));" + NL + "end" + NL; String drl = ex.expand(source); // System.out.println("["+drl+"]" ); // System.out.println("["+expected+"]" ); assertFalse(ex.hasErrors()); equalsIgnoreWhiteSpace(expected, drl); }
@Test public void testLineNumberError() throws Exception { DSLMappingFile file = new DSLTokenizedMappingFile(); String dsl = "[when]foo=Foo()" + NL + "[then]bar {num}=baz({num});"; file.parseAndLoad(new StringReader(dsl)); DefaultExpander ex = new DefaultExpander(); ex.addDSLMapping(file.getMapping()); String source = "rule 'q'" + NL + "agenda-group 'x'" + NL + "when" + NL + " __ " + NL + "then" + NL + " bar 42" + NL + "\tgoober" + NL + "end"; ex.expand(source); assertTrue(ex.hasErrors()); assertEquals(2, ex.getErrors().size()); ExpanderException err = (ExpanderException) ex.getErrors().get(0); assertEquals(4, err.getLine()); err = (ExpanderException) ex.getErrors().get(1); assertEquals(7, err.getLine()); }