Search in sources :

Example 1 with DSLMappingEntry

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

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

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

the class DSLMappingEntryTest method testPatternCalculation2.

@Test
public void testPatternCalculation2() throws IOException {
    final String inputKey = "-name is {name}";
    final String inputValue = "name == \"{name}\"";
    final String expectedKeyP = lookbehind + "-\\s*name\\s+is\\s+(.*?)$";
    final String expectedValP = "name == \"{name}\"";
    final DSLMappingEntry entry = createEntry(inputKey, inputValue);
    assertEquals(inputKey, entry.getMappingKey());
    assertEquals(expectedKeyP, entry.getKeyPattern().pattern());
    assertEquals(inputValue, entry.getMappingValue());
    assertEquals(expectedValP, entry.getValuePattern());
}
Also used : DSLMappingEntry(org.drools.compiler.lang.dsl.DSLMappingEntry) Test(org.junit.Test)

Example 4 with DSLMappingEntry

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

the class DSLMappingEntryTest method testPatternCalculation3.

@Test
public void testPatternCalculation3() throws IOException {
    final String inputKey = "- name is {name}";
    final String inputValue = "name == \"{name}\"";
    final String expectedKeyP = lookbehind + "-\\s*name\\s+is\\s+(.*?)$";
    final String expectedValP = "name == \"{name}\"";
    final DSLMappingEntry entry = createEntry(inputKey, inputValue);
    assertEquals(inputKey, entry.getMappingKey());
    assertEquals(entry.getKeyPattern().pattern(), expectedKeyP, entry.getKeyPattern().pattern());
    assertEquals(inputValue, entry.getMappingValue());
    assertEquals(expectedValP, entry.getValuePattern());
}
Also used : DSLMappingEntry(org.drools.compiler.lang.dsl.DSLMappingEntry) Test(org.junit.Test)

Example 5 with DSLMappingEntry

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

the class DSLMappingEntryTest method testExpandWithBrackets.

@Test
public void testExpandWithBrackets() throws IOException {
    DSLMappingEntry entry1 = this.createEntry("attr {attr_name} is in \\[ {values} \\]", "{attr_name} in ( {values} )");
    DSLMappingEntry entry2 = this.createEntry("((H|h)e|(S|s)he) \\(is\\) (a|an) $xx {attribute} (man|woman)", "Person( attribute == \"{attribute}\" )");
    DSLMappingEntry entry3 = this.createEntry("DSL sentence with {key1} {key2}", "Sentence( {key1} == {key2} )");
    DSLMappingEntry entry4 = this.createEntry("When the credit rating is {rating:ENUM:Applicant.creditRating}", "applicant:Applicant(credit=={rating})");
    DSLMappingEntry entry5 = this.createEntry("When the credit rating is {rating:regex:\\d{3}}", "applicant:Applicant(credit=={rating})");
    assertEquals(lookbehind + "When\\s+the\\s+credit\\s+rating\\s+is\\s+(\\d{3})(?=\\W|$)", entry5.getKeyPattern().toString());
    assertEquals("applicant:Applicant(credit=={rating})", entry5.getValuePattern());
    DSLMappingEntry entry6 = this.createEntry("This is a sentence with line breaks", "Cheese\\n(price == 10)");
    assertEquals(lookbehind + "This\\s+is\\s+a\\s+sentence\\s+with\\s+line\\s+breaks(?=\\W|$)", entry6.getKeyPattern().toString());
    assertEquals("Cheese\n(price == 10)", entry6.getValuePattern());
    DSLMappingEntry entry7 = this.createEntry("Bedingung-\\#19-MKM4", "eval ( $p.getTempVal(\"\\#UML-ATZ-1\") < $p.getZvUmlStfr() )");
    assertEquals(lookbehind + "Bedingung-#19-MKM4(?=\\W|$)", entry7.getKeyPattern().toString());
    assertEquals("eval ( $p.getTempVal(\"#UML-ATZ-1\") < $p.getZvUmlStfr() )", entry7.getValuePattern());
    DefaultExpander ex = makeExpander(entry1, entry2, entry3, entry4, entry5, entry6, entry7);
    StringBuilder sb = new StringBuilder("rule x\n").append("when\n");
    sb.append("attr name is in [ 'Edson', 'Bob' ]").append(NL);
    sb.append("he (is) a $xx handsome man").append(NL);
    sb.append("DSL sentence with mykey myvalue").append(NL);
    sb.append("When the credit rating is AA").append(NL);
    sb.append("When the credit rating is 555").append(NL);
    sb.append("This is a sentence with line breaks").append(NL);
    sb.append("Bedingung-#19-MKM4").append(NL);
    sb.append("then\n" + "end\n");
    String dslr = sb.toString();
    String drl = ex.expand(dslr);
    for (String exp : new String[] { "name in ( 'Edson', 'Bob' )", "Person( attribute == \"handsome\" )", "Sentence( mykey == myvalue )", // "applicant:Applicant(credit==AA)",
    "applicant:Applicant(credit==555)", "Cheese\n(price == 10)", "eval ( $p.getTempVal(\"#UML-ATZ-1\") < $p.getZvUmlStfr() )" }) {
        assertTrue("failed to expand to: " + exp, drl.contains(exp));
    }
}
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