public void onExpressionTypeChanged( ExpressionTypeChangeEvent event ) { try { //Change "operator" drop-down as the content depends on data-type constraint.setFieldType( event.getNewType() ); inner.setWidget( row, 1 + col, operatorDropDown( constraint, inner, row, 2 + col ) ); //Change "value" editor to the pen icon as the applicable Widget depends on data-type constraint.setConstraintValueType( SingleFieldConstraint.TYPE_UNDEFINED ); constraint.setValue( "" ); inner.setWidget( row, 2 + col, valueEditor( constraint ) ); } catch ( Exception e ) { e.printStackTrace(); } } } );
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.getConnectives() != null ) { clone.setConnectives( new ConnectiveConstraint[ sfc.getConnectives().length ] ); for ( int i = 0; i < sfc.getConnectives().length; i++ ) { clone.getConnectives()[ i ] = (ConnectiveConstraint) visit( sfc.getConnectives()[ i ] ); } } return clone; }
@Test public void testInOperatorNumber() { RuleModel m = new RuleModel(); m.name = "in"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( DataType.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 expected = "rule \"in\" \n" + "dialect \"mvel\" \n" + "when \n" + " Person(field1 in ( 55, 66 ) ) \n" + " then \n" + "end"; checkMarshallUnmarshall( expected, m ); }
@Test public void testInOperatorString() { RuleModel m = new RuleModel(); m.name = "in"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( DataType.TYPE_STRING ); con.setFieldName( "field1" ); con.setOperator( "in" ); con.setValue( "value1, value2" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL ); p.addConstraint( con ); m.addLhsItem( p ); String expected = "rule \"in\" \n" + "dialect \"mvel\" \n" + "when \n" + " Person(field1 in ( \"value1\", \"value2\" ) ) \n" + " then \n" + "end"; checkMarshallUnmarshall( expected, m ); }
@Test public void testNotInOperatorString() { RuleModel m = new RuleModel(); m.name = "not in"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( DataType.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 expected = "rule \"not in\" \n" + "dialect \"mvel\" \n" + "when \n" + " Person(field1 not in ( \"value1\", \"value2\" ) ) \n" + " then \n" + "end"; checkMarshallUnmarshall( expected, m ); }
@Test public void testNotInOperatorNumber() { RuleModel m = new RuleModel(); m.name = "not in"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( DataType.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 expected = "rule \"not in\" \n" + "dialect \"mvel\" \n" + "when \n" + " Person(field1 not in ( 55, 66 ) ) \n" + " then \n" + "end"; checkMarshallUnmarshall( expected, m ); }
@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( DataType.TYPE_STRING ); con.setConstraintValueType( BaseSingleFieldConstraint.TYPE_ENUM ); pat.addConstraint( con ); m.addRhsItem( new ActionInsertFact( "Report" ) ); m.name = "my rule"; checkMarshallUnmarshall( expected, m ); }
@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( DataType.TYPE_NUMERIC_INTEGER ); con.setConstraintValueType( BaseSingleFieldConstraint.TYPE_ENUM ); pat.addConstraint( con ); m.addRhsItem( new ActionInsertFact( "Report" ) ); m.name = "my rule"; checkMarshallUnmarshall( expected, m ); }
@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( DataType.TYPE_BOOLEAN ); con.setConstraintValueType( BaseSingleFieldConstraint.TYPE_ENUM ); pat.addConstraint( con ); m.addRhsItem( new ActionInsertFact( "Report" ) ); m.name = "my rule"; checkMarshallUnmarshall( expected, m ); }
@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( DataType.TYPE_DATE ); con.setConstraintValueType( BaseSingleFieldConstraint.TYPE_ENUM ); pat.addConstraint( con ); m.addRhsItem( new ActionInsertFact( "Report" ) ); m.name = "my rule"; checkMarshallUnmarshall( expected, m ); }
@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( DataType.TYPE_COMPARABLE ); con.setConstraintValueType( BaseSingleFieldConstraint.TYPE_ENUM ); pat.addConstraint( con ); m.addRhsItem( new ActionInsertFact( "Report" ) ); m.name = "my rule"; checkMarshallUnmarshall( expected, m ); }
@Test public void testEnumTypeStringInOperator() { //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 in ( \"CHEDDAR\", \"STILTON\" ) )\n" + "\tthen\n" + "\t\tinsert( new Report() );\n" + "end\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( "in" ); con.setValue( "( \"CHEDDAR\",\"STILTON\" )" ); con.setFieldType( DataType.TYPE_STRING ); con.setConstraintValueType( BaseSingleFieldConstraint.TYPE_ENUM ); pat.addConstraint( con ); m.addRhsItem( new ActionInsertFact( "Report" ) ); m.name = "my rule"; checkMarshallUnmarshall( expected, m ); }
@Test public void testLiteralNumerics() { RuleModel m = new RuleModel(); m.name = "test literal numerics"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( DataType.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 expected = "rule \"test literal numerics\"" + "\tdialect \"mvel\"\n when " + " Person(field1 == 44, field2 == variableHere)" + " then " + "end"; checkMarshallUnmarshall( expected, m ); }
@Test public void testLiteralBooleans() { RuleModel m = new RuleModel(); m.name = "test literal booleans"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( DataType.TYPE_BOOLEAN ); con.setFieldName( "field1" ); con.setOperator( "==" ); con.setValue( "true" ); 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 expected = "rule \"test literal booleans\"" + "\tdialect \"mvel\"\n when " + " Person(field1 == true, field2 == variableHere)" + " then " + "end"; checkMarshallUnmarshall( expected, m ); }
@Test public void testLiteralDates() { RuleModel m = new RuleModel(); m.name = "test literal dates"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( DataType.TYPE_DATE ); con.setFieldName( "field1" ); con.setOperator( "==" ); con.setValue( "31-Jan-2010" ); 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 expected = "rule \"test literal dates\"" + "\tdialect \"mvel\"\n when " + " Person(field1 == \"31-Jan-2010\", field2 == variableHere)" + " then " + "end"; checkMarshallUnmarshall( expected, m ); }
@Test public void testLiteralStrings() { RuleModel m = new RuleModel(); m.name = "test literal strings"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( DataType.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 expected = "rule \"test literal strings\"" + "\tdialect \"mvel\"\n when " + " Person(field1 == \"goo\", field2 == variableHere)" + " then " + "end"; checkMarshallUnmarshall( expected, m ); }
@Test public void testLiteralBigIntegerJava() { RuleModel m = new RuleModel(); m.name = "test literal biginteger"; m.addAttribute( new RuleAttribute( "dialect", "java" ) ); FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( DataType.TYPE_NUMERIC_BIGINTEGER ); con.setFieldName( "field1" ); con.setOperator( "==" ); con.setValue( "44" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL ); p.addConstraint( con ); m.addLhsItem( p ); ActionInsertFact ai = new ActionInsertFact( "Person" ); ai.addFieldValue( new ActionFieldValue( "field1", "55", DataType.TYPE_NUMERIC_BIGINTEGER ) ); m.addRhsItem( ai ); String expected = "rule \"test literal biginteger\" \n" + "\tdialect \"java\"\n when \n" + " Person(field1 == 44I ) \n" + " then \n" + "Person fact0 = new Person(); \n" + "fact0.setField1( new java.math.BigInteger( \"55\" ) ); \n" + "insert( fact0 ); \n" + "end"; checkMarshallUnmarshall( expected, m ); }
@Test public void testConnective() { RuleModel m = new RuleModel(); m.name = "test literal strings"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( DataType.TYPE_STRING ); con.setFieldName( "field1" ); con.setOperator( "==" ); con.setValue( "goo" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_VARIABLE ); p.addConstraint( con ); ConnectiveConstraint connective = new ConnectiveConstraint(); connective.setConstraintValueType( BaseSingleFieldConstraint.TYPE_LITERAL ); connective.setFieldType( DataType.TYPE_STRING ); connective.setOperator( "|| ==" ); connective.setValue( "blah" ); con.setConnectives( new ConnectiveConstraint[ 1 ] ); con.getConnectives()[ 0 ] = connective; m.addLhsItem( p ); String expected = "rule \"test literal strings\" " + "\tdialect \"mvel\"\n when " + "Person( field1 == goo || == \"blah\" )" + " then " + "end"; checkMarshallUnmarshall( expected, m ); }
@Test public void testLiteralBigDecimalMvel() { RuleModel m = new RuleModel(); m.name = "test literal bigdecimal"; m.addAttribute( new RuleAttribute( "dialect", "mvel" ) ); FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( DataType.TYPE_NUMERIC_BIGDECIMAL ); con.setFieldName( "field1" ); con.setOperator( "==" ); con.setValue( "44" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL ); p.addConstraint( con ); m.addLhsItem( p ); ActionInsertFact ai = new ActionInsertFact( "Person" ); ai.addFieldValue( new ActionFieldValue( "field1", "55", DataType.TYPE_NUMERIC_BIGDECIMAL ) ); m.addRhsItem( ai ); String expected = "rule \"test literal bigdecimal\" \n" + "\tdialect \"mvel\"\n when \n" + " Person(field1 == 44B) \n" + " then \n" + "Person fact0 = new Person(); \n" + "fact0.setField1( 55B ); \n" + "insert( fact0 ); \n" + "end"; checkMarshallUnmarshall( expected, m ); }
@Test public void testLiteralBigDecimalJava() { RuleModel m = new RuleModel(); m.name = "test literal bigdecimal"; m.addAttribute( new RuleAttribute( "dialect", "java" ) ); FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( DataType.TYPE_NUMERIC_BIGDECIMAL ); con.setFieldName( "field1" ); con.setOperator( "==" ); con.setValue( "44" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL ); p.addConstraint( con ); m.addLhsItem( p ); ActionInsertFact ai = new ActionInsertFact( "Person" ); ai.addFieldValue( new ActionFieldValue( "field1", "55", DataType.TYPE_NUMERIC_BIGDECIMAL ) ); m.addRhsItem( ai ); String expected = "rule \"test literal bigdecimal\" \n" + "\tdialect \"java\"\n when \n" + " Person(field1 == 44B) \n" + " then \n" + "Person fact0 = new Person(); \n" + "fact0.setField1( new java.math.BigDecimal( \"55\" ) ); \n" + "insert( fact0 ); \n" + "end"; checkMarshallUnmarshall( expected, m ); }