2015-08-18 50 views
0

我是新的在drools。我從我的drl文件執行了規則。 這是我的drl文件。如何在使用dsl和dslr文件時創建Kie會話?

rule "Person is 21" 
salience 1 
    when 
     $person : Person(age>=21,car.equalsIgnoreCase("duster"))    
    then 
     System.out.println($person.name); 
end 

這裏是我的模型文件

public class Person 
{ 
    private String name; 
    private int age; 
    private String car; 




    public Person() 
    { 

    } 

    public Person(String name, int age,String car) 
    { 
     this.name = name; 
     this.age = age; 
     this.car=car; 
    } 

    public String getName() 
    { 
     return name; 
    } 

    public void setName(String name) 
    { 
     this.name = name; 
    } 

    public int getAge() 
    { 
     return age; 
    } 

    public void setAge(int age) 
    { 
     this.age = age; 
    } 


    public String getCar() { 
     return car; 
    } 

    public void setCar(String car) { 
     this.car = car; 
    } 

} 

這是我認爲需要規則和事實作爲論據,並建立乃紀伊會議和火災規則

public void runRules(String[] rules, Object[] facts) 
    { 

     KieServices kieServices = KieServices.Factory.get(); 
     KieResources kieResources = kieServices.getResources(); 
     KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); 
     KieRepository kieRepository = kieServices.getRepository(); 

     for(String ruleFile : rules) 
     { 
      Resource resource = kieResources.newClassPathResource(ruleFile); 

      // path has to start with src/main/resources 
      // append it with the package from the rule 
      kieFileSystem.write(resource); 
     } 

     KieBuilder kb = kieServices.newKieBuilder(kieFileSystem); 

     kb.buildAll(); 

     if (kb.getResults().hasMessages(Level.ERROR)) 
     { 
      throw new RuntimeException("Build Errors:\n" + kb.getResults().toString()); 
     } 

     KieContainer kContainer = kieServices.newKieContainer(kieRepository.getDefaultReleaseId()); 

     KieSession kSession = kContainer.newKieSession(); 

     for (Object fact : facts) 
     { 
      kSession.insert(fact); 
     } 

     kSession.fireAllRules(); 
    } 

什麼樣的變化應該統治亞軍方法的代碼我在使用dslr文件時(以下是dslr文件)

rule "My First Rule" 

    when 
     Person is at least 21 and car is "duster" 
    then 
     Log : "hello world" 

end 

在eclipse中,dslr文件生成的drl文件是正確的。 下面是由DSL膨脹產生的DRL文件中的規則(我會在DRL觀衆看到日食對上述DSLR)

rule "My First Rule" 

    when 
     i: Person(age > 21, car=="duster") 
    then 
     System.out.println("hello world"); 

end 

我想從我的DRL文件運行相同的規則,並執行成功

我現在面臨的問題是,我試圖通過DSL和DSLR文件作爲參數傳遞給規則資源

Resource resource = kieResources.newClassPathResource("MyDslr.dslr","dslExample.dsl"); 

,我收到以下異常

Exception in thread "main" java.lang.RuntimeException: Build Errors: 
Error Messages: 
Message [id=1, level=ERROR, path=MyDslr.dslr, line=17, column=0 
    text=[17] No mapping entries for expanding:   Person is at least 21 and 
car is "duster"] 
Message [id=2, level=ERROR, path=MyDslr.dslr, line=17, column=0 
    text=[17] Unable to expand: Person is at least 21 and car is "duster"] 
Message [id=3, level=ERROR, path=MyDslr.dslr, line=19, column=0 
    text=[19] No mapping entries for expanding:   Log : "hello world"] 
Message [id=4, level=ERROR, path=MyDslr.dslr, line=19, column=0 
    text=[19] Unable to expand:   Log : "hello world"] 
Message [id=5, level=ERROR, path=MyDslr.dslr, line=17, column=0 
    text=[ERR 102] Line 17:15 mismatched input 'is' in rule "My First Rule"] 
--- 
Warning Messages: 
--- 
Info Messages: 

    at controller.RuleRunner.runRules(RuleRunner.java:43) 
    at mainPackage.Main.main(Main.java:21) 

這裏是我的DSL文件看起來像在日食(和它轉換DSLR在DRL是當RUL它直接從DRL文件轉換後的規則沒有給出例外) enter image description here

請幫我在這裏。如果有例子說明如何使用kie api和dslr和dsl文件激發規則,那麼這也將有所幫助。

感謝

回答

2

呼叫

... = kieResources.newClassPathResource("MyDslr.dslr","dslExample.dsl") 

不能正確 - 比較的Javadoc此方法:第二個變元的應說明的編碼。

這一系列語句,然而,工作原理:

FileInputStream fis1 = new FileInputStream("simple/simple.dsl"); 
kfs.write("src/main/resources/simple.dsl", 
      kieServices.getResources().newInputStreamResource(fis1)); 
FileInputStream fis2 = new FileInputStream("simple/simple.dslr"); 
kfs.write("src/main/resources/simple.dslr", 
      kieServices.getResources().newInputStreamResource(fis2)); 
KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll(); 

你應該打電話給你的方法runRules(String[] rules, Object[] facts)與路徑名的數組,DSL以及DSLR。

+0

非常感謝..完美的答案..謝謝.. – Yatin