Search in sources :

Example 16 with DSLMappingEntry

use of org.drools.compiler.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) 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 17 with DSLMappingEntry

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

the class DSLMappingFileTest method testParseFileWithEscaptedEquals.

@Test
@Ignore
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("something:={value}", entry.getMappingKey());
        assertEquals("Attribute( something == \"{value}\" )", entry.getMappingValue());
    } 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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 18 with DSLMappingEntry

use of org.drools.compiler.lang.dsl.DSLMappingEntry in project drools-wb by kiegroup.

the class DslFileIndexer method fillIndexBuilder.

@Override
public DefaultIndexBuilder fillIndexBuilder(final Path path) throws Exception {
    final List<String> lhs = new ArrayList<String>();
    final List<String> rhs = new ArrayList<String>();
    final String dsl = ioService.readAllString(path);
    // Construct a dummy DRL file to parse index elements
    final DSLTokenizedMappingFile dslLoader = new DSLTokenizedMappingFile();
    if (dslLoader.parseAndLoad(new StringReader(dsl))) {
        DSLMapping dslMapping = dslLoader.getMapping();
        for (DSLMappingEntry e : dslMapping.getEntries()) {
            switch(e.getSection()) {
                case CONDITION:
                    lhs.add(e.getValuePattern());
                    break;
                case CONSEQUENCE:
                    rhs.add(e.getValuePattern());
                    break;
                default:
            }
        }
        final String drl = makeDrl(path, lhs, rhs);
        return fillDrlIndexBuilder(path, drl);
    }
    return null;
}
Also used : DSLMapping(org.drools.compiler.lang.dsl.DSLMapping) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) DSLMappingEntry(org.drools.compiler.lang.dsl.DSLMappingEntry) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile)

Aggregations

DSLMappingEntry (org.drools.compiler.lang.dsl.DSLMappingEntry)18 Test (org.junit.Test)14 DSLTokenizedMappingFile (org.drools.compiler.lang.dsl.DSLTokenizedMappingFile)11 StringReader (java.io.StringReader)10 IOException (java.io.IOException)8 InputStreamReader (java.io.InputStreamReader)8 Reader (java.io.Reader)8 DefaultExpander (org.drools.compiler.lang.dsl.DefaultExpander)6 ArrayList (java.util.ArrayList)3 Iterator (java.util.Iterator)1 DSLMapping (org.drools.compiler.lang.dsl.DSLMapping)1 DefaultDSLMapping (org.drools.compiler.lang.dsl.DefaultDSLMapping)1 DSLSentence (org.drools.workbench.models.datamodel.rule.DSLSentence)1 Ignore (org.junit.Ignore)1