Search in sources :

Example 16 with DSLTokenizedMappingFile

use of org.drools.drl.parser.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.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 17 with DSLTokenizedMappingFile

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

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

the class DSLMappingFileTest 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) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) IOException(java.io.IOException) DSLTokenizedMappingFile(org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile) Test(org.junit.Test)

Example 19 with DSLTokenizedMappingFile

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

Example 20 with DSLTokenizedMappingFile

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

the class DSLMappingFileTest method testNoRHS.

/**
 * Right now this test fails because there is no RHS for the rule. It connects the "then" and "end" to "thenend".
 */
@Test
public void testNoRHS() {
    String file = "[then]TEST=System.out.println(\"DO_SOMETHING\");" + NL + "" + "[when]code {code1} occurs and sum of all digit not equal \\( {code2} \\+ {code3} \\)=AAAA( cd1 == {code1}, cd2 != ( {code2} + {code3} ))" + NL + "" + "[when]code {code1} occurs=BBBB" + NL + "";
    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());
        final String LHS = "code 1041 occurs and sum of all digit not equal ( 1034 + 1035 )";
        final String rule = "rule \"x\"" + NL + "when" + NL + "" + LHS + "" + NL + "then" + NL + "end";
        DefaultExpander de = new DefaultExpander();
        de.addDSLMapping(this.file.getMapping());
        final String ruleAfterExpansion = de.expand(rule);
        final String expected = "rule \"x\"" + NL + "when" + NL + "AAAA( cd1 == 1041, cd2 != ( 1034 + 1035 ))" + NL + "then" + NL + "end";
        assertEquals(expected, ruleAfterExpansion);
    } 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) IOException(java.io.IOException) DSLTokenizedMappingFile(org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile) DefaultExpander(org.drools.drl.parser.lang.dsl.DefaultExpander) Test(org.junit.Test)

Aggregations

DSLTokenizedMappingFile (org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile)33 StringReader (java.io.StringReader)32 Test (org.junit.Test)29 DefaultExpander (org.drools.drl.parser.lang.dsl.DefaultExpander)23 Reader (java.io.Reader)15 InputStreamReader (java.io.InputStreamReader)14 IOException (java.io.IOException)12 DSLMappingEntry (org.drools.drl.parser.lang.dsl.DSLMappingEntry)9 DSLMappingFile (org.drools.drl.parser.lang.dsl.DSLMappingFile)7 ExpanderException (org.drools.drl.parser.lang.ExpanderException)2 Iterator (java.util.Iterator)1 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)1 XmlChangeSetReader (org.drools.core.xml.XmlChangeSetReader)1 DrlParser (org.drools.drl.parser.DrlParser)1 Expander (org.drools.drl.parser.lang.Expander)1 DefaultExpanderResolver (org.drools.drl.parser.lang.dsl.DefaultExpanderResolver)1 Before (org.junit.Before)1 Ignore (org.junit.Ignore)1