Search in sources :

Example 1 with DefaultExpander

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

the class ErrorsParserTest method testExpanderErrorsAfterExpansion.

@Test
public void testExpanderErrorsAfterExpansion() throws Exception {
    String name = "expander_post_errors.dslr";
    Expander expander = new DefaultExpander();
    String expanded = expander.expand(this.getReader(name));
    DRLParser parser = parse(name, expanded);
    parser.compilationUnit();
    assertTrue(parser.hasErrors());
    assertEquals(1, parser.getErrors().size());
    DroolsParserException err = (DroolsParserException) parser.getErrors().get(0);
    assertEquals(6, err.getLineNumber());
}
Also used : DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 2 with DefaultExpander

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

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

Example 4 with DefaultExpander

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

the class DSLMappingFileTest method testNoRHS.

/**
 * Right now this test fails because there is no RHS for the rule. It connects the "then" and "end" to "thenend".
 */
@Test
public void testNoRHS() {
    String file = "[then]TEST=System.out.println(\"DO_SOMETHING\");" + NL + "" + "[when]code {code1} occurs and sum of all digit not equal \\( {code2} \\+ {code3} \\)=AAAA( cd1 == {code1}, cd2 != ( {code2} + {code3} ))" + NL + "" + "[when]code {code1} occurs=BBBB" + NL + "";
    try {
        final Reader reader = new StringReader(file);
        this.file = new DSLTokenizedMappingFile();
        final boolean parsingResult = this.file.parseAndLoad(reader);
        reader.close();
        assertTrue(this.file.getErrors().toString(), parsingResult);
        assertTrue(this.file.getErrors().isEmpty());
        final String LHS = "code 1041 occurs and sum of all digit not equal ( 1034 + 1035 )";
        final String rule = "rule \"x\"" + NL + "when" + NL + "" + LHS + "" + NL + "then" + NL + "end";
        DefaultExpander de = new DefaultExpander();
        de.addDSLMapping(this.file.getMapping());
        final String ruleAfterExpansion = de.expand(rule);
        final String expected = "rule \"x\"" + NL + "when" + NL + "AAAA( cd1 == 1041, cd2 != ( 1034 + 1035 ))" + NL + "then" + NL + "end";
        assertEquals(expected, ruleAfterExpansion);
    } catch (final IOException e) {
        e.printStackTrace();
        fail("Should not raise exception ");
    }
}
Also used : StringReader(java.io.StringReader) StringReader(java.io.StringReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 5 with DefaultExpander

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

the class DrlParserTest method testExpandDRLUsingInjectedExpander.

@Test
public void testExpandDRLUsingInjectedExpander() throws Exception {
    String dsl = "[condition]Something=Something()" + NL + "[then]another=another();";
    String drl = "rule 'foo' " + NL + " when " + NL + " Something " + NL + " then " + NL + " another " + NL + "end";
    DefaultExpanderResolver resolver = new DefaultExpanderResolver(new StringReader(dsl));
    final DSLMappingFile file = new DSLTokenizedMappingFile();
    if (file.parseAndLoad(new StringReader(dsl))) {
        final Expander expander = new DefaultExpander();
        expander.addDSLMapping(file.getMapping());
        resolver.addExpander("*", expander);
    } else {
        throw new RuntimeException("Error parsing and loading DSL file." + file.getErrors());
    }
    DrlParser parser = new DrlParser(LanguageLevelOption.DRL5);
    String result = parser.getExpandedDRL(drl, resolver);
    Assertions.assertThat("rule 'foo' " + NL + " when " + NL + " Something() " + NL + " then " + NL + " another(); " + NL + "end").isEqualToIgnoringWhitespace(result);
}
Also used : DefaultExpanderResolver(org.drools.compiler.lang.dsl.DefaultExpanderResolver) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) Expander(org.drools.compiler.lang.Expander) StringReader(java.io.StringReader) DSLMappingFile(org.drools.compiler.lang.dsl.DSLMappingFile) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) Test(org.junit.Test)

Aggregations

DefaultExpander (org.drools.compiler.lang.dsl.DefaultExpander)17 Test (org.junit.Test)11 Expander (org.drools.compiler.lang.Expander)7 DSLMappingFile (org.drools.compiler.lang.dsl.DSLMappingFile)7 DSLTokenizedMappingFile (org.drools.compiler.lang.dsl.DSLTokenizedMappingFile)7 StringReader (java.io.StringReader)6 DSLMappingEntry (org.drools.compiler.lang.dsl.DSLMappingEntry)6 IOException (java.io.IOException)5 InputStreamReader (java.io.InputStreamReader)5 Reader (java.io.Reader)5 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)1 DroolsParserException (org.drools.compiler.compiler.DroolsParserException)1 DefaultDSLMapping (org.drools.compiler.lang.dsl.DefaultDSLMapping)1 DefaultExpanderResolver (org.drools.compiler.lang.dsl.DefaultExpanderResolver)1 DSLSentence (org.drools.workbench.models.datamodel.rule.DSLSentence)1 DSLVariableValue (org.drools.workbench.models.datamodel.rule.DSLVariableValue)1 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)1