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);
}
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();
}
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);
}
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;
}
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));
}
}
Aggregations