我正在尝试从规则中获取新插入的事实:
import com.neu.als.thesis.db.beans.FLTBean
import com.neu.als.thesis.db.beans.StudentBean
rule "Excellent"
no-loop
when
$m: FLTBean ( listeningScore > 85 && listeningScore < 101 )
then
StudentBean studentBean = new StudentBean();
studentBean.setKnowledgeLevel( "Excellent" );
insert( studentBean );
end
要从有状态会话中获得一些东西,我应该这样做:
protected Collection<Object> findFacts( final StatefulKnowledgeSession session, final String factClass )
{
ObjectFilter filter = new ObjectFilter()
{
@Override
public boolean accept( Object object )
{
return object.getClass().equals( factClass );
}
};
Collection<Object> results = session.getObjects( filter );
return results;
}
鉴于我已经设置了代码来阅读规则,我所做的下一步是:
protected void processFacts( KnowledgeBase aKnowledgeBase, Object aBean )
{
StatefulKnowledgeSession ksession = aKnowledgeBase.newStatefulKnowledgeSession();
ksession.insert( aBean );
ksession.fireAllRules();
Collection<Object> result = findFacts( ksession, "StudentBean" );
ksession.dispose();
for( Object test : result )
{
System.out.println( test == null );
System.out.println( test );
}
}
为什么我没有得到对象的任何响应?它不打印任何东西。没有堆栈跟踪,没有错误。我什至检查它是否为空。但仍然没有任何反应。我究竟做错了什么?
更新:
这是读取 .drl 文件的方法。
protected KnowledgeBase readKnowledgeBase( String aRuleFileName ) throws Exception
{
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add( ResourceFactory.newClassPathResource( aRuleFileName, getClass() ), ResourceType.DRL );
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if( errors.size() > 0 )
{
for( KnowledgeBuilderError error : errors )
{
System.err.println( error );
}
throw new IllegalArgumentException( "Could not parse knowledge." );
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
return kbase;
}
调用 processFacts 方法的是这个
public String evaluateConceptKnowledgeLevel( double FLTmark )
{
String knowledgeLevel = null;
// test
InferenceEngine ie = new InferenceEngine();
KnowledgeBase kbase;
try
{
kbase = ie.readKnowledgeBase( "KnowledgeLevel.drl" );
FLTBean bean = new FLTBean();
bean.setListeningScore( FLTmark );
ie.processFacts( kbase, bean );
}
catch( Exception e )
{
e.printStackTrace();
}
// up to here
return knowledgeLevel;
}