@Test public void testRemoveConstraint() { final FactPattern p = new FactPattern(); final SingleFieldConstraint x = new SingleFieldConstraint( "x" ); p.addConstraint( x ); final SingleFieldConstraint y = new SingleFieldConstraint( "y" ); p.addConstraint( y ); assertEquals( 2, p.constraintList.constraints.length ); p.removeConstraint( 1 ); assertEquals( 1, p.constraintList.constraints.length ); assertEquals( x, p.constraintList.constraints[0] ); }
@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 testWithCompositeNesting() { final FactPattern p = new FactPattern(); final SingleFieldConstraint x = new SingleFieldConstraint( "x" ); p.addConstraint( x ); assertEquals( 1, p.constraintList.constraints.length ); assertEquals( x, p.constraintList.constraints[0] ); final CompositeFieldConstraint y = new CompositeFieldConstraint(); y.addConstraint( new SingleFieldConstraint("y") ); y.addConstraint( new SingleFieldConstraint("z") ); p.addConstraint( y ); assertEquals( 2, p.constraintList.constraints.length ); assertEquals( x, p.constraintList.constraints[0] ); assertEquals( y, p.constraintList.constraints[1] ); }
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; }
@Test public void testAddConstraint() { final FactPattern p = new FactPattern(); final SingleFieldConstraint x = new SingleFieldConstraint( "x" ); p.addConstraint( x ); assertEquals( 1, p.constraintList.constraints.length ); assertEquals( x, p.constraintList.constraints[0] ); final SingleFieldConstraint y = new SingleFieldConstraint( "y" ); p.addConstraint( y ); assertEquals( 2, p.constraintList.constraints.length ); assertEquals( x, p.constraintList.constraints[0] ); assertEquals( y, p.constraintList.constraints[1] ); }
@Test public void testAddConstraint() { final CompositeFieldConstraint p = new CompositeFieldConstraint(); final SingleFieldConstraint x = new SingleFieldConstraint( "x" ); p.addConstraint( x ); assertEquals( 1, p.constraints.length ); assertEquals( x, p.constraints[0] ); final SingleFieldConstraint y = new SingleFieldConstraint( "y" ); p.addConstraint( y ); assertEquals( 2, p.constraints.length ); assertEquals( x, p.constraints[0] ); assertEquals( y, p.constraints[1] ); }
@Test public void testAdd() { final SingleFieldConstraint con = new SingleFieldConstraint(); con.addNewConnective(); assertEquals( 1, con.connectives.length ); assertNotNull( con.connectives[0] ); con.addNewConnective(); assertEquals( 2, con.connectives.length ); assertNotNull( con.connectives[1] ); }
@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 textIsNotNullOperator() { final RuleModel m = new RuleModel(); m.name = "IsNotNullOperator"; 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 ); }
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 testGetFieldConstraints() { FactPattern pat = new FactPattern(); assertEquals(0, pat.getFieldConstraints().length); assertNull(pat.constraintList); pat.addConstraint( new SingleFieldConstraint() ); assertNotNull(pat.constraintList); assertEquals(1, pat.getFieldConstraints().length); }
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; }
@Test public void testSingleExists() throws Exception { RuleModel m = new RuleModel(); m.name = "or"; CompositeFactPattern cp = new CompositeFactPattern( CompositeFactPattern.COMPOSITE_TYPE_EXISTS ); FactPattern p1 = new FactPattern( "Person" ); SingleFieldConstraint sf1 = new SingleFieldConstraint( "age" ); sf1.setOperator( "==" ); sf1.setValue( "42" ); p1.addConstraint( sf1 ); cp.addFactPattern( p1 ); m.addLhsItem( cp ); String result = BRDRLPersistence.getInstance().marshal( m ); assertTrue( result.indexOf( "exists (Person( age == 42 )) " ) > 0 ); }
@Test public void testRemoveConstraint() { final CompositeFieldConstraint p = new CompositeFieldConstraint(); final SingleFieldConstraint x = new SingleFieldConstraint( "x" ); p.addConstraint( x ); final CompositeFieldConstraint y = new CompositeFieldConstraint( ); p.addConstraint( y ); assertEquals( 2, p.constraints.length ); p.removeConstraint( 1 ); assertEquals( 1, p.constraints.length ); assertEquals( x, p.constraints[0] ); }
@Test public void testPredicateConstraint() { RuleModel m = new RuleModel(); m.name = "yeah"; FactPattern p = new FactPattern( "Goober" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setConstraintValueType( SingleFieldConstraint.TYPE_PREDICATE ); con.setValue( "field soundslike 'poo'" ); p.addConstraint( con ); m.addLhsItem( p ); String actual = BRDRLPersistence.getInstance().marshal( m ); // System.err.println(actual); String expected = "rule \"yeah\" " + "\tdialect \"mvel\"\n when " + "Goober( eval( field soundslike 'poo' ) )" + " then " + "end"; assertEqualsIgnoreWhitespace( expected, actual ); }
@Test public void testReturnValueConstraint() { RuleModel m = new RuleModel(); m.name = "yeah"; FactPattern p = new FactPattern( "Goober" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setConstraintValueType( SingleFieldConstraint.TYPE_RET_VALUE ); con.setValue( "someFunc(x)" ); con.setOperator( "==" ); con.setFieldName( "goo" ); p.addConstraint( con ); m.addLhsItem( p ); String actual = BRDRLPersistence.getInstance().marshal( m ); // System.err.println(actual); String expected = "rule \"yeah\" " + "\tdialect \"mvel\"\n when " + "Goober( goo == ( someFunc(x) ) )" + " then " + "end"; assertEqualsIgnoreWhitespace( expected, actual ); }
@Test public void testInOperatorString() { RuleModel m = new RuleModel(); m.name = "in"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( SuggestionCompletionEngine.TYPE_STRING ); con.setFieldName( "field1" ); con.setOperator( "in" ); con.setValue( "value1, value2" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL ); p.addConstraint( con ); m.addLhsItem( p ); String result = BRDRLPersistence.getInstance().marshal( m ); assertEqualsIgnoreWhitespace( "rule \"in\" \n" + "dialect \"mvel\" \n" + "when \n" + " Person(field1 in ( \"value1\", \"value2\" ) ) \n" + " then \n" + "end", result ); }
@Test public void testNotInOperatorString() { RuleModel m = new RuleModel(); m.name = "not in"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( SuggestionCompletionEngine.TYPE_STRING ); con.setFieldName( "field1" ); con.setOperator( "not in" ); con.setValue( "value1, value2" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL ); p.addConstraint( con ); m.addLhsItem( p ); String result = BRDRLPersistence.getInstance().marshal( m ); assertEqualsIgnoreWhitespace( "rule \"not in\" \n" + "dialect \"mvel\" \n" + "when \n" + " Person(field1 not in ( \"value1\", \"value2\" ) ) \n" + " then \n" + "end", result ); }
@Test public void testEnumTypeComparable() { //Java 1.5+ "true" enums are of type Comparable String expected = "rule \"my rule\"\n\tdialect \"mvel\"\n\twhen\n\t\tCheese( type == Cheese.CHEDDAR )\n" + "\tthen\n\t\tinsert( new Report() );\nend\n"; final RuleModel m = new RuleModel(); final FactPattern pat = new FactPattern( "Cheese" ); m.addLhsItem( pat ); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldName( "type" ); con.setOperator( "==" ); con.setValue( "Cheese.CHEDDAR" ); con.setFieldType( SuggestionCompletionEngine.TYPE_COMPARABLE ); con.setConstraintValueType( BaseSingleFieldConstraint.TYPE_ENUM ); pat.addConstraint( con ); m.addRhsItem( new ActionInsertFact( "Report" ) ); m.name = "my rule"; final String drl = brlPersistence.marshal( m ); assertEquals( expected, drl ); }
@Test public void testEnumTypeBoolean() { //A legacy "Guvnor" enums (i.e pick-list of underlying field data-type) String expected = "rule \"my rule\"\n\tdialect \"mvel\"\n\twhen\n\t\tCheese( smelly == true )\n" + "\tthen\n\t\tinsert( new Report() );\nend\n"; final RuleModel m = new RuleModel(); final FactPattern pat = new FactPattern( "Cheese" ); m.addLhsItem( pat ); final SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldName( "smelly" ); con.setOperator( "==" ); con.setValue( "true" ); con.setFieldType( SuggestionCompletionEngine.TYPE_BOOLEAN ); con.setConstraintValueType( BaseSingleFieldConstraint.TYPE_ENUM ); pat.addConstraint( con ); m.addRhsItem( new ActionInsertFact( "Report" ) ); m.name = "my rule"; final String drl = brlPersistence.marshal( m ); assertEquals( expected, drl ); }