use of org.drools.drl.parser.lang.Expander 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.Expander in project drools by kiegroup.
the class DrlParser method getExpandedDRL.
/**
* This will expand the DRL using the given expander resolver. useful for
* debugging.
*
* @param source -
* the source which use a DSL
* @param resolver -
* the DSL expander resolver itself.
* @throws DroolsParserException
* If unable to expand in any way.
*/
public String getExpandedDRL(final String source, final DefaultExpanderResolver resolver) throws DroolsParserException {
final Expander expander = resolver.get("*", null);
final String expanded = expander.expand(source);
if (expander.hasErrors()) {
String err = "";
for (ExpanderException ex : expander.getErrors()) {
err = err + "\n Line:[" + ex.getLine() + "] " + ex.getMessage();
}
throw new DroolsParserException(err);
}
return expanded;
}
use of org.drools.drl.parser.lang.Expander 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.Expander in project drools by kiegroup.
the class ErrorsParserTest method testExpanderErrorsAfterExpansion.
@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());
}
use of org.drools.drl.parser.lang.Expander 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