@Before public void setUp() throws Exception { brlPersistence = BRDRLPersistence.getInstance(); }
public Reader getKnowledgeReader(Resource ruleResource) throws IOException { String brl = loadBrlFile(ruleResource.getReader()); RuleModel model = BRXMLPersistence.getInstance().unmarshal(brl); this.hasDSLSentences=model.hasDSLSentences(); return new StringReader(BRDRLPersistence.getInstance().marshal(model)); }
public Reader getKnowledgeReader(Resource ruleResource) throws IOException { String brl = loadBrlFile(ruleResource.getReader()); RuleModel model = BRXMLPersistence.getInstance().unmarshal(brl); this.hasDSLSentences=model.hasDSLSentences(); return new StringReader(BRDRLPersistence.getInstance().marshal(model)); }
@Test public void testDefaultMVEL() { RuleModel m = new RuleModel(); String s = BRDRLPersistence.getInstance().marshal( m ); assertTrue( s.indexOf( "mvel" ) > -1 ); m.addAttribute( new RuleAttribute( "dialect", "goober" ) ); s = BRDRLPersistence.getInstance().marshal( m ); assertFalse( s.indexOf( "mvel" ) > -1 ); assertTrue( s.indexOf( "goober" ) > -1 ); }
@Test public void testFieldBindingWithNoConstraints() { // to satisfy JBRULES-850 RuleModel m = getModelWithNoConstraints(); String s = BRDRLPersistence.getInstance().marshal( m ); // System.out.println(s); assertTrue( s.indexOf( "Person( f1 : age)" ) != -1 ); }
@Test public void testLockOnActive() { RuleModel m = new RuleModel(); m.addAttribute( new RuleAttribute( "lock-on-active", "true" ) ); m.addAttribute( new RuleAttribute( "auto-focus", "true" ) ); m.addAttribute( new RuleAttribute( "duration", "42" ) ); String s = BRDRLPersistence.getInstance().marshal( m ); assertTrue( s.indexOf( "lock-on-active true" ) > -1 ); assertTrue( s.indexOf( "auto-focus true" ) > -1 ); assertTrue( s.indexOf( "duration 42" ) > -1 ); }
@Test public void testInvalidComposite() throws Exception { RuleModel m = new RuleModel(); CompositeFactPattern com = new CompositeFactPattern( "not" ); m.addLhsItem( com ); String s = BRDRLPersistence.getInstance().marshal( m ); assertNotNull( s ); m.addLhsItem( new CompositeFactPattern( "or" ) ); m.addLhsItem( new CompositeFactPattern( "exists" ) ); s = BRDRLPersistence.getInstance().marshal( m ); assertNotNull( s ); }
@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 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 ); }
@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 ); }
@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 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 testNotInOperatorNumber() { RuleModel m = new RuleModel(); m.name = "not in"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraint con = new SingleFieldConstraint(); con.setFieldType( SuggestionCompletionEngine.TYPE_NUMERIC_INTEGER ); con.setFieldName( "field1" ); con.setOperator( "not in" ); con.setValue( "55, 66" ); 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 ( 55, 66 ) ) \n" + " then \n" + "end", result ); }
@Test public void testLHSExpressionString1() { RuleModel m = new RuleModel(); m.name = "test expressionsString1"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraintEBLeftSide con = new SingleFieldConstraintEBLeftSide(); con.getExpressionLeftSide().appendPart( new ExpressionText( "field1" ) ); con.setOperator( "==" ); con.setValue( "goo" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL ); p.addConstraint( con ); m.addLhsItem( p ); String result = BRDRLPersistence.getInstance().marshal( m ); assertEqualsIgnoreWhitespace( "rule \"test expressionsString1\"" + "\tdialect \"mvel\"\n when " + " Person( field1 == \"goo\" )" + " then " + "end", result ); }
@Test public void testLHSExpressionNumber() { RuleModel m = new RuleModel(); m.name = "test expressionsNumber"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraintEBLeftSide con = new SingleFieldConstraintEBLeftSide(); con.getExpressionLeftSide().appendPart( new ExpressionUnboundFact( p ) ); con.getExpressionLeftSide().appendPart( new ExpressionField( "field1", "java.lang.Integer", SuggestionCompletionEngine.TYPE_NUMERIC_INTEGER ) ); con.setOperator( "==" ); con.setValue( "55" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL ); p.addConstraint( con ); m.addLhsItem( p ); String result = BRDRLPersistence.getInstance().marshal( m ); assertEqualsIgnoreWhitespace( "rule \"test expressionsNumber\"" + "\tdialect \"mvel\"\n when " + " Person( field1 == 55 )" + " then " + "end", result ); }
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 testLHSExpressionBoolean() { RuleModel m = new RuleModel(); m.name = "test expressionsBoolean"; FactPattern p = new FactPattern( "Person" ); SingleFieldConstraintEBLeftSide con = new SingleFieldConstraintEBLeftSide(); con.getExpressionLeftSide().appendPart( new ExpressionUnboundFact( p ) ); con.getExpressionLeftSide().appendPart( new ExpressionField( "field1", "java.lang.Boolean", SuggestionCompletionEngine.TYPE_BOOLEAN ) ); con.setOperator( "==" ); con.setValue( "true" ); con.setConstraintValueType( SingleFieldConstraint.TYPE_LITERAL ); p.addConstraint( con ); m.addLhsItem( p ); String result = BRDRLPersistence.getInstance().marshal( m ); assertEqualsIgnoreWhitespace( "rule \"test expressionsBoolean\"" + "\tdialect \"mvel\"\n when " + " Person( field1 == true )" + " then " + "end", result ); }