Search in sources :

Example 11 with DefaultExpander

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

the class DSLMappingEntryTest method testExpandSpaces.

@Test
public void testExpandSpaces() throws IOException {
    DSLMappingEntry entry = this.setupEntry();
    DefaultExpander ex = makeExpander(entry);
    String[] strs = new String[] { "0_sp", " 1_sp", "   3_sp", "0_sp_1 ", "0_sp_3   ", "0_sp 1_sp 2_sp", "   3_sp   3_sp 1_sp 1_sp_2  " };
    StringBuilder sb = new StringBuilder("rule x\n" + "when\n");
    for (String str : strs) {
        sb.append("String is \"" + str + "\"" + NL);
    }
    sb.append("then\n" + "end\n");
    String dslr = sb.toString();
    String drl = ex.expand(dslr);
    for (String str : strs) {
        assertTrue(drl.contains('"' + str + '"'));
    }
}
Also used : DSLMappingEntry(org.drools.compiler.lang.dsl.DSLMappingEntry) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 12 with DefaultExpander

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

the class DSLMappingEntryTest method testExpandWithDots.

@Test
public void testExpandWithDots() throws IOException {
    DSLMappingEntry entry1 = this.createEntry("- {prop} is not {val} ", "{prop} != {val}");
    DSLMappingEntry entry2 = this.createEntry("- {prop} is {val} ", "{prop} == {val}");
    DSLMappingEntry entry3 = this.createEntry("- {prop} is_not {val} ", "{prop} != {val}");
    DefaultExpander ex = makeExpander(entry1, entry2, entry3);
    StringBuilder sb = new StringBuilder("rule x\n").append("when\n");
    sb.append("> Foo()").append(NL);
    sb.append("- type1 is ClientServiceType.TypeGOLD").append(NL);
    sb.append("- type2 is_not ClientServiceType.TypeGOLD").append(NL);
    sb.append("- type3 is not ClientServiceType.TypeGOLD").append(NL);
    sb.append("then\n").append("end\n");
    String dslr = sb.toString();
    String drl = ex.expand(dslr);
    System.out.println(dslr);
    System.out.println(drl);
    assertTrue("failure type1", drl.contains("type1 == ClientServiceType.TypeGOLD"));
    assertTrue("failure type2", drl.contains("type2 != ClientServiceType.TypeGOLD"));
    assertTrue("failure type3", drl.contains("type3 != ClientServiceType.TypeGOLD"));
}
Also used : DSLMappingEntry(org.drools.compiler.lang.dsl.DSLMappingEntry) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 13 with DefaultExpander

use of org.drools.compiler.lang.dsl.DefaultExpander 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.compiler.lang.dsl.DSLTokenizedMappingFile) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 14 with DefaultExpander

use of org.drools.compiler.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) StringReader(java.io.StringReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) DSLMappingEntry(org.drools.compiler.lang.dsl.DSLMappingEntry) IOException(java.io.IOException) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 15 with DefaultExpander

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

the class DSLMappingFileTest 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.compiler.lang.dsl.DSLTokenizedMappingFile) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) Test(org.junit.Test)

Aggregations

DefaultExpander (org.drools.compiler.lang.dsl.DefaultExpander)18 Test (org.junit.Test)11 Expander (org.drools.compiler.lang.Expander)7 DSLMappingFile (org.drools.compiler.lang.dsl.DSLMappingFile)7 DSLTokenizedMappingFile (org.drools.compiler.lang.dsl.DSLTokenizedMappingFile)7 IOException (java.io.IOException)6 StringReader (java.io.StringReader)6 DSLMappingEntry (org.drools.compiler.lang.dsl.DSLMappingEntry)6 InputStreamReader (java.io.InputStreamReader)5 Reader (java.io.Reader)5 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)1 DrlParser (org.drools.compiler.compiler.DrlParser)1 DroolsParserException (org.drools.compiler.compiler.DroolsParserException)1 ExpanderException (org.drools.compiler.lang.ExpanderException)1 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)1 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)1 DefaultDSLMapping (org.drools.compiler.lang.dsl.DefaultDSLMapping)1 DefaultExpanderResolver (org.drools.compiler.lang.dsl.DefaultExpanderResolver)1 DSLSentence (org.drools.workbench.models.datamodel.rule.DSLSentence)1