use of org.drools.drl.parser.lang.dsl.DefaultExpanderResolver in project drools by kiegroup.
the class DrlParser method parse.
/**
* Parse and build a rule package from a DRL source with a domain specific
* language.
*
* @param source
* As Text.
* @param dsl
* @return
* @throws DroolsParserException
*/
public PackageDescr parse(boolean isEditor, final String source, final Reader dsl) throws DroolsParserException {
DefaultExpanderResolver resolver = getDefaultResolver(dsl);
final Expander expander = resolver.get("*", null);
final String expanded = expander.expand(source);
if (expander.hasErrors()) {
this.results.addAll(expander.getErrors());
}
return this.parse(isEditor, expanded);
}
use of org.drools.drl.parser.lang.dsl.DefaultExpanderResolver 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.DefaultExpanderResolver in project drools by kiegroup.
the class DslTest method testMultiLineTemplates.
@Test
public void testMultiLineTemplates() throws Exception {
final Reader source = new InputStreamReader(getClass().getResourceAsStream("rule_with_expander_multiline.dslr"));
final Reader dsl = new InputStreamReader(getClass().getResourceAsStream("test_dsl_multiline.dsl"));
Expander ex = new DefaultExpanderResolver(dsl).get("*", null);
String r = ex.expand(source);
assertEquals("when Car(color==\"Red\") then doSomething();", r.trim());
}
Aggregations