Search in sources :

Example 1 with DefaultExpanderResolver

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);
}
Also used : DefaultExpanderResolver(org.drools.drl.parser.lang.dsl.DefaultExpanderResolver) Expander(org.drools.drl.parser.lang.Expander)

Example 2 with DefaultExpanderResolver

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);
}
Also used : DefaultExpanderResolver(org.drools.drl.parser.lang.dsl.DefaultExpanderResolver) DefaultExpander(org.drools.drl.parser.lang.dsl.DefaultExpander) Expander(org.drools.drl.parser.lang.Expander) StringReader(java.io.StringReader) DrlParser(org.drools.drl.parser.DrlParser) DSLMappingFile(org.drools.drl.parser.lang.dsl.DSLMappingFile) DSLTokenizedMappingFile(org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile) DefaultExpander(org.drools.drl.parser.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 3 with DefaultExpanderResolver

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());
}
Also used : DefaultExpanderResolver(org.drools.drl.parser.lang.dsl.DefaultExpanderResolver) Expander(org.drools.drl.parser.lang.Expander) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) Test(org.junit.Test)

Aggregations

Expander (org.drools.drl.parser.lang.Expander)3 DefaultExpanderResolver (org.drools.drl.parser.lang.dsl.DefaultExpanderResolver)3 StringReader (java.io.StringReader)2 Test (org.junit.Test)2 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 DrlParser (org.drools.drl.parser.DrlParser)1 DSLMappingFile (org.drools.drl.parser.lang.dsl.DSLMappingFile)1 DSLTokenizedMappingFile (org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile)1 DefaultExpander (org.drools.drl.parser.lang.dsl.DefaultExpander)1