private String setOperatorAndValueOnConstraint( String operator, String value, SingleFieldConstraint con ) { if ( operator != null ) { con.setOperator( operator ); boolean isAnd = value.contains( "&&" ); String[] splittedValue = isAnd ? value.split( "\\&\\&" ) : value.split( "\\|\\|" ); String type = setValueOnConstraint( operator, con, splittedValue[ 0 ].trim() ); if ( splittedValue.length > 1 ) { ConnectiveConstraint[] connectiveConstraints = new ConnectiveConstraint[ splittedValue.length - 1 ]; for ( int i = 0; i < connectiveConstraints.length; i++ ) { String constraint = splittedValue[ i + 1 ].trim(); String connectiveOperator = findOperator( constraint ); String connectiveValue = constraint.substring( connectiveOperator.length() ).trim(); connectiveConstraints[ i ] = new ConnectiveConstraint(); connectiveConstraints[ i ].setOperator( ( isAnd ? "&& " : "|| " ) + connectiveOperator ); setValueOnConstraint( operator, connectiveConstraints[ i ], connectiveValue ); } con.setConnectives( connectiveConstraints ); } return type; } return null; }
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 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 ); }
X.setValue( "foo" ); X.setOperator( "==" ); X.setConnectives( new ConnectiveConstraint[ 1 ] ); X.getConnectives()[ 0 ] = new ConnectiveConstraint(); X.getConnectives()[ 0 ].setConstraintValueType( ConnectiveConstraint.TYPE_LITERAL );