[jboss-svn-commits] JBL Code SVN: r22261 - in labs/jbossrules/trunk/drools-guvnor/src: test/java/org/drools/guvnor/server/util and 1 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Sat Aug 30 17:54:11 EDT 2008
Author: Rikkola
Date: 2008-08-30 17:54:11 -0400 (Sat, 30 Aug 2008)
New Revision: 22261
Modified:
labs/jbossrules/trunk/drools-guvnor/src/main/java/org/drools/guvnor/server/util/ClassicDRLImporter.java
labs/jbossrules/trunk/drools-guvnor/src/test/java/org/drools/guvnor/server/util/ClassicDRLImporterTest.java
labs/jbossrules/trunk/drools-guvnor/src/test/resources/org/drools/guvnor/server/util/sample_legacy_functions.drl
Log:
JBRULES-1749: ClassicDRLImporter does not parse curly brackets correctly
Modified: labs/jbossrules/trunk/drools-guvnor/src/main/java/org/drools/guvnor/server/util/ClassicDRLImporter.java
===================================================================
--- labs/jbossrules/trunk/drools-guvnor/src/main/java/org/drools/guvnor/server/util/ClassicDRLImporter.java 2008-08-30 17:03:37 UTC (rev 22260)
+++ labs/jbossrules/trunk/drools-guvnor/src/main/java/org/drools/guvnor/server/util/ClassicDRLImporter.java 2008-08-30 21:54:11 UTC (rev 22261)
@@ -1,4 +1,5 @@
package org.drools.guvnor.server.util;
+
/*
* Copyright 2005 JBoss Inc
*
@@ -15,8 +16,6 @@
* limitations under the License.
*/
-
-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
@@ -43,33 +42,31 @@
*/
public class ClassicDRLImporter {
- private String source;
+ private String source;
- private String packageName;
+ private String packageName;
- private List<Asset> assets = new ArrayList<Asset>();
+ private List<Asset> assets = new ArrayList<Asset>();
- private StringBuffer header;
+ private StringBuffer header;
- private boolean usesDSL;
+ private boolean usesDSL;
- private static Pattern functionPattern = Pattern.compile("function\\s+.*\\s+(.*)\\(.*\\).*");
+ private static Pattern functionPattern = Pattern.compile( "function\\s+.*\\s+(.*)\\(.*\\).*" );
-
- public ClassicDRLImporter(InputStream in) throws IOException, DroolsParserException {
+ public ClassicDRLImporter(InputStream in) throws IOException,
+ DroolsParserException {
String line = "";
StringBuffer drl = new StringBuffer();
- BufferedReader reader = new BufferedReader(new InputStreamReader(in));
- while ( (line = reader.readLine()) != null) {
- drl.append( "\n" + line);
+ BufferedReader reader = new BufferedReader( new InputStreamReader( in ) );
+ while ( (line = reader.readLine()) != null ) {
+ drl.append( "\n" + line );
}
this.source = drl.toString();
parse();
}
-
-
private void parse() throws DroolsParserException {
StringTokenizer lines = new StringTokenizer( source,
"\r\n" );
@@ -83,19 +80,44 @@
} else if ( line.startsWith( "rule" ) ) {
String ruleName = getRuleName( line );
StringBuffer currentRule = new StringBuffer();
- laConsumeToEnd( lines, currentRule, "end" );
- addRule( ruleName, currentRule );
+ laConsumeToEnd( lines,
+ currentRule,
+ "end" );
+ addRule( ruleName,
+ currentRule );
- } else if (line.startsWith("function")) {
- String functionName = getFuncName( line );
- StringBuffer currentFunc = new StringBuffer();
- currentFunc.append(line + "\n");
- laConsumeToEnd( lines, currentFunc, "}");
- currentFunc.append("}\n");
- addFunction( functionName, currentFunc );
+ } else if ( line.startsWith( "function" ) ) {
+ String functionName = getFuncName( line );
+ StringBuffer currentFunc = new StringBuffer();
- }else if ( line.startsWith( "expander" ) ) {
+ int counter = 0;
+ currentFunc.append( line + "\n" );
+
+ counter = countBrackets( counter,
+ line );
+
+ if ( counter > 0 ) {
+ laConsumeBracketsToEnd( counter,
+ lines,
+ currentFunc );
+ }
+ addFunction( functionName,
+ currentFunc );
+
+ } else if ( line.startsWith( "/*" ) ) {
+
+ StringBuffer comment = new StringBuffer();
+ laConsumeToEnd( lines,
+ comment,
+ "*/" );
+
+ // TODO: What to do with commented lines?
+
+ header.append( comment );
+
+ } else if ( line.startsWith( "expander" ) ) {
+
usesDSL = true;
} else {
@@ -105,26 +127,169 @@
}
}
+ private void addFunction(String functionName,
+ StringBuffer currentFunc) {
+ this.assets.add( new Asset( functionName,
+ currentFunc.toString(),
+ AssetFormats.FUNCTION ) );
+ }
+ private String getFuncName(String line) {
+ Matcher m = functionPattern.matcher( line );
+ m.matches();
+ return m.group( 1 );
+ }
+ /**
+ * Consumes function to the ending curly bracket.
+ *
+ * @param lines
+ * @param currentFunc
+ */
+ private void laConsumeBracketsToEnd(int counter,
+ StringTokenizer lines,
+ StringBuffer currentFunc) {
+ /*
+ * Check if the first line contains matching amount of brackets.
+ */
+ boolean multilineIsOpen = false;
+ // Start counting brackets
+ while ( lines.hasMoreTokens() ) {
+ String line = lines.nextToken();
- private void addFunction(String functionName, StringBuffer currentFunc) {
- this.assets.add(new Asset(functionName, currentFunc.toString(), AssetFormats.FUNCTION));
- }
+ currentFunc.append( line );
+ currentFunc.append( "\n" );
+ if ( multilineIsOpen ) {
+ int commentEnd = line.indexOf( "*/" );
+ if ( commentEnd != -1 ) {
+ multilineIsOpen = false;
+ line = line.substring( commentEnd );
+ }
+ } else {
+ multilineIsOpen = checkIfMultilineCommentStarts( line );
+ line = removeComments( line );
+ }
- private String getFuncName(String line) {
- Matcher m = functionPattern.matcher(line);
- m.matches();
- return m.group(1);
- }
+ if ( !multilineIsOpen ) {
+ counter = countBrackets( counter,
+ line );
+ }
+ if ( counter == 0 ) {
+ break;
+ }
+ }
+ }
+ /**
+ * @param line
+ * @return
+ */
+ private boolean checkIfMultilineCommentStarts(String line) {
- private void laConsumeToEnd(StringTokenizer lines, StringBuffer currentRule, String end) {
+ int commentMultiLineStart = line.indexOf( "/*" );
+ int commentMultiLineEnd = line.indexOf( "*/" );
+ // int commentSingleLine = line.indexOf( "//" );
+
+ if ( commentMultiLineStart != -1 && commentMultiLineEnd == -1 ) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ private int countBrackets(int counter,
+ String line) {
+ char[] chars = line.toCharArray();
+ for ( int i = 0; i < chars.length; i++ ) {
+ if ( chars[i] == '{' ) {
+ counter++;
+ } else if ( chars[i] == '}' ) {
+ counter--;
+ }
+ }
+ return counter;
+ }
+
+ private String removeComments(String line) {
+
+ int commentMultiLineStart = line.indexOf( "/*" );
+ int commentMultiLineEnd = line.indexOf( "*/" );
+ int commentSingleLine = line.indexOf( "//" );
+
+ // Single line comment is first
+ // Case: some code // /* */
+ // Another case: some code // No comments
+ if ( commentSingleLine != -1 && commentMultiLineStart > commentSingleLine ) {
+ return line.substring( 0,
+ commentSingleLine );
+ }
+
+ // There is only a start for the multiline comment.
+ // Case: some code here /* commented out
+ if ( commentMultiLineStart != -1 && commentMultiLineEnd == -1 ) {
+ return line.substring( 0,
+ commentMultiLineStart );
+ }
+
+ // Two ends are on the same line
+ // some code /* comment */
+ if ( commentMultiLineStart != -1 && commentMultiLineEnd != -1 ) {
+
+ line = line.substring( commentMultiLineEnd );
+ line = line.substring( 0,
+ commentMultiLineStart );
+
+ return line;
+ }
+
+ return line;
+ }
+
+ //
+ // private String consumeUntillEndOfComment(StringTokenizer lines,
+ // StringBuffer currentRule) {
+ // String line;
+ // while ( lines.hasMoreTokens() ) {
+ // line = lines.nextToken();
+ // currentRule.append( line );
+ // currentRule.append( "\n" );
+ // if ( line.trim().contains( "*/" ) ) {
+ // return line;
+ // }
+ // }
+ //
+ // return "";
+ // }
+ //
+ // private String removeComments(String line,
+ // int commentMultiLineStart,
+ // int commentMultiLineEnd) {
+ // if ( commentMultiLineStart == -1 && commentMultiLineEnd != -1 ) {
+ // line = line.substring( 0,
+ // commentMultiLineEnd );
+ // } else if ( commentMultiLineStart != -1 && commentMultiLineEnd == -1 ) {
+ // line = line.substring( commentMultiLineStart,
+ // line.length() - 1 );
+ // } else if ( commentMultiLineStart != -1 && commentMultiLineEnd != -1 ) {
+ // line = line.substring( commentMultiLineStart,
+ // commentMultiLineEnd );
+ // // In case there is another
+ // line = removeComments( line,
+ // commentMultiLineStart,
+ // commentMultiLineEnd );
+ // }
+ //
+ // return line;
+ // }
+
+ private void laConsumeToEnd(StringTokenizer lines,
+ StringBuffer currentRule,
+ String end) {
String line;
- while ( true && lines.hasMoreTokens()) {
+ while ( lines.hasMoreTokens() ) {
line = lines.nextToken();
if ( line.trim().startsWith( end ) ) {
break;
@@ -134,14 +299,17 @@
}
}
- private void addRule(String ruleName, StringBuffer currentRule) {
- if (this.isDSLEnabled()) {
- this.assets.add( new Asset( ruleName,
- currentRule.toString(), AssetFormats.DSL_TEMPLATE_RULE ));
- } else {
- this.assets.add( new Asset( ruleName,
- currentRule.toString(), AssetFormats.DRL ));
- }
+ private void addRule(String ruleName,
+ StringBuffer currentRule) {
+ if ( this.isDSLEnabled() ) {
+ this.assets.add( new Asset( ruleName,
+ currentRule.toString(),
+ AssetFormats.DSL_TEMPLATE_RULE ) );
+ } else {
+ this.assets.add( new Asset( ruleName,
+ currentRule.toString(),
+ AssetFormats.DRL ) );
+ }
}
private String getRuleName(String line) throws DroolsParserException {
@@ -161,7 +329,6 @@
return this.assets;
}
-
public String getPackageName() {
return this.packageName;
}
@@ -181,8 +348,9 @@
*/
public static class Asset {
- public Asset(
- String name, String content, String format) {
+ public Asset(String name,
+ String content,
+ String format) {
this.name = name;
this.content = content;
this.format = format;
@@ -196,26 +364,26 @@
/**
* This merges the toMerge new schtuff into the existing. Line by line, simple stuff.
*/
- public static String mergeLines(String existing, String toMerge) {
+ public static String mergeLines(String existing,
+ String toMerge) {
- if (toMerge == null || toMerge.equals("")) {
- return existing;
- }
- if (existing == null || existing.equals("")) {
- return toMerge;
- }
- Set existingLines = new HashSet<String>(Arrays.asList(existing.split("\n")));
- String[] newLines = toMerge.split("\n");
- for (int i = 0; i < newLines.length; i++) {
- String newLine = newLines[i].trim();
+ if ( toMerge == null || toMerge.equals( "" ) ) {
+ return existing;
+ }
+ if ( existing == null || existing.equals( "" ) ) {
+ return toMerge;
+ }
+ Set existingLines = new HashSet<String>( Arrays.asList( existing.split( "\n" ) ) );
+ String[] newLines = toMerge.split( "\n" );
+ for ( int i = 0; i < newLines.length; i++ ) {
+ String newLine = newLines[i].trim();
- if (!newLine.equals("") && !existingLines.contains(newLines[i].trim())) {
- existing = existing + "\n" + newLines[i];
- }
- }
- return existing;
+ if ( !newLine.equals( "" ) && !existingLines.contains( newLines[i].trim() ) ) {
+ existing = existing + "\n" + newLines[i];
+ }
+ }
+ return existing;
- }
+ }
-
}
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-guvnor/src/test/java/org/drools/guvnor/server/util/ClassicDRLImporterTest.java
===================================================================
--- labs/jbossrules/trunk/drools-guvnor/src/test/java/org/drools/guvnor/server/util/ClassicDRLImporterTest.java 2008-08-30 17:03:37 UTC (rev 22260)
+++ labs/jbossrules/trunk/drools-guvnor/src/test/java/org/drools/guvnor/server/util/ClassicDRLImporterTest.java 2008-08-30 21:54:11 UTC (rev 22261)
@@ -1,4 +1,5 @@
package org.drools.guvnor.server.util;
+
/*
* Copyright 2005 JBoss Inc
*
@@ -15,8 +16,6 @@
* limitations under the License.
*/
-
-
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
@@ -32,82 +31,114 @@
public class ClassicDRLImporterTest extends TestCase {
-
-
public void testStandardDRL() throws Exception {
+ ClassicDRLImporter imp = new ClassicDRLImporter( getDrl( "sample_legacy.drl" ) );
+ assertEquals( "foo",
+ imp.getPackageName() );
+ assertEquals( 2,
+ imp.getAssets().size() );
+ assertEquals( "blah",
+ imp.getAssets().get( 0 ).name );
+ assertEquals( "cha",
+ imp.getAssets().get( 1 ).name );
+ System.err.println( imp.getPackageHeader() );
- ClassicDRLImporter imp = new ClassicDRLImporter(getDrl("sample_legacy.drl"));
- assertEquals( "foo", imp.getPackageName() );
- assertEquals(2, imp.getAssets().size());
+ assertTrue( imp.getPackageHeader().indexOf( "import goo.wee" ) > -1 );
+ assertTrue( imp.getPackageHeader().indexOf( "package" ) == -1 );
- assertEquals("blah", imp.getAssets().get( 0 ).name);
- assertEquals("cha", imp.getAssets().get( 1 ).name);
+ assertFalse( imp.isDSLEnabled() );
- System.err.println(imp.getPackageHeader());
+ assertEqualsIgnoreWhitespace( "when Whee() then goo();",
+ imp.getAssets().get( 0 ).content );
+ assertEqualsIgnoreWhitespace( "when Sup() then ka();",
+ imp.getAssets().get( 1 ).content );
+ assertTrue( imp.getAssets().get( 0 ).content.indexOf( " Whee()" ) > -1 );
- assertTrue(imp.getPackageHeader().indexOf( "import goo.wee" ) > -1);
- assertTrue(imp.getPackageHeader().indexOf( "package" ) == -1);
+ }
- assertFalse(imp.isDSLEnabled());
+ public void testWithFunction() throws Exception {
+ // Pattern p = Pattern.compile("function\\s+.*\\s+(.*)\\(.*\\).*");
+ // Matcher m = p.matcher("function void fooBar() {");
+ // assertTrue(m.matches());
+ // System.err.println(m.group());
+ // assertEquals("fooBar", m.group(1));
- assertEqualsIgnoreWhitespace( "when Whee() then goo();", imp.getAssets().get( 0 ).content);
- assertEqualsIgnoreWhitespace( "when Sup() then ka();", imp.getAssets().get( 1 ).content);
- assertTrue(imp.getAssets().get( 0 ).content.indexOf( " Whee()") > -1);
+ ClassicDRLImporter imp = new ClassicDRLImporter( getDrl( "sample_legacy_functions.drl" ) );
+ assertFalse( imp.isDSLEnabled() );
- }
+ assertEquals( 7,
+ imp.getAssets().size() );
+ assertEquals( AssetFormats.FUNCTION,
+ imp.getAssets().get( 0 ).format );
+ assertEquals( AssetFormats.FUNCTION,
+ imp.getAssets().get( 1 ).format );
- public void testWithFunction() throws Exception {
-// Pattern p = Pattern.compile("function\\s+.*\\s+(.*)\\(.*\\).*");
-// Matcher m = p.matcher("function void fooBar() {");
-// assertTrue(m.matches());
-// System.err.println(m.group());
-// assertEquals("fooBar", m.group(1));
+ assertEquals( "goo1",
+ imp.getAssets().get( 0 ).name );
+ assertEqualsIgnoreWhitespace( "function void goo1() { //do something ! { yeah } }",
+ imp.getAssets().get( 0 ).content );
- ClassicDRLImporter imp = new ClassicDRLImporter(getDrl("sample_legacy_functions.drl"));
- assertFalse(imp.isDSLEnabled());
+ assertEquals( "goo2",
+ imp.getAssets().get( 1 ).name );
+ assertEqualsIgnoreWhitespace( "function String goo2(String la) { //yeah man ! return \"whee\"; }",
+ imp.getAssets().get( 1 ).content );
- assertEquals(4, imp.getAssets().size());
- assertEquals(AssetFormats.FUNCTION, imp.getAssets().get(0).format);
- assertEquals(AssetFormats.FUNCTION, imp.getAssets().get(1).format);
+ assertEquals( "goo3",
+ imp.getAssets().get( 2 ).name );
+ assertEqualsIgnoreWhitespace( "function String goo3() { return \"HELLO\"; }",
+ imp.getAssets().get( 2 ).content );
- assertEquals("goo1", imp.getAssets().get(0).name);
- assertEqualsIgnoreWhitespace("function void goo1() { //do something ! { yeah } }", imp.getAssets().get(0).content);
+ assertEquals( "goo4",
+ imp.getAssets().get( 3 ).name );
+ assertEqualsIgnoreWhitespace( "function String goo4() { if( true ) { return \"HELLO\"; } }",
+ imp.getAssets().get( 3 ).content );
- assertEquals("goo2", imp.getAssets().get(1).name);
- assertEqualsIgnoreWhitespace("function String goo2(String la) { //yeah man ! return \"whee\"; }", imp.getAssets().get(1).content);
+ assertEquals( "goo6",
+ imp.getAssets().get( 4 ).name );
+ assertEqualsIgnoreWhitespace( "function String goo6() { return \"HELLO\"; /* } */ /* } } } */ }",
+ imp.getAssets().get( 4 ).content );
- assertEquals(AssetFormats.DRL, imp.getAssets().get(2).format);
- assertEquals(AssetFormats.DRL, imp.getAssets().get(3).format);
- assertNotNull(imp.getAssets().get(3).content);
+ assertEquals( AssetFormats.DRL,
+ imp.getAssets().get( 5 ).format );
+ assertEquals( AssetFormats.DRL,
+ imp.getAssets().get( 6 ).format );
+ assertNotNull( imp.getAssets().get( 6 ).content );
}
public void testWithDSL() throws Exception {
- ClassicDRLImporter imp = new ClassicDRLImporter(getDrl("sample_legacy_with_dsl.drl"));
+ ClassicDRLImporter imp = new ClassicDRLImporter( getDrl( "sample_legacy_with_dsl.drl" ) );
- assertTrue(imp.isDSLEnabled());
- assertEquals(2, imp.getAssets().size());
- assertEquals("foo", imp.getPackageName());
- assertEqualsIgnoreWhitespace( "import goo.wee global ka.cha", imp.getPackageHeader() );
+ assertTrue( imp.isDSLEnabled() );
+ assertEquals( 2,
+ imp.getAssets().size() );
+ assertEquals( "foo",
+ imp.getPackageName() );
+ assertEqualsIgnoreWhitespace( "import goo.wee global ka.cha",
+ imp.getPackageHeader() );
- assertEqualsIgnoreWhitespace( "when ka chow then bam", imp.getAssets().get( 0 ).content );
- assertEqualsIgnoreWhitespace( "when ka chiga then ka chow", imp.getAssets().get( 1 ).content );
+ assertEqualsIgnoreWhitespace( "when ka chow then bam",
+ imp.getAssets().get( 0 ).content );
+ assertEqualsIgnoreWhitespace( "when ka chiga then ka chow",
+ imp.getAssets().get( 1 ).content );
+ Asset as = imp.getAssets().get( 0 );
+ assertEquals( AssetFormats.DSL_TEMPLATE_RULE,
+ as.format );
- Asset as = imp.getAssets().get(0);
- assertEquals(AssetFormats.DSL_TEMPLATE_RULE, as.format);
+ }
- }
public void testComplexExample() throws Exception {
- ClassicDRLImporter imp = new ClassicDRLImporter(getDrl("sample_complex.drl"));
- assertFalse(imp.isDSLEnabled());
- assertEquals(2, imp.getAssets().size());
+ ClassicDRLImporter imp = new ClassicDRLImporter( getDrl( "sample_complex.drl" ) );
+ assertFalse( imp.isDSLEnabled() );
+ assertEquals( 2,
+ imp.getAssets().size() );
- assertTrue(DRLFileContentHandler.isStandAloneRule(imp.getAssets().get(0).content));
+ assertTrue( DRLFileContentHandler.isStandAloneRule( imp.getAssets().get( 0 ).content ) );
}
private InputStream getDrl(String file) throws IOException {
@@ -126,19 +157,29 @@
}
public void testMergeHeader() {
- String header = "import foo.bar\nimport wee.waa\n\nglobal goo.ber baz\n";
- String toMerge = "import ninja\nimport foo.bar\nimport slack.bladder\n\nimport wee.waa";
+ String header = "import foo.bar\nimport wee.waa\n\nglobal goo.ber baz\n";
+ String toMerge = "import ninja\nimport foo.bar\nimport slack.bladder\n\nimport wee.waa";
- String result = ClassicDRLImporter.mergeLines(header, toMerge);
+ String result = ClassicDRLImporter.mergeLines( header,
+ toMerge );
- assertEquals("import foo.bar\nimport wee.waa\n\nglobal goo.ber baz\n\nimport ninja\nimport slack.bladder", result);
+ assertEquals( "import foo.bar\nimport wee.waa\n\nglobal goo.ber baz\n\nimport ninja\nimport slack.bladder",
+ result );
- assertEquals("abc", ClassicDRLImporter.mergeLines("abc", ""));
+ assertEquals( "abc",
+ ClassicDRLImporter.mergeLines( "abc",
+ "" ) );
- assertEquals("qed", ClassicDRLImporter.mergeLines("qed", null));
+ assertEquals( "qed",
+ ClassicDRLImporter.mergeLines( "qed",
+ null ) );
- assertEquals("xyz", ClassicDRLImporter.mergeLines("", "xyz"));
- assertEquals("xyz", ClassicDRLImporter.mergeLines(null, "xyz"));
+ assertEquals( "xyz",
+ ClassicDRLImporter.mergeLines( "",
+ "xyz" ) );
+ assertEquals( "xyz",
+ ClassicDRLImporter.mergeLines( null,
+ "xyz" ) );
}
}
\ No newline at end of file
Modified: labs/jbossrules/trunk/drools-guvnor/src/test/resources/org/drools/guvnor/server/util/sample_legacy_functions.drl
===================================================================
--- labs/jbossrules/trunk/drools-guvnor/src/test/resources/org/drools/guvnor/server/util/sample_legacy_functions.drl 2008-08-30 17:03:37 UTC (rev 22260)
+++ labs/jbossrules/trunk/drools-guvnor/src/test/resources/org/drools/guvnor/server/util/sample_legacy_functions.drl 2008-08-30 21:54:11 UTC (rev 22261)
@@ -12,6 +12,33 @@
return "whee";
}
+function String goo3() { return "HELLO"; }
+
+function String goo4() {
+
+ if( true ) {
+ return "HELLO";
+ }
+}
+
+/*
+ function String goo5() {
+
+ if( true ) {
+ return "HELLO";
+ }
+ }
+*/
+
+function String goo6() {
+
+ return "HELLO";
+ /* } */
+ /* }
+ }
+ } */
+}
+
rule "blah"
when
Whee()
More information about the jboss-svn-commits
mailing list