Search in sources :

Example 21 with DefaultExpander

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

the class DefaultExpanderTest method testANTLREnumExpand.

@Test
public void testANTLREnumExpand() throws Exception {
    DSLTokenizedMappingFile file = new DSLTokenizedMappingFile();
    String dsl = "[when]When the credit rating is {rating:ENUM:Applicant.creditRating} = applicant:Applicant(credit=={rating})";
    file.parseAndLoad(new StringReader(dsl));
    assertEquals(0, file.getErrors().size());
    DefaultExpander ex = new DefaultExpander();
    ex.addDSLMapping(file.getMapping());
    String source = "rule \"TestNewDslSetup\"" + NL + "dialect \"mvel\"" + NL + "when" + NL + "When the credit rating is AA" + NL + "then " + NL + "end";
    // String source="rule \"TestNewDslSetup\"" + NL+
    // "dialect \"mvel\"" + NL+
    // "when" + NL+
    // "When the credit rating is OK" + NL+
    // "then" + NL+
    // "end" + NL;
    String drl = ex.expand(source);
    String expected = "rule \"TestNewDslSetup\"" + NL + "dialect \"mvel\"" + NL + "when" + NL + "applicant:Applicant(credit==AA)" + NL + "then  " + NL + "end";
    assertFalse(ex.getErrors().toString(), ex.hasErrors());
    assertEquals(expected, drl);
// System.err.println(ex.expand( "rule 'x' " + NL + " when " + NL + " foo " + NL + " then " + NL + " end" ));
}
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 22 with DefaultExpander

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

the class DefaultExpanderTest method testExpandFailure.

@Test
public void testExpandFailure() throws Exception {
    DSLMappingFile file = new DSLTokenizedMappingFile();
    String dsl = "[when]foo=Foo()" + NL + "[then]bar {num}=baz({num});";
    file.parseAndLoad(new StringReader(dsl));
    assertEquals(0, file.getErrors().size());
    DefaultExpander ex = new DefaultExpander();
    ex.addDSLMapping(file.getMapping());
    String source = "rule 'q'" + NL + "agenda-group 'x'" + NL + "when" + NL + "    foo  " + NL + "then" + NL + "    bar 42" + NL + "end";
    String drl = ex.expand(source);
    assertFalse(ex.hasErrors());
    ex = new DefaultExpander();
    ex.addDSLMapping(file.getMapping());
    source = "rule 'q' agenda-group 'x'" + NL + "when" + NL + "    foos " + NL + "then" + NL + "    bar 42" + NL + " end";
    drl = ex.expand(source);
    // System.out.println( drl );
    assertTrue(ex.hasErrors());
    assertEquals(1, ex.getErrors().size());
// System.err.println(( (ExpanderException) ex.getErrors().get( 0 )).getMessage());
}
Also used : StringReader(java.io.StringReader) DSLMappingFile(org.drools.drl.parser.lang.dsl.DSLMappingFile) DSLTokenizedMappingFile(org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile) DefaultExpander(org.drools.drl.parser.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 23 with DefaultExpander

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

the class DefaultExpanderTest method checkExpansion.

private void checkExpansion(String source, String expected) throws Exception {
    DSLTokenizedMappingFile file = new DSLTokenizedMappingFile();
    String dsl = "[when]There is an TestObject=TestObject()" + NL + "[when]-startDate is before {date}=startDate>DateUtils.parseDate(\"{date}\")" + NL + "[when]-endDate is after {date}=endDate>DateUtils.parseDate(\"{date}\")";
    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)

Example 24 with DefaultExpander

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

the class DefaultExpanderTest method testANTLRExpandParts.

@Test
public void testANTLRExpandParts() throws Exception {
    DSLTokenizedMappingFile file = new DSLTokenizedMappingFile();
    String dsl = "[when]foo=Foo()" + NL + "[then]bar {num}=baz({num});";
    file.parseAndLoad(new StringReader(dsl));
    assertEquals(0, file.getErrors().size());
    DefaultExpander ex = new DefaultExpander();
    ex.addDSLMapping(file.getMapping());
// System.err.println(ex.expand( "rule 'x' " + NL + " when " + NL + " foo " + NL + " then " + NL + " end" ));
}
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 25 with DefaultExpander

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

the class DefaultExpanderTest method testANTLRExpandFailure.

@Test
public void testANTLRExpandFailure() throws Exception {
    DSLTokenizedMappingFile file = new DSLTokenizedMappingFile();
    String dsl = "[when]foo=Foo()" + NL + "[then]bar {num}=baz({num});";
    file.parseAndLoad(new StringReader(dsl));
    assertEquals(0, file.getErrors().size());
    DefaultExpander ex = new DefaultExpander();
    ex.addDSLMapping(file.getMapping());
    String source = "rule 'q'" + NL + "agenda-group 'x'" + NL + "when" + NL + "    foo  " + NL + "then" + NL + "    bar 42" + NL + "end";
    String drl = ex.expand(source);
    assertFalse(ex.hasErrors());
    ex = new DefaultExpander();
    ex.addDSLMapping(file.getMapping());
    source = "rule 'q' agenda-group 'x'" + NL + "when" + NL + "    foos " + NL + "then" + NL + "    bar 42" + NL + " end";
    drl = ex.expand(source);
    // System.out.println( drl );
    assertTrue(ex.hasErrors());
    assertEquals(1, ex.getErrors().size());
// System.err.println(( (ExpanderException) ex.getErrors().get( 0 )).getMessage());
}
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)

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