Search in sources :

Example 1 with DSLTokenizedMappingFile

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

the class ANTLRDSLTest method testMe.

@Test
public void testMe() throws Exception {
    DSLTokenizedMappingFile tokenizedFile = null;
    final String filename = "test_antlr.dsl";
    final Reader reader = new InputStreamReader(this.getClass().getResourceAsStream(filename));
    tokenizedFile = new DSLTokenizedMappingFile();
    tokenizedFile.parseAndLoad(reader);
    reader.close();
    for (Iterator it = tokenizedFile.getMapping().getEntries().iterator(); it.hasNext(); ) {
        DSLMappingEntry entry = (DSLMappingEntry) it.next();
    // System.out.println("ENTRY: " + entry.getKeyPattern() + "   :::::   " + entry.getValuePattern());
    }
    DefaultExpander ex = new DefaultExpander();
    ex.addDSLMapping(tokenizedFile.getMapping());
    System.err.println(ex.expand("rule 'x' \n when \n address is present where name is \"foo\" and age is \"32\" \n then \n end"));
}
Also used : InputStreamReader(java.io.InputStreamReader) Iterator(java.util.Iterator) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) DSLMappingEntry(org.drools.compiler.lang.dsl.DSLMappingEntry) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 2 with DSLTokenizedMappingFile

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

Example 3 with DSLTokenizedMappingFile

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

the class DSLTokenizedMappingFileTest method testParseFile.

@Test
public void testParseFile() {
    try {
        final Reader reader = new InputStreamReader(this.getClass().getResourceAsStream(this.filename));
        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(31, this.file.getMapping().getEntries().size());
    } catch (final IOException e) {
        e.printStackTrace();
        fail("Should not raise exception ");
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile) Test(org.junit.Test)

Example 4 with DSLTokenizedMappingFile

use of org.drools.compiler.lang.dsl.DSLTokenizedMappingFile 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 5 with DSLTokenizedMappingFile

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

Aggregations

DSLTokenizedMappingFile (org.drools.compiler.lang.dsl.DSLTokenizedMappingFile)24 StringReader (java.io.StringReader)23 IOException (java.io.IOException)20 Test (org.junit.Test)15 InputStreamReader (java.io.InputStreamReader)13 Reader (java.io.Reader)13 DSLMappingEntry (org.drools.compiler.lang.dsl.DSLMappingEntry)11 ArrayList (java.util.ArrayList)9 DSLMappingFile (org.drools.compiler.lang.dsl.DSLMappingFile)7 DefaultExpander (org.drools.compiler.lang.dsl.DefaultExpander)7 Path (org.uberfire.java.nio.file.Path)5 Expander (org.drools.compiler.lang.Expander)2 DSLSentence (org.drools.workbench.models.datamodel.rule.DSLSentence)2 Iterator (java.util.Iterator)1 DSLMapping (org.drools.compiler.lang.dsl.DSLMapping)1 DefaultExpanderResolver (org.drools.compiler.lang.dsl.DefaultExpanderResolver)1 DSLVariableValue (org.drools.workbench.models.datamodel.rule.DSLVariableValue)1 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)1 ValidationMessage (org.guvnor.common.services.shared.validation.model.ValidationMessage)1 Ignore (org.junit.Ignore)1