Search in sources :

Example 6 with DSLTokenizedMappingFile

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

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

the class DefaultExpanderTest method testDotInPattern.

@Test
public void testDotInPattern() throws Exception {
    // BZ-1013960
    String source = "import org.drools.mvel.compiler.Person;" + NL + "global java.util.List list" + NL + "rule R1" + NL + "when" + NL + "then" + NL + "Log X" + NL + "end" + NL;
    String dsl = "[then]Log {message:.}=list.add(\"{message}\");";
    String expected = "import org.drools.mvel.compiler.Person;" + NL + "global java.util.List list" + NL + "rule R1" + NL + "when" + NL + "then" + NL + "list.add(\"X\");" + NL + "end" + NL;
    DSLTokenizedMappingFile file = new DSLTokenizedMappingFile();
    file.parseAndLoad(new StringReader(dsl));
    assertEquals(file.getErrors().toString(), 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 8 with DSLTokenizedMappingFile

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

the class DefaultExpanderTest method setUp.

@Before
public void setUp() throws Exception {
    final String filename = "test_metainfo.dsl";
    final Reader reader = new InputStreamReader(this.getClass().getResourceAsStream(filename));
    this.file = new DSLTokenizedMappingFile();
    this.tokenizedFile = new DSLTokenizedMappingFile();
    this.file.parseAndLoad(reader);
    reader.close();
    final Reader reader2 = new InputStreamReader(this.getClass().getResourceAsStream(filename));
    this.tokenizedFile.parseAndLoad(reader2);
    reader2.close();
    this.expander = new DefaultExpander();
}
Also used : InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) DSLTokenizedMappingFile(org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile) DefaultExpander(org.drools.drl.parser.lang.dsl.DefaultExpander) Before(org.junit.Before)

Example 9 with DSLTokenizedMappingFile

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

the class DefaultExpanderTest method testExpandExpr.

@Test
public void testExpandExpr() throws Exception {
    DSLTokenizedMappingFile file = new DSLTokenizedMappingFile();
    String dsl = "[when]Name of Applicant {nameVar:CF:Applicant.age}= System.out.println({nameVar})";
    String source = "rule \"test rule for custom form in DSL\"" + NL + "     dialect \"mvel\"" + NL + "     when" + NL + "         Name of Applicant Bojan Oklahoma and NJ,Andrew AMW Test" + NL + "     then" + NL + "end";
    String expected = "rule \"test rule for custom form in DSL\"" + NL + "     dialect \"mvel\"" + NL + "     when" + NL + "         System.out.println(Bojan Oklahoma and NJ,Andrew AMW Test)" + NL + "     then" + NL + "end";
    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 10 with DSLTokenizedMappingFile

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

the class DSLTokenizedMappingFileTest method testParseFileWithEscaptedEquals.

@Test
public void testParseFileWithEscaptedEquals() {
    String file = "[when]something:\\={value}=Attribute( something == \"{value}\" )";
    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.CONDITION, entry.getSection());
        assertEquals(DSLMappingEntry.EMPTY_METADATA, entry.getMetaData());
        assertEquals(lookbehind + "something:\\=(.*?)$", entry.getKeyPattern().toString());
        assertEquals("Attribute( something == \"{value}\" )", entry.getValuePattern());
    } catch (final IOException e) {
        e.printStackTrace();
        fail("Should not raise exception ");
    }
}
Also used : StringReader(java.io.StringReader) StringReader(java.io.StringReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) DSLMappingEntry(org.drools.drl.parser.lang.dsl.DSLMappingEntry) IOException(java.io.IOException) DSLTokenizedMappingFile(org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile) Test(org.junit.Test)

Aggregations

DSLTokenizedMappingFile (org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile)33 StringReader (java.io.StringReader)32 Test (org.junit.Test)29 DefaultExpander (org.drools.drl.parser.lang.dsl.DefaultExpander)23 Reader (java.io.Reader)15 InputStreamReader (java.io.InputStreamReader)14 IOException (java.io.IOException)12 DSLMappingEntry (org.drools.drl.parser.lang.dsl.DSLMappingEntry)9 DSLMappingFile (org.drools.drl.parser.lang.dsl.DSLMappingFile)7 ExpanderException (org.drools.drl.parser.lang.ExpanderException)2 Iterator (java.util.Iterator)1 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)1 XmlChangeSetReader (org.drools.core.xml.XmlChangeSetReader)1 DrlParser (org.drools.drl.parser.DrlParser)1 Expander (org.drools.drl.parser.lang.Expander)1 DefaultExpanderResolver (org.drools.drl.parser.lang.dsl.DefaultExpanderResolver)1 Before (org.junit.Before)1 Ignore (org.junit.Ignore)1