从Guvnor执行的规则未激活

时间:2013-09-20 05:28:35

标签: drools drools-guvnor

我正在尝试从guvnor网络应用程序运行drl文件。

调用guvnor的程序如下所示

public class RunGuvnorRules {

public static final void main(String[] args) {
 try {  
        // load up the knowledge base
        KnowledgeBuilderConfiguration kbuilderconfiguration = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
        KnowledgeBase kbase = readKnowledgeBase();  
        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();  
        Map<String, Object> params = new HashMap<String, Object>();
        LoanApplication loanApplication = new LoanApplication();  
        loanApplication.setAge(18);
        ksession.insert(loanApplication);
        ksession.addEventListener( new DebugAgendaEventListener() ); 
        ksession.addEventListener( new DebugWorkingMemoryEventListener() ); 
        ksession.fireAllRules();
        ksession.dispose();
        System.out.println("LoanApplication" + loanApplication.toString());
    } catch (Throwable t) {  
        t.printStackTrace();  
    }  

}



private static KnowledgeBase readKnowledgeBase() throws Exception {  
    String url = "http://localhost:8080/guvnor-5.5.0.Final-tomcat-6.0/org.drools.guvnor.Guvnor/package/mortgages/LATEST/assets/Underage.drl";  
    System.out.println("Going: " + url);  

    UrlResource resource = (UrlResource) ResourceFactory.newUrlResource(url);  
    resource.setBasicAuthentication("enabled");  
    resource.setUsername("admin");  
    resource.setPassword("admin");  

    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();  
    kbuilder.add(resource, ResourceType.DRL);  
    final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages( kbuilder.getKnowledgePackages() );
    return kbase;  
}  


}

drl文件Underage.drl是从guvnor获取的,点击URL如下

package mortgages


declare LoanApplication
firstName: String
lastName: String
state: String
bank: String
age: Integer
creditLimit: Integer
previousApplications: Integer
loanApproved: Boolean
interestRate: Integer
explanation: String
end
rule "Underage"
salience 10
dialect "mvel"
when
    application : LoanApplication( age < 21 )
then
    application.setExplanation( "Underage" );
    application.setLoanApproved( false );
end

为什么在从guvnor检索到的drl中添加了模型声明部分。如果删除此规则,则规则执行正常。删除声明部分并在本地执行drl后的日志语句

SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。 SLF4J:默认为无操作(NOP)记录器实现 SLF4J:有关详细信息,请参阅http://www.slf4j.org/codes.html#StaticLoggerBinder。 ==&gt; [BeforeActivationFiredEvent:getActivation()= [激活规则=未成年,行为#= 0,突出= 10,元组= [事实0:1:64587262:64587262:1:默认:LoanApplication [firstName = null,lastName = null,state = null,bank = null,age = 18,creditLimit = 0,previousApplications = 0,loanApproved = false,interestRate = 0,explanation = null]] ],getKnowledgeRuntime()= org.drools.impl.StatefulKnowledgeSessionImpl@1828ef60] ==&gt; [AfterActivationFiredEvent:getActivation()= [激活规则=未成年,行为#= 0,突出= 10,元组= [事实0:1:64587262:64587262:1:默认:LoanApplication [firstName = null,lastName = null,state = null,bank = null,age = 18,creditLimit = 0,previousApplications = 0,loanApproved = false,interestRate = 0,explanation = Underage]] ],getKnowledgeRuntime()= org.drools.impl.StatefulKnowledgeSessionImpl@1828ef60] LoanApplicationLoanApplication [firstName = null,lastName = null,state = null,bank = null,age = 18,creditLimit = 0,previousApplications = 0,loanApproved = false,interestRate = 0,说明=未成年人]

如果我做错了,请告诉我

1 个答案:

答案 0 :(得分:1)

我想说你的应用程序中有一个名为'LoanApplication'的重复类:一个是在drl中定义并被规则使用的类,另一个是你在java代码中使用的类。 问题在于你的问题的答案:

  

为什么在从中检索到的drl中添加了模型声明部分   的Guvnor。?

答案是:因为你(或你团队中的某个人)在Guvnor中将其定义为“声明模型”。如果已经将LoanApplication定义为java类,那么你应该做的是在Guvnor中将包含它的jar文件导入为'POJO Model Jar'。

希望它有所帮助,