@Test public void testANTLRExpandFailure() throws Exception { DSLTokenizedMappingFile 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 testANTLRRegexp() throws Exception { this.expander.addDSLMapping(this.tokenizedFile.getMapping()); final Reader rules = new InputStreamReader(this.getClass().getResourceAsStream("test_expansion.dslr")); final String result = this.expander.expand(rules); }
private DefaultExpander makeExpander( DSLMappingEntry... entries ){ DefaultExpander expander = new DefaultExpander(); DefaultDSLMapping mapping = new DefaultDSLMapping(); for( DSLMappingEntry entry: entries ){ mapping.addEntry( entry ); } List<String> options = new ArrayList<String>(); options.add("result"); options.add("when"); options.add("steps"); mapping.setOptions(options); expander.addDSLMapping(mapping); return expander; }
@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 testMe() throws Exception{ DSLTokenizedMappingFile tokenizedFile = null; final String filename = "test_antlr.dsl"; final Reader reader = new InputStreamReader( this.getClass().getResourceAsStream( filename ) ); tokenizedFile = new DSLTokenizedMappingFile(); tokenizedFile.parseAndLoad( reader ); reader.close(); for (Iterator it = tokenizedFile.getMapping().getEntries().iterator(); it.hasNext();) { DSLMappingEntry entry = (DSLMappingEntry) it.next(); // System.out.println("ENTRY: " + entry.getKeyPattern() + " ::::: " + entry.getValuePattern()); } DefaultExpander ex = new DefaultExpander(); ex.addDSLMapping( tokenizedFile.getMapping() ); System.err.println(ex.expand( "rule 'x' \n when \n address is present where name is \"foo\" and age is \"32\" \n then \n end" )); }
@Test public void testExpandSpaces() throws IOException { DSLMappingEntry entry = this.setupEntry(); DefaultExpander ex = makeExpander( entry ); String[] strs = new String[]{ "0_sp", " 1_sp", " 3_sp", "0_sp_1 ", "0_sp_3 ", "0_sp 1_sp 2_sp", " 3_sp 3_sp 1_sp 1_sp_2 " }; StringBuilder sb = new StringBuilder( "rule x\n" + "when\n" ); for( String str: strs ){ sb.append( "String is \"" + str + "\"" + NL ); } sb.append( "then\n" + "end\n" ); String dslr = sb.toString(); String drl = ex.expand( dslr ); for( String str: strs ){ assertTrue( drl.contains( '"' + str + '"' ) ); } }
@Test public void testAddDSLMapping() { this.expander.addDSLMapping(this.file.getMapping()); // should not raise any exception }
@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 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 testANTLRExpandParts() throws Exception { DSLTokenizedMappingFile 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 testExpandWithDots() throws IOException { DSLMappingEntry entry1 = this.createEntry( "- {prop} is not {val} ", "{prop} != {val}" ); DSLMappingEntry entry2 = this.createEntry( "- {prop} is {val} ", "{prop} == {val}" ); DSLMappingEntry entry3 = this.createEntry( "- {prop} is_not {val} ", "{prop} != {val}" ); DefaultExpander ex = makeExpander( entry1, entry2, entry3 ); StringBuilder sb = new StringBuilder( "rule x\n" ).append( "when\n" ); sb.append( "> Foo()").append( NL ); sb.append( "- type1 is ClientServiceType.TypeGOLD" ).append( NL ); sb.append( "- type2 is_not ClientServiceType.TypeGOLD" ).append( NL ); sb.append( "- type3 is not ClientServiceType.TypeGOLD" ).append( NL ); sb.append( "then\n" ).append( "end\n" ); String dslr = sb.toString(); String drl = ex.expand( dslr ); System.out.println( dslr ); System.out.println( drl ); assertTrue( "failure type1", drl.contains( "type1 == ClientServiceType.TypeGOLD" ) ); assertTrue( "failure type2", drl.contains( "type2 != ClientServiceType.TypeGOLD" ) ); assertTrue( "failure type3", drl.contains( "type3 != ClientServiceType.TypeGOLD" ) ); }
@Test public void testANTLRAddDSLMapping() { this.expander.addDSLMapping(this.tokenizedFile.getMapping()); // should not raise any 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 testExpanderErrorsAfterExpansion() throws Exception { String name = "expander_post_errors.dslr"; Expander expander = new DefaultExpander(); String expanded = expander.expand( this.getReader( name ) ); DRLParser parser = parse( name, expanded ); parser.compilationUnit(); assertTrue( parser.hasErrors() ); assertEquals( 1, parser.getErrors().size() ); DroolsParserException err = (DroolsParserException) parser.getErrors().get( 0 ); assertEquals(6, err.getLineNumber()); }
@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 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" )); }
sb.append( "then\n" + "end\n" ); String dslr = sb.toString(); String drl = ex.expand( dslr );
private void checkExpansion(String source, String expected) throws Exception { DSLTokenizedMappingFile file = new DSLTokenizedMappingFile(); String dsl = "[when]There is an TestObject=TestObject()" + NL + "[when]-startDate is before {date}=startDate>DateUtils.parseDate(\"{date}\")" + NL + "[when]-endDate is after {date}=endDate>DateUtils.parseDate(\"{date}\")"; file.parseAndLoad(new StringReader(dsl)); assertEquals(0, file.getErrors().size()); DefaultExpander ex = new DefaultExpander(); ex.addDSLMapping(file.getMapping()); String drl = ex.expand(source); assertFalse(ex.hasErrors()); assertEquals(expected, drl); }
/** * 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; }