Search in sources :

Example 1 with DSLMappingEntry

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

the class DSLMappingFileTest method testParseFileWithEscaptedBrackets.

@Test
public void testParseFileWithEscaptedBrackets() {
    String file = "[when][]ATTRIBUTE \"{attr}\" IS IN \\[{list}\\]=Attribute( {attr} in ({list}) )";
    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());
        assertEquals(1, this.file.getMapping().getEntries().size());
        DSLMappingEntry entry = (DSLMappingEntry) this.file.getMapping().getEntries().get(0);
        assertEquals(DSLMappingEntry.CONDITION, entry.getSection());
        assertEquals(DSLMappingEntry.EMPTY_METADATA, entry.getMetaData());
        assertEquals("ATTRIBUTE \"{attr}\" IS IN \\[{list}\\]", entry.getMappingKey());
        assertEquals("Attribute( {attr} in ({list}) )", entry.getMappingValue());
    } catch (final IOException e) {
        e.printStackTrace();
        fail("Should not raise exception ");
    }
}
Also used : StringReader(java.io.StringReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) DSLMappingEntry(org.drools.drl.parser.lang.dsl.DSLMappingEntry) IOException(java.io.IOException) DSLTokenizedMappingFile(org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile) Test(org.junit.Test)

Example 2 with DSLMappingEntry

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

the class DSLMappingEntryTest method testPatternCalculation.

@Test
public void testPatternCalculation() throws IOException {
    final String inputKey = "The Customer name is {name} and surname is {surname} and it has US$ 50,00 on his {pocket}";
    final String inputValue = "Customer( name == \"{name}\", surname == \"{surname}\", money > $money )";
    final String expectedKeyP = lookbehind + "The\\s+Customer\\s+name\\s+is\\s+(.*?)\\s+and\\s+surname\\s+is\\s+(.*?)\\s+and\\s+it\\s+has\\s+US\\$\\s+50,00\\s+on\\s+his\\s+(.*?)$";
    final String expectedValP = "Customer( name == \"{name}\", surname == \"{surname}\", money > $money )";
    final DSLMappingEntry entry = createEntry(inputKey, inputValue);
    assertEquals(inputKey, entry.getMappingKey());
    assertEquals(expectedKeyP, entry.getKeyPattern().pattern());
    assertEquals(inputValue, entry.getMappingValue());
    assertEquals(expectedValP, entry.getValuePattern());
}
Also used : DSLMappingEntry(org.drools.drl.parser.lang.dsl.DSLMappingEntry) Test(org.junit.Test)

Example 3 with DSLMappingEntry

use of org.drools.drl.parser.lang.dsl.DSLMappingEntry 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 4 with DSLMappingEntry

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

Example 5 with DSLMappingEntry

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

the class DSLTokenizedMappingFileTest method testParseFileWithEscaptedEquals.

@Test
public void testParseFileWithEscaptedEquals() {
    String file = "[when]something:\\={value}=Attribute( something == \"{value}\" )";
    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());
        assertEquals(1, this.file.getMapping().getEntries().size());
        DSLMappingEntry entry = (DSLMappingEntry) this.file.getMapping().getEntries().get(0);
        assertEquals(DSLMappingEntry.CONDITION, entry.getSection());
        assertEquals(DSLMappingEntry.EMPTY_METADATA, entry.getMetaData());
        assertEquals(lookbehind + "something:\\=(.*?)$", entry.getKeyPattern().toString());
        assertEquals("Attribute( something == \"{value}\" )", entry.getValuePattern());
    } 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) DSLMappingEntry(org.drools.drl.parser.lang.dsl.DSLMappingEntry) IOException(java.io.IOException) DSLTokenizedMappingFile(org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile) Test(org.junit.Test)

Aggregations

DSLMappingEntry (org.drools.drl.parser.lang.dsl.DSLMappingEntry)16 Test (org.junit.Test)14 DSLTokenizedMappingFile (org.drools.drl.parser.lang.dsl.DSLTokenizedMappingFile)9 InputStreamReader (java.io.InputStreamReader)8 Reader (java.io.Reader)8 StringReader (java.io.StringReader)8 IOException (java.io.IOException)7 DefaultExpander (org.drools.drl.parser.lang.dsl.DefaultExpander)6 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 DefaultDSLMapping (org.drools.drl.parser.lang.dsl.DefaultDSLMapping)1 Ignore (org.junit.Ignore)1