use of org.drools.drl.parser.lang.dsl.DSLMappingFile in project drools by kiegroup.
the class DefaultExpanderTest method testExpandKeyword.
@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" ));
}
use of org.drools.drl.parser.lang.dsl.DSLMappingFile in project drools by kiegroup.
the class DefaultExpanderTest method testLineNumberError.
@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());
}
use of org.drools.drl.parser.lang.dsl.DSLMappingFile in project drools by kiegroup.
the class DefaultExpanderTest method testExpandParts.
@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" ));
}
use of org.drools.drl.parser.lang.dsl.DSLMappingFile in project drools by kiegroup.
the class DrlParserTest method testExpandDRLUsingInjectedExpander.
@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);
}
use of org.drools.drl.parser.lang.dsl.DSLMappingFile in project drools by kiegroup.
the class DefaultExpanderTest method testExpandWithKeywordClashes.
@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);
}
Aggregations