Search in sources :

Example 1 with DSLMappingFile

use of org.drools.drl.parser.lang.dsl.DSLMappingFile in project drools by kiegroup.

the class DefaultExpanderTest method testExpandKeyword.

@Test
public void testExpandKeyword() throws Exception {
    DSLMappingFile file = new DSLTokenizedMappingFile();
    String dsl = "[keyword]key {param}=Foo( attr=={param} )";
    file.parseAndLoad(new StringReader(dsl));
    assertEquals(0, file.getErrors().size());
    DefaultExpander ex = new DefaultExpander();
    ex.addDSLMapping(file.getMapping());
    String source = "rule x" + NL + "when" + NL + " key 1 " + NL + " key 2 " + NL + "then" + NL + "end";
    String drl = ex.expand(source);
    System.out.println(drl);
    assertTrue(drl.contains("attr==1"));
    assertTrue(drl.contains("attr==2"));
// System.err.println(ex.expand( "rule 'x' " + NL + " when " + NL + " foo " + NL + " then " + NL + " end" ));
}
Also used : StringReader(java.io.StringReader) 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 2 with DSLMappingFile

use of org.drools.drl.parser.lang.dsl.DSLMappingFile in project drools by kiegroup.

the class DefaultExpanderTest method testLineNumberError.

@Test
public void testLineNumberError() throws Exception {
    DSLMappingFile file = new DSLTokenizedMappingFile();
    String dsl = "[when]foo=Foo()" + NL + "[then]bar {num}=baz({num});";
    file.parseAndLoad(new StringReader(dsl));
    DefaultExpander ex = new DefaultExpander();
    ex.addDSLMapping(file.getMapping());
    String source = "rule 'q'" + NL + "agenda-group 'x'" + NL + "when" + NL + "    __  " + NL + "then" + NL + "    bar 42" + NL + "\tgoober" + NL + "end";
    ex.expand(source);
    assertTrue(ex.hasErrors());
    assertEquals(2, ex.getErrors().size());
    ExpanderException err = (ExpanderException) ex.getErrors().get(0);
    assertEquals(4, err.getLine());
    err = (ExpanderException) ex.getErrors().get(1);
    assertEquals(7, err.getLine());
}
Also used : StringReader(java.io.StringReader) ExpanderException(org.drools.drl.parser.lang.ExpanderException) 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 DSLMappingFile

use of org.drools.drl.parser.lang.dsl.DSLMappingFile in project drools by kiegroup.

the class DefaultExpanderTest method testExpandParts.

@Test
public void testExpandParts() throws Exception {
    DSLMappingFile file = new DSLTokenizedMappingFile();
    String dsl = "[when]foo=Foo()" + NL + "[then]bar {num}=baz({num});";
    file.parseAndLoad(new StringReader(dsl));
    assertEquals(0, file.getErrors().size());
    DefaultExpander ex = new DefaultExpander();
    ex.addDSLMapping(file.getMapping());
// System.err.println(ex.expand( "rule 'x' " + NL + " when " + NL + " foo " + NL + " then " + NL + " end" ));
}
Also used : StringReader(java.io.StringReader) 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 DSLMappingFile

use of org.drools.drl.parser.lang.dsl.DSLMappingFile 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 5 with DSLMappingFile

use of org.drools.drl.parser.lang.dsl.DSLMappingFile in project drools by kiegroup.

the class DefaultExpanderTest method testExpandWithKeywordClashes.

@Test
public void testExpandWithKeywordClashes() throws Exception {
    DSLMappingFile file = new DSLTokenizedMappingFile();
    String dsl = "[when]Invoke rule executor=ruleExec: RuleExecutor()" + NL + "[then]Execute rule \"{id}\"=ruleExec.ExecuteSubRule( new Long({id}));";
    file.parseAndLoad(new StringReader(dsl));
    assertEquals(0, file.getErrors().size());
    DefaultExpander ex = new DefaultExpander();
    ex.addDSLMapping(file.getMapping());
    String source = "package something;" + NL + NL + "rule \"1\"" + NL + "when" + NL + "    Invoke rule executor" + NL + "then" + NL + "    Execute rule \"5\"" + NL + "end";
    String expected = "package something;" + NL + NL + "rule \"1\"" + NL + "when" + NL + "   ruleExec: RuleExecutor()" + NL + "then" + NL + "   ruleExec.ExecuteSubRule( new Long(5));" + NL + "end" + NL;
    String drl = ex.expand(source);
    // System.out.println("["+drl+"]" );
    // System.out.println("["+expected+"]" );
    assertFalse(ex.hasErrors());
    equalsIgnoreWhiteSpace(expected, drl);
}
Also used : StringReader(java.io.StringReader) 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)

Aggregations

StringReader (java.io.StringReader)7 DSLMappingFile (org.drools.drl.parser.lang.dsl.DSLMappingFile)7 DSLTokenizedMappingFile (org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile)7 DefaultExpander (org.drools.drl.parser.lang.dsl.DefaultExpander)7 Test (org.junit.Test)7 DrlParser (org.drools.drl.parser.DrlParser)1 Expander (org.drools.drl.parser.lang.Expander)1 ExpanderException (org.drools.drl.parser.lang.ExpanderException)1 DefaultExpanderResolver (org.drools.drl.parser.lang.dsl.DefaultExpanderResolver)1