@Override FieldConstraint constructSingleFieldConstraint(ConditionCol52 c, List<DTCellValue52> columnData) { SingleFieldConstraint sfc = new SingleFieldConstraint( c.getFactField() ); sfc.setConstraintValueType( c.getConstraintValueType() ); sfc.setFieldType( c.getFieldType() ); sfc.setOperator( "not in" ); List<String> consumedValues = new ArrayList<String>(); StringBuilder value = new StringBuilder(); value.append( "( " ); for ( DTCellValue52 cv : columnData ) { //Ensure cell values start and end with quotes String scv = GuidedDTDRLUtilities.convertDTCellValueToString( cv ); if ( scv != null ) { if ( !consumedValues.contains( scv ) ) { value.append( scv ).append( ", " ); } consumedValues.add( scv ); } } value.delete( value.lastIndexOf( "," ), value.length() - 1 ); value.append( ")" ); sfc.setValue( value.toString() ); return sfc; }
@Override SingleFieldConstraint constructSingleFieldConstraint(ConditionCol52 c, List<DTCellValue52> columnData) { SingleFieldConstraint sfc = new SingleFieldConstraint( c.getFactField() ); sfc.setConstraintValueType( c.getConstraintValueType() ); sfc.setFieldType( c.getFieldType() ); sfc.setOperator( "in" ); List<String> consumedValues = new ArrayList<String>(); StringBuilder value = new StringBuilder(); value.append( "( " ); for ( DTCellValue52 cv : columnData ) { //Ensure cell values start and end with quotes String scv = GuidedDTDRLUtilities.convertDTCellValueToString( cv ); if ( scv != null ) { if ( !consumedValues.contains( scv ) ) { value.append( scv ).append( ", " ); } consumedValues.add( scv ); } } value.delete( value.lastIndexOf( "," ), value.length() - 1 ); value.append( ")" ); sfc.setValue( value.toString() ); return sfc; }
private SingleFieldConstraint visitSingleFieldConstraint(SingleFieldConstraint sfc) { SingleFieldConstraint clone = new SingleFieldConstraint(); clone.setConstraintValueType( sfc.getConstraintValueType() ); clone.setExpressionValue( (ExpressionFormLine) visit( sfc.getExpressionValue() ) ); clone.setFieldBinding( sfc.getFieldBinding() ); clone.setFactType( sfc.getFactType() ); clone.setFieldName( sfc.getFieldName() ); clone.setFieldType( sfc.getFieldType() ); clone.setOperator( sfc.getOperator() ); for ( Map.Entry<String, String> entry : sfc.getParameters().entrySet() ) { clone.setParameter( entry.getKey(), entry.getValue() ); } clone.setValue( sfc.getValue() ); if ( sfc.connectives != null ) { clone.connectives = new ConnectiveConstraint[sfc.connectives.length]; for ( int i = 0; i < sfc.connectives.length; i++ ) { clone.connectives[i] = (ConnectiveConstraint) visit( sfc.connectives[i] ); } } return clone; }
private SingleFieldConstraint visitSingleFieldConstraint(SingleFieldConstraint sfc) { SingleFieldConstraint clone = new SingleFieldConstraint(); clone.setConstraintValueType( sfc.getConstraintValueType() ); clone.setExpressionValue( (ExpressionFormLine) visit( sfc.getExpressionValue() ) ); clone.setFieldBinding( sfc.getFieldBinding() ); clone.setFactType( sfc.getFactType() ); clone.setFieldName( sfc.getFieldName() ); clone.setFieldType( sfc.getFieldType() ); clone.setOperator( sfc.getOperator() ); for ( Map.Entry<String, String> entry : sfc.getParameters().entrySet() ) { clone.setParameter( entry.getKey(), entry.getValue() ); } clone.setValue( sfc.getValue() ); if ( sfc.connectives != null ) { clone.connectives = new ConnectiveConstraint[sfc.connectives.length]; for ( int i = 0; i < sfc.connectives.length; i++ ) { clone.connectives[i] = (ConnectiveConstraint) visit( sfc.connectives[i] ); } } return clone; }
@Test public void testPredicateConstraint() { RuleModel m = new RuleModel(); m.name = "yeah"; FactPattern p = new FactPattern( "Goober" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setConstraintValueType( SingleFieldConstraint.TYPE_PREDICATE ); con.setValue( "field soundslike 'poo'" ); p.addConstraint( con ); m.addLhsItem( p ); String actual = BRDRLPersistence.getInstance().marshal( m ); // System.err.println(actual); String expected = "rule \"yeah\" " + "\tdialect \"mvel\"\n when " + "Goober( eval( field soundslike 'poo' ) )" + " then " + "end"; assertEqualsIgnoreWhitespace( expected, actual ); }
@Test public void testLiteralNoType() { RuleModel m = new RuleModel(); m.name = "test literal no type"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldName( "field1" ); con.setOperator( "==" ); con.setValue( "bananna" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL ); p.addConstraint( con ); SingleFieldConstraint con2 = new SingleFieldConstraint(); con2.setFieldName( "field2" ); con2.setOperator( "==" ); con2.setValue( "variableHere" ); con2.setConstraintValueType( SingleFieldConstraint.TYPE_VARIABLE ); p.addConstraint( con2 ); m.addLhsItem( p ); String result = BRDRLPersistence.getInstance().marshal( m ); assertEqualsIgnoreWhitespace( "rule \"test literal no type\"" + "\tdialect \"mvel\"\n when " + " Person(field1 == \"bananna\", field2 == variableHere)" + " then " + "end", result ); }
@Test public void testLiteralStrings() { RuleModel m = new RuleModel(); m.name = "test literal strings"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( SuggestionCompletionEngine.TYPE_STRING ); con.setFieldName( "field1" ); con.setOperator( "==" ); con.setValue( "goo" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL ); p.addConstraint( con ); SingleFieldConstraint con2 = new SingleFieldConstraint(); con2.setFieldName( "field2" ); con2.setOperator( "==" ); con2.setValue( "variableHere" ); con2.setConstraintValueType( SingleFieldConstraint.TYPE_VARIABLE ); p.addConstraint( con2 ); m.addLhsItem( p ); String result = BRDRLPersistence.getInstance().marshal( m ); assertEqualsIgnoreWhitespace( "rule \"test literal strings\"" + "\tdialect \"mvel\"\n when " + " Person(field1 == \"goo\", field2 == variableHere)" + " then " + "end", result ); }
@Test public void testLiteralNumerics() { RuleModel m = new RuleModel(); m.name = "test literal numerics"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( SuggestionCompletionEngine.TYPE_NUMERIC_INTEGER ); con.setFieldName( "field1" ); con.setOperator( "==" ); con.setValue( "44" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL ); p.addConstraint( con ); SingleFieldConstraint con2 = new SingleFieldConstraint(); con2.setFieldName( "field2" ); con2.setOperator( "==" ); con2.setValue( "variableHere" ); con2.setConstraintValueType( SingleFieldConstraint.TYPE_VARIABLE ); p.addConstraint( con2 ); m.addLhsItem( p ); String result = BRDRLPersistence.getInstance().marshal( m ); assertEqualsIgnoreWhitespace( "rule \"test literal numerics\"" + "\tdialect \"mvel\"\n when " + " Person(field1 == 44, field2 == variableHere)" + " then " + "end", result ); }
@Test public void testReturnValueConstraint() { RuleModel m = new RuleModel(); m.name = "yeah"; FactPattern p = new FactPattern( "Goober" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setConstraintValueType( SingleFieldConstraint.TYPE_RET_VALUE ); con.setValue( "someFunc(x)" ); con.setOperator( "==" ); con.setFieldName( "goo" ); p.addConstraint( con ); m.addLhsItem( p ); String actual = BRDRLPersistence.getInstance().marshal( m ); // System.err.println(actual); String expected = "rule \"yeah\" " + "\tdialect \"mvel\"\n when " + "Goober( goo == ( someFunc(x) ) )" + " then " + "end"; assertEqualsIgnoreWhitespace( expected, actual ); }
@Test public void testEnumNoType() { //A legacy "Guvnor" enums (i.e pick-list of underlying field data-type) String expected = "rule \"my rule\"\n\tdialect \"mvel\"\n\twhen\n\t\tCheese( type == \"CheeseType.CHEDDAR\" )\n" + "\tthen\n\t\tinsert( new Report() );\nend\n"; final RuleModel m = new RuleModel(); final FactPattern pat = new FactPattern( "Cheese" ); m.addLhsItem( pat ); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldName( "type" ); con.setOperator( "==" ); con.setValue( "CheeseType.CHEDDAR" ); con.setConstraintValueType( BaseSingleFieldConstraint.TYPE_ENUM ); pat.addConstraint( con ); m.addRhsItem( new ActionInsertFact( "Report" ) ); m.name = "my rule"; final String drl = brlPersistence.marshal( m ); assertEquals( expected, drl ); }
@Test public void testEnumTypeComparable() { //Java 1.5+ "true" enums are of type Comparable String expected = "rule \"my rule\"\n\tdialect \"mvel\"\n\twhen\n\t\tCheese( type == Cheese.CHEDDAR )\n" + "\tthen\n\t\tinsert( new Report() );\nend\n"; final RuleModel m = new RuleModel(); final FactPattern pat = new FactPattern( "Cheese" ); m.addLhsItem( pat ); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldName( "type" ); con.setOperator( "==" ); con.setValue( "Cheese.CHEDDAR" ); con.setFieldType( SuggestionCompletionEngine.TYPE_COMPARABLE ); con.setConstraintValueType( BaseSingleFieldConstraint.TYPE_ENUM ); pat.addConstraint( con ); m.addRhsItem( new ActionInsertFact( "Report" ) ); m.name = "my rule"; final String drl = brlPersistence.marshal( m ); assertEquals( expected, drl ); }
@Test public void testInOperatorNumber() { RuleModel m = new RuleModel(); m.name = "in"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( SuggestionCompletionEngine.TYPE_NUMERIC_INTEGER ); con.setFieldName( "field1" ); con.setOperator( "in" ); con.setValue( "55, 66" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL ); p.addConstraint( con ); m.addLhsItem( p ); String result = BRDRLPersistence.getInstance().marshal( m ); assertEqualsIgnoreWhitespace( "rule \"in\" \n" + "dialect \"mvel\" \n" + "when \n" + " Person(field1 in ( 55, 66 ) ) \n" + " then \n" + "end", result ); }
@Test public void testInOperatorString() { RuleModel m = new RuleModel(); m.name = "in"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( SuggestionCompletionEngine.TYPE_STRING ); con.setFieldName( "field1" ); con.setOperator( "in" ); con.setValue( "value1, value2" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL ); p.addConstraint( con ); m.addLhsItem( p ); String result = BRDRLPersistence.getInstance().marshal( m ); assertEqualsIgnoreWhitespace( "rule \"in\" \n" + "dialect \"mvel\" \n" + "when \n" + " Person(field1 in ( \"value1\", \"value2\" ) ) \n" + " then \n" + "end", result ); }
@Test public void testNotInOperatorString() { RuleModel m = new RuleModel(); m.name = "not in"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( SuggestionCompletionEngine.TYPE_STRING ); con.setFieldName( "field1" ); con.setOperator( "not in" ); con.setValue( "value1, value2" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL ); p.addConstraint( con ); m.addLhsItem( p ); String result = BRDRLPersistence.getInstance().marshal( m ); assertEqualsIgnoreWhitespace( "rule \"not in\" \n" + "dialect \"mvel\" \n" + "when \n" + " Person(field1 not in ( \"value1\", \"value2\" ) ) \n" + " then \n" + "end", result ); }
@Test public void testNotInOperatorNumber() { RuleModel m = new RuleModel(); m.name = "not in"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( SuggestionCompletionEngine.TYPE_NUMERIC_INTEGER ); con.setFieldName( "field1" ); con.setOperator( "not in" ); con.setValue( "55, 66" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL ); p.addConstraint( con ); m.addLhsItem( p ); String result = BRDRLPersistence.getInstance().marshal( m ); assertEqualsIgnoreWhitespace( "rule \"not in\" \n" + "dialect \"mvel\" \n" + "when \n" + " Person(field1 not in ( 55, 66 ) ) \n" + " then \n" + "end", result ); }
@Test public void testEnumTypeBoolean() { //A legacy "Guvnor" enums (i.e pick-list of underlying field data-type) String expected = "rule \"my rule\"\n\tdialect \"mvel\"\n\twhen\n\t\tCheese( smelly == true )\n" + "\tthen\n\t\tinsert( new Report() );\nend\n"; final RuleModel m = new RuleModel(); final FactPattern pat = new FactPattern( "Cheese" ); m.addLhsItem( pat ); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldName( "smelly" ); con.setOperator( "==" ); con.setValue( "true" ); con.setFieldType( SuggestionCompletionEngine.TYPE_BOOLEAN ); con.setConstraintValueType( BaseSingleFieldConstraint.TYPE_ENUM ); pat.addConstraint( con ); m.addRhsItem( new ActionInsertFact( "Report" ) ); m.name = "my rule"; final String drl = brlPersistence.marshal( m ); assertEquals( expected, drl ); }
@Test public void testEnumTypeString() { //A legacy "Guvnor" enums (i.e pick-list of underlying field data-type) String expected = "rule \"my rule\"\n\tdialect \"mvel\"\n\twhen\n\t\tCheese( type == \"CHEDDAR\" )\n" + "\tthen\n\t\tinsert( new Report() );\nend\n"; final RuleModel m = new RuleModel(); final FactPattern pat = new FactPattern( "Cheese" ); m.addLhsItem( pat ); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldName( "type" ); con.setOperator( "==" ); con.setValue( "CHEDDAR" ); con.setFieldType( SuggestionCompletionEngine.TYPE_STRING ); con.setConstraintValueType( BaseSingleFieldConstraint.TYPE_ENUM ); pat.addConstraint( con ); m.addRhsItem( new ActionInsertFact( "Report" ) ); m.name = "my rule"; final String drl = brlPersistence.marshal( m ); assertEquals( expected, drl ); }
@Test public void testEnumTypeNumeric() { //A legacy "Guvnor" enums (i.e pick-list of underlying field data-type) String expected = "rule \"my rule\"\n\tdialect \"mvel\"\n\twhen\n\t\tCheese( age == 100 )\n" + "\tthen\n\t\tinsert( new Report() );\nend\n"; final RuleModel m = new RuleModel(); final FactPattern pat = new FactPattern( "Cheese" ); m.addLhsItem( pat ); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldName( "age" ); con.setOperator( "==" ); con.setValue( "100" ); con.setFieldType( SuggestionCompletionEngine.TYPE_NUMERIC_INTEGER ); con.setConstraintValueType( BaseSingleFieldConstraint.TYPE_ENUM ); pat.addConstraint( con ); m.addRhsItem( new ActionInsertFact( "Report" ) ); m.name = "my rule"; final String drl = brlPersistence.marshal( m ); assertEquals( expected, drl ); }
@Test public void testEnumTypeDate() { //A legacy "Guvnor" enums (i.e pick-list of underlying field data-type) String expected = "rule \"my rule\"\n\tdialect \"mvel\"\n\twhen\n\t\tCheese( dateMade == \"31-Jan-2010\" )\n" + "\tthen\n\t\tinsert( new Report() );\nend\n"; final RuleModel m = new RuleModel(); final FactPattern pat = new FactPattern( "Cheese" ); m.addLhsItem( pat ); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldName( "dateMade" ); con.setOperator( "==" ); con.setValue( "31-Jan-2010" ); con.setFieldType( SuggestionCompletionEngine.TYPE_DATE ); con.setConstraintValueType( BaseSingleFieldConstraint.TYPE_ENUM ); pat.addConstraint( con ); m.addRhsItem( new ActionInsertFact( "Report" ) ); m.name = "my rule"; final String drl = brlPersistence.marshal( m ); assertEquals( expected, drl ); }
@Test public void testSubConstraints() { RuleModel m = new RuleModel(); m.name = "test sub constraints"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldName( "field1" ); p.addConstraint( con ); SingleFieldConstraint con2 = new SingleFieldConstraint(); con2.setFieldName( "field2" ); con2.setOperator( "==" ); con2.setValue( "variableHere" ); con2.setConstraintValueType( SingleFieldConstraint.TYPE_VARIABLE ); con2.setParent( con ); p.addConstraint( con2 ); m.addLhsItem( p ); String result = BRDRLPersistence.getInstance().marshal( m ); assertEqualsIgnoreWhitespace( "rule \"test sub constraints\"" + "\tdialect \"mvel\"\n when " + " Person(field1.field2 == variableHere)" + " then " + "end", result ); }