Search in sources :

Example 1 with Expander

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

Example 2 with Expander

use of org.drools.compiler.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.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 3 with Expander

use of org.drools.compiler.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.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 4 with Expander

use of org.drools.compiler.lang.Expander in project drools by kiegroup.

the class RuleModelDRLPersistenceUnmarshallingTest method testDSLExpansionLHS_WithKeyword_then.

@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1173842
void testDSLExpansionLHS_WithKeyword_then() {
    String expected_dslr = "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "There is an Applicant\n" + "- age more then 55\n" + "then\n" + "end\n";
    String expected_drl = "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "Applicant( age > 55 )\n" + "then\n" + "end\n";
    final String dslDefinition1 = "There is an Applicant";
    final String dslFile1 = "[when]" + dslDefinition1 + "=Applicant( )";
    final String dslDefinition2 = "- age more then {age}";
    final String dslFile2 = "[when]" + dslDefinition2 + "=age > {age}";
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(expected_dslr, Collections.emptyList(), dmo, dslFile1, dslFile2);
    assertNotNull(m);
    assertEquals(2, m.lhs.length);
    assertTrue(m.lhs[0] instanceof DSLSentence);
    assertTrue(m.lhs[1] instanceof DSLSentence);
    DSLSentence dslSentence1 = (DSLSentence) m.lhs[0];
    assertEquals(dslDefinition1, dslSentence1.getDefinition());
    assertEquals(0, dslSentence1.getValues().size());
    DSLSentence dslSentence2 = (DSLSentence) m.lhs[1];
    assertEquals(dslDefinition2, dslSentence2.getDefinition());
    assertEquals(1, dslSentence2.getValues().size());
    DSLVariableValue dslVariableValue = dslSentence2.getValues().get(0);
    assertEquals("55", dslVariableValue.getValue());
    // Check round-trip
    assertEqualsIgnoreWhitespace(expected_dslr, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
    // Check DSL expansion (as BZ stated runtime was flawed as well)
    final Expander expander = new DefaultExpander();
    final List<DSLMappingFile> dsls = new ArrayList<>();
    try {
        final DSLTokenizedMappingFile dslTokenizer1 = new DSLTokenizedMappingFile();
        if (dslTokenizer1.parseAndLoad(new StringReader(dslFile1))) {
            dsls.add(dslTokenizer1);
        } else {
            fail();
        }
        final DSLTokenizedMappingFile dslTokenizer2 = new DSLTokenizedMappingFile();
        if (dslTokenizer2.parseAndLoad(new StringReader(dslFile2))) {
            dsls.add(dslTokenizer2);
        } else {
            fail();
        }
    } catch (IOException e) {
        fail();
    }
    for (DSLMappingFile dsl : dsls) {
        expander.addDSLMapping(dsl.getMapping());
    }
    final String actual_drl = expander.expand(expected_dslr);
    assertEqualsIgnoreWhitespace(expected_drl, actual_drl);
}
Also used : Expander(org.drools.compiler.lang.Expander) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) DSLVariableValue(org.drools.workbench.models.datamodel.rule.DSLVariableValue) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) IOException(java.io.IOException) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) DSLMappingFile(org.drools.compiler.lang.dsl.DSLMappingFile) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile) Test(org.junit.Test)

Example 5 with Expander

use of org.drools.compiler.lang.Expander in project drools-wb by kiegroup.

the class GuidedRuleDSLRSourceService method getSource.

@Override
public String getSource(final Path path, final RuleModel model) throws SourceGenerationFailedException {
    try {
        final String dslr = RuleModelDRLPersistenceImpl.getInstance().marshal(model);
        final Expander expander = getDSLExpander(path);
        final String drl = expander.expand(dslr);
        return drl;
    } catch (Exception e) {
        throw new SourceGenerationFailedException(e.getMessage());
    }
}
Also used : SourceGenerationFailedException(org.kie.workbench.common.services.shared.source.SourceGenerationFailedException) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) Expander(org.drools.compiler.lang.Expander) SourceGenerationFailedException(org.kie.workbench.common.services.shared.source.SourceGenerationFailedException) IOException(java.io.IOException)

Aggregations

Expander (org.drools.compiler.lang.Expander)15 DefaultExpander (org.drools.compiler.lang.dsl.DefaultExpander)12 DSLMappingFile (org.drools.compiler.lang.dsl.DSLMappingFile)7 IOException (java.io.IOException)4 StringReader (java.io.StringReader)3 DefaultExpanderResolver (org.drools.compiler.lang.dsl.DefaultExpanderResolver)3 Test (org.junit.Test)3 SourceGenerationFailedException (org.kie.workbench.common.services.shared.source.SourceGenerationFailedException)3 DSLTokenizedMappingFile (org.drools.compiler.lang.dsl.DSLTokenizedMappingFile)2 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1 ExpanderException (org.drools.compiler.lang.ExpanderException)1 DSLSentence (org.drools.workbench.models.datamodel.rule.DSLSentence)1 DSLVariableValue (org.drools.workbench.models.datamodel.rule.DSLVariableValue)1 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)1