Search in sources :

Example 6 with DSLMappingEntry

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

the class DSLMappingFileTest method testParseFileWithEscaptedBrackets.

@Test
public void testParseFileWithEscaptedBrackets() {
    String file = "[when][]ATTRIBUTE \"{attr}\" IS IN \\[{list}\\]=Attribute( {attr} in ({list}) )";
    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("ATTRIBUTE \"{attr}\" IS IN \\[{list}\\]", entry.getMappingKey());
        assertEquals("Attribute( {attr} in ({list}) )", 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) Test(org.junit.Test)

Example 7 with DSLMappingEntry

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

the class DSLMappingFileTest method testEnum.

@Test
public void testEnum() {
    String file = "[when][]ATTRIBUTE {attr:ENUM:Attribute.value} in {list}=Attribute( {attr} in ({list}) )";
    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());
        System.out.println(entry.getValuePattern());
        System.out.println(entry.getVariables());
        assertEquals("ATTRIBUTE {attr:ENUM:Attribute.value} in {list}", entry.getMappingKey());
        assertEquals("Attribute( {attr} in ({list}) )", entry.getMappingValue());
        assertEquals("(?:(?<=^)|(?<=\\W))ATTRIBUTE\\s+(.*?)\\s+in\\s+(.*?)$", entry.getKeyPattern().toString());
    } 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) Test(org.junit.Test)

Example 8 with DSLMappingEntry

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

the class DSLExtension method getExtensions.

@Override
public List<ExtensionMapping<?>> getExtensions(Path path, String content) {
    final DSLTokenizedMappingFile dslLoader = new DSLTokenizedMappingFile();
    List<DSLSentence> actionSentences = new ArrayList<>();
    List<DSLSentence> conditionSentences = new ArrayList<>();
    try {
        if (dslLoader.parseAndLoad(new StringReader(content))) {
            for (DSLMappingEntry entry : dslLoader.getMapping().getEntries()) {
                if (entry.getSection() == DSLMappingEntry.CONDITION) {
                    final DSLMappingEntry definition = entry;
                    final DSLSentence sentence = new DSLSentence();
                    sentence.setDrl(definition.getMappingValue());
                    sentence.setDefinition(definition.getMappingKey());
                    conditionSentences.add(sentence);
                } else if (entry.getSection() == DSLMappingEntry.CONSEQUENCE) {
                    final DSLMappingEntry definition = entry;
                    final DSLSentence sentence = new DSLSentence();
                    sentence.setDrl(definition.getMappingValue());
                    sentence.setDefinition(definition.getMappingKey());
                    actionSentences.add(sentence);
                }
            }
        }
    } catch (IOException e) {
        log.error(e.getMessage());
    }
    return Arrays.asList(new DSLMapping(DSLActionSentence.INSTANCE, actionSentences), new DSLMapping(DSLConditionSentence.INSTANCE, conditionSentences));
}
Also used : ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) DSLMappingEntry(org.drools.compiler.lang.dsl.DSLMappingEntry) IOException(java.io.IOException) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence)

Example 9 with DSLMappingEntry

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

the class DSLMappingEntryTest method makeExpander.

private DefaultExpander makeExpander(DSLMappingEntry... entries) {
    DefaultExpander expander = new DefaultExpander();
    DefaultDSLMapping mapping = new DefaultDSLMapping();
    for (DSLMappingEntry entry : entries) {
        mapping.addEntry(entry);
    }
    List<String> options = new ArrayList<String>();
    options.add("result");
    options.add("when");
    options.add("steps");
    mapping.setOptions(options);
    expander.addDSLMapping(mapping);
    return expander;
}
Also used : DefaultDSLMapping(org.drools.compiler.lang.dsl.DefaultDSLMapping) ArrayList(java.util.ArrayList) DSLMappingEntry(org.drools.compiler.lang.dsl.DSLMappingEntry) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander)

Example 10 with DSLMappingEntry

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

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