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; }
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 testRemoveConstraintWithNullParent() { final FactPattern fp = new FactPattern(); final SingleFieldConstraint con1 = new SingleFieldConstraint( "parent" ); fp.addConstraint( con1 ); final SingleFieldConstraint con2 = new SingleFieldConstraint(); con2.setParent( con1 ); fp.addConstraint( con2 ); final SingleFieldConstraint con3 = new SingleFieldConstraint(); con3.setParent( con2 ); fp.addConstraint( con3 ); assertEquals( 3, fp.constraintList.constraints.length ); fp.removeConstraint( 0 ); assertEquals( 2, fp.constraintList.constraints.length ); assertEquals( con2, fp.constraintList.constraints[0] ); assertEquals( con3, fp.constraintList.constraints[1] ); assertNull( ((SingleFieldConstraint) fp.constraintList.constraints[0]).getParent() ); assertEquals( con2, ((SingleFieldConstraint) fp.constraintList.constraints[1]).getParent() ); }
@Test public void testRemoveConstraintWithNonNullParent() { final FactPattern fp = new FactPattern(); final SingleFieldConstraint con1 = new SingleFieldConstraint( "parent" ); fp.addConstraint( con1 ); final SingleFieldConstraint con2 = new SingleFieldConstraint(); con2.setParent( con1 ); fp.addConstraint( con2 ); final SingleFieldConstraint con3 = new SingleFieldConstraint(); con3.setParent( con2 ); fp.addConstraint( con3 ); assertEquals( 3, fp.constraintList.constraints.length ); fp.removeConstraint( 1 ); assertEquals( 2, fp.constraintList.constraints.length ); assertEquals( con1, fp.constraintList.constraints[0] ); assertEquals( con3, fp.constraintList.constraints[1] ); assertNull( ((SingleFieldConstraint) fp.constraintList.constraints[0]).getParent() ); assertEquals( con1, ((SingleFieldConstraint) fp.constraintList.constraints[1]).getParent() ); }
@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 ); }
sfc3.setFactType( "Bar" ); sfc3.setFieldName( "barField" ); sfc3.setParent( sfc2 ); sfc3.setFieldType( SuggestionCompletionEngine.TYPE_STRING ); sfc3.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL );
sfc3.setFactType( "Bar" ); sfc3.setFieldName( "barField" ); sfc3.setParent( sfc2 ); sfc3.setFieldType( SuggestionCompletionEngine.TYPE_STRING ); sfc3.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL );
sfc3.setFactType( "Bar" ); sfc3.setFieldName( "barField" ); sfc3.setParent( sfc2 ); sfc3.setFieldType( SuggestionCompletionEngine.TYPE_STRING ); sfc3.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL );
sfc1.setFactType( "Bar" ); sfc3.setFieldName( "barField" ); sfc3.setParent( sfc2 ); sfc3.setFieldType( SuggestionCompletionEngine.TYPE_STRING ); sfc3.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL );