2016-09-15 62 views

回答

0

您可以使用Drools API爲規則創作編譯DRL和其他Drools格式,並且您可以使用編譯的KieBase創建執行規則的會話。下面是一個例子,但是你必須因各種原因來適應它。

KieServices kieServices = KieServices.Factory.get(); 
KieFileSystem kfs = kieServices.newKieFileSystem(); 
FileInputStream fis = new FileInputStream("simple/simple.drl"); 
kfs.write("src/main/resources/simple.drl", 
      kieServices.getResources().newInputStreamResource(fis)); 
KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll(); 
Results results = kieBuilder.getResults(); 
if(results.hasMessages(Message.Level.ERROR)){ 
    System.out.println(results.getMessages()); 
    throw new IllegalStateException("### errors ###"); 
} 
KieContainer kieContainer = 
    kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId()); 
KieBase kieBase = kieContainer.getKieBase(); 
kieSession = kieBase.newKieSession(); 
// ... insert facts ... 
kieSession.fireAllRules(); 

對於配置KieBase和/或KieSession可能需要額外的調用。有關衆多詳細信息,請參閱API和Drools文檔。

+0

感謝您的回覆 – Xeetu

1
  1. 創建一個Maven項目(使用你喜歡的IDE或COMAND在線)

  2. 添加的Drools編譯器depedency和一些登錄兼容庫,你的pom.xml(主Maven項目文件):

    <dependencies> 
        <dependency> 
         <groupId>org.drools</groupId> 
         <artifactId>drools-compiler</artifactId> 
         <version>6.3.0.Final</version> 
        </dependency> 
        <dependency> 
         <groupId>ch.qos.logback</groupId> 
         <artifactId>logback-classic</artifactId> 
         <version>1.1.5</version> 
        </dependency> 
    </dependencies> 
    
  3. 創建的src /主/資源/ META-INF/kmodule.xml與文件的內容:

    <?xml version="1.0" encoding="UTF-8"?> 
    <kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule" /> 
    
  4. 創建類似的src /主/資源/ myrules.drl您的DRL文件

    rule "hello" 
        when 
         $name : String() 
        then 
         System.out.println("Hello "+$name);  
    end 
    
  5. 創建KieService基於代碼:

    import org.kie.api.KieServices; 
    import org.kie.api.runtime.KieContainer; 
    import org.kie.api.runtime.KieSession; 
    
    public class Main { 
    
        public static void main(String[] args) { 
    
         KieServices ks = KieServices.Factory.get(); 
         KieContainer kcontainer = ks.getKieClasspathContainer();     
         KieSession ksession = kcontainer.newKieSession(); 
    
         String name="Xeetu";  
         ksession.insert(name); 
    
         ksession.fireAllRules();     
        } 
    } 
    
相關問題