Skip to content

Commit

Permalink
GUVNOR-2030: Guided Decision Table: Empty field results in all Condit…
Browse files Browse the repository at this point in the history
…ions being cancelled
  • Loading branch information
manstis authored and mariofusco committed Feb 24, 2016
1 parent b271fc9 commit 0774811
Show file tree
Hide file tree
Showing 7 changed files with 452 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ private ActionFieldFunction getActionFieldFunction( String param,
break;
case FieldNatureType.TYPE_VARIABLE:
break;
case FieldNatureType.TYPE_TEMPLATE:
paramValue = unwrapTemplateKey( param );
break;
default:
paramValue = adjustParam( dataType,
param,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3223,6 +3223,9 @@ private ActionFieldValue buildFieldValue( final boolean isJavaDialect,
case FieldNatureType.TYPE_VARIABLE:
paramValue = "=" + paramValue;
break;
case FieldNatureType.TYPE_TEMPLATE:
paramValue = unwrapTemplateKey( value );
break;
default:
paramValue = adjustParam( dataType,
value,
Expand Down Expand Up @@ -3926,11 +3929,16 @@ private String setValueOnConstraint( final RuleModel m,
final BaseSingleFieldConstraint con,
String value ) {
String type = null;
if ( value.startsWith( "\"" ) ) {
if ( value.contains( "@{" ) ) {
con.setConstraintValueType( BaseSingleFieldConstraint.TYPE_TEMPLATE );
con.setValue( unwrapTemplateKey( value ) );

} else if ( value.startsWith( "\"" ) ) {
type = DataType.TYPE_STRING;
con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL );
con.setValue( value.substring( 1,
value.length() - 1 ) );

} else if ( value.startsWith( "(" ) ) {
if ( operator != null && operator.contains( "in" ) ) {
value = unwrapParenthesis( value );
Expand All @@ -3941,6 +3949,7 @@ private String setValueOnConstraint( final RuleModel m,
con.setConstraintValueType( SingleFieldConstraint.TYPE_RET_VALUE );
con.setValue( unwrapParenthesis( value ) );
}

} else {
if ( !Character.isDigit( value.charAt( 0 ) ) ) {
if ( value.equals( "true" ) || value.equals( "false" ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,27 @@ class RuleModelPersistenceHelper {
static String unwrapParenthesis( final String s ) {
int start = s.indexOf( '(' );
int end = s.lastIndexOf( ')' );
if ( start < 0 || end < 0 ) {
return s;
}
return s.substring( start + 1,
end ).trim();
}

static String unwrapTemplateKey( final String s ) {
int start = s.indexOf( "@{" );
if ( start < 0 ) {
return s;
}
int end = s.indexOf( "}",
start );
if ( end < 0 ) {
return s;
}
return s.substring( start + 2,
end ).trim();
}

static String getSimpleFactType( final String className,
final PackageDataModelOracle dmo ) {
for ( String type : dmo.getProjectModelFields().keySet() ) {
Expand All @@ -63,10 +80,12 @@ static int inferFieldNature( final String dataType,
final String value,
final Map<String, String> boundParams,
final boolean isJavaDialect ) {

if ( boundParams.containsKey( value ) ) {
return FieldNatureType.TYPE_VARIABLE;
}
if ( value.contains( "@{" ) ) {
return FieldNatureType.TYPE_TEMPLATE;
}

return inferFieldNature( dataType,
value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8217,4 +8217,131 @@ public void testRHSAppendToList() throws Exception {
RuleModelDRLPersistenceImpl.getInstance().marshal( m ) );
}

@Test
//https://issues.jboss.org/browse/GUVNOR-2030
public void testLHSTemplateKeys() throws Exception {
String drl = "package org.test;\n" +
"rule \"MyRule\"\n" +
"dialect \"mvel\"\n" +
"when\n" +
" Person( name == \"@{k1}\" )\n" +
"then\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( SingleFieldConstraint.TYPE_TEMPLATE,
sfc1.getConstraintValueType() );
assertEquals( "k1",
sfc1.getValue() );

assertEquals( 0,
m.rhs.length );
}

@Test
//https://issues.jboss.org/browse/GUVNOR-2030
public void testRHSTemplateKeys() throws Exception {
String drl = "package org.test;\n" +
"rule \"MyRule\"\n" +
"dialect \"mvel\"\n" +
"when\n" +
" $p : Person( name == \"Fred\" )\n" +
"then\n" +
" modify( $p ) { setName( \"@{k1}\" ) }\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( SingleFieldConstraint.TYPE_LITERAL,
sfc1.getConstraintValueType() );
assertEquals( "Fred",
sfc1.getValue() );

assertEquals( 1,
m.rhs.length );

assertTrue( m.rhs[ 0 ] instanceof ActionUpdateField );
ActionUpdateField auf = (ActionUpdateField) m.rhs[ 0 ];
assertEquals( "$p",
auf.getVariable() );
assertEquals( 1,
auf.getFieldValues().length );
ActionFieldValue afv = auf.getFieldValues()[ 0 ];
assertEquals( "name",
afv.getField() );
assertEquals( "k1",
afv.getValue() );
assertEquals( FieldNatureType.TYPE_TEMPLATE,
afv.getNature() );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint;
import org.drools.workbench.models.datamodel.rule.FactPattern;
import org.drools.workbench.models.datamodel.rule.FieldConstraint;
import org.drools.workbench.models.datamodel.rule.FreeFormLine;
import org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern;
import org.drools.workbench.models.datamodel.rule.IAction;
import org.drools.workbench.models.datamodel.rule.IPattern;
Expand Down Expand Up @@ -287,13 +288,20 @@ private void doAction( List<BaseColumn> allColumns,
} else if ( ivs.size() > 0 ) {

//Ensure every key has a value and substitute keys for values
int templateKeyCount = 0;
for ( InterpolationVariable variable : ivs.keySet() ) {
String value = rowDataProvider.getTemplateKeyValue( variable.getVarName() );
if ( !"".equals( value ) ) {
addAction = true;
break;
templateKeyCount++;
}
}

//Ensure at least one key has a value (FreeFormLines need all values to be provided)
if ( action instanceof FreeFormLine ) {
addAction = templateKeyCount == ivs.size();
} else if ( templateKeyCount > 0 ) {
addAction = true;
}
}

if ( addAction ) {
Expand All @@ -303,7 +311,6 @@ private void doAction( List<BaseColumn> allColumns,

}
}

}

private boolean hasVariables( BRLActionColumn column ) {
Expand Down Expand Up @@ -596,14 +603,20 @@ private void doCondition( List<BaseColumn> allColumns,
addPattern = true;
} else if ( ivs.size() > 0 ) {

//Ensure every key has a value and substitute keys for values
int templateKeyCount = 0;
for ( InterpolationVariable variable : ivs.keySet() ) {
String value = rowDataProvider.getTemplateKeyValue( variable.getVarName() );
if ( !"".equals( value ) ) {
addPattern = true;
break;
templateKeyCount++;
}
}

//Ensure at least one key has a value (FreeFormLines need all values to be provided)
if ( pattern instanceof FreeFormLine ) {
addPattern = templateKeyCount == ivs.size();
} else if ( templateKeyCount > 0 ) {
addPattern = true;
}
}

if ( addPattern ) {
Expand Down
Loading

0 comments on commit 0774811

Please sign in to comment.