Search in sources :

Example 31 with DSLTokenizedMappingFile

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

the class DefaultExpanderTest method testANTLRExpandFailure.

@Test
public void testANTLRExpandFailure() throws Exception {
    DSLTokenizedMappingFile 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());
    String source = "rule 'q'" + NL + "agenda-group 'x'" + NL + "when" + NL + "    foo  " + NL + "then" + NL + "    bar 42" + NL + "end";
    String drl = ex.expand(source);
    assertFalse(ex.hasErrors());
    ex = new DefaultExpander();
    ex.addDSLMapping(file.getMapping());
    source = "rule 'q' agenda-group 'x'" + NL + "when" + NL + "    foos " + NL + "then" + NL + "    bar 42" + NL + " end";
    drl = ex.expand(source);
    // System.out.println( drl );
    assertTrue(ex.hasErrors());
    assertEquals(1, ex.getErrors().size());
// System.err.println(( (ExpanderException) ex.getErrors().get( 0 )).getMessage());
}
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 32 with DSLTokenizedMappingFile

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

the class DSLTokenizedMappingFileTest method testParseFileWithEscapes.

@Test
public void testParseFileWithEscapes() {
    String file = "[then]TEST=System.out.println(\"DO_SOMETHING\");" + NL + "" + "[when]code {code1} occurs and sum of all digit not equal \\( {code2} \\+ {code3} \\)=AAAA( cd1 == {code1}, cd2 != ( {code2} + {code3} ))" + NL + "" + "[when]code {code1} occurs=BBBB" + NL + "";
    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());
        final String LHS = "code 1041 occurs and sum of all digit not equal ( 1034 + 1035 )";
        final String rule = "rule \"x\"" + NL + "when" + NL + "" + LHS + "" + NL + "then" + NL + "TEST" + NL + "end";
        DefaultExpander de = new DefaultExpander();
        de.addDSLMapping(this.file.getMapping());
        final String ruleAfterExpansion = de.expand(rule);
        final String expected = "rule \"x\"" + NL + "when" + NL + "AAAA( cd1 == 1041, cd2 != ( 1034 + 1035 ))" + NL + "then" + NL + "System.out.println(\"DO_SOMETHING\");" + NL + "end";
        assertEquals(expected, ruleAfterExpansion);
    } 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) 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 33 with DSLTokenizedMappingFile

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

the class DSLMappingEntryTest method createEntry.

private DSLMappingEntry createEntry(final String inputKey, final String inputValue) throws IOException {
    String mapping = "[condition][]" + inputKey + "=" + inputValue;
    StringReader dsl = new StringReader(mapping);
    DSLMappingEntry entry = null;
    try {
        DSLTokenizedMappingFile parser = new DSLTokenizedMappingFile();
        if (parser.parseAndLoad(dsl)) {
            entry = parser.getMapping().getEntries().get(0);
        } else {
            throw new RuntimeException("Error parsing entry: " + mapping + ": " + parser.getErrors().toString());
        }
    } finally {
        dsl.close();
    }
    return entry;
}
Also used : StringReader(java.io.StringReader) DSLMappingEntry(org.drools.drl.parser.lang.dsl.DSLMappingEntry) DSLTokenizedMappingFile(org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile)

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