📄 statelessrulesessiontest.java
字号:
*/
public void testExecuteRules() throws Exception {
final StatelessRuleSession statelessSession = this.sessionBuilder.getStatelessRuleSession( this.bindUri );
final List inObjects = new ArrayList();
final Person bob = new Person( "bob" );
inObjects.add( bob );
final Person jeannie = new Person( "jeannie" );
jeannie.addSister( "rebecca" );
inObjects.add( jeannie );
final Person rebecca = new Person( "rebecca" );
rebecca.addSister( "jeannie" );
inObjects.add( rebecca );
// execute the rules
final List outList = statelessSession.executeRules( inObjects );
assertEquals( "incorrect size",
5,
outList.size() );
assertContains( outList,
bob );
assertContains( outList,
rebecca );
assertContains( outList,
jeannie );
assertContains( outList,
"rebecca and jeannie are sisters" );
assertContains( outList,
"jeannie and rebecca are sisters" );
statelessSession.release();
}
/**
* Test executeRules with normal drl.
*/
public void testExecuteRulesWithXml() throws Exception {
final StatelessRuleSession statelessSession = this.sessionBuilder.getStatelessRuleSession( this.bindUri_xml );
final List inObjects = new ArrayList();
final Person bob = new Person( "bob" );
inObjects.add( bob );
final Person jeannie = new Person( "jeannie" );
jeannie.addSister( "rebecca" );
inObjects.add( jeannie );
final Person rebecca = new Person( "rebecca" );
rebecca.addSister( "jeannie" );
inObjects.add( rebecca );
// execute the rules
final List outList = statelessSession.executeRules( inObjects );
assertEquals( "incorrect size",
5,
outList.size() );
assertContains( outList,
bob );
assertContains( outList,
rebecca );
assertContains( outList,
jeannie );
assertContains( outList,
"rebecca and jeannie are sisters" );
assertContains( outList,
"jeannie and rebecca are sisters" );
statelessSession.release();
}
/**
* Test executeRules drl with dsl.
*/
public void testExecuteRules_dsl() throws Exception {
final StatelessRuleSession statelessSession = this.sessionBuilder.getStatelessRuleSession( this.bindUri_drl );
final List inObjects = new ArrayList();
final Person bob = new Person( "bob" );
inObjects.add( bob );
final Person jeannie = new Person( "jeannie" );
jeannie.addSister( "rebecca" );
inObjects.add( jeannie );
final Person rebecca = new Person( "rebecca" );
rebecca.addSister( "jeannie" );
inObjects.add( rebecca );
// execute the rules
final List outList = statelessSession.executeRules( inObjects );
assertEquals( "incorrect size",
5,
outList.size() );
assertContains( outList,
bob );
assertContains( outList,
rebecca );
assertContains( outList,
jeannie );
assertContains( outList,
"rebecca and jeannie are sisters" );
assertContains( outList,
"jeannie and rebecca are sisters" );
statelessSession.release();
}
/**
* Test executeRules with ObjectFilter.
*/
public void testExecuteRulesWithFilter() throws Exception {
final StatelessRuleSession statelessSession = this.sessionBuilder.getStatelessRuleSession( this.bindUri );
final List inObjects = new ArrayList();
final Person bob = new Person( "bob" );
inObjects.add( bob );
final Person rebecca = new Person( "rebecca" );
rebecca.addSister( "jeannie" );
inObjects.add( rebecca );
final Person jeannie = new Person( "jeannie" );
jeannie.addSister( "rebecca" );
inObjects.add( jeannie );
// execute the rules
final List outList = statelessSession.executeRules( inObjects,
new PersonFilter() );
assertEquals( "incorrect size",
3,
outList.size() );
assertTrue( "where is bob",
outList.contains( bob ) );
assertTrue( "where is rebecca",
outList.contains( rebecca ) );
assertTrue( "where is jeannie",
outList.contains( jeannie ) );
}
/**
* Test executeRules with ObjectFilter drl with dsl.
*/
public void testExecuteRulesWithFilter_dsl() throws Exception {
final StatelessRuleSession statelessSession = this.sessionBuilder.getStatelessRuleSession( this.bindUri_drl );
final List inObjects = new ArrayList();
final Person bob = new Person( "bob" );
inObjects.add( bob );
final Person rebecca = new Person( "rebecca" );
rebecca.addSister( "jeannie" );
inObjects.add( rebecca );
final Person jeannie = new Person( "jeannie" );
jeannie.addSister( "rebecca" );
inObjects.add( jeannie );
// execute the rules
final List outList = statelessSession.executeRules( inObjects,
new PersonFilter() );
assertEquals( "incorrect size",
3,
outList.size() );
assertTrue( "where is bob",
outList.contains( bob ) );
assertTrue( "where is rebecca",
outList.contains( rebecca ) );
assertTrue( "where is jeannie",
outList.contains( jeannie ) );
}
/**
* Filter accepts only objects of type Person.
*/
static class PersonFilter
implements
ObjectFilter {
public Object filter(final Object object) {
return (object instanceof Person ? object : null);
}
public void reset() {
// nothing to reset
}
}
protected void assertContains(final List expected,
final Object object) {
if ( expected.contains( object ) ) {
return;
}
fail( object + " not in " + expected );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -