Search in sources :

Example 6 with DSLMappingEntry

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

the class DSLTokenizedMappingFileTest method testParseFileWithBrackets.

@Test
public void testParseFileWithBrackets() {
    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(lookbehind + "ATTRIBUTE\\s+\"(.*?)\"\\s+IS\\s+IN\\s+[(.*?)](?=\\W|$)", entry.getKeyPattern().toString());
        // Attribute( {attr} in ({list}) )
        assertEquals("Attribute( {attr} in ({list}) )", entry.getValuePattern());
    } 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.drl.parser.lang.dsl.DSLMappingEntry) IOException(java.io.IOException) DSLTokenizedMappingFile(org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile) Test(org.junit.Test)

Example 7 with DSLMappingEntry

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

the class DSLTokenizedMappingFileTest 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(lookbehind + "ATTRIBUTE\\s+\"(.*?)\"\\s+IS\\s+IN\\s+\\[(.*?)\\](?=\\W|$)", entry.getKeyPattern().toString());
        // Attribute( {attr} in ({list}) )
        assertEquals("Attribute( {attr} in ({list}) )", entry.getValuePattern());
    } 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.drl.parser.lang.dsl.DSLMappingEntry) IOException(java.io.IOException) DSLTokenizedMappingFile(org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile) Test(org.junit.Test)

Example 8 with DSLMappingEntry

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

Example 9 with DSLMappingEntry

use of org.drools.drl.parser.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) 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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 10 with DSLMappingEntry

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

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