使用readKnowledgebase导致Drools错误

时间:2015-04-23 18:22:54

标签: drools

我正在尝试一个非常基本的规则来测试drools是否有效,我将其与hibernate连接,但我发现错误“java.lang.ArrayIndexOutOfBoundsException:-1” 这是主要的: 包装方法;

import java.util.Iterator;
import java.util.List;

import metier.environnements.Reseau;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.hibernate.Query;
import org.hibernate.Session;

import util.HibernateUtil;



/* This is a sample class to launch a rule. */

public class testingRules {
public static void main(String[] args) {

        Session session =HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();



        Query query=session.createQuery("from Reseau");
        List l =query.list();
        Iterator reseaux = query .iterate();

        while (reseaux.hasNext()) {

        Reseau reseau = (Reseau) reseaux.next();

        // Set the process start arguments.


        StatefulKnowledgeSession sks = getSKS();
        sks.insert(reseau);
        sks.fireAllRules();
}

session.getTransaction().commit();



    }

    /**
     * Creates a basic {@link StatefulKnowledgeSession} which contains the <code>BPMN2</code> process definition of our sample process.
     * 
     * @return
     */
    private static StatefulKnowledgeSession getSKS() {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(
                ResourceFactory
                        .newClassPathResource("Reseau.drl"),
                ResourceType.DRL);
        KnowledgeBase kbase = kbuilder.newKnowledgeBase();

        return kbase.newStatefulKnowledgeSession();
    }

    }

和.drl文件是:

package metier.environnements

import metier.taches.ActiviteProfessionnelle;
import metier.taches.Utilisateur;



rule "Réseaux_disponibles"  // donne les réseaux disponibles dans la localisation de l'utilisateur
ruleflow-group "networks"
    when
      reseau :  Reseau (reseau.getCle() == "mot")
    then
        System.out.println ("Réseaux disponibles");

end

出现的错误是:

java.lang.ArrayIndexOutOfBoundsException: -1
    at org.eclipse.jdt.internal.compiler.parser.Parser.consumeCastExpressionWithNameArray(Parser.java:1984)
    at org.eclipse.jdt.internal.compiler.parser.Parser.consumeRule(Parser.java:5992)
    at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:9272)
    at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:9500)
    at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:9457)
    at org.eclipse.jdt.internal.compiler.parser.Parser.dietParse(Parser.java:8102)
    at org.eclipse.jdt.internal.compiler.Compiler.internalBeginToCompile(Compiler.java:696)
    at org.eclipse.jdt.internal.compiler.Compiler.beginToCompile(Compiler.java:376)
    at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:420)
    at org.drools.commons.jci.compilers.EclipseJavaCompiler.compile(EclipseJavaCompiler.java:389)
    at org.drools.commons.jci.compilers.AbstractJavaCompiler.compile(AbstractJavaCompiler.java:49)
    at org.drools.rule.builder.dialect.java.JavaDialect.compileAll(JavaDialect.java:371)
    at org.drools.compiler.DialectCompiletimeRegistry.compileAll(DialectCompiletimeRegistry.java:46)
    at org.drools.compiler.PackageRegistry.compileAll(PackageRegistry.java:102)
    at org.drools.compiler.PackageBuilder.compileAll(PackageBuilder.java:1006)
    at org.drools.compiler.PackageBuilder.compileAllRules(PackageBuilder.java:842)
    at org.drools.compiler.PackageBuilder.addPackage(PackageBuilder.java:831)
    at org.drools.compiler.PackageBuilder.addPackageFromDrl(PackageBuilder.java:467)
    at org.drools.compiler.PackageBuilder.addKnowledgeResource(PackageBuilder.java:673)
    at org.drools.builder.impl.KnowledgeBuilderImpl.add(KnowledgeBuilderImpl.java:45)
    at org.drools.builder.impl.KnowledgeBuilderImpl.add(KnowledgeBuilderImpl.java:34)
    at metier.testingRules.getSKS(testingRules.java:66)
    at metier.testingRules.main(testingRules.java:48)
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
    at org.eclipse.jdt.internal.compiler.parser.Parser.consumeCastExpressionWithNameArray(Parser.java:1984)
    at org.eclipse.jdt.internal.compiler.parser.Parser.consumeRule(Parser.java:5992)
    at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:9272)
    at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:9500)
    at org.eclipse.jdt.internal.compiler.parser.Parser.parse(Parser.java:9457)
    at org.eclipse.jdt.internal.compiler.parser.Parser.dietParse(Parser.java:8102)
    at org.eclipse.jdt.internal.compiler.Compiler.internalBeginToCompile(Compiler.java:696)
    at org.eclipse.jdt.internal.compiler.Compiler.beginToCompile(Compiler.java:376)
    at org.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:420)
    at org.drools.commons.jci.compilers.EclipseJavaCompiler.compile(EclipseJavaCompiler.java:389)
    at org.drools.commons.jci.compilers.AbstractJavaCompiler.compile(AbstractJavaCompiler.java:49)
    at org.drools.rule.builder.dialect.java.JavaDialect.compileAll(JavaDialect.java:371)
    at org.drools.compiler.DialectCompiletimeRegistry.compileAll(DialectCompiletimeRegistry.java:46)
    at org.drools.compiler.PackageRegistry.compileAll(PackageRegistry.java:102)
    at org.drools.compiler.PackageBuilder.compileAll(PackageBuilder.java:1006)
    at org.drools.compiler.PackageBuilder.compileAllRules(PackageBuilder.java:842)
    at org.drools.compiler.PackageBuilder.addPackage(PackageBuilder.java:831)
    at org.drools.compiler.PackageBuilder.addPackageFromDrl(PackageBuilder.java:467)
    at org.drools.compiler.PackageBuilder.addKnowledgeResource(PackageBuilder.java:673)
    at org.drools.builder.impl.KnowledgeBuilderImpl.add(KnowledgeBuilderImpl.java:45)
    at org.drools.builder.impl.KnowledgeBuilderImpl.add(KnowledgeBuilderImpl.java:34)
    at metier.testingRules.getSKS(testingRules.java:66)
    at metier.testingRules.main(testingRules.java:48)

你知道它来自哪里以及我如何解决它? 我真的很感激

the class Reseau definition is : 
package metier.environnements;

public class Reseau {

    private Long idReseau;
    private String nom; 
    private String cle;//mot de passe  
    private String etat ;  // disponible ou non 
    private String vitesseTransfert;  //speed
    private String type ; // 3G , LTE , Wifi ...
    private Localisation  localisation;

    // Les getters and setters.

    public Long getIdReseau() {
        return idReseau;
    }


    public void setIdReseau(Long idReseau) {
        this.idReseau = idReseau;
    }


    public String getNom() {
        return nom;
    }


    public void setNom(String nom) {
        this.nom = nom;
    }


    public String getCle() {
        return cle;
    }


    public void setCle(String cle) {
        this.cle = cle;
    }


    public String getEtat() {
        return etat;
    }


    public void setEtat(String etat) {
        this.etat = etat;
    }


    public String getVitesseTransfert() {
        return vitesseTransfert;
    }


    public void setVitesseTransfert(String vitesseTransfert) {
        this.vitesseTransfert = vitesseTransfert;
    }


    public String getType() {
        return type;
    }


    public void setType(String type) {
        this.type = type;
    }


    public Localisation getLocalisation() {
        return localisation;
    }


    public void setLocalisation(Localisation localisation) {
        this.localisation = localisation;
    }



    // Constructeur par defaut  

    public Reseau() {
        super();
        // TODO Auto-generated constructor stub
    }

    // Le constructeur avec les champs , le id sera genere automatiquement  
    public Reseau(String nom, String cle, String etat, String vitesseTransfert,
            String type, Localisation localisation) {
        super();
        this.nom = nom;
        this.cle = cle;
        this.etat = etat;
        this.vitesseTransfert = vitesseTransfert;
        this.type = type;
        this.localisation = localisation;
    }

1 个答案:

答案 0 :(得分:0)

永远不要在没有检查错误的情况下调用DRL编译器:

kbuilder.add(..., ResourceType.DRL);
if( kbuilder.hasErrors() ){
    System.err.println( "### compilation errors ###" );
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    for( KnowledgeBuilderError err: errors ){
        System.err.println( err.toString() );
    }
    throw new IllegalStateException( "compile errors" );
}

但问题看起来像是DRL / Java解析步骤中的错误。你使用哪个Drools版本?而Reseau类的定义对于重现错误是必要的。

旧(5.x)Drools版本中的错误不太可能得到解决。尝试6.2。