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 void generateSingleFieldConstraint( final SingleFieldConstraint constr, StringBuilder buf ) { if ( constr.getConstraintValueType() == BaseSingleFieldConstraint.TYPE_PREDICATE ) { buf.append( "eval( " ); buf.append( constr.getValue() ); buf.append( " )" ); } else { if ( constr.isBound() ) { bindingsFields.put( constr.getFieldBinding(), constr ); buf.append( constr.getFieldBinding() ); buf.append( " : " ); if ( ( constr.getOperator() != null && ( constr.getValue() != null || constr.getOperator().equals( "== null" ) || constr.getOperator().equals( "!= null" ) ) ) || constr.getFieldBinding() != null || constr.getConstraintValueType() == BaseSingleFieldConstraint.TYPE_EXPR_BUILDER_VALUE || constr instanceof SingleFieldConstraintEBLeftSide ) { SingleFieldConstraint parent = (SingleFieldConstraint) constr.getParent(); StringBuilder parentBuf = new StringBuilder(); while ( parent != null ) { String fieldName = parent.getFieldName(); parentBuf.insert( 0, fieldName + "." ); parent = (SingleFieldConstraint) parent.getParent(); buf.append( ( (SingleFieldConstraintEBLeftSide) constr ).getExpressionLeftSide().getText() ); } else {
private boolean isParentFactPattern(final SingleFieldConstraint sfc) { if ( sfc.getConstraintValueType() == BaseSingleFieldConstraint.TYPE_TEMPLATE ) { if ( sfc.getValue().equals( this.baseVariableName ) ) { return true; } } return false; }
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; }
@Override FieldConstraint constructSingleFieldConstraint(ConditionCol52 c, List<DTCellValue52> columnData) { SingleFieldConstraint sfc = new SingleFieldConstraint( c.getFactField() ); sfc.setConstraintValueType( c.getConstraintValueType() ); sfc.setFieldType( c.getFieldType() ); sfc.setOperator( "not in" ); List<String> consumedValues = new ArrayList<String>(); StringBuilder value = new StringBuilder(); value.append( "( " ); for ( DTCellValue52 cv : columnData ) { //Ensure cell values start and end with quotes String scv = GuidedDTDRLUtilities.convertDTCellValueToString( cv ); if ( scv != null ) { if ( !consumedValues.contains( scv ) ) { value.append( scv ).append( ", " ); } consumedValues.add( scv ); } } value.delete( value.lastIndexOf( "," ), value.length() - 1 ); value.append( ")" ); sfc.setValue( value.toString() ); return sfc; }
@Test public void testFromCollectWithEmbeddedFromEntryPoint() { RuleModel m = new RuleModel(); m.name = "r1"; SingleFieldConstraint sfc = new SingleFieldConstraint( "bar" ); sfc.setFactType( SuggestionCompletionEngine.TYPE_NUMERIC_INTEGER ); sfc.setFieldBinding( "$a" ); sfc.setOperator( "==" ); sfc.setValue( "777" ); FactPattern fp = new FactPattern( "Foo" ); fp.addConstraint( sfc ); FromEntryPointFactPattern fep = new FromEntryPointFactPattern(); fep.setEntryPointName( "ep" ); fep.setFactPattern( fp ); FromCollectCompositeFactPattern fac = new FromCollectCompositeFactPattern(); fac.setRightPattern( fep ); fac.setFactPattern( new FactPattern( "java.util.List" ) ); m.addLhsItem( fac ); String actual = BRDRLPersistence.getInstance().marshal( m ); String expected = "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "java.util.List( ) from collect ( Foo( $a : bar == 777 ) from entry-point \"ep\" ) \n" + "then\n" + "end"; assertEqualsIgnoreWhitespace( expected, actual ); }
SingleFieldConstraint brl1DefinitionFactPattern1Constraint1 = new SingleFieldConstraint(); brl1DefinitionFactPattern1Constraint1.setFieldType( SuggestionCompletionEngine.TYPE_STRING ); brl1DefinitionFactPattern1Constraint1.setConstraintValueType( SingleFieldConstraint.TYPE_TEMPLATE ); brl1DefinitionFactPattern1Constraint1.setFieldName( "name" ); brl1DefinitionFactPattern1Constraint1.setOperator( "==" ); brl1DefinitionFactPattern1Constraint1.setValue( "$name" ); brl1DefinitionFactPattern1.addConstraint( brl1DefinitionFactPattern1Constraint1 ); SingleFieldConstraint brl1DefinitionFactPattern1Constraint2 = new SingleFieldConstraint(); brl1DefinitionFactPattern1Constraint2.setFieldType( SuggestionCompletionEngine.TYPE_NUMERIC_INTEGER ); brl1DefinitionFactPattern1Constraint2.setConstraintValueType( SingleFieldConstraint.TYPE_TEMPLATE ); brl1DefinitionFactPattern1Constraint2.setFieldName( "age" ); brl1DefinitionFactPattern1Constraint2.setOperator( "==" ); brl1DefinitionFactPattern1Constraint2.setValue( "$age" ); brl1DefinitionFactPattern1.addConstraint( brl1DefinitionFactPattern1Constraint2 ); result0Fp1Con1.getConstraintValueType() ); assertEquals( "name", result0Fp1Con1.getFieldName() ); assertEquals( "==", result0Fp1Con1.getOperator() ); assertEquals( "Gargamel", result0Fp1Con1.getValue() ); result0Fp2Con1.getConstraintValueType() ); assertEquals( "name", result0Fp2Con1.getFieldName() ); assertEquals( "==", result0Fp2Con1.getOperator() );
private FieldConstraint makeSingleFieldConstraint(ConditionCol52 c, String cell) { SingleFieldConstraint sfc = new SingleFieldConstraint( c.getFactField() ); operator.append( a[i] ); sfc.setOperator( operator.toString() ); sfc.setValue( a[a.length - 1] ); } else { sfc.setValue( cell ); sfc.setOperator( c.getOperator() ); if ( SuggestionCompletionEngine.operatorRequiresList( c.getOperator() ) ) { sfc.setValue( makeInList( cell ) ); } else { if ( !c.getOperator().equals( "== null" ) && !c.getOperator().equals( "!= null" ) ) { sfc.setValue( cell ); sfc.setFieldBinding( c.getBinding() ); sfc.setParameters( c.getParameters() ); sfc.setConstraintValueType( c.getConstraintValueType() ); sfc.setFieldType( c.getFieldType() ); return sfc;
fp.setBoundName( "$brl1" ); SingleFieldConstraint sfc1 = new SingleFieldConstraint(); sfc1.setFieldBinding( "$sfc1" ); sfc1.setOperator( "==" ); sfc1.setFactType( "Driver" ); sfc1.setFieldName( "name" ); sfc1.setFieldType( SuggestionCompletionEngine.TYPE_STRING ); SingleFieldConstraint fcr1sfc = (SingleFieldConstraint) fcr1; assertEquals( "name", fcr1sfc.getFieldName() ); assertEquals( SuggestionCompletionEngine.TYPE_STRING, fcr1sfc.getFieldType() );
cons.getConstraintValueType() ); assertEquals( "name", cons.getFieldName() ); assertEquals( "==", cons.getOperator() ); assertEquals( "mike", cons.getValue() ); assertEquals( "$name", cons.getFieldBinding() ); cons.getConstraintValueType() ); assertEquals( "age", cons.getFieldName() ); assertEquals( "<", cons.getOperator() ); assertEquals( "33 + 1", cons.getValue() ); assertNull( cons.getFieldBinding() ); cons.getConstraintValueType() ); assertEquals( "age > 6", cons.getValue() ); assertNull( cons.getFieldBinding() );
fp.setBoundName( "$brl1" ); SingleFieldConstraint sfc1 = new SingleFieldConstraint(); sfc1.setFieldBinding( "$sfc1" ); sfc1.setOperator( "==" ); sfc1.setFactType( "Driver" ); sfc1.setFieldName( "name" ); sfc1.setFieldType( SuggestionCompletionEngine.TYPE_STRING );
protected List<String> getFieldBinding(FieldConstraint f) { List<String> result = new ArrayList<String>(); if ( f instanceof SingleFieldConstraint ) { SingleFieldConstraint con = (SingleFieldConstraint) f; if ( con.isBound() ) { result.add( con.getFieldBinding() ); } if ( con.getExpressionValue() != null && con.getExpressionValue().isBound() ) { result.add( con.getExpressionValue().getBinding() ); } if ( con instanceof SingleFieldConstraintEBLeftSide ) { SingleFieldConstraintEBLeftSide exp = (SingleFieldConstraintEBLeftSide) con; if ( exp.getExpressionLeftSide() != null && exp.getExpressionLeftSide().isBound() ) { result.add( exp.getExpressionLeftSide().getBinding() ); } } } else if ( f instanceof CompositeFieldConstraint ) { CompositeFieldConstraint cfc = (CompositeFieldConstraint) f; if ( cfc.constraints != null ) { for ( FieldConstraint ss : cfc.constraints ) { List<String> t = getFieldBinding( ss ); result.addAll( t ); } } } return result; }
private void visitSingleFieldConstraint(SingleFieldConstraint sfc) { InterpolationVariable var = new InterpolationVariable( sfc.getValue(), sfc.getFieldType(), factPattern.getFactType(), sfc.getFieldName() ); if ( BaseSingleFieldConstraint.TYPE_TEMPLATE == sfc.getConstraintValueType() && !vars.containsKey( var ) ) { vars.put( var, vars.size() ); } //Visit Connection constraints if ( sfc.connectives != null ) { for ( int i = 0; i < sfc.connectives.length; i++ ) { final ConnectiveConstraint cc = sfc.connectives[i]; InterpolationVariable ccVar = new InterpolationVariable( cc.getValue(), cc.getFieldType(), factPattern.getFactType(), cc.getFieldName() ); if ( BaseSingleFieldConstraint.TYPE_TEMPLATE == cc.getConstraintValueType() && !vars.containsKey( ccVar ) ) { vars.put( ccVar, vars.size() ); } } } }
private String getCompositeFOL( String type ) { RuleModel m = new RuleModel(); m.name = "or"; CompositeFactPattern cp = new CompositeFactPattern( type ); FactPattern p1 = new FactPattern( "Person" ); SingleFieldConstraint sf1 = new SingleFieldConstraint( "age" ); sf1.setOperator( "==" ); sf1.setValue( "42" ); p1.addConstraint( sf1 ); cp.addFactPattern( p1 ); FactPattern p2 = new FactPattern( "Person" ); SingleFieldConstraint sf2 = new SingleFieldConstraint( "age" ); sf2.setOperator( "==" ); sf2.setValue( "43" ); p2.addConstraint( sf2 ); cp.addFactPattern( p2 ); m.addLhsItem( cp ); String result = BRDRLPersistence.getInstance().marshal( m ); return result; }
private void visitSingleFieldConstraint(final SingleFieldConstraint sfc) { if ( sfc.getConstraintValueType() == BaseSingleFieldConstraint.TYPE_TEMPLATE ) { this.peerVariables.add( new ValueHolder( sfc.getFieldName(), sfc.getValue(), ValueHolder.Type.TEMPLATE_KEY ) ); } else { this.peerVariables.add( new ValueHolder( sfc.getFieldName(), sfc.getValue(), ValueHolder.Type.VALUE ) ); } }
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() ) ); } }
@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 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() ); }
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; }
protected String getFieldBinding(FieldConstraint fc, String var) { String fieldType = null; if ( fc instanceof SingleFieldConstraint ) { SingleFieldConstraint s = (SingleFieldConstraint) fc; if ( s.isBound() && var.equals( s.getFieldBinding() ) ) { fieldType = s.getFieldType(); } } if ( fc instanceof SingleFieldConstraintEBLeftSide ) { SingleFieldConstraintEBLeftSide s = (SingleFieldConstraintEBLeftSide) fc; if ( s.isBound() && var.equals( s.getFieldBinding() ) ) { fieldType = s.getExpressionLeftSide().getGenericType(); } } if ( fc instanceof CompositeFieldConstraint ) { CompositeFieldConstraint s = (CompositeFieldConstraint) fc; if ( s.constraints != null ) { for ( FieldConstraint ss : s.constraints ) { fieldType = getFieldBinding( ss, var ); } } } return fieldType; }