use of org.drools.drl.parser.lang.dsl.DSLMappingEntry in project drools by kiegroup.
the class DSLMappingFileTest method testParseFileWithEscaptedCurlyBrackets.
@Test
public void testParseFileWithEscaptedCurlyBrackets() {
String file = "[consequence][$policy]Add surcharge {surcharge} to Policy=modify(policy) \\{price = {surcharge}\\}";
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.CONSEQUENCE, entry.getSection());
assertEquals("$policy", entry.getMetaData().toString());
assertEquals("Add surcharge {surcharge} to Policy", entry.getMappingKey());
assertEquals("modify(policy) \\{price = {surcharge}\\}", entry.getMappingValue());
String input = "rule x" + NL + "when" + NL + "then" + NL + "Add surcharge 300 to Policy" + NL + "end" + NL + "";
String expected = "rule x" + NL + "when" + NL + "then" + NL + "modify(policy) {price = 300}" + NL + "end" + NL + "";
DefaultExpander de = new DefaultExpander();
de.addDSLMapping(this.file.getMapping());
final String result = de.expand(input);
// String result = entry.getKeyPattern().matcher( input ).replaceAll( entry.getValuePattern() );
assertEquals(expected, result);
} catch (final IOException e) {
e.printStackTrace();
fail("Should not raise exception ");
}
}
use of org.drools.drl.parser.lang.dsl.DSLMappingEntry in project drools by kiegroup.
the class DSLMappingEntryTest method testPatternCalculation2.
@Test
public void testPatternCalculation2() throws IOException {
final String inputKey = "-name is {name}";
final String inputValue = "name == \"{name}\"";
final String expectedKeyP = lookbehind + "-\\s*name\\s+is\\s+(.*?)$";
final String expectedValP = "name == \"{name}\"";
final DSLMappingEntry entry = createEntry(inputKey, inputValue);
assertEquals(inputKey, entry.getMappingKey());
assertEquals(expectedKeyP, entry.getKeyPattern().pattern());
assertEquals(inputValue, entry.getMappingValue());
assertEquals(expectedValP, entry.getValuePattern());
}
use of org.drools.drl.parser.lang.dsl.DSLMappingEntry in project drools by kiegroup.
the class DSLMappingEntryTest method testPatternCalculation3.
@Test
public void testPatternCalculation3() throws IOException {
final String inputKey = "- name is {name}";
final String inputValue = "name == \"{name}\"";
final String expectedKeyP = lookbehind + "-\\s*name\\s+is\\s+(.*?)$";
final String expectedValP = "name == \"{name}\"";
final DSLMappingEntry entry = createEntry(inputKey, inputValue);
assertEquals(inputKey, entry.getMappingKey());
assertEquals(entry.getKeyPattern().pattern(), expectedKeyP, entry.getKeyPattern().pattern());
assertEquals(inputValue, entry.getMappingValue());
assertEquals(expectedValP, entry.getValuePattern());
}
use of org.drools.drl.parser.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"));
}
use of org.drools.drl.parser.lang.dsl.DSLMappingEntry in project drools by kiegroup.
the class DSLMappingEntryTest method createEntry.
private DSLMappingEntry createEntry(final String inputKey, final String inputValue) throws IOException {
String mapping = "[condition][]" + inputKey + "=" + inputValue;
StringReader dsl = new StringReader(mapping);
DSLMappingEntry entry = null;
try {
DSLTokenizedMappingFile parser = new DSLTokenizedMappingFile();
if (parser.parseAndLoad(dsl)) {
entry = parser.getMapping().getEntries().get(0);
} else {
throw new RuntimeException("Error parsing entry: " + mapping + ": " + parser.getErrors().toString());
}
} finally {
dsl.close();
}
return entry;
}
Aggregations