Search in sources :

Example 6 with DefaultExpander

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

the class DefaultExpanderTest method testDotInPattern.

@Test
public void testDotInPattern() throws Exception {
    // BZ-1013960
    String source = "import org.drools.mvel.compiler.Person;" + NL + "global java.util.List list" + NL + "rule R1" + NL + "when" + NL + "then" + NL + "Log X" + NL + "end" + NL;
    String dsl = "[then]Log {message:.}=list.add(\"{message}\");";
    String expected = "import org.drools.mvel.compiler.Person;" + NL + "global java.util.List list" + NL + "rule R1" + NL + "when" + NL + "then" + NL + "list.add(\"X\");" + NL + "end" + NL;
    DSLTokenizedMappingFile file = new DSLTokenizedMappingFile();
    file.parseAndLoad(new StringReader(dsl));
    assertEquals(file.getErrors().toString(), 0, file.getErrors().size());
    DefaultExpander ex = new DefaultExpander();
    ex.addDSLMapping(file.getMapping());
    String drl = ex.expand(source);
    assertFalse(ex.hasErrors());
    assertEquals(expected, drl);
}
Also used : StringReader(java.io.StringReader) DSLTokenizedMappingFile(org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile) DefaultExpander(org.drools.drl.parser.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 7 with DefaultExpander

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

the class DefaultExpanderTest method setUp.

@Before
public void setUp() throws Exception {
    final String filename = "test_metainfo.dsl";
    final Reader reader = new InputStreamReader(this.getClass().getResourceAsStream(filename));
    this.file = new DSLTokenizedMappingFile();
    this.tokenizedFile = new DSLTokenizedMappingFile();
    this.file.parseAndLoad(reader);
    reader.close();
    final Reader reader2 = new InputStreamReader(this.getClass().getResourceAsStream(filename));
    this.tokenizedFile.parseAndLoad(reader2);
    reader2.close();
    this.expander = new DefaultExpander();
}
Also used : InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) DSLTokenizedMappingFile(org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile) DefaultExpander(org.drools.drl.parser.lang.dsl.DefaultExpander) Before(org.junit.Before)

Example 8 with DefaultExpander

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

the class DefaultExpanderTest method testExpandExpr.

@Test
public void testExpandExpr() throws Exception {
    DSLTokenizedMappingFile file = new DSLTokenizedMappingFile();
    String dsl = "[when]Name of Applicant {nameVar:CF:Applicant.age}= System.out.println({nameVar})";
    String source = "rule \"test rule for custom form in DSL\"" + NL + "     dialect \"mvel\"" + NL + "     when" + NL + "         Name of Applicant Bojan Oklahoma and NJ,Andrew AMW Test" + NL + "     then" + NL + "end";
    String expected = "rule \"test rule for custom form in DSL\"" + NL + "     dialect \"mvel\"" + NL + "     when" + NL + "         System.out.println(Bojan Oklahoma and NJ,Andrew AMW Test)" + NL + "     then" + NL + "end";
    file.parseAndLoad(new StringReader(dsl));
    assertEquals(0, file.getErrors().size());
    DefaultExpander ex = new DefaultExpander();
    ex.addDSLMapping(file.getMapping());
    String drl = ex.expand(source);
    assertFalse(ex.hasErrors());
    assertEquals(expected, drl);
}
Also used : StringReader(java.io.StringReader) DSLTokenizedMappingFile(org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile) DefaultExpander(org.drools.drl.parser.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 9 with DefaultExpander

use of org.drools.drl.parser.lang.dsl.DefaultExpander 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.drl.parser.lang.dsl.DefaultDSLMapping) ArrayList(java.util.ArrayList) DSLMappingEntry(org.drools.drl.parser.lang.dsl.DSLMappingEntry) DefaultExpander(org.drools.drl.parser.lang.dsl.DefaultExpander)

Example 10 with DefaultExpander

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

Aggregations

DefaultExpander (org.drools.drl.parser.lang.dsl.DefaultExpander)29 Test (org.junit.Test)25 DSLTokenizedMappingFile (org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile)23 StringReader (java.io.StringReader)22 DSLMappingFile (org.drools.drl.parser.lang.dsl.DSLMappingFile)7 InputStreamReader (java.io.InputStreamReader)6 Reader (java.io.Reader)6 DSLMappingEntry (org.drools.drl.parser.lang.dsl.DSLMappingEntry)6 IOException (java.io.IOException)5 ExpanderException (org.drools.drl.parser.lang.ExpanderException)3 DrlParser (org.drools.drl.parser.DrlParser)2 Expander (org.drools.drl.parser.lang.Expander)2 UncheckedIOException (java.io.UncheckedIOException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)1 PackageDescr (org.drools.drl.ast.descr.PackageDescr)1 DroolsParserException (org.drools.drl.parser.DroolsParserException)1 DRLParser (org.drools.drl.parser.lang.DRLParser)1 DefaultDSLMapping (org.drools.drl.parser.lang.dsl.DefaultDSLMapping)1