Drools没有使用spring和Glassfish编译web资源中的drl文件

时间:2017-09-28 03:56:03

标签: spring maven glassfish drools

我有一个使用Spring和JSF的maven web项目,Drools没有使用Glassfish容器编译资源目录下的drl文件,但是使用Wildfly它会显示并显示syntaxis错误,但是在glassfish中没有。每次插入对象时,都不会声明任何规则,因为未编译drl文件。我尝试将这些文件放在类包中,没有结果。 如果在beans配置中删除了packages属性标志

 <kie:kbase name="kbase5"  packages="rulez" default="true">
像这样

<kie:kbase name="kbase5"  default="true">

另一个垃圾drl文件是show编译的,因此有错误,因为这些类不属于当前项目。

任何帮助都将受到赞赏。

我的xhtml文件

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:b="http://bootsfaces.net/ui"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <title>Materia</title>
    <meta name="author" content="Cesar Garcia"></meta>
</h:head>
<h:body style="padding: 60px; min-height: 2000px;">
 <h:form>
    <p:panelGrid class="ui-noborder">
    <p:row> 
        <p:column>
            Calificacion
        </p:column>
        <p:column>  
                <p:inputNumber id="input1" value="#{calificacionManageBean.calificacion}">  

                </p:inputNumber>  
        </p:column>   
    </p:row>
        <p:row> 
            <p:column>
               Nombre Materia
            </p:column>
            <p:column>
                <p:inputText value="#{calificacionManageBean.nombreMateria}"/>
            </p:column>
        </p:row>

        <p:row> 
            <p:column>
               <p:commandButton value="Agregar" id="nonAjax" actionListener="#{calificacionManageBean.buttonAction}" update="table"/>
            </p:column>
        </p:row>
    </p:panelGrid>
    <p:dataTable id="table" var="calif"
            value="#{calificacionManageBean.calificaciones}">
            <p:column headerText="Materia">
                <h:outputText value="#{calif.materia}" />
            </p:column>

            <p:column headerText="Valor">
                <h:outputText value="#{calif.valor}"  >
                    <f:convertNumber minFractionDigits="2" />
                </h:outputText>
            </p:column>
    </p:dataTable>

</h:form>
</h:body>
</html>

我的服务

package drools.services;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.Message;
import org.kie.api.builder.Results;
import org.kie.api.cdi.KSession;
import org.kie.api.definition.KiePackage;
import org.kie.api.logger.KieRuntimeLogger;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import drools.model.Calificacion;

@Service("CalificacionServiceImpl")
@Transactional(readOnly = false)
public class CalificacionServiceImpl implements CalificacionService{

    @Autowired
    private KieBase kieBase;

    @Autowired 
    ApplicationContext context; 

    @Override
    public Boolean evaluarCalificacion(Double calificacion) {

        return false;
    }

    public KieBase getKieBase() {
        return kieBase;
    }

    public void setKieBase(KieBase kieBase) {
        this.kieBase = kieBase;
    }

    @Override
    public boolean  evaluaCalificaion(Calificacion calif) {


    /*  ClassPathResource resource = new ClassPathResource("drools/rules/ruless.drl");
        try {
            InputStream inputStream = resource.getInputStream();
            System.out.println("Encontrado");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("No Encontrado");
            e.printStackTrace();
        }
        KieSession ks = (KieSession) kieBase.newKieSession();

        ks.insert(calif);
        int countRules = ks.fireAllRules();*/
        int countRules =0;
        ClassPathResource resource = new ClassPathResource("drools/rules/ruless.drl");
        try {
            InputStream inputStream = resource.getInputStream();
            KieServices kieServices = KieServices.Factory.get();
            KieContainer kieContainer = kieServices.getKieClasspathContainer();
             Results results = kieContainer.verify();
                results.getMessages().stream().forEach((message) -> {
                    System.out.println(">> Message ( "+message.getLevel()+" ): "+message.getText());
                });

                kieContainer.getKieBaseNames().stream().map((kieBase) -> {
                    System.out.println(">> Loading KieBase: "+ kieBase );
                    return kieBase;
                }).forEach((kieBase) -> {
                    kieContainer.getKieSessionNamesInKieBase(kieBase).stream().forEach((kieSession) -> {
                        System.out.println("\t >> Containing KieSession: "+ kieSession );
                    });
                });

                // Let's load the configurations for the kmodule.xml file defined 
                //  in the chapter-03-simple-discounts/src/main/resources/META-INF/ directory
                // Notice that here the rule defined in the tests is not loaded
            //    KieSession kieSession = kieContainer.newKieSession("rulesz");
            //    kieSession.insert(calif);

            byte[] bytes = IOUtils.toByteArray(resource.getInputStream());


            KieSession kieSession = (KieSession) context.getBean("ksession6");
            KieRuntimeLogger logger =   
            KieServices.Factory.get().getLoggers().newFileLogger(kieSession, "/Aplicaciones_Cesar/drools/mylogfile");   

            kieSession.insert(calif);
            countRules= kieSession.fireAllRules();
            System.out.println("EvaluaCalificacion "+ countRules);
            Collection<KiePackage> ki = kieBase.getKiePackages();
            for(KiePackage k:ki){
             System.out.println("kie package "+k.getName());    
            }

            logger.close();
        //  System.out.println("Encontrado");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            System.out.println("No Encontrado");
            e.printStackTrace();
        }
        return countRules>=1?true:false;
    }
}

我的管理员

package drools.managebean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.springframework.beans.factory.annotation.Autowired;

import drools.model.*;
import drools.services.CalificacionService;

@ManagedBean(name = "calificacionManageBean")
@SessionScoped
public class CalificacionManageBean implements Serializable{

    List<Calificacion> calificaciones;


    Double calificacion=0.0d;
    String nombreMateria="";

    @ManagedProperty(value = "#{CalificacionServiceImpl}")  
    CalificacionService calificacionService;

    private boolean value1;  

    @PostConstruct
    public void init() {
        calificaciones = new ArrayList<Calificacion>();
        Calificacion  historia = new Calificacion(1.0,"Historia");
        Calificacion  mat = new Calificacion(1.0,"Matematicas");
        calificaciones.add(historia);
        calificaciones.add(mat);
    }

    public List<Calificacion> getCalificaciones() {
        return calificaciones;
    }

    public void setCalificaciones(List<Calificacion> calificaciones) {
        this.calificaciones = calificaciones;
    }

    public Double getCalificacion() {
        return calificacion;
    }

    public void setCalificacion(Double calificacion) {
        this.calificacion = calificacion;
    }

    public void buttonAction(ActionEvent actionEvent) {
        addMessage("Welcome to Primefaces!!");
        Calificacion tempCalif = new Calificacion(this.getCalificacion(),this.getNombreMateria());
        calificaciones.add(tempCalif);
        calificacionService.evaluaCalificaion(tempCalif);
    }

    public void addMessage(String summary) {
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, summary,  null);
        FacesContext.getCurrentInstance().addMessage(null, message);

    }

    public String getNombreMateria() {
        return nombreMateria;
    }

    public void setNombreMateria(String nombreMateria) {
        this.nombreMateria = nombreMateria;
    }

       public boolean isValue1() {
            return value1;
        }

        public void setValue1(boolean value1) {
            this.value1 = value1;
        }

        public CalificacionService getCalificacionService() {
            return calificacionService;
        }

        public void setCalificacionService(CalificacionService calificacionService) {
            this.calificacionService = calificacionService;
        }

我的kie语境

<?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:kie="http://drools.org/schema/kie-spring"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                        http://drools.org/schema/kie-spring 
                        https://raw.github.com/droolsjbpm/droolsjbpm-integration/master/kie-spring/src/main/resources/org/kie/spring/kie-spring-6.0.0.xsd">



     <kie:import />
     <kie:kmodule id="kie-spring-primerafase">
        <kie:kbase name="kbase5"  packages="rulez" default="true">
           <kie:ksession name="ksession6" >
                <kie:consoleLogger/>
                <kie:fileLogger id="fl_logger" file="/Aplicaciones_Cesar/drools/mylog"/>
           </kie:ksession>
           <kie:ksession name="ksession7"/>
        </kie:kbase>
      </kie:kmodule>

      <bean id="kiePostProcessor" class="org.kie.spring.KModuleBeanFactoryPostProcessor"/>

    </beans>

我的口水文件

 package rulez;

    import drools.model.Calificacion;

    rule "Regla"
      when
         $calification : Calificacion( valor >= 10 )
      then
         System.out.println("Valor Excelente" + $calification.getValor());

    end 

    rule  "regla calif"
        when
           Calificacion()
        then
        System.out.println("Calificacion creada" );
    end

    rule  "regla calif2"
        when
           Object()
        then
           System.out.println("Calif Creado");
    end

my faces-config

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
    version="2.2">

    <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>

    </application>
</faces-config>

我的ApplicationContext

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:kie="http://drools.org/schema/kie-spring"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans.xsd                                                 
                        http://www.springframework.org/schema/context                                                
                        http://www.springframework.org/schema/context/spring-context.xsd                                                 
                        http://www.springframework.org/schema/tx                                                 
                        http://www.springframework.org/schema/tx/spring-tx.xsd
                        http://www.springframework.org/schema/faces
                        http://www.springframework.org/schema/faces/spring-faces.xsd">

    <context:component-scan base-package="drools.services" />



    <!-- Enable the configuration of transactional behavior based on annotations -->
    <!--  tx:annotation-driven transaction-manager="transactionManager" /-->

    <bean id="calificacionbean" class="drools.model.Calificacion">  
        <!-- property name="valor" value="1"></property-->  
    </bean> 

我的web.xml

    <?xml version='1.0' encoding='UTF-8'?>

<web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

    <display-name>mojarra-regression-test</display-name>
    <description>A simple regression test to make it easier to get your bug fixed. The only reason we need a web.xml is to set the PROJECT_STAGE to Develoment.  If you have a web.xml, then you need to map the FacesServlet.</description>
     <context-param>
      <param-name>contextConfigLocation</param-name>
       <param-value>                
                 /WEB-INF/resources/applicationContext.xml      
                 /WEB-INF/resources/kie-context.xml                        
        </param-value>
    </context-param>
      <context-param>
    <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
    <param-value>true</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

    <context-param>
        <description>
            Tell the runtime where we are in the project development
            lifecycle.  Valid values are: 
            Development, UnitTest, SystemTest, or Production.
            The runtime will display helpful hints to correct common mistakes
            when the value is Development.
        </description>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

    <!-- Faces Servlet -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

   <servlet-mapping>
       <servlet-name>Faces Servlet</servlet-name>
       <url-pattern>/faces/*</url-pattern>
   </servlet-mapping>

   <welcome-file-list>
     <welcome-file>faces/calificacion.xhtml</welcome-file>
   </welcome-file-list>

</web-app>

我的pom

<?xml version="1.0" encoding="UTF-8"?>

http://maven.apache.org/xsd/maven-4.0.0.xsd“&GT;     4.0.0

<groupId>droolstezt</groupId>
<artifactId>web</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>${project.artifactId}</name>
<description>A simple project with war packaging that depends on JSF 2.2 and 
    javaee 6, in that order.</description>
<url>http://jsf-spec.java.net/</url>
<build>
    <finalName>${project.artifactId}</finalName>
   <resources>
    <!--  resource>
        <filtering>true</filtering>
        <directory>src/main/resources</directory>
    </resource-->

        <resource>
            <directory>src/main/resources</directory>
        </resource>

  <!--  -resource>
    <directory>${basedir}/src/main/resources</directory>
  </resource-->

</resources>

    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <configuration>
              <failOnMissingWebXml>true</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>

                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.20.1</version>
                <configuration>
                    <parallel>methods</parallel>
                    <threadCount>10</threadCount>
                </configuration>
                <!-- configuration>
                    <forkCount>3</forkCount>
                    <reuseForks>true</reuseForks>
                    <argLine>-Xmx1024m</argLine>
                    <systemPropertyVariables>
                        <databaseSchema>MY_TEST_SCHEMA_${surefire.forkNumber}</databaseSchema>
                    </systemPropertyVariables>
                    <workingDirectory>FORK_DIRECTORY_${surefire.forkNumber}</workingDirectory>
                </configuration -->
            </plugin>

            <!--  plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                  <webResources>
                    <resource>
                       this is relative to the pom.xml directory >
                      <directory>src/main/resources/csrules</directory>
                          <includes>
                            <include>**/*.drl</include>
                          </includes>
                    </resource>
                  </webResources>
                </configuration>
            </plugin-->
            <plugin>

                <groupId>org.kie</groupId>

                <artifactId>kie-maven-plugin</artifactId>

                <version>${drools.version}</version>

                <extensions>true</extensions>

              </plugin>
    </plugins>

    <!--  resources>
      <resource>
        <targetPath>target/classes</targetPath>
        <filtering>false</filtering>
        <directory>${basedir}/src/main/resources/csrules</directory>
        <includes>
          <include>*.drl</include>
        </includes>
        <excludes>
          <exclude>**/*.properties</exclude>
        </excludes>
      </resource>
    </resources>
    <testResources>

    </testResources-->
</build>
<properties>
  <spec.snapshot.version>2.2</spec.snapshot.version>
  <spring.version>4.2.5.RELEASE</spring.version>
    <springsecurity.version>3.2.9.RELEASE</springsecurity.version>
    <drools.version>6.5.0.Final</drools.version>
</properties>

<repositories>
    <repository>
        <id>java.net-maven2-SNAPSHOT-repository</id>
        <name>Java.net SNAPSHOT-Repository for Maven</name>
        <url>https://maven.java.net/content/repositories/snapshots/</url>
        <layout>default</layout>
    </repository>
    <repository>
        <id>java.net-maven2-repository</id>
        <name>Java.net Repository for Maven</name>
        <url>https://maven.java.net/content/repositories/releases/</url>
        <layout>default</layout>
    </repository>
    <repository>
        <id>prime-repo</id>
        <name>Prime Repo</name>
        <url>http://repository.primefaces.org</url>
    </repository>
    <!-- repository> <id>spring-milestone</id> <url>http://repo.spring.io/libs-milestone</url> 
        <snapshots> <enabled>false</enabled> </snapshots> </repository -->
    <repository>
        <id>spring-milestone</id>
        <name>Spring Maven MILESTONE Repository</name>
        <url>http://repo.springsource.org/libs-milestone</url>
    </repository>

    <repository>
        <id>jboss-public-repository-group</id>
        <name>JBoss Public Repository Group</name>
        <url>http://repository.jboss.org/nexus/content/groups/public/</url>
        <releases>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
        </releases>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>daily</updatePolicy>
        </snapshots>
    </repository>

</repositories>

   <dependencies>

 ...
</dependencies>

1 个答案:

答案 0 :(得分:-1)

添加流口水{compiler,core ...}所需的依赖项注入