[jboss-svn-commits] JBL Code SVN: r13577 - in labs/jbossrules/trunk/drools-compiler/src: test/java/org/drools/integrationtests and 1 other directories.
jboss-svn-commits at lists.jboss.org
jboss-svn-commits at lists.jboss.org
Tue Jul 17 12:14:43 EDT 2007
Author: tirelli
Date: 2007-07-17 12:14:43 -0400 (Tue, 17 Jul 2007)
New Revision: 13577
Modified:
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/dsl/DefaultDSLMappingEntry.java
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/DslTest.java
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/lang/dsl/DefaultDSLMappingEntryTest.java
Log:
JBRULES-1012: fixing bug with space sensitive expressions
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/dsl/DefaultDSLMappingEntry.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/dsl/DefaultDSLMappingEntry.java 2007-07-17 16:13:13 UTC (rev 13576)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/lang/dsl/DefaultDSLMappingEntry.java 2007-07-17 16:14:43 UTC (rev 13577)
@@ -102,25 +102,25 @@
/**
* @param key the key to set
*/
- public void setMappingKey(final String key) {
- this.key = key;
+ public void setMappingKey(String key) {
+ this.key = key = key.trim();
if ( key != null ) {
int substr = 0;
// escape '$' to avoid errors
final Matcher m = varFinder.matcher( key.replaceAll( "\\$",
- "\\\\\\$" ) );
+ "\\\\\\$" ) );
// retrieving variables list and creating key pattern
final StringBuffer buf = new StringBuffer();
int counter = 1;
- if( ! key.startsWith( "^" ) ) {
+ if ( !key.startsWith( "^" ) ) {
// making it start with a space char or a line start
buf.append( "(\\W|^)" );
substr += buf.length();
counter++;
}
-
+
while ( m.find() ) {
if ( this.variables == Collections.EMPTY_MAP ) {
this.variables = new HashMap( 2 );
@@ -131,7 +131,7 @@
m.group( 1 ) + "(.*?)" );
}
m.appendTail( buf );
-
+
// if pattern ends with a variable, append a line end to avoid multiple line matching
if ( buf.toString().endsWith( "(.*?)" ) ) {
buf.append( "$" );
@@ -140,12 +140,13 @@
}
// setting the key pattern and making it space insensitive
- String pat = buf.toString().replaceAll( "\\s+",
- "\\\\s*" );
+ String pat = buf.toString();
if ( pat.substring( substr ).trim().startsWith( "-" ) && (!pat.substring( substr ).trim().startsWith( "-\\s*" )) ) {
pat = pat.substring( 0,
- pat.indexOf( '-' ) + 1 ) + "\\s*" + pat.substring( pat.indexOf( '-' ) + 1 );
+ pat.indexOf( '-' ) + 1 ) + "\\s*" + pat.substring( pat.indexOf( '-' ) + 1 ).trim();
}
+ pat = pat.replaceAll( "\\s+",
+ "\\\\s+" );
this.keyPattern = Pattern.compile( pat,
Pattern.DOTALL | Pattern.MULTILINE );
@@ -179,7 +180,7 @@
final int pos = ((Integer) entry.getValue()).intValue();
this.valuePattern = this.valuePattern.replaceAll( "\\{" + var + "\\}",
- "\\$" + pos );
+ "\\$" + pos );
}
}
}
Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/DslTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/DslTest.java 2007-07-17 16:13:13 UTC (rev 13576)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/integrationtests/DslTest.java 2007-07-17 16:14:43 UTC (rev 13577)
@@ -29,7 +29,7 @@
return RuleBaseFactory.newRuleBase( RuleBase.RETEOO,
config );
}
-
+
public void testWithExpanderDSL() throws Exception {
final PackageBuilder builder = new PackageBuilder();
final Reader source = new InputStreamReader( getClass().getResourceAsStream( "rule_with_expander_dsl.drl" ) );
@@ -55,9 +55,9 @@
final WorkingMemory wm = ruleBase.newStatefulSession();
wm.insert( new Person( "Bob",
- "http://foo.bar" ) );
+ "http://foo.bar" ) );
wm.insert( new Cheese( "stilton",
- 42 ) );
+ 42 ) );
final List messages = new ArrayList();
wm.setGlobal( "messages",
@@ -95,7 +95,7 @@
final WorkingMemory wm = ruleBase.newStatefulSession();
wm.insert( new Person( "rage" ) );
wm.insert( new Cheese( "cheddar",
- 15 ) );
+ 15 ) );
final List messages = new ArrayList();
wm.setGlobal( "messages",
@@ -114,15 +114,15 @@
messages.size() );
wm.insert( new Cheese( "brie",
- 15 ) );
+ 15 ) );
wm.fireAllRules();
// YOUR FIRED
assertEquals( 1,
- messages.size() );
- }
-
+ messages.size() );
+ }
+
public void testEmptyDSL() throws Exception {
final String DSL = "# This is an empty dsl file.";
final PackageBuilder builder = new PackageBuilder();
@@ -134,5 +134,45 @@
final Package pkg = builder.getPackage();
assertFalse( pkg.isValid() );
- }
+ }
+
+ public void testDSLWithIndividualConstraintMappings() throws Exception {
+ final PackageBuilder builder = new PackageBuilder();
+ final Reader source = new InputStreamReader( getClass().getResourceAsStream( "test_dslWithIndividualConstraints.dslr" ) );
+ final Reader dsl = new InputStreamReader( getClass().getResourceAsStream( "test_dslWithIndividualConstraints.dsl" ) );
+ builder.addPackageFromDrl( source,
+ dsl );
+
+ // the compiled package
+ final Package pkg = builder.getPackage();
+ assertTrue( pkg.getErrorSummary(),
+ pkg.isValid() );
+ assertEquals( pkg.getErrorSummary(),
+ null,
+ pkg.getErrorSummary() );
+ // Check errors
+ assertEquals( 0,
+ builder.getErrors().getErrors().length );
+
+ final RuleBase ruleBase = getRuleBase();
+ ruleBase.addPackage( pkg );
+
+ final WorkingMemory wm = ruleBase.newStatefulSession();
+ List results = new ArrayList();
+ wm.setGlobal( "results",
+ results );
+ Cheese cheese = new Cheese( "stilton",
+ 42 );
+ wm.insert( cheese );
+
+ wm.fireAllRules();
+
+ // should have fired
+ assertEquals( 1,
+ results.size() );
+ assertEquals( cheese,
+ results.get( 0 ) );
+
+ }
+
}
Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/lang/dsl/DefaultDSLMappingEntryTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/lang/dsl/DefaultDSLMappingEntryTest.java 2007-07-17 16:13:13 UTC (rev 13576)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/lang/dsl/DefaultDSLMappingEntryTest.java 2007-07-17 16:14:43 UTC (rev 13577)
@@ -25,7 +25,7 @@
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 = "(\\W|^)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 expectedKeyP = "(\\W|^)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 == \"$2\", surname == \"$3\", money > \\$money )";
final DSLMappingEntry entry = new DefaultDSLMappingEntry( DSLMappingEntry.CONDITION,
@@ -48,7 +48,7 @@
final String inputKey = "-name is {name}";
final String inputValue = "name == \"{name}\"";
- final String expectedKeyP = "(\\W|^)-\\s*name\\s*is\\s*(.*?)$";
+ final String expectedKeyP = "(\\W|^)-\\s*name\\s+is\\s+(.*?)$";
final String expectedValP = "name == \"$2\"";
final DSLMappingEntry entry = new DefaultDSLMappingEntry( DSLMappingEntry.CONDITION,
@@ -71,7 +71,7 @@
final String inputKey = "- name is {name}";
final String inputValue = "name == \"{name}\"";
- final String expectedKeyP = "(\\W|^)-\\s*name\\s*is\\s*(.*?)$";
+ final String expectedKeyP = "(\\W|^)-\\s*name\\s+is\\s+(.*?)$";
final String expectedValP = "name == \"$2\"";
final DSLMappingEntry entry = new DefaultDSLMappingEntry( DSLMappingEntry.CONDITION,
@@ -81,7 +81,8 @@
assertEquals( inputKey,
entry.getMappingKey() );
- assertEquals( expectedKeyP,
+ assertEquals( entry.getKeyPattern().pattern(),
+ expectedKeyP,
entry.getKeyPattern().pattern() );
assertEquals( inputValue,
entry.getMappingValue() );
@@ -132,4 +133,65 @@
assertEquals( "SomeFact(value==\" bl ah \")",
result );
}
+
+ public void testExpandWithDots() {
+ final String inputKey = "- {prop} is {val} ";
+ final String inputValue = "{prop} == {val}";
+
+ this.entry = new DefaultDSLMappingEntry( DSLMappingEntry.CONDITION,
+ null,
+ inputKey,
+ inputValue );
+
+ String result = this.entry.getKeyPattern().matcher( "- type is ClientServiceType.TypeGOLD" ).replaceAll( this.entry.getValuePattern() );
+ assertEquals( result,
+ "type == ClientServiceType.TypeGOLD",
+ result );
+ }
+
+ public void testExpandPartialWords() {
+ final String inputKey = "- {prop} is {val} ";
+ final String inputValue = "{prop} == {val}";
+
+ this.entry = new DefaultDSLMappingEntry( DSLMappingEntry.CONDITION,
+ null,
+ inputKey,
+ inputValue );
+ // not supposed to expand
+ String result = this.entry.getKeyPattern().matcher( "- type is_not ClientServiceType.TypeGOLD" ).replaceAll( this.entry.getValuePattern() );
+ assertEquals( result,
+ "- type is_not ClientServiceType.TypeGOLD",
+ result );
+ }
+
+ public void testExpandPartialWords2() {
+ final String inputKey = "- {prop} is_not {val} ";
+ final String inputValue = "{prop} != {val}";
+
+ this.entry = new DefaultDSLMappingEntry( DSLMappingEntry.CONDITION,
+ null,
+ inputKey,
+ inputValue );
+
+ String result = this.entry.getKeyPattern().matcher( "- type is_not ClientServiceType.TypeGOLD" ).replaceAll( this.entry.getValuePattern() );
+ assertEquals( result,
+ "type != ClientServiceType.TypeGOLD",
+ result );
+ }
+
+ public void testExpandPartialWords3() {
+ final String inputKey = "- {prop} is not {val} ";
+ final String inputValue = "{prop} != {val}";
+
+ this.entry = new DefaultDSLMappingEntry( DSLMappingEntry.CONDITION,
+ null,
+ inputKey,
+ inputValue );
+
+ String result = this.entry.getKeyPattern().matcher( "- type is not ClientServiceType.TypeGOLD" ).replaceAll( this.entry.getValuePattern() );
+ assertEquals( result,
+ "type != ClientServiceType.TypeGOLD",
+ result );
+ }
+
}
More information about the jboss-svn-commits
mailing list