Search in sources :

Example 1 with DefaultExpanderResolver

use of org.drools.compiler.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.compiler.lang.dsl.DefaultExpanderResolver) Expander(org.drools.compiler.lang.Expander) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 2 with DefaultExpanderResolver

use of org.drools.compiler.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.compiler.lang.dsl.DefaultExpanderResolver) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) Expander(org.drools.compiler.lang.Expander) StringReader(java.io.StringReader) DSLMappingFile(org.drools.compiler.lang.dsl.DSLMappingFile) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 3 with DefaultExpanderResolver

use of org.drools.compiler.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.compiler.lang.dsl.DefaultExpanderResolver) Expander(org.drools.compiler.lang.Expander)

Aggregations

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