org.kie.internal.agent.KnowledgeAgentFactory.newKnowledgeAgentConfiguration中的空指针异常

时间:2014-07-02 05:15:55

标签: drools drools-guvnor

我正在使用guvnor.war并在tomcat中部署drools 6.0.0.Beta5。我成功地能够创建业务规则。但是当我尝试从独立的JAVA应用程序访问这些规则时,我得到了空指针异常    KnowledgeAgentFactory.newKnowledgeAgentConfiguration();

错误堆栈:

Exception in thread "main" java.lang.NullPointerException
at org.kie.internal.agent.KnowledgeAgentFactory.newKnowledgeAgentConfiguration(KnowledgeAgentFactory.java:128)
at com.guvnor.GuvnorTest.createKnowledgeBase(GuvnorTest.java:48)
at com.guvnor.GuvnorTest.testDroolsWithGuvnor(GuvnorTest.java:33)
at com.guvnor.Test.main(Test.java:12)
ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
JDWP exit error AGENT_ERROR_NO_JNI_ENV(183):  [../../../src/share/back/util.c:820]

代码:

public class GuvnorTest  {

@SuppressWarnings("deprecation")
public void testDroolsWithGuvnor() throws Exception {
    KnowledgeBase knowledgeBase = createKnowledgeBase();
    StatefulKnowledgeSession session = knowledgeBase.newStatefulKnowledgeSession();

    try {
        Account account = new Account();
        account.setBalance(10);
        session.insert(account);
        session.fireAllRules();
    }
    finally {
        session.dispose();
    }
}
@SuppressWarnings("deprecation")
private static KnowledgeBase createKnowledgeBase() {
    KnowledgeAgentConfiguration kaconf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration();
    kaconf.setProperty( "drools.agent.scanDirectories", "false" );        
    kaconf.setProperty( MonitorChangesetEventsOption.PROPERTY_NAME,"true");
    kaconf.setProperty( ScanDirectoriesOption.PROPERTY_NAME,"true");      
    kaconf.setProperty( ScanResourcesOption.PROPERTY_NAME,"true");
    kaconf.setProperty( NewInstanceOption.PROPERTY_NAME,"true" );
    kaconf.setProperty( UseKnowledgeBaseClassloaderOption.PROPERTY_NAME,"false" );
    kaconf.setProperty( ValidationTimeoutOption.PROPERTY_NAME,"0" );

    KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent( "test agent", kaconf );

    URL url =GuvnorTest.class.getClass().getResource("/src/guvnor-jboss.xml");
 kagent.applyChangeSet( ResourceFactory.newClassPathResource("guvnor-jboss.xml"));
   // kagent.applyChangeSet(ResourceFactory.newUrlResource(url));
    return kagent.getKnowledgeBase();
}

}

1 个答案:

答案 0 :(得分:0)

我没有使用上面的代码,而是尝试使用代码,它的工作原理很适合我:

源代码:

String sourceUrl = "http://localhost:8080/guvnor/rest/packages/com.sample.model/source";

    UrlResource resource = (UrlResource) ResourceFactory
            .newUrlResource(sourceUrl);
    resource.setBasicAuthentication("enable");
    resource.setUsername("suraj");
    resource.setPassword("suraj");
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
            .newKnowledgeBuilder();
    kbuilder.add(resource, ResourceType.DRL);
    KnowledgeBase kbase = kbuilder.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return kbase;