Search in sources :

Example 16 with DefaultExpander

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

the class DSLMappingFileTest method testParseFileWithEscaptedCurlyBrackets.

@Test
public void testParseFileWithEscaptedCurlyBrackets() {
    String file = "[consequence][$policy]Add surcharge {surcharge} to Policy=modify(policy) \\{price = {surcharge}\\}";
    try {
        final Reader reader = new StringReader(file);
        this.file = new DSLTokenizedMappingFile();
        final boolean parsingResult = this.file.parseAndLoad(reader);
        reader.close();
        assertTrue(this.file.getErrors().toString(), parsingResult);
        assertTrue(this.file.getErrors().isEmpty());
        assertEquals(1, this.file.getMapping().getEntries().size());
        DSLMappingEntry entry = (DSLMappingEntry) this.file.getMapping().getEntries().get(0);
        assertEquals(DSLMappingEntry.CONSEQUENCE, entry.getSection());
        assertEquals("$policy", entry.getMetaData().toString());
        assertEquals("Add surcharge {surcharge} to Policy", entry.getMappingKey());
        assertEquals("modify(policy) \\{price = {surcharge}\\}", entry.getMappingValue());
        String input = "rule x" + NL + "when" + NL + "then" + NL + "Add surcharge 300 to Policy" + NL + "end" + NL + "";
        String expected = "rule x" + NL + "when" + NL + "then" + NL + "modify(policy) {price = 300}" + NL + "end" + NL + "";
        DefaultExpander de = new DefaultExpander();
        de.addDSLMapping(this.file.getMapping());
        final String result = de.expand(input);
        // String result = entry.getKeyPattern().matcher( input ).replaceAll( entry.getValuePattern() );
        assertEquals(expected, result);
    } catch (final IOException e) {
        e.printStackTrace();
        fail("Should not raise exception ");
    }
}
Also used : StringReader(java.io.StringReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) DSLMappingEntry(org.drools.drl.parser.lang.dsl.DSLMappingEntry) IOException(java.io.IOException) DSLTokenizedMappingFile(org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile) DefaultExpander(org.drools.drl.parser.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 17 with DefaultExpander

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

the class DefaultExpanderTest method testANTLRLineNumberError.

@Test
public void testANTLRLineNumberError() throws Exception {
    DSLTokenizedMappingFile 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) DSLTokenizedMappingFile(org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile) DefaultExpander(org.drools.drl.parser.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 18 with DefaultExpander

use of org.drools.drl.parser.lang.dsl.DefaultExpander 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)

Example 19 with DefaultExpander

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

the class DefaultExpanderTest method testExpandQuery.

@Test
public void testExpandQuery() throws Exception {
    DSLTokenizedMappingFile file = new DSLTokenizedMappingFile();
    String dsl = "[when]There is a person=Person()" + NL + "[when]- {field:\\w*} {operator} {value:\\d*}={field} {operator} {value}" + NL + "[when]is greater than=>";
    String source = "query \"isMature\"" + NL + "There is a person" + NL + "- age is greater than 18" + NL + "end" + NL;
    String expected = "query \"isMature\"" + NL + "Person(age  >  18)" + NL + "end" + NL;
    file.parseAndLoad(new StringReader(dsl));
    assertEquals(0, file.getErrors().size());
    DefaultExpander ex = new DefaultExpander();
    ex.addDSLMapping(file.getMapping());
    String drl = ex.expand(source);
    assertFalse(ex.hasErrors());
    assertEquals(expected, drl);
}
Also used : StringReader(java.io.StringReader) DSLTokenizedMappingFile(org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile) DefaultExpander(org.drools.drl.parser.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 20 with DefaultExpander

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

the class DefaultExpanderTest method testExpandInfiniteLoop.

@Test(timeout = 1000)
public void testExpandInfiniteLoop() throws Exception {
    // DROOLS-73
    DSLMappingFile file = new DSLTokenizedMappingFile();
    String dsl = "[when]Foo with {var} bars=Foo( bars == {var} )";
    file.parseAndLoad(new StringReader(dsl));
    assertEquals(0, file.getErrors().size());
    DefaultExpander ex = new DefaultExpander();
    ex.addDSLMapping(file.getMapping());
    String source = "rule 'dsl rule'" + NL + "when" + NL + " Foo with {var} bars" + NL + "then" + NL + NL + "end";
    ex.expand(source);
    assertFalse(ex.hasErrors());
}
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

DefaultExpander (org.drools.drl.parser.lang.dsl.DefaultExpander)29 Test (org.junit.Test)25 DSLTokenizedMappingFile (org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile)23 StringReader (java.io.StringReader)22 DSLMappingFile (org.drools.drl.parser.lang.dsl.DSLMappingFile)7 InputStreamReader (java.io.InputStreamReader)6 Reader (java.io.Reader)6 DSLMappingEntry (org.drools.drl.parser.lang.dsl.DSLMappingEntry)6 IOException (java.io.IOException)5 ExpanderException (org.drools.drl.parser.lang.ExpanderException)3 DrlParser (org.drools.drl.parser.DrlParser)2 Expander (org.drools.drl.parser.lang.Expander)2 UncheckedIOException (java.io.UncheckedIOException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)1 PackageDescr (org.drools.drl.ast.descr.PackageDescr)1 DroolsParserException (org.drools.drl.parser.DroolsParserException)1 DRLParser (org.drools.drl.parser.lang.DRLParser)1 DefaultDSLMapping (org.drools.drl.parser.lang.dsl.DefaultDSLMapping)1