private String setFieldBindingOnContraint( String fieldName, SingleFieldConstraint con ) { int colonPos = fieldName.indexOf( ':' ); if ( colonPos > 0 ) { String fieldBinding = fieldName.substring( 0, colonPos ).trim(); con.setFieldBinding( fieldBinding ); fieldName = fieldName.substring( colonPos + 1 ).trim(); } return fieldName; }
public void onClick( ClickEvent event ) { String var = varName.getText(); if ( modeller.isVariableNameUsed( var ) ) { Window.alert( Constants.INSTANCE.TheVariableName0IsAlreadyTaken( var ) ); return; } con.setFieldBinding( var ); modeller.refreshWidget(); popup.hide(); } } );
@Test public void testAllVariableBindings() { final RuleModel model = new RuleModel(); model.lhs = new IPattern[2]; final FactPattern x = new FactPattern( "Car" ); model.lhs[0] = x; x.setBoundName( "boundFact" ); SingleFieldConstraint sfc = new SingleFieldConstraint( "q" ); x.addConstraint( sfc ); sfc.setFieldBinding( "field1" ); SingleFieldConstraint sfc2 = new SingleFieldConstraint( "q" ); x.addConstraint( sfc2 ); sfc2.setFieldBinding( "field2" ); model.lhs[1] = new CompositeFactPattern(); List vars = model.getAllVariables(); assertEquals( 3, vars.size() ); assertEquals( "boundFact", vars.get( 0 ) ); assertEquals( "field1", vars.get( 1 ) ); assertEquals( "field2", vars.get( 2 ) ); assertTrue( model.isVariableNameUsed( "field2" ) ); }
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 testScopedVariablesWithCompositeFact() { RuleModel m = new RuleModel(); FactPattern p = new FactPattern(); CompositeFieldConstraint cf = new CompositeFieldConstraint(); cf.addConstraint( new SingleFieldConstraint( "x" ) ); p.addConstraint( cf ); SingleFieldConstraint sf = new SingleFieldConstraint( "q" ); sf.setFieldBinding( "abc" ); p.addConstraint( sf ); SingleFieldConstraint sf2 = new SingleFieldConstraint( "q" ); sf2.setFieldBinding( "qed" ); cf.addConstraint( sf2 ); m.addLhsItem( p ); List vars = m.getAllVariables(); assertEquals( 1, vars.size() ); assertEquals( "abc", vars.get( 0 ) ); } /*
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; }
@Test public void testAllVariableBindings2() { final RuleModel model = new RuleModel(); model.lhs = new IPattern[1]; final FactPattern fp = new FactPattern( "Car" ); model.lhs[0] = fp; fp.setBoundName( "$c" ); SingleFieldConstraint sfc = new SingleFieldConstraintEBLeftSide( "make" ); sfc.getExpressionValue().appendPart( new ExpressionField( "make", "java.lang.String", "String" ) ); sfc.setFieldBinding( "$m" ); fp.addConstraint( sfc ); List<String> vars = model.getAllVariables(); assertEquals( 2, vars.size() ); assertEquals( "$c", vars.get( 0 ) ); assertEquals( "$m", vars.get( 1 ) ); }
sfc1.setFieldBinding( "$sfc1" ); sfc1.setOperator( "==" ); sfc1.setFactType( "Driver" );
@Test public void testFieldsDeclaredButNoConstraints() { RuleModel m = new RuleModel(); m.name = "boo"; FactPattern p = new FactPattern( "Person" ); // this isn't an effective constraint, so it should be ignored. p.addConstraint( new SingleFieldConstraint( "field1" ) ); m.addLhsItem( p ); String actual = BRDRLPersistence.getInstance().marshal( m ); String expected = "rule \"boo\" \tdialect \"mvel\"\n when Person() then end"; checkMarshallUnmarshall( expected, m ); SingleFieldConstraint con = (SingleFieldConstraint) p.getConstraintList().getConstraint( 0 ); con.setFieldBinding( "q" ); // now it should appear, as we are binding a var to it expected = "rule \"boo\" dialect \"mvel\" when Person(q : field1) then end"; checkMarshallUnmarshall( expected, m ); }
sfc1.setFieldBinding( "$sfc1" ); sfc1.setOperator( "==" ); sfc1.setFactType( "Driver" );
sfc1.setFieldBinding( "$sfc1" ); sfc1.setOperator( "==" ); sfc1.setFactType( "Driver" ); sfc2.setFieldBinding( "$sfc1" ); sfc2.setOperator( "==" ); sfc2.setFactType( "Driver" );
x.setBoundName( "x" ); final SingleFieldConstraint a = new SingleFieldConstraint( "name" ); a.setFieldBinding( "a" ); a.setFieldType( "String" ); x.addConstraint( a );
sfc1.setFieldBinding( "$sfc1" ); sfc1.setOperator( "==" ); sfc1.setFactType( "Driver" );
@Test public void testFromCollectWithEmbeddedFromEntryPoint() { RuleModel m = new RuleModel(); m.name = "r1"; SingleFieldConstraint sfc = new SingleFieldConstraint( "bar" ); sfc.setFactType( DataType.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 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"; checkMarshallUnmarshall( expected, m ); }
sfc1.setFieldBinding( "$sfc1" ); sfc1.setOperator( "==" ); sfc1.setFactType( "Driver" );
@Test public void testFromAccumulateWithEmbeddedFromEntryPoint() { RuleModel m = new RuleModel(); m.name = "r1"; SingleFieldConstraint sfc = new SingleFieldConstraint( "bar" ); sfc.setFactType( DataType.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 ); FromAccumulateCompositeFactPattern fac = new FromAccumulateCompositeFactPattern(); fac.setSourcePattern( fep ); fac.setFactPattern( new FactPattern( "java.util.List" ) ); fac.setFunction( "max($a)" ); m.addLhsItem( fac ); String expected = "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "java.util.List( ) from accumulate ( Foo( $a : bar == 777 ) from entry-point \"ep\", \n" + "max($a))\n" + "then\n" + "end"; checkMarshallUnmarshall( expected, m ); }
pat.setBoundName( "p1" ); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldBinding( "f1" ); con.setFieldName( "age" ); con.setOperator( "<" );