private DSLMappingEntry setupEntry() throws IOException { final String inputKey = "String is \"{value}\""; final String inputValue = "SomeFact(value==\"{value}\")"; return createEntry( inputKey, inputValue ); }
@Test public void testAddDSLMapping() { this.expander.addDSLMapping(this.file.getMapping()); // should not raise any exception }
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; }
@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" )); }
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); }
@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" )); }
private DSLMappingEntry createEntry(final String inputKey, final String inputValue) throws IOException { String mapping = "[condition][]" + inputKey + "=" + inputValue; StringReader dsl = new StringReader( mapping ); DSLMappingEntry entry = null; try { DSLTokenizedMappingFile parser = new DSLTokenizedMappingFile(); if ( parser.parseAndLoad( dsl ) ) { entry = parser.getMapping().getEntries().get( 0 ); } else { throw new RuntimeException( "Error parsing entry: " + mapping + ": " + parser.getErrors().toString() ); } } finally { dsl.close(); } return entry; }
@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 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 testPatternCalculation() throws IOException { final String inputKey = "The Customer name is {name} and surname is {surname} and it has US$ 50,00 on his {pocket}"; final String inputValue = "Customer( name == \"{name}\", surname == \"{surname}\", money > $money )"; final String expectedKeyP = lookbehind + "The\\s+Customer\\s+name\\s+is\\s+(.*?)\\s+and\\s+surname\\s+is\\s+(.*?)\\s+and\\s+it\\s+has\\s+US\\$\\s+50,00\\s+on\\s+his\\s+(.*?)$"; final String expectedValP = "Customer( name == \"{name}\", surname == \"{surname}\", money > $money )"; final DSLMappingEntry entry = createEntry( inputKey, inputValue ); assertEquals( inputKey, entry.getMappingKey() ); assertEquals( expectedKeyP, entry.getKeyPattern().pattern() ); assertEquals( inputValue, entry.getMappingValue() ); assertEquals( expectedValP, entry.getValuePattern() ); }
@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 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); }
@Test public void testANTLRAddDSLMapping() { this.expander.addDSLMapping(this.tokenizedFile.getMapping()); // should not raise any exception }
@Test public void testExpandComplex() throws Exception { String source = "rule \"R\"" + NL + "dialect \"mvel\"" + NL + "when" + NL + "There is an TestObject" + NL + "-startDate is before 01-Jul-2011" + NL + "-endDate is after 01-Jul-2011" + NL + "then" + NL + "end" + NL; String expected = "rule \"R\"" + NL + "dialect \"mvel\"" + NL + "when" + NL + "TestObject(startDate>DateUtils.parseDate(\"01-Jul-2011\"), endDate>DateUtils.parseDate(\"01-Jul-2011\"))" + NL + "then" + NL + "end" + NL; checkExpansion(source, expected); }
@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 testPatternCalculation2() throws IOException { final String inputKey = "-name is {name}"; final String inputValue = "name == \"{name}\""; final String expectedKeyP = lookbehind + "-\\s*name\\s+is\\s+(.*?)$"; final String expectedValP = "name == \"{name}\""; final DSLMappingEntry entry = createEntry( inputKey, inputValue ); assertEquals( inputKey, entry.getMappingKey() ); assertEquals( expectedKeyP, entry.getKeyPattern().pattern() ); assertEquals( inputValue, entry.getMappingValue() ); assertEquals( expectedValP, entry.getValuePattern() ); }
@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 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 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 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()); }