Search in sources :

Example 11 with DSLMappingEntry

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

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

the class DSLMappingEntryTest method testPatternCalculation2.

@Test
public void testPatternCalculation2() throws IOException {
    final String inputKey = "-name is {name}";
    final String inputValue = "name == \"{name}\"";
    final String expectedKeyP = lookbehind + "-\\s*name\\s+is\\s+(.*?)$";
    final String expectedValP = "name == \"{name}\"";
    final DSLMappingEntry entry = createEntry(inputKey, inputValue);
    assertEquals(inputKey, entry.getMappingKey());
    assertEquals(expectedKeyP, entry.getKeyPattern().pattern());
    assertEquals(inputValue, entry.getMappingValue());
    assertEquals(expectedValP, entry.getValuePattern());
}
Also used : DSLMappingEntry(org.drools.drl.parser.lang.dsl.DSLMappingEntry) Test(org.junit.Test)

Example 13 with DSLMappingEntry

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

the class DSLMappingEntryTest method testPatternCalculation3.

@Test
public void testPatternCalculation3() throws IOException {
    final String inputKey = "- name is {name}";
    final String inputValue = "name == \"{name}\"";
    final String expectedKeyP = lookbehind + "-\\s*name\\s+is\\s+(.*?)$";
    final String expectedValP = "name == \"{name}\"";
    final DSLMappingEntry entry = createEntry(inputKey, inputValue);
    assertEquals(inputKey, entry.getMappingKey());
    assertEquals(entry.getKeyPattern().pattern(), expectedKeyP, entry.getKeyPattern().pattern());
    assertEquals(inputValue, entry.getMappingValue());
    assertEquals(expectedValP, entry.getValuePattern());
}
Also used : DSLMappingEntry(org.drools.drl.parser.lang.dsl.DSLMappingEntry) Test(org.junit.Test)

Example 14 with DSLMappingEntry

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

Example 15 with DSLMappingEntry

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

DSLMappingEntry (org.drools.drl.parser.lang.dsl.DSLMappingEntry)16 Test (org.junit.Test)14 DSLTokenizedMappingFile (org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile)9 InputStreamReader (java.io.InputStreamReader)8 Reader (java.io.Reader)8 StringReader (java.io.StringReader)8 IOException (java.io.IOException)7 DefaultExpander (org.drools.drl.parser.lang.dsl.DefaultExpander)6 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 DefaultDSLMapping (org.drools.drl.parser.lang.dsl.DefaultDSLMapping)1 Ignore (org.junit.Ignore)1