2

我是流口水的新手,我无法访问包内的 DRL 文件。这就是我所想的。我的项目规则层次结构是这样的

在此处输入图像描述

我尝试使用以下代码访问 DRL 文件:

KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add( ResourceFactory.newClassPathResource( "neu/als/KnowledgeLevel", 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;

这导致错误:

java.lang.RuntimeException: Unable to get LastModified for ClasspathResource
    at org.drools.io.impl.ClassPathResource.getLastModified(ClassPathResource.java:204)
    at org.drools.io.impl.ClassPathResource.getInputStream(ClassPathResource.java:141)
    at org.drools.compiler.DrlParser.parse(DrlParser.java:145)
    at org.drools.compiler.DrlParser.parse(DrlParser.java:139)
    at org.drools.compiler.PackageBuilder.drlToPackageDescr(PackageBuilder.java:477)
    at org.drools.compiler.PackageBuilder.addPackageFromDrl(PackageBuilder.java:466)
    at org.drools.compiler.PackageBuilder.addKnowledgeResource(PackageBuilder.java:694)
    at org.drools.builder.impl.KnowledgeBuilderImpl.add(KnowledgeBuilderImpl.java:51)
    at org.drools.builder.impl.KnowledgeBuilderImpl.add(KnowledgeBuilderImpl.java:40)
    at com.neu.als.thesis.units.InferenceEngine.readKnowledgeBase(InferenceEngine.java:52)
    at com.neu.als.thesis.web.controllers.FLTController.evaluateFLT(FLTController.java:124)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:444)
    at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:432)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:946)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:848)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:822)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.io.FileNotFoundException: 'neu/als/KnowledgeLevel.drl' cannot be opened because it does not exist
    at org.drools.io.impl.ClassPathResource.getURL(ClassPathResource.java:165)
    at org.drools.io.impl.ClassPathResource.getLastModified(ClassPathResource.java:177)
    ... 40 more

我究竟做错了什么?

更新:

这是我的 web.xml

<servlet>
      <servlet-name>ThesisProject</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>ThesisProject</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>/WEB-INF/ThesisProject-servlet.xml</param-value>
  </context-param>

  <listener>
     <listener-class>
        org.springframework.web.context.ContextLoaderListener
     </listener-class>
  </listener>

和我的 bean 配置

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="
   http://www.springframework.org/schema/beans     
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <context:component-scan base-package="com.neu.als.thesis.web.controllers" />

  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/" />
    <property name="suffix" value=".jsp" />
  </bean>

</beans>

添加了一些配置

4

3 回答 3

4

您的知识库是使用“neu/als/KnowledgeLevel”作为类路径构建的,而根据您的目录结构,正确的路径应该是“com/neu/als/KnowledgeLevel”。

我不希望 src/main/rules 出现在项目类路径中,这也可能会影响这一点。

我通常将我的规则保存在 src/main/resources 下,以便 Maven 将它们构建到类路径中。

于 2013-10-24T08:22:52.537 回答
2

只是为了帮助:如果有人使用代码:

KieFileSystem kieFileSystem = kieServices.newKieFileSystem();    
Resource resource = kieResources.newClassPathResource("EvaluationDossier.drl");
String resourcepath = "src/main/resources/rules" + "/" + "RulesCompiled.drl";
kieFileSystem.write(resourcepath, resource);

并面临这个例外:

Exception in thread « main » java.lang.RuntimeException: Unable to get LastModified for ClasspathResource
..........................
Caused by: java.io.FileNotFoundException: ‘EvaluationDossier.drl’ cannot be opened because it does not exist

经过几次搜索后,我通过在 pom.xml 中添加这些行来解决它

<build>
        <resources>
            <resource>
                <directory>src/main/resources/rules</directory>
            </resource>
            <resource>
                <directory>src/main/java</directory>
            </resource>
        </resources>
    </build>
于 2017-11-23T10:29:31.027 回答
1

您应该将规则文件放在资源文件夹中(这对我有用)。

在此处输入图像描述

在您的代码中,当您添加知识库时,只需将文件名保留为Example1.drlnot Example

于 2017-02-03T10:02:11.917 回答