@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 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 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()); }
@Before public void setUp() throws Exception { final String filename = "test_metainfo.dsl"; final Reader reader = new InputStreamReader(this.getClass().getResourceAsStream(filename)); this.file = new DSLTokenizedMappingFile(); this.tokenizedFile = new DSLTokenizedMappingFile(); this.file.parseAndLoad(reader); reader.close(); final Reader reader2 = new InputStreamReader(this.getClass().getResourceAsStream(filename)); this.tokenizedFile.parseAndLoad(reader2); reader2.close(); this.expander = new DefaultExpander(); }
@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 " ); } }
/** * Returns an expander for DSLs (only if there is a DSL configured for this package). */ private Expander getDSLExpander(final Path path) { final Expander expander = new DefaultExpander(); final List<DSLMappingFile> dsls = getDSLMappingFiles(path); for (DSLMappingFile dsl : dsls) { expander.addDSLMapping(dsl.getMapping()); } return expander; }
@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 " ); } }
/** * Returns an expander for DSLs (only if there is a DSL configured for this package). */ private Expander getDSLExpander(final Path path) { final Expander expander = new DefaultExpander(); final List<DSLMappingFile> dsls = getDSLMappingFiles(path); for (DSLMappingFile dsl : dsls) { expander.addDSLMapping(dsl.getMapping()); } return expander; }
@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" )); }
/** * Returns an expander for DSLs (only if there is a DSL configured for this package). */ private Expander getDSLExpander(final Path path) { final Expander expander = new DefaultExpander(); final List<DSLMappingFile> dsls = getDSLMappingFiles(path); for (DSLMappingFile dsl : dsls) { expander.addDSLMapping(dsl.getMapping()); } return expander; }
@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()); }
/** * Returns an expander for DSLs (only if there is a DSL configured for this package). */ private Expander getDSLExpander(final Path path) { final Expander expander = new DefaultExpander(); final List<DSLMappingFile> dsls = getDSLMappingFiles(path); for (DSLMappingFile dsl : dsls) { expander.addDSLMapping(dsl.getMapping()); } return expander; }
@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 " ); } }
/** * Returns an expander for DSLs (only if there is a DSL configured for this package). */ private Expander getDSLExpander(final Path path) { final Expander expander = new DefaultExpander(); final List<DSLMappingFile> dsls = getDSLMappingFiles(path); for (DSLMappingFile dsl : dsls) { expander.addDSLMapping(dsl.getMapping()); } return expander; }
@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 " ); } }
/** * Returns an expander for DSLs (only if there is a DSL configured for this package). * @param path The {@link Path} of the resource to index * @return a {@link Expander} used to produce a DRL of the file */ public Expander getDSLExpander(final Path path) { final Expander expander = new DefaultExpander(); final List<DSLMappingFile> dsls = getDSLMappingFiles(path); for (DSLMappingFile dsl : dsls) { expander.addDSLMapping(dsl.getMapping()); } return expander; }
@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); }
/** * Returns an expander for DSLs (only if there is a DSL configured for this package). */ private Expander getDSLExpander(final Path path) { final Expander expander = new DefaultExpander(); final List<DSLMappingFile> dsls = getDSLMappingFiles(path); for (DSLMappingFile dsl : dsls) { expander.addDSLMapping(dsl.getMapping()); } return expander; }
/** * 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 " ); } }