Search in sources :

Example 1 with Expander

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

Example 2 with Expander

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

Example 3 with Expander

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);
}
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 4 with Expander

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());
}
Also used : DefaultExpander(org.drools.drl.parser.lang.dsl.DefaultExpander) Expander(org.drools.drl.parser.lang.Expander) DRLParser(org.drools.drl.parser.lang.DRLParser) DroolsParserException(org.drools.drl.parser.DroolsParserException) DefaultExpander(org.drools.drl.parser.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 5 with Expander

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());
}
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)5 DefaultExpanderResolver (org.drools.drl.parser.lang.dsl.DefaultExpanderResolver)3 Test (org.junit.Test)3 StringReader (java.io.StringReader)2 DefaultExpander (org.drools.drl.parser.lang.dsl.DefaultExpander)2 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 DrlParser (org.drools.drl.parser.DrlParser)1 DroolsParserException (org.drools.drl.parser.DroolsParserException)1 DRLParser (org.drools.drl.parser.lang.DRLParser)1 ExpanderException (org.drools.drl.parser.lang.ExpanderException)1 DSLMappingFile (org.drools.drl.parser.lang.dsl.DSLMappingFile)1 DSLTokenizedMappingFile (org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile)1