Question

I am trying to run a drl file from the guvnor web app.

The program that calls guvnor is as below

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;  
}  


}

The drl file Underage.drl is obtained from the guvnor on hitting the URL is as below

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

Why is the model declare section added in the drl retrieved from guvnor. If this is removed the rule executes fine. Log statement after removing the declare section and executing the drl locally

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. ==>[BeforeActivationFiredEvent: getActivation()=[Activation rule=Underage, 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, explanation=null]] ], getKnowledgeRuntime()=org.drools.impl.StatefulKnowledgeSessionImpl@1828ef60] ==>[AfterActivationFiredEvent: getActivation()=[Activation rule=Underage, 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, 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, explanation=Underage]

Please let me know if i am doing something wrong

Was it helpful?

Solution

I would say you are having a duplicated class called 'LoanApplication' in your application: one is the one defined in the drl and being used by the rule, and the other is the class you are using in your java code. The problem resides in the answer of your question:

Why is the model declare section added in the drl retrieved from guvnor.?

The answer is: because you (or someone in your team) defined this inside Guvnor as a 'declarative model'. What you should do, if you already have LoanApplication defined as a java class, is to import the jar file containing it as a 'POJO Model Jar' in Guvnor.

Hope it helps,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top