Skip to content

Commit

Permalink
BZ1127303: Guided Editor breaks free form DRL which includes setter m…
Browse files Browse the repository at this point in the history
…ethod with multi parameters.
  • Loading branch information
manstis committed Feb 16, 2015
1 parent cf143ff commit 955d434
Show file tree
Hide file tree
Showing 3 changed files with 302 additions and 14 deletions.
Expand Up @@ -43,6 +43,41 @@ public ActionCallMethodBuilder( RuleModel model,
this.boundParams = boundParams;
}

//ActionCallMethods do not support chained method invocations
public boolean supports( final String line ) {
final List<String> splits = new ArrayList<String>();
int depth = 0;
int textDepth = 0;
boolean escape = false;
StringBuffer split = new StringBuffer();
for ( char c : line.toCharArray() ) {
if ( depth == 0 && c == '.' ) {
splits.add( split.toString() );
split = new StringBuffer();
depth = 0;
textDepth = 0;
escape = false;
continue;
} else if ( c == '\\' ) {
escape = true;
split.append( c );
continue;
} else if ( textDepth == 0 && c == '"' ) {
textDepth++;
} else if ( !escape && textDepth > 0 && c == '"' ) {
textDepth--;
} else if ( textDepth == 0 && c == '(' ) {
depth++;
} else if ( textDepth == 0 && c == ')' ) {
depth--;
}
split.append( c );
escape = false;
}
splits.add( split.toString() );
return splits.size() == 2;
}

public ActionCallMethod get( String variable,
String methodName,
String[] parameters ) {
Expand Down
Expand Up @@ -2921,20 +2921,25 @@ private void parseRhs( final RuleModel m,
}
}

private ActionCallMethod getActionCallMethod( final RuleModel model,
final boolean isJavaDialect,
final Map<String, String> boundParams,
final PackageDataModelOracle dmo,
final String line,
final String variable,
final String methodName ) {

return new ActionCallMethodBuilder( model,
dmo,
isJavaDialect,
boundParams ).get( variable,
methodName,
unwrapParenthesis( line ).split( "," ) );
private IAction getActionCallMethod( final RuleModel model,
final boolean isJavaDialect,
final Map<String, String> boundParams,
final PackageDataModelOracle dmo,
final String line,
final String variable,
final String methodName ) {
final ActionCallMethodBuilder builder = new ActionCallMethodBuilder( model,
dmo,
isJavaDialect,
boundParams );
if ( !builder.supports( line ) ) {
final FreeFormLine ffl = new FreeFormLine();
ffl.setText( line );
return ffl;
}
return builder.get( variable,
methodName,
unwrapParenthesis( line ).split( "," ) );
}

private boolean isInsertedFact( final String[] lines,
Expand Down
Expand Up @@ -7314,6 +7314,254 @@ public void testLHSFormula() throws Exception {
RuleModelDRLPersistenceImpl.getInstance().marshal( m ) );
}

@Test
//https://bugzilla.redhat.com/show_bug.cgi?id=1127303
public void testRHSChainedMethodCalls1() throws Exception {
String drl = "package org.test;\n" +
"rule \"MyRule\"\n" +
"dialect \"mvel\"\n" +
"when\n" +
" Person( $n : name )\n" +
"then\n" +
" $n.toUpperCase().indexOf(\"S\", 1);\n" +
"end";

addModelField( "org.test.Person",
"this",
"org.test.Person",
DataType.TYPE_THIS );
addModelField( "org.test.Person",
"name",
String.class.getName(),
DataType.TYPE_STRING );

when( dmo.getPackageName() ).thenReturn( "org.test" );

final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal( drl,
new ArrayList<String>(),
dmo );

assertNotNull( m );

assertEquals( 1,
m.lhs.length );
final IPattern p0 = m.lhs[ 0 ];
assertTrue( p0 instanceof FactPattern );
final FactPattern fp0 = (FactPattern) p0;
assertEquals( "Person",
fp0.getFactType() );

assertEquals( 1,
fp0.getNumberOfConstraints() );
assertTrue( fp0.getConstraint( 0 ) instanceof SingleFieldConstraint );

final SingleFieldConstraint sfc1 = (SingleFieldConstraint) fp0.getConstraint( 0 );
assertEquals( "Person",
sfc1.getFactType() );
assertEquals( "name",
sfc1.getFieldName() );
assertEquals( DataType.TYPE_STRING,
sfc1.getFieldType() );

assertEquals( 1,
m.rhs.length );
final IAction a0 = m.rhs[ 0 ];
assertTrue( a0 instanceof FreeFormLine );
final FreeFormLine ffl1 = (FreeFormLine) a0;
assertEquals( "$n.toUpperCase().indexOf(\"S\", 1);",
ffl1.getText() );

//Check round-trip
assertEqualsIgnoreWhitespace( drl,
RuleModelDRLPersistenceImpl.getInstance().marshal( m ) );
}

@Test
//https://bugzilla.redhat.com/show_bug.cgi?id=1127303
public void testRHSChainedMethodCalls2() throws Exception {
String drl = "package org.test;\n" +
"rule \"MyRule\"\n" +
"dialect \"mvel\"\n" +
"when\n" +
" Person( $n : name )\n" +
"then\n" +
" $n.toUpperCase().indexOf(\".\", 1);\n" +
"end";

addModelField( "org.test.Person",
"this",
"org.test.Person",
DataType.TYPE_THIS );
addModelField( "org.test.Person",
"name",
String.class.getName(),
DataType.TYPE_STRING );

when( dmo.getPackageName() ).thenReturn( "org.test" );

final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal( drl,
new ArrayList<String>(),
dmo );

assertNotNull( m );

assertEquals( 1,
m.lhs.length );
final IPattern p0 = m.lhs[ 0 ];
assertTrue( p0 instanceof FactPattern );
final FactPattern fp0 = (FactPattern) p0;
assertEquals( "Person",
fp0.getFactType() );

assertEquals( 1,
fp0.getNumberOfConstraints() );
assertTrue( fp0.getConstraint( 0 ) instanceof SingleFieldConstraint );

final SingleFieldConstraint sfc1 = (SingleFieldConstraint) fp0.getConstraint( 0 );
assertEquals( "Person",
sfc1.getFactType() );
assertEquals( "name",
sfc1.getFieldName() );
assertEquals( DataType.TYPE_STRING,
sfc1.getFieldType() );

assertEquals( 1,
m.rhs.length );
final IAction a0 = m.rhs[ 0 ];
assertTrue( a0 instanceof FreeFormLine );
final FreeFormLine ffl1 = (FreeFormLine) a0;
assertEquals( "$n.toUpperCase().indexOf(\".\", 1);",
ffl1.getText() );

//Check round-trip
assertEqualsIgnoreWhitespace( drl,
RuleModelDRLPersistenceImpl.getInstance().marshal( m ) );
}

@Test
//https://bugzilla.redhat.com/show_bug.cgi?id=1127303
public void testRHSChainedMethodCalls3() throws Exception {
String drl = "package org.test;\n" +
"rule \"MyRule\"\n" +
"dialect \"mvel\"\n" +
"when\n" +
" Person( $n : name )\n" +
"then\n" +
" $n.toUpperCase().indexOf(\"(\", 1);\n" +
"end";

addModelField( "org.test.Person",
"this",
"org.test.Person",
DataType.TYPE_THIS );
addModelField( "org.test.Person",
"name",
String.class.getName(),
DataType.TYPE_STRING );

when( dmo.getPackageName() ).thenReturn( "org.test" );

final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal( drl,
new ArrayList<String>(),
dmo );

assertNotNull( m );

assertEquals( 1,
m.lhs.length );
final IPattern p0 = m.lhs[ 0 ];
assertTrue( p0 instanceof FactPattern );
final FactPattern fp0 = (FactPattern) p0;
assertEquals( "Person",
fp0.getFactType() );

assertEquals( 1,
fp0.getNumberOfConstraints() );
assertTrue( fp0.getConstraint( 0 ) instanceof SingleFieldConstraint );

final SingleFieldConstraint sfc1 = (SingleFieldConstraint) fp0.getConstraint( 0 );
assertEquals( "Person",
sfc1.getFactType() );
assertEquals( "name",
sfc1.getFieldName() );
assertEquals( DataType.TYPE_STRING,
sfc1.getFieldType() );

assertEquals( 1,
m.rhs.length );
final IAction a0 = m.rhs[ 0 ];
assertTrue( a0 instanceof FreeFormLine );
final FreeFormLine ffl1 = (FreeFormLine) a0;
assertEquals( "$n.toUpperCase().indexOf(\"(\", 1);",
ffl1.getText() );

//Check round-trip
assertEqualsIgnoreWhitespace( drl,
RuleModelDRLPersistenceImpl.getInstance().marshal( m ) );
}

@Test
//https://bugzilla.redhat.com/show_bug.cgi?id=1127303
public void testRHSChainedMethodCalls4() throws Exception {
String drl = "package org.test;\n" +
"rule \"MyRule\"\n" +
"dialect \"mvel\"\n" +
"when\n" +
" Person( $n : name )\n" +
"then\n" +
" $n.toUpperCase().indexOf(\"\\\").\", 1);\n" +
"end";

addModelField( "org.test.Person",
"this",
"org.test.Person",
DataType.TYPE_THIS );
addModelField( "org.test.Person",
"name",
String.class.getName(),
DataType.TYPE_STRING );

when( dmo.getPackageName() ).thenReturn( "org.test" );

final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal( drl,
new ArrayList<String>(),
dmo );

assertNotNull( m );

assertEquals( 1,
m.lhs.length );
final IPattern p0 = m.lhs[ 0 ];
assertTrue( p0 instanceof FactPattern );
final FactPattern fp0 = (FactPattern) p0;
assertEquals( "Person",
fp0.getFactType() );

assertEquals( 1,
fp0.getNumberOfConstraints() );
assertTrue( fp0.getConstraint( 0 ) instanceof SingleFieldConstraint );

final SingleFieldConstraint sfc1 = (SingleFieldConstraint) fp0.getConstraint( 0 );
assertEquals( "Person",
sfc1.getFactType() );
assertEquals( "name",
sfc1.getFieldName() );
assertEquals( DataType.TYPE_STRING,
sfc1.getFieldType() );

assertEquals( 1,
m.rhs.length );
final IAction a0 = m.rhs[ 0 ];
assertTrue( a0 instanceof FreeFormLine );
final FreeFormLine ffl1 = (FreeFormLine) a0;
assertEquals( "$n.toUpperCase().indexOf(\"\\\").\", 1);",
ffl1.getText() );

//Check round-trip
assertEqualsIgnoreWhitespace( drl,
RuleModelDRLPersistenceImpl.getInstance().marshal( m ) );
}

private void assertEqualsIgnoreWhitespace( final String expected,
final String actual ) {
final String cleanExpected = expected.replaceAll( "\\s+",
Expand Down

0 comments on commit 955d434

Please sign in to comment.