From 0e3af3eb8f295a865c58943201e2142d2de8bf53 Mon Sep 17 00:00:00 2001 From: Magnus Vojbacke Date: Sat, 22 Dec 2012 13:57:29 +0100 Subject: [PATCH 1/2] Fixing issue where indented keywords would fail template expansion. --- .../parser/DefaultTemplateContainer.java | 17 ++++----- .../parser/DefaultTemplateContainerTest.java | 11 ++++++ .../templates/rule_template_indented.drl | 35 +++++++++++++++++++ 3 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 drools-templates/src/test/resources/templates/rule_template_indented.drl diff --git a/drools-templates/src/main/java/org/drools/template/parser/DefaultTemplateContainer.java b/drools-templates/src/main/java/org/drools/template/parser/DefaultTemplateContainer.java index 4c2a8134923..2afc7fc1048 100644 --- a/drools-templates/src/main/java/org/drools/template/parser/DefaultTemplateContainer.java +++ b/drools-templates/src/main/java/org/drools/template/parser/DefaultTemplateContainer.java @@ -71,10 +71,11 @@ 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; String quotedName = line.substring(8).trim(); quotedName = quotedName.substring(1, quotedName @@ -82,7 +83,7 @@ private void parseTemplate(final InputStream templateStream) { 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"); @@ -90,13 +91,13 @@ private void parseTemplate(final InputStream templateStream) { 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; @@ -104,7 +105,7 @@ private void parseTemplate(final InputStream templateStream) { } else if (inContents) { contents.append(line).append("\n"); } else if (inTemplate) { - template.addColumn(line.trim()); + template.addColumn(trimmed); } } diff --git a/drools-templates/src/test/java/org/drools/template/parser/DefaultTemplateContainerTest.java b/drools-templates/src/test/java/org/drools/template/parser/DefaultTemplateContainerTest.java index b5339002311..c384ce8824e 100644 --- a/drools-templates/src/test/java/org/drools/template/parser/DefaultTemplateContainerTest.java +++ b/drools-templates/src/test/java/org/drools/template/parser/DefaultTemplateContainerTest.java @@ -32,6 +32,17 @@ public void testParseTemplate() { 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 diff --git a/drools-templates/src/test/resources/templates/rule_template_indented.drl b/drools-templates/src/test/resources/templates/rule_template_indented.drl new file mode 100644 index 00000000000..1636865b3ba --- /dev/null +++ b/drools-templates/src/test/resources/templates/rule_template_indented.drl @@ -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.drools.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 From 7b07e7adf6912ed02b3ee2929524e8dcbbf1ed1c Mon Sep 17 00:00:00 2001 From: Magnus Vojbacke Date: Sat, 22 Dec 2012 19:55:56 +0100 Subject: [PATCH 2/2] Trim line before extracting template name (oversight in previous commit) --- .../template/parser/DefaultTemplateContainer.java | 2 +- .../org/drools/template/DataProviderCompilerTest.java | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/drools-templates/src/main/java/org/drools/template/parser/DefaultTemplateContainer.java b/drools-templates/src/main/java/org/drools/template/parser/DefaultTemplateContainer.java index 2afc7fc1048..d69aecf52ef 100644 --- a/drools-templates/src/main/java/org/drools/template/parser/DefaultTemplateContainer.java +++ b/drools-templates/src/main/java/org/drools/template/parser/DefaultTemplateContainer.java @@ -77,7 +77,7 @@ private void parseTemplate(final InputStream templateStream) { inHeader = true; } else if (trimmed.startsWith("template")) { inTemplate = true; - String quotedName = line.substring(8).trim(); + String quotedName = trimmed.substring(8).trim(); quotedName = quotedName.substring(1, quotedName .length() - 1); template = new RuleTemplate(quotedName, this); diff --git a/drools-templates/src/test/java/org/drools/template/DataProviderCompilerTest.java b/drools-templates/src/test/java/org/drools/template/DataProviderCompilerTest.java index 5b825098cbd..b85384ba45c 100644 --- a/drools-templates/src/test/java/org/drools/template/DataProviderCompilerTest.java +++ b/drools-templates/src/test/java/org/drools/template/DataProviderCompilerTest.java @@ -128,6 +128,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 {