public void removeConstraint( final int idx ) { //If the constraint being is a parent of another correct the other constraint's parent accordingly FieldConstraint constraintToRemove = this.constraints[ idx ]; if ( constraintToRemove instanceof SingleFieldConstraint ) { final SingleFieldConstraint sfc = (SingleFieldConstraint) constraintToRemove; FieldConstraint parent = sfc.getParent(); for ( FieldConstraint child : this.constraints ) { if ( child instanceof SingleFieldConstraint ) { SingleFieldConstraint sfcChild = (SingleFieldConstraint) child; if ( sfcChild.getParent() == constraintToRemove ) { sfcChild.setParent( parent ); break; } } } } final FieldConstraint[] newList = new FieldConstraint[ this.constraints.length - 1 ]; int newIdx = 0; for ( int i = 0; i < this.constraints.length; i++ ) { if ( i != idx ) { newList[ newIdx ] = this.constraints[ i ]; newIdx++; } } this.constraints = newList; }
@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 expected = "rule \"test sub constraints\"" + "\tdialect \"mvel\"\n when " + " Person(field1.field2 == variableHere)" + " then " + "end"; checkMarshallUnmarshall( expected, m ); }
sfc3.setFactType( "Bar" ); sfc3.setFieldName( "barField" ); sfc3.setParent( sfc2 ); sfc3.setFieldType( DataType.TYPE_STRING ); sfc3.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL );
sfc3.setFactType( "Bar" ); sfc3.setFieldName( "barField" ); sfc3.setParent( sfc2 ); sfc3.setFieldType( DataType.TYPE_STRING ); sfc3.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL );
sfc3.setFactType( "Bar" ); sfc3.setFieldName( "barField" ); sfc3.setParent( sfc2 ); sfc3.setFieldType( DataType.TYPE_STRING ); sfc3.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL );
sfc1.setFactType( "Bar" ); sfc3.setFieldName( "barField" ); sfc3.setParent( sfc2 ); sfc3.setFieldType( DataType.TYPE_STRING ); sfc3.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL );