0

我正在尝试从 guvnor Web 应用程序运行一个 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;  
}  


}

在访问 URL 时从 guvnor 获得的 drl 文件 Underage.drl 如下

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了解更多详情。==>[BeforeActivationFiredEvent: getActivation()=[激活规则=未成年,行为#=0,显着性=10,元组=[事实 0:1:64587262:64587262:1:DEFAULT:LoanApplication [firstName=null, lastName=null , state=null, bank=null, age=18, creditLimit=0, previousApplications=0, loanApproved=false, interestRate=0, explain=null]] ], getKnowledgeRuntime()=org.drools.impl.StatefulKnowledgeSessionImpl@1828ef60] ==>[AfterActivationFiredEvent: getActivation()=[激活规则=未成年,act#=0,salience=10,tuple=[fact 0:1:64587262:64587262:1:DEFAULT:LoanApplication [firstName=null, lastName=null ,state=null,bank=null,age=18,creditLimit=0,previousApplications=0,loanApproved=false,interestRate=0,explain=Underage]]],getKnowledgeRuntime()=org.drools.impl。

如果我做错了什么,请告诉我

4

1 回答 1

1

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

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

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

希望能帮助到你,

于 2013-09-20T07:41:00.060 回答