From 07748111bb51b482b3eb7ffa83e0010ad6d663c1 Mon Sep 17 00:00:00 2001 From: Michael Anstis Date: Wed, 10 Feb 2016 15:39:09 +0000 Subject: [PATCH] GUVNOR-2030: Guided Decision Table: Empty field results in all Conditions being cancelled --- .../backend/rule/ActionCallMethodBuilder.java | 3 + .../rule/RuleModelDRLPersistenceImpl.java | 11 +- .../rule/RuleModelPersistenceHelper.java | 21 ++- ...eModelDRLPersistenceUnmarshallingTest.java | 127 +++++++++++++ .../backend/GuidedDTDRLPersistence.java | 25 ++- .../dtable/backend/BRLRuleModelTest.java | 115 +++++++++++- .../backend/GuidedDTDRLPersistenceTest.java | 173 +++++++++++++++++- 7 files changed, 452 insertions(+), 23 deletions(-) diff --git a/drools-workbench-models/drools-workbench-models-commons/src/main/java/org/drools/workbench/models/commons/backend/rule/ActionCallMethodBuilder.java b/drools-workbench-models/drools-workbench-models-commons/src/main/java/org/drools/workbench/models/commons/backend/rule/ActionCallMethodBuilder.java index c984931e024..bd77431b9ba 100644 --- a/drools-workbench-models/drools-workbench-models-commons/src/main/java/org/drools/workbench/models/commons/backend/rule/ActionCallMethodBuilder.java +++ b/drools-workbench-models/drools-workbench-models-commons/src/main/java/org/drools/workbench/models/commons/backend/rule/ActionCallMethodBuilder.java @@ -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, diff --git a/drools-workbench-models/drools-workbench-models-commons/src/main/java/org/drools/workbench/models/commons/backend/rule/RuleModelDRLPersistenceImpl.java b/drools-workbench-models/drools-workbench-models-commons/src/main/java/org/drools/workbench/models/commons/backend/rule/RuleModelDRLPersistenceImpl.java index 071929ad062..b7079cadba6 100644 --- a/drools-workbench-models/drools-workbench-models-commons/src/main/java/org/drools/workbench/models/commons/backend/rule/RuleModelDRLPersistenceImpl.java +++ b/drools-workbench-models/drools-workbench-models-commons/src/main/java/org/drools/workbench/models/commons/backend/rule/RuleModelDRLPersistenceImpl.java @@ -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, @@ -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 ); @@ -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" ) ) { diff --git a/drools-workbench-models/drools-workbench-models-commons/src/main/java/org/drools/workbench/models/commons/backend/rule/RuleModelPersistenceHelper.java b/drools-workbench-models/drools-workbench-models-commons/src/main/java/org/drools/workbench/models/commons/backend/rule/RuleModelPersistenceHelper.java index 521fc906da7..3465a4e38dd 100644 --- a/drools-workbench-models/drools-workbench-models-commons/src/main/java/org/drools/workbench/models/commons/backend/rule/RuleModelPersistenceHelper.java +++ b/drools-workbench-models/drools-workbench-models-commons/src/main/java/org/drools/workbench/models/commons/backend/rule/RuleModelPersistenceHelper.java @@ -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() ) { @@ -63,10 +80,12 @@ static int inferFieldNature( final String dataType, final String value, final Map boundParams, final boolean isJavaDialect ) { - if ( boundParams.containsKey( value ) ) { return FieldNatureType.TYPE_VARIABLE; } + if ( value.contains( "@{" ) ) { + return FieldNatureType.TYPE_TEMPLATE; + } return inferFieldNature( dataType, value, diff --git a/drools-workbench-models/drools-workbench-models-commons/src/test/java/org/drools/workbench/models/commons/backend/rule/RuleModelDRLPersistenceUnmarshallingTest.java b/drools-workbench-models/drools-workbench-models-commons/src/test/java/org/drools/workbench/models/commons/backend/rule/RuleModelDRLPersistenceUnmarshallingTest.java index fa6efb038a8..09b3f6d42fc 100644 --- a/drools-workbench-models/drools-workbench-models-commons/src/test/java/org/drools/workbench/models/commons/backend/rule/RuleModelDRLPersistenceUnmarshallingTest.java +++ b/drools-workbench-models/drools-workbench-models-commons/src/test/java/org/drools/workbench/models/commons/backend/rule/RuleModelDRLPersistenceUnmarshallingTest.java @@ -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(), + 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(), + 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() ); + } + } diff --git a/drools-workbench-models/drools-workbench-models-guided-dtable/src/main/java/org/drools/workbench/models/guided/dtable/backend/GuidedDTDRLPersistence.java b/drools-workbench-models/drools-workbench-models-guided-dtable/src/main/java/org/drools/workbench/models/guided/dtable/backend/GuidedDTDRLPersistence.java index 74bfe8bc995..f6046979d8b 100644 --- a/drools-workbench-models/drools-workbench-models-guided-dtable/src/main/java/org/drools/workbench/models/guided/dtable/backend/GuidedDTDRLPersistence.java +++ b/drools-workbench-models/drools-workbench-models-guided-dtable/src/main/java/org/drools/workbench/models/guided/dtable/backend/GuidedDTDRLPersistence.java @@ -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; @@ -287,13 +288,20 @@ private void doAction( List 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 ) { @@ -303,7 +311,6 @@ private void doAction( List allColumns, } } - } private boolean hasVariables( BRLActionColumn column ) { @@ -596,14 +603,20 @@ private void doCondition( List 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 ) { diff --git a/drools-workbench-models/drools-workbench-models-guided-dtable/src/test/java/org/drools/workbench/models/guided/dtable/backend/BRLRuleModelTest.java b/drools-workbench-models/drools-workbench-models-guided-dtable/src/test/java/org/drools/workbench/models/guided/dtable/backend/BRLRuleModelTest.java index b4702427716..5b1cca63552 100644 --- a/drools-workbench-models/drools-workbench-models-guided-dtable/src/test/java/org/drools/workbench/models/guided/dtable/backend/BRLRuleModelTest.java +++ b/drools-workbench-models/drools-workbench-models-guided-dtable/src/test/java/org/drools/workbench/models/guided/dtable/backend/BRLRuleModelTest.java @@ -22,6 +22,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.SingleFieldConstraint; import org.drools.workbench.models.guided.dtable.backend.util.DataUtilities; import org.drools.workbench.models.guided.dtable.shared.model.ActionInsertFactCol52; @@ -321,7 +322,6 @@ public void testDecisionTableColumnsWithRHS() { assertNull( raif2.getFieldValues()[ 0 ].getValue() ); assertEquals( BaseSingleFieldConstraint.TYPE_LITERAL, raif2.getFieldValues()[ 0 ].getNature() ); - } @Test @@ -416,7 +416,6 @@ public void testDecisionTableColumnsWithRHSBoundFacts() { assertNull( raif3.getFieldValues()[ 0 ].getValue() ); assertEquals( BaseSingleFieldConstraint.TYPE_LITERAL, raif3.getFieldValues()[ 0 ].getNature() ); - } @Test @@ -736,6 +735,7 @@ public void testUpdateModifyMultipleFields() { " setName( \"Fred\" )\n" + "}\n" + "end\n"; + assertEqualsIgnoreWhitespace( expected1, drl ); @@ -754,6 +754,7 @@ public void testUpdateModifyMultipleFields() { " setName( \"Fred\" )\n" + "}\n" + "end\n"; + assertEqualsIgnoreWhitespace( expected2, drl ); @@ -772,6 +773,7 @@ public void testUpdateModifyMultipleFields() { " setAge( 55 ) \n" + "}\n" + "end\n"; + assertEqualsIgnoreWhitespace( expected3, drl ); } @@ -844,6 +846,7 @@ public void testUpdateModifyMultipleFieldsWithMultipleSkipped1() { " setF3( \"v3\" )\n" + "}\n" + "end\n"; + assertEqualsIgnoreWhitespace( expected, drl ); } @@ -915,6 +918,7 @@ public void testUpdateModifyMultipleFieldsWithMultipleSkipped2() { " setF3( \"v3\" )\n" + "}\n" + "end\n"; + assertEqualsIgnoreWhitespace( expected, drl ); } @@ -985,6 +989,7 @@ public void testUpdateModifyMultipleFieldsWithMultipleSkipped3() { " setF3( \"v3\" )\n" + "}\n" + "end\n"; + assertEqualsIgnoreWhitespace( expected, drl ); } @@ -1056,6 +1061,7 @@ public void testUpdateModifyMultipleFieldsWithMultipleSkipped4() { " setF3( \"v3\" )\n" + "}\n" + "end\n"; + assertEqualsIgnoreWhitespace( expected, drl ); } @@ -1105,7 +1111,7 @@ public void testLHSNonEmptyStringValues() { //Test 1 dt.setData( DataUtilities.makeDataLists( new Object[][]{ - new Object[]{ 1l, "desc-row1", null, null }, + new Object[]{ 1l, "desc-row1", "Pupa", null }, } ) ); String drl1 = p.marshal( dt ); @@ -1114,6 +1120,7 @@ public void testLHSNonEmptyStringValues() { "rule \"Row 1 extended-entry\"\n" + " dialect \"mvel\"\n" + " when\n" + + " p1 : Smurf( name == \"Pupa\" )\n" + " then\n" + "end"; @@ -1122,7 +1129,7 @@ public void testLHSNonEmptyStringValues() { //Test 2 dt.setData( DataUtilities.makeDataLists( new Object[][]{ - new Object[]{ 2l, "desc-row2", " ", 35l }, + new Object[]{ 2l, "desc-row2", null, 35l }, } ) ); String drl2 = p.marshal( dt ); @@ -1140,7 +1147,7 @@ public void testLHSNonEmptyStringValues() { //Test 3 dt.setData( DataUtilities.makeDataLists( new Object[][]{ - new Object[]{ 3l, "desc-row3", "", null }, + new Object[]{ 3l, "desc-row3", "Pupa", 35l }, } ) ); String drl3 = p.marshal( dt ); @@ -1149,6 +1156,7 @@ public void testLHSNonEmptyStringValues() { "rule \"Row 3 extended-entry\"\n" + " dialect \"mvel\"\n" + " when\n" + + " p1 : Smurf( name == \"Pupa\", age == 35 )\n" + " then\n" + "end"; @@ -1157,7 +1165,7 @@ public void testLHSNonEmptyStringValues() { //Test 4 dt.setData( DataUtilities.makeDataLists( new Object[][]{ - new Object[]{ 4l, "desc-row4", "", 35l }, + new Object[]{ 4l, "desc-row4", null, null }, } ) ); String drl4 = p.marshal( dt ); @@ -1166,7 +1174,6 @@ public void testLHSNonEmptyStringValues() { "rule \"Row 4 extended-entry\"\n" + " dialect \"mvel\"\n" + " when\n" + - " p1 : Smurf( age == 35 )\n" + " then\n" + "end"; @@ -1289,6 +1296,100 @@ public void testLHSDelimitedNonEmptyStringValues() { drl4 ); } + @Test + public void testLHSNonEmptyStringValuesFreeFormLine() { + GuidedDecisionTable52 dt = new GuidedDecisionTable52(); + dt.setTableFormat( GuidedDecisionTable52.TableFormat.EXTENDED_ENTRY ); + dt.setTableName( "extended-entry" ); + + BRLConditionColumn brlCondition = new BRLConditionColumn(); + FreeFormLine ffl = new FreeFormLine(); + ffl.setText( "p1 : Smurf( name ==\"@{$f1}\", age == @{$f2} )" ); + + brlCondition.getDefinition().add( ffl ); + brlCondition.getChildColumns().add( new BRLConditionVariableColumn( "$f1", + DataType.TYPE_STRING, + "Smurf", + "name" ) ); + brlCondition.getChildColumns().add( new BRLConditionVariableColumn( "$f2", + DataType.TYPE_NUMERIC_INTEGER, + "Smurf", + "age" ) ); + + dt.getConditions().add( brlCondition ); + + GuidedDTDRLPersistence p = GuidedDTDRLPersistence.getInstance(); + + //Test 1 + dt.setData( DataUtilities.makeDataLists( new Object[][]{ + new Object[]{ 1l, "desc-row1", "Pupa", null }, + } ) ); + + String drl1 = p.marshal( dt ); + final String expected1 = "//from row number: 1\n" + + "//desc-row1\n" + + "rule \"Row 1 extended-entry\"\n" + + " dialect \"mvel\"\n" + + " when\n" + + " then\n" + + "end"; + + assertEqualsIgnoreWhitespace( expected1, + drl1 ); + + //Test 2 + dt.setData( DataUtilities.makeDataLists( new Object[][]{ + new Object[]{ 2l, "desc-row2", null, 35l }, + } ) ); + + String drl2 = p.marshal( dt ); + final String expected2 = "//from row number: 1\n" + + "//desc-row2\n" + + "rule \"Row 2 extended-entry\"\n" + + " dialect \"mvel\"\n" + + " when\n" + + " then\n" + + "end"; + + assertEqualsIgnoreWhitespace( expected2, + drl2 ); + + //Test 3 + dt.setData( DataUtilities.makeDataLists( new Object[][]{ + new Object[]{ 3l, "desc-row3", "Pupa", 35l }, + } ) ); + + String drl3 = p.marshal( dt ); + final String expected3 = "//from row number: 1\n" + + "//desc-row3\n" + + "rule \"Row 3 extended-entry\"\n" + + " dialect \"mvel\"\n" + + " when\n" + + " p1 : Smurf( name == \"Pupa\", age == 35 )\n" + + " then\n" + + "end"; + + assertEqualsIgnoreWhitespace( expected3, + drl3 ); + + //Test 4 + dt.setData( DataUtilities.makeDataLists( new Object[][]{ + new Object[]{ 4l, "desc-row4", null, null }, + } ) ); + + String drl4 = p.marshal( dt ); + final String expected4 = "//from row number: 1\n" + + "//desc-row4\n" + + "rule \"Row 4 extended-entry\"\n" + + " dialect \"mvel\"\n" + + " when\n" + + " then\n" + + "end"; + + assertEqualsIgnoreWhitespace( expected4, + drl4 ); + } + @Test public void testRHSNonEmptyStringValues() { GuidedDecisionTable52 dt = new GuidedDecisionTable52(); diff --git a/drools-workbench-models/drools-workbench-models-guided-dtable/src/test/java/org/drools/workbench/models/guided/dtable/backend/GuidedDTDRLPersistenceTest.java b/drools-workbench-models/drools-workbench-models-guided-dtable/src/test/java/org/drools/workbench/models/guided/dtable/backend/GuidedDTDRLPersistenceTest.java index 635d9d971f8..3df0316c6ee 100644 --- a/drools-workbench-models/drools-workbench-models-guided-dtable/src/test/java/org/drools/workbench/models/guided/dtable/backend/GuidedDTDRLPersistenceTest.java +++ b/drools-workbench-models/drools-workbench-models-guided-dtable/src/test/java/org/drools/workbench/models/guided/dtable/backend/GuidedDTDRLPersistenceTest.java @@ -30,6 +30,7 @@ import org.drools.workbench.models.datamodel.rule.ActionWorkItemFieldValue; import org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint; import org.drools.workbench.models.datamodel.rule.FactPattern; +import org.drools.workbench.models.datamodel.rule.FreeFormLine; import org.drools.workbench.models.datamodel.rule.IAction; import org.drools.workbench.models.datamodel.rule.IPattern; import org.drools.workbench.models.datamodel.rule.RuleAttribute; @@ -3669,9 +3670,9 @@ public void testLHSWithBRLColumn_ParseToRuleModel() { // examine the second pattern FactPattern result1Fp2 = (FactPattern) rm.lhs[ 1 ]; assertEquals( 2, - result0Fp2.getConstraintList().getConstraints().length ); + result1Fp2.getConstraintList().getConstraints().length ); - SingleFieldConstraint result1Fp2Con1 = (SingleFieldConstraint) result0Fp2.getConstraint( 0 ); + SingleFieldConstraint result1Fp2Con1 = (SingleFieldConstraint) result1Fp2.getConstraint( 0 ); assertEquals( BaseSingleFieldConstraint.TYPE_TEMPLATE, result1Fp2Con1.getConstraintValueType() ); assertEquals( "name", @@ -3681,7 +3682,7 @@ public void testLHSWithBRLColumn_ParseToRuleModel() { assertEquals( "$name", result1Fp2Con1.getValue() ); - SingleFieldConstraint result1Fp2Con2 = (SingleFieldConstraint) result0Fp2.getConstraint( 1 ); + SingleFieldConstraint result1Fp2Con2 = (SingleFieldConstraint) result1Fp2.getConstraint( 1 ); assertEquals( BaseSingleFieldConstraint.TYPE_TEMPLATE, result1Fp2Con2.getConstraintValueType() ); assertEquals( "age", @@ -3725,9 +3726,9 @@ public void testLHSWithBRLColumn_ParseToRuleModel() { // examine the second pattern FactPattern result2Fp2 = (FactPattern) rm.lhs[ 1 ]; assertEquals( 2, - result0Fp2.getConstraintList().getConstraints().length ); + result2Fp2.getConstraintList().getConstraints().length ); - SingleFieldConstraint result2Fp2Con1 = (SingleFieldConstraint) result0Fp2.getConstraint( 0 ); + SingleFieldConstraint result2Fp2Con1 = (SingleFieldConstraint) result2Fp2.getConstraint( 0 ); assertEquals( BaseSingleFieldConstraint.TYPE_TEMPLATE, result2Fp2Con1.getConstraintValueType() ); assertEquals( "name", @@ -3737,7 +3738,7 @@ public void testLHSWithBRLColumn_ParseToRuleModel() { assertEquals( "$name", result2Fp2Con1.getValue() ); - SingleFieldConstraint result2Fp2Con2 = (SingleFieldConstraint) result0Fp2.getConstraint( 1 ); + SingleFieldConstraint result2Fp2Con2 = (SingleFieldConstraint) result2Fp2.getConstraint( 1 ); assertEquals( BaseSingleFieldConstraint.TYPE_TEMPLATE, result2Fp2Con2.getConstraintValueType() ); assertEquals( "age", @@ -3981,7 +3982,6 @@ public void testLHSWithBRLColumn_ParseToDRL_NoVariables() { //Row 0 should become an IPattern in the resulting RuleModel as it contains getValue()s for all Template fields in the BRL Column //Row 1 should *NOT* become an IPattern in the resulting RuleModel as it does *NOT* contain getValue()s for all Template fields in the BRL Column - //Row 2 should *NOT* become an IPattern in the resulting RuleModel as it does *NOT* contain getValue()s for all Template fields in the BRL Column Object[][] data = new Object[][]{ new Object[]{ "1", "desc", Boolean.TRUE }, new Object[]{ "2", "desc", Boolean.FALSE } @@ -4040,6 +4040,85 @@ public void testLHSWithBRLColumn_ParseToDRL_NoVariables() { } + @Test + //This test checks a Decision Table involving BRL columns is correctly converted into DRL + public void testLHSWithBRLColumn_ParseToDRL_FreeFormLine() { + + GuidedDecisionTable52 dtable = new GuidedDecisionTable52(); + + //Row 0 should become an IPattern in the resulting RuleModel as it contains values for all Template fields in the BRL Column + //Row 1 should *NOT* become an IPattern in the resulting RuleModel as it does *NOT* contain values for all Template fields in the BRL Column + //Row 2 should *NOT* become an IPattern in the resulting RuleModel as it does *NOT* contain values for all Template fields in the BRL Column + //Row 3 should *NOT* become an IPattern in the resulting RuleModel as it does *NOT* contain values for all Template fields in the BRL Column + String[][] data = new String[][]{ + new String[]{ "1", "desc", "Pupa", "50" }, + new String[]{ "2", "desc", "", "50" }, + new String[]{ "3", "desc", "Pupa", "" }, + new String[]{ "4", "desc", "", "" } + }; + + //Simple (mandatory) columns + dtable.setRowNumberCol( new RowNumberCol52() ); + dtable.setDescriptionCol( new DescriptionCol52() ); + + //BRL Column + BRLConditionColumn brl1 = new BRLConditionColumn(); + + //BRL Column definition + List brl1Definition = new ArrayList(); + FreeFormLine brl1DefinitionFreeFormLine = new FreeFormLine(); + brl1DefinitionFreeFormLine.setText( "Smurf( name == \"@{name}\", age == @{age} )" ); + + brl1Definition.add( brl1DefinitionFreeFormLine ); + + brl1.setDefinition( brl1Definition ); + + //Setup BRL column bindings + BRLConditionVariableColumn brl1Variable1 = new BRLConditionVariableColumn( "name", + DataType.TYPE_STRING ); + BRLConditionVariableColumn brl1Variable2 = new BRLConditionVariableColumn( "age", + DataType.TYPE_NUMERIC_INTEGER ); + brl1.getChildColumns().add( brl1Variable1 ); + brl1.getChildColumns().add( brl1Variable2 ); + + dtable.getConditions().add( brl1 ); + dtable.setData( DataUtilities.makeDataLists( data ) ); + + //Now to test conversion + int ruleStartIndex; + int pattern1StartIndex; + GuidedDTDRLPersistence p = GuidedDTDRLPersistence.getInstance(); + String drl = p.marshal( dtable ); + + //Row 0 + ruleStartIndex = drl.indexOf( "//from row number: 1" ); + assertFalse( ruleStartIndex == -1 ); + pattern1StartIndex = drl.indexOf( "Smurf( name == \"Pupa\", age == 50 )", + ruleStartIndex ); + assertFalse( pattern1StartIndex == -1 ); + + //Row 1 + ruleStartIndex = drl.indexOf( "//from row number: 2" ); + assertFalse( ruleStartIndex == -1 ); + pattern1StartIndex = drl.indexOf( "Smurf(", + ruleStartIndex ); + assertTrue( pattern1StartIndex == -1 ); + + //Row 2 + ruleStartIndex = drl.indexOf( "//from row number: 3" ); + assertFalse( ruleStartIndex == -1 ); + pattern1StartIndex = drl.indexOf( "Smurf(", + ruleStartIndex ); + assertTrue( pattern1StartIndex == -1 ); + + //Row 3 + ruleStartIndex = drl.indexOf( "//from row number: 4" ); + assertFalse( ruleStartIndex == -1 ); + pattern1StartIndex = drl.indexOf( "Smurf(", + ruleStartIndex ); + assertTrue( pattern1StartIndex == -1 ); + } + @Test //This test checks a Decision Table involving BRL columns is correctly converted into a RuleModel public void testRHSWithBRLColumn_ParseToRuleModel() { @@ -4258,7 +4337,6 @@ public void testRHSWithBRLColumn_ParseToRuleModel() { result3Action2FieldValue2.getField() ); assertEquals( "$age", result3Action2FieldValue2.getValue() ); - } @Test @@ -4626,6 +4704,85 @@ public void testRHSWithBRLColumn_ParseToDRL_NoVariables() { assertTrue( action1StartIndex == -1 ); } + @Test + //This test checks a Decision Table involving BRL columns is correctly converted into DRL + public void testRHSWithBRLColumn_ParseToDRL_FreeFormLine() { + + GuidedDecisionTable52 dtable = new GuidedDecisionTable52(); + + //Row 0 should become an IAction in the resulting RuleModel as it contains values for all Template fields in the BRL Column + //Row 1 should *NOT* become an IAction in the resulting RuleModel as it does *NOT* contain values for all Template fields in the BRL Column + //Row 2 should *NOT* become an IAction in the resulting RuleModel as it does *NOT* contain values for all Template fields in the BRL Column + //Row 3 should *NOT* become an IAction in the resulting RuleModel as it does *NOT* contain values for all Template fields in the BRL Column + String[][] data = new String[][]{ + new String[]{ "1", "desc", "Pupa", "50" }, + new String[]{ "2", "desc", "", "50" }, + new String[]{ "3", "desc", "Pupa", "" }, + new String[]{ "4", "desc", "", "" } + }; + + //Simple (mandatory) columns + dtable.setRowNumberCol( new RowNumberCol52() ); + dtable.setDescriptionCol( new DescriptionCol52() ); + + //BRL Action + BRLActionColumn brl1 = new BRLActionColumn(); + + //BRL Action definition + List brl1Definition = new ArrayList(); + FreeFormLine brl1DefinitionFreeFormLine = new FreeFormLine(); + brl1DefinitionFreeFormLine.setText( "System.out.println( \"name == @{name}, age == @{age}\" );" ); + + brl1Definition.add( brl1DefinitionFreeFormLine ); + + brl1.setDefinition( brl1Definition ); + + //Setup BRL column bindings + BRLActionVariableColumn brl1Variable1 = new BRLActionVariableColumn( "name", + DataType.TYPE_STRING ); + BRLActionVariableColumn brl1Variable2 = new BRLActionVariableColumn( "age", + DataType.TYPE_NUMERIC_INTEGER ); + brl1.getChildColumns().add( brl1Variable1 ); + brl1.getChildColumns().add( brl1Variable2 ); + + dtable.getActionCols().add( brl1 ); + dtable.setData( DataUtilities.makeDataLists( data ) ); + + //Now to test conversion + int ruleStartIndex; + int pattern1StartIndex; + GuidedDTDRLPersistence p = GuidedDTDRLPersistence.getInstance(); + String drl = p.marshal( dtable ); + + //Row 0 + ruleStartIndex = drl.indexOf( "//from row number: 1" ); + assertFalse( ruleStartIndex == -1 ); + pattern1StartIndex = drl.indexOf( "System.out.println( \"name == Pupa, age == 50\" );", + ruleStartIndex ); + assertFalse( pattern1StartIndex == -1 ); + + //Row 1 + ruleStartIndex = drl.indexOf( "//from row number: 2" ); + assertFalse( ruleStartIndex == -1 ); + pattern1StartIndex = drl.indexOf( "System.out.println(", + ruleStartIndex ); + assertTrue( pattern1StartIndex == -1 ); + + //Row 2 + ruleStartIndex = drl.indexOf( "//from row number: 3" ); + assertFalse( ruleStartIndex == -1 ); + pattern1StartIndex = drl.indexOf( "System.out.println(", + ruleStartIndex ); + assertTrue( pattern1StartIndex == -1 ); + + //Row 3 + ruleStartIndex = drl.indexOf( "//from row number: 4" ); + assertFalse( ruleStartIndex == -1 ); + pattern1StartIndex = drl.indexOf( "System.out.println(", + ruleStartIndex ); + assertTrue( pattern1StartIndex == -1 ); + } + @Test public void testPackageNameAndImports() throws Exception { GuidedDecisionTable52 dt = new GuidedDecisionTable52();