Search in sources :

Example 11 with DSLMappingEntry

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

Example 12 with DSLMappingEntry

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

the class DSLMappingEntryTest method testExpandWithDots.

@Test
public void testExpandWithDots() throws IOException {
    DSLMappingEntry entry1 = this.createEntry("- {prop} is not {val} ", "{prop} != {val}");
    DSLMappingEntry entry2 = this.createEntry("- {prop} is {val} ", "{prop} == {val}");
    DSLMappingEntry entry3 = this.createEntry("- {prop} is_not {val} ", "{prop} != {val}");
    DefaultExpander ex = makeExpander(entry1, entry2, entry3);
    StringBuilder sb = new StringBuilder("rule x\n").append("when\n");
    sb.append("> Foo()").append(NL);
    sb.append("- type1 is ClientServiceType.TypeGOLD").append(NL);
    sb.append("- type2 is_not ClientServiceType.TypeGOLD").append(NL);
    sb.append("- type3 is not ClientServiceType.TypeGOLD").append(NL);
    sb.append("then\n").append("end\n");
    String dslr = sb.toString();
    String drl = ex.expand(dslr);
    System.out.println(dslr);
    System.out.println(drl);
    assertTrue("failure type1", drl.contains("type1 == ClientServiceType.TypeGOLD"));
    assertTrue("failure type2", drl.contains("type2 != ClientServiceType.TypeGOLD"));
    assertTrue("failure type3", drl.contains("type3 != ClientServiceType.TypeGOLD"));
}
Also used : DSLMappingEntry(org.drools.compiler.lang.dsl.DSLMappingEntry) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) Test(org.junit.Test)

Example 13 with DSLMappingEntry

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

the class DSLTokenizedMappingFileTest method testParseFileWithBrackets.

@Test
public void testParseFileWithBrackets() {
    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(lookbehind + "ATTRIBUTE\\s+\"(.*?)\"\\s+IS\\s+IN\\s+[(.*?)](?=\\W|$)", entry.getKeyPattern().toString());
        // Attribute( {attr} in ({list}) )
        assertEquals("Attribute( {attr} in ({list}) )", 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.compiler.lang.dsl.DSLMappingEntry) IOException(java.io.IOException) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile) Test(org.junit.Test)

Example 14 with DSLMappingEntry

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

the class DSLTokenizedMappingFileTest 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(lookbehind + "ATTRIBUTE\\s+\"(.*?)\"\\s+IS\\s+IN\\s+\\[(.*?)\\](?=\\W|$)", entry.getKeyPattern().toString());
        // Attribute( {attr} in ({list}) )
        assertEquals("Attribute( {attr} in ({list}) )", 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.compiler.lang.dsl.DSLMappingEntry) IOException(java.io.IOException) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile) Test(org.junit.Test)

Example 15 with DSLMappingEntry

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

Aggregations

DSLMappingEntry (org.drools.compiler.lang.dsl.DSLMappingEntry)18 Test (org.junit.Test)14 DSLTokenizedMappingFile (org.drools.compiler.lang.dsl.DSLTokenizedMappingFile)11 StringReader (java.io.StringReader)10 IOException (java.io.IOException)8 InputStreamReader (java.io.InputStreamReader)8 Reader (java.io.Reader)8 DefaultExpander (org.drools.compiler.lang.dsl.DefaultExpander)6 ArrayList (java.util.ArrayList)3 Iterator (java.util.Iterator)1 DSLMapping (org.drools.compiler.lang.dsl.DSLMapping)1 DefaultDSLMapping (org.drools.compiler.lang.dsl.DefaultDSLMapping)1 DSLSentence (org.drools.workbench.models.datamodel.rule.DSLSentence)1 Ignore (org.junit.Ignore)1