sfc.setFieldBinding( c.getBinding() );
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 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; }
sfc.setFieldBinding( c.getBinding() );
@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 ) ); }
@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" ) ); }
@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", SuggestionCompletionEngine.TYPE_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 ) ); }
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; }
cons[0] = new SingleFieldConstraint( "age" ); cons[1] = new SingleFieldConstraint( "make" ); cons[0].setFieldBinding( "qbc" ); cons[0].connectives = new ConnectiveConstraint[1]; cons[0].connectives[0] = new ConnectiveConstraint( "Car",
con.setFieldBinding( "qbc" ); con.setFieldType( "String" ); con.connectives = new ConnectiveConstraint[1];
sfc1.setFieldBinding( "$sfc1" ); sfc1.setOperator( "==" ); sfc1.setFactType( "Driver" );
y.constraintList.constraints = cons; cons[0] = new SingleFieldConstraint( "age" ); cons[0].setFieldBinding( "qbc" ); cons[0].setFieldType( "String" ); cons[0].connectives = new ConnectiveConstraint[1];
@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"; assertEqualsIgnoreWhitespace( expected, actual ); SingleFieldConstraint con = (SingleFieldConstraint) p.constraintList.constraints[ 0 ]; con.setFieldBinding( "q" ); // now it should appear, as we are binding a var to it actual = BRDRLPersistence.getInstance().marshal( m ); expected = "rule \"boo\" dialect \"mvel\" when Person(q : field1) then end"; assertEqualsIgnoreWhitespace( expected, actual ); }
sfc1.setFieldBinding( "$sfc1" ); sfc1.setOperator( "==" ); sfc1.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( 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 ); }
@Test public void testFromAccumulateWithEmbeddedFromEntryPoint() { 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 ); FromAccumulateCompositeFactPattern fac = new FromAccumulateCompositeFactPattern(); fac.setSourcePattern( fep ); fac.setFactPattern( new FactPattern( "java.util.List" ) ); fac.setFunction( "max($a)" ); m.addLhsItem( fac ); String actual = BRDRLPersistence.getInstance().marshal( m ); 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"; assertEqualsIgnoreWhitespace( expected, actual ); }
private RuleModel getComplexModel() { final RuleModel m = new RuleModel(); m.addAttribute( new RuleAttribute( "no-loop", "true" ) ); final FactPattern pat = new FactPattern( "Person" ); pat.setBoundName( "p1" ); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFactType( "Person" ); con.setFieldBinding( "f1" ); con.setFieldName( "age" ); con.setOperator( "<" ); con.setValue( "42" ); pat.addConstraint( con ); m.addLhsItem( pat ); final CompositeFactPattern comp = new CompositeFactPattern( "not" ); comp.addFactPattern( new FactPattern( "Cancel" ) ); m.addLhsItem( comp ); final ActionUpdateField set = new ActionUpdateField(); set.variable = "p1"; set.addFieldValue( new ActionFieldValue( "status", "rejected", SuggestionCompletionEngine.TYPE_STRING ) ); m.addRhsItem( set ); final ActionRetractFact ret = new ActionRetractFact( "p1" ); m.addRhsItem( ret ); final DSLSentence sen = new DSLSentence(); sen.setDefinition( "Send an email to {administrator}" ); m.addRhsItem( sen ); return m; }
pat.setBoundName( "p1" ); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldBinding( "f1" ); con.setFieldName( "age" ); con.setOperator( "<" );