Search in sources :

Example 1 with DefaultExpander

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

the class DefaultExpanderTest method testExpandKeyword.

@Test
public void testExpandKeyword() throws Exception {
    DSLMappingFile file = new DSLTokenizedMappingFile();
    String dsl = "[keyword]key {param}=Foo( attr=={param} )";
    file.parseAndLoad(new StringReader(dsl));
    assertEquals(0, file.getErrors().size());
    DefaultExpander ex = new DefaultExpander();
    ex.addDSLMapping(file.getMapping());
    String source = "rule x" + NL + "when" + NL + " key 1 " + NL + " key 2 " + NL + "then" + NL + "end";
    String drl = ex.expand(source);
    System.out.println(drl);
    assertTrue(drl.contains("attr==1"));
    assertTrue(drl.contains("attr==2"));
// System.err.println(ex.expand( "rule 'x' " + NL + " when " + NL + " foo " + NL + " then " + NL + " end" ));
}
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 2 with DefaultExpander

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

the class DefaultExpanderTest method testLineNumberError.

@Test
public void testLineNumberError() throws Exception {
    DSLMappingFile file = new DSLTokenizedMappingFile();
    String dsl = "[when]foo=Foo()" + NL + "[then]bar {num}=baz({num});";
    file.parseAndLoad(new StringReader(dsl));
    DefaultExpander ex = new DefaultExpander();
    ex.addDSLMapping(file.getMapping());
    String source = "rule 'q'" + NL + "agenda-group 'x'" + NL + "when" + NL + "    __  " + NL + "then" + NL + "    bar 42" + NL + "\tgoober" + NL + "end";
    ex.expand(source);
    assertTrue(ex.hasErrors());
    assertEquals(2, ex.getErrors().size());
    ExpanderException err = (ExpanderException) ex.getErrors().get(0);
    assertEquals(4, err.getLine());
    err = (ExpanderException) ex.getErrors().get(1);
    assertEquals(7, err.getLine());
}
Also used : StringReader(java.io.StringReader) ExpanderException(org.drools.drl.parser.lang.ExpanderException) 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 3 with DefaultExpander

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

the class DefaultExpanderTest method testANTLRExpandWithKeywordClashes.

@Test
public void testANTLRExpandWithKeywordClashes() throws Exception {
    DSLTokenizedMappingFile file = new DSLTokenizedMappingFile();
    String dsl = "[when]Invoke rule executor=ruleExec: RuleExecutor()" + NL + "[then]Execute rule \"{id}\"=ruleExec.ExecuteSubRule( new Long({id}));";
    file.parseAndLoad(new StringReader(dsl));
    assertEquals(0, file.getErrors().size());
    DefaultExpander ex = new DefaultExpander();
    ex.addDSLMapping(file.getMapping());
    String source = "package something;" + NL + NL + "rule \"1\"" + NL + "when" + NL + "    Invoke rule executor" + NL + "then" + NL + "    Execute rule \"5\"" + NL + "end";
    String expected = "package something;" + NL + NL + "rule \"1\"" + NL + "when" + NL + "    ruleExec: RuleExecutor()" + NL + "then" + NL + "    ruleExec.ExecuteSubRule( new Long(5));" + NL + "end";
    String drl = ex.expand(source);
    // System.out.println("["+drl+"]" );
    // System.out.println("["+expected+"]" );
    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 4 with DefaultExpander

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

the class DefaultExpanderTest method testEqualSignInTernaryOp.

@Test
public void testEqualSignInTernaryOp() throws Exception {
    // BZ-1013960
    String source = "declare Person" + NL + "    age : int" + NL + "    name : String" + NL + "end" + NL + "" + NL + "rule \"Your First Rule\"" + NL + "    when" + NL + "        There is a Person" + NL + "            - with a negative age" + NL + "            - with a positive age" + NL + "            - with a zero age" + NL + "    then" + NL + "        print \"Your First Rule\"" + NL + "" + NL + "end" + NL;
    String dsl = "[when][]There is an? {entity}=${entity!lc}: {entity!ucfirst}()" + NL + "[when][]- with an? {attr} greater than {amount}={attr} > {amount!num}" + NL + "[then]print \"{text}\"=System.out.println(\"{text}\");" + NL + "" + NL + "[when]- with a {what} {attr}={attr} {what!zero?==0/!=0}" + NL;
    String expected = "declare Person" + NL + "    age : int" + NL + "    name : String" + NL + "end" + NL + "" + NL + "rule \"Your First Rule\"" + NL + "    when" + NL + "        $person: Person(age  !=0, age  !=0, age  ==0)" + NL + "    then" + NL + "        System.out.println(\"Your First Rule\");" + NL + "" + 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 5 with DefaultExpander

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

the class DefaultExpanderTest method testExpandParts.

@Test
public void testExpandParts() 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());
// System.err.println(ex.expand( "rule 'x' " + NL + " when " + NL + " foo " + NL + " then " + NL + " end" ));
}
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)

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