private void fixConstraints(FactPattern fp, SingleFieldConstraint sfc) { sfc.setFactType( fp.getFactType() ); sfc.setFieldName( fixFieldName( sfc.getFieldName() ) ); if ( sfc.connectives == null ) { return; } for ( ConnectiveConstraint cc : sfc.connectives ) { cc.setFactType( fp.getFactType() ); cc.setFieldName( fixFieldName( cc.getFieldName() ) ); } }
private void fixConstraints(FactPattern fp, SingleFieldConstraint sfc) { sfc.setFactType( fp.getFactType() ); sfc.setFieldName( fixFieldName( sfc.getFieldName() ) ); if ( sfc.connectives == null ) { return; } for ( ConnectiveConstraint cc : sfc.connectives ) { cc.setFactType( fp.getFactType() ); cc.setFieldName( fixFieldName( cc.getFieldName() ) ); } }
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 textIsNullOperator() { final RuleModel m = new RuleModel(); m.name = "IsNullOperator"; final FactPattern pat = new FactPattern( "Person" ); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldName( "age" ); con.setOperator( "== null" ); pat.addConstraint( con ); m.addLhsItem( pat ); String s = BRDRLPersistence.getInstance().marshal( m ); assertTrue( s.indexOf( "Person( age == null )" ) != -1 ); }
@Test public void textIsNotNullOperator() { final RuleModel m = new RuleModel(); m.name = "IsNotNullOperator"; final FactPattern pat = new FactPattern( "Person" ); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldName( "age" ); con.setOperator( "!= null" ); pat.addConstraint( con ); m.addLhsItem( pat ); String s = BRDRLPersistence.getInstance().marshal( m ); assertTrue( s.indexOf( "Person( age != null )" ) != -1 ); }
private RuleModel getModelWithNoConstraints() { final RuleModel m = new RuleModel(); m.name = "Complex Rule"; final FactPattern pat = new FactPattern( "Person" ); pat.setBoundName( "p1" ); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldBinding( "f1" ); con.setFieldName( "age" ); // con.operator = "<"; // con.value = "42"; pat.addConstraint( con ); m.addLhsItem( pat ); return m; }
@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 ); }
@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 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 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 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 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 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 ); }