Skip to content

Commit

Permalink
[JBRULES-3447] fix dsl expander regexp for parametrized queries
Browse files Browse the repository at this point in the history
  • Loading branch information
mariofusco committed Apr 4, 2012
1 parent 574dd1e commit ef3892b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 5 deletions.
Expand Up @@ -50,8 +50,8 @@ public class DefaultExpander
"(.*?) " + // 4: consequence
"(^\\s*end.*?$) " + // 5: end starts a line
"|\\s*(query\\s+ " +
"(?:\"[^\"]+\"|'[^']+'|\\S+)" +
"(?:\\s+\\([^)]+)?) " + // 6: query, name, arguments
"(?:\"[^\"]+\"|'[^']+'|\\S+) " +
"(?:\\s*\\([^)]+\\))?) " + // 6: query, name, arguments
"(.*?) " + // 7: condition
"(^\\s*end.*?$) " + // 8: end starts a line
")";
Expand Down
Expand Up @@ -10,6 +10,7 @@
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;

import org.drools.lang.ExpanderException;

Expand Down Expand Up @@ -200,8 +201,8 @@ public void testANTLRExpandWithKeywordClashes() throws Exception {
// System.out.println("["+drl+"]" );
// System.out.println("["+expected+"]" );
assertFalse( ex.hasErrors() );
assertEquals( expected,
drl );
assertEquals(expected,
drl);
}

@Test
Expand Down Expand Up @@ -378,4 +379,64 @@ private void checkExpansion(String source, String expected) throws Exception {

assertEquals( expected, drl );
}
}

@Test
public void testExpandQuery() throws Exception {
DSLTokenizedMappingFile file = new DSLTokenizedMappingFile();
String dsl = "[when]There is a person=Person()\n" +
"[when]- {field:\\w*} {operator} {value:\\d*}={field} {operator} {value}\n" +
"[when]is greater than=>";

String source = "query \"isMature\"\n" +
"There is a person\n" +
"- age is greater than 18\n" +
"end\n";

String expected = "query \"isMature\"\n" +
"Person(age > 18)\n" +
"end\n";

file.parseAndLoad( new StringReader( dsl ) );
assertEquals( 0,
file.getErrors().size() );

DefaultExpander ex = new DefaultExpander();
ex.addDSLMapping( file.getMapping() );

String drl = ex.expand( source );
assertFalse( ex.hasErrors() );

assertEquals( expected, drl );
}

@Test
public void testExpandQueryWithParams() throws Exception {
DSLTokenizedMappingFile file = new DSLTokenizedMappingFile();
String dsl = "[when]There is a person=Person()\n" +
"[when]- {field:\\w*} {operator} {value:\\d*}={field} {operator} {value}\n" +
"[when]- equals {variable}=this == {variable}\n" +
"[when]is greater than=>";

String source = "query \"isMature\"(Person p)\n" +
"There is a person\n" +
"- age is greater than 18\n" +
"- equals p\n" +
"end\n";

String expected = "query \"isMature\"(Person p)\n" +
"Person(age > 18, this == p)\n" +
"end\n";

file.parseAndLoad( new StringReader( dsl ) );
assertEquals( 0,
file.getErrors().size() );

DefaultExpander ex = new DefaultExpander();
ex.addDSLMapping( file.getMapping() );

String drl = ex.expand( source );
assertFalse( ex.hasErrors() );

assertEquals( expected, drl );
}
}

0 comments on commit ef3892b

Please sign in to comment.