Skip to content

Commit

Permalink
BZ1010864 - Drools Templates: Indented keywords would fail template e…
Browse files Browse the repository at this point in the history
…xpansion
  • Loading branch information
magnusvojbacke authored and manstis committed Sep 23, 2013
1 parent 406bbcc commit 0bd9854
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
Expand Up @@ -70,40 +70,41 @@ private void parseTemplate(final InputStream templateStream) {
RuleTemplate template = null;
StringBuffer contents = new StringBuffer();
while ((line = templateReader.readLine()) != null) {
if (line.trim().length() > 0) {
if (line.startsWith("template header")) {
String trimmed = line.trim();
if (trimmed.length() > 0) {
if (trimmed.startsWith("template header")) {
inHeader = true;
} else if (line.startsWith("template")) {
} else if (trimmed.startsWith("template")) {
inTemplate = true;
inHeader = false;
String quotedName = line.substring(8).trim();
String quotedName = trimmed.substring(8).trim();
quotedName = quotedName.substring(1, quotedName.length() - 1);
template = new RuleTemplate(quotedName, this);
addTemplate(template);

} else if (line.startsWith("package")) {
} else if (trimmed.startsWith("package")) {
if (inHeader == false) {
throw new DecisionTableParseException(
"Missing header");
}
inHeader = false;
header.append(line).append("\n");
} else if (inHeader) {
addColumn(cf.getColumn(line.trim()));
addColumn(cf.getColumn(trimmed));
} else if (!inTemplate && !inHeader) {
header.append(line).append("\n");
} else if (!inContents && line.startsWith("rule")) {
} else if (!inContents && trimmed.startsWith("rule")) {
inContents = true;
contents.append(line).append("\n");
} else if (line.equals("end template")) {
} else if (trimmed.equals("end template")) {
template.setContents(contents.toString());
contents.setLength(0);
inTemplate = false;
inContents = false;
} else if (inContents) {
contents.append(line).append("\n");
} else if (inTemplate) {
template.addColumn(line.trim());
template.addColumn(trimmed);
}
}

Expand Down
Expand Up @@ -122,6 +122,16 @@ public void testCompiler() throws Exception {
assertEqualsIgnoreWhitespace(EXPECTED_RULES.toString(),
drl);
}

@Test
public void testCompileIndentedKeywords() throws Exception {
TestDataProvider tdp = new TestDataProvider( rows );
final DataProviderCompiler converter = new DataProviderCompiler();
final String drl = converter.compile( tdp,
"/templates/rule_template_indented.drl" );
assertEqualsIgnoreWhitespace( EXPECTED_RULES.toString(),
drl );
}

@Test
public void testCompilerMaps() throws Exception {
Expand Down
Expand Up @@ -54,6 +54,17 @@ public void testParseTemplateNoPackage() {
assertTrue( contents.endsWith( "then\nend\n" ) );
}

/*
* Smoke-test to verify it's possible to load a template containing
* indented keywords without exception
*/
@Test
public void testParseTemplateIndentedKeywords() {
InputStream is = DefaultTemplateContainerTest.class
.getResourceAsStream("/templates/rule_template_indented.drl");
new DefaultTemplateContainer(is);
}

@Test
public void testParseTemplateConditions() {
InputStream is = DefaultTemplateContainerTest.class.getResourceAsStream( "/templates/test_template_conditions.drl" );
Expand Down
@@ -0,0 +1,35 @@
template header
FEE_SCHEDULE_ID
FEE_SCHEDULE_TYPE
FEE_MODE_TYPE
ENTITY_BRANCH
PRODUCT_TYPE
ACTIVITY_TYPE
FEE_TYPE
OWNING_PARTY
CCY
LC_AMOUNT
AMOUNT


package org.kie.decisiontable;
//generated from Decision Table

global FeeResult result;

template "Fee Schedule"
rule "Fee Schedule_@{row.rowNumber}"
agenda-group "@{FEE_SCHEDULE_TYPE}"
when
FeeEvent(productType == "@{PRODUCT_TYPE}",
activityType == "@{ACTIVITY_TYPE}",
feeType == "@{FEE_TYPE}",
txParty == "@{OWNING_PARTY}",
entityBranch == "@{ENTITY_BRANCH}",
amount @{LC_AMOUNT},
ccy == "@{CCY}"
)
then
result.setSchedule(new FeeSchedule("@{FEE_SCHEDULE_ID}", "@{FEE_SCHEDULE_TYPE}", @{AMOUNT}));
end
end template

0 comments on commit 0bd9854

Please sign in to comment.