2011-03-13 91 views
1

我正在嘗試對我的動態Web應用程序使用簡單的Drools規則。我創建了我的servlet,現在試圖整合我的規則。但它給我的錯誤在這行在Glassfish服務器上使用Drools

RuleBase ruleBase = RuleBaseFactory.newRuleBase(); 

java.lang.NoSuchMethodError: org.drools.util.ChainedProperties.<init>(Ljava/lang/String;Ljava/lang/ClassLoader;Z)V 
    at org.drools.RuleBaseConfiguration.init(RuleBaseConfiguration.java:395) 
    at org.drools.RuleBaseConfiguration.<init>(RuleBaseConfiguration.java:251) 
    at org.drools.common.AbstractRuleBase.<init>(AbstractRuleBase.java:147) 
    at org.drools.reteoo.ReteooRuleBase.<init>(ReteooRuleBase.java:160) 
    at org.drools.reteoo.ReteooRuleBase.<init>(ReteooRuleBase.java:137) 
    at org.drools.RuleBaseFactory.newRuleBase(RuleBaseFactory.java:87) 
    at org.drools.RuleBaseFactory.newRuleBase(RuleBaseFactory.java:75) 
    at org.drools.RuleBaseFactory.newRuleBase(RuleBaseFactory.java:38) 
    at server.HelloServlet.runRules(HelloServlet.java:148) 
    at server.HelloServlet.NewRule(HelloServlet.java:181) 
    at server.HelloServlet.doGet(HelloServlet.java:82) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) 
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523) 

,在這裏我Helloservlet.java類

@WebServlet( 「/ HelloServlet」) 公共類HelloServlet延伸的HttpServlet { 私有靜態最後的serialVersionUID長= 1升;

@EJB HelloBean bean; 

@PersistenceUnit 
EntityManagerFactory emf; 




/** 
* @see HttpServlet#HttpServlet() 
*/ 
public HelloServlet() { 
    super(); 
    // TODO Auto-generated constructor stub 
} 

/** 
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
*/ 
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    PrintWriter out=response.getWriter(); 
    out.print("<html><body><title>servlet page</title>"); 
    out.print("<h1> " +request.getServletPath() + "</h1>"); 
    out.println("<h2>" + bean.sayHello("nabiyin") + "</h2>"); 
MyUser user=(MyUser)emf.createEntityManager().createQuery("select u from MyUser u").getResultList().get(0); 

try { 
NewRule(); 
} catch (Exception e) { 
    // TODO Auto-generated catch block 
    System.out.println("HATAAAAA"); 
    e.printStackTrace(); 
} 




    out.print("age: " + user.getAge()); 

    if (user.getAge()<23) { 
     out.println("hello"); 
    } 

    out.print("</html></body>"); 
} 

public void runRules(String[] rules, Object... facts) 
throws Exception 
{  
    RuleBase ruleBase = RuleBaseFactory.newRuleBase(); 
    PackageBuilder builder = new PackageBuilder();  

    for(String ruleFile : rules) 
    { 
     System.out.println("Loading file: "+ruleFile); 
     builder.addPackageFromDrl(
       new InputStreamReader(
        this.getClass().getResourceAsStream(ruleFile))); 
    } 

    Package pkg = builder.getPackage();  
    ruleBase.addPackage(pkg); 

    WorkingMemory workingMemory 
     = ruleBase.newStatefulSession(); 

    for(Object fact : facts) 
    { 
     System.out.println("Inserting fact: "+fact); 
     workingMemory.insert(fact); 
    } 

    workingMemory.fireAllRules(); 
} 


public void NewRule() 
throws Exception 
{ 
    System.out.println("New rule started"); 

    Number n1=3 , n2=1, n3=4 , n4=1 ,n5=5; 
    new HelloServlet().runRules(
      new String[]{"model/NewRule.drl"}, 
      n1,n2,n3,n4,n5); 

    System.out.println("New rule finisheds"); 
} 

/** 
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
*/ 
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    // TODO Auto-generated method stub 
} 

回答

0

我建議使用規則引擎在你的web應用程序singelton EJB,避免每個Web會話創建自己的規則引擎實例(爲簡單的演示上面並不重要)。

你可以嘗試使用當前的庫並再試一次。 (也許現在是錯誤的詞,但是是有區別的!)
看看在當前的Drools分佈的樣品或簡單樣品:http://javadude.wordpress.com/2012/03/01/drools-expert-read-rules-from-string-and-database/

.. 
import org.drools.KnowledgeBase; 
import org.drools.KnowledgeBaseFactory; 
import org.drools.builder.KnowledgeBuilder; 
import org.drools.builder.KnowledgeBuilderFactory; 
import org.drools.builder.ResourceType; 
import org.drools.definition.KnowledgePackage; 
.. 

(專家可能會如果我錯了糾正)