2016-04-13 16 views
0

私はDrools規則を書くのに助けが必要です。私はContextとCreditReportの2つのクラスを持っています。Drools:文字列からオブジェクトへのマップを含む規則

コンテキストは、ルールが起動される前にファクトとしてナレッジセッションに挿入されます。

は、私は直接セッションにCreditReportを挿入したい、版画「エクセレント」コンソールには、クレジットスコアが800以上

理想的であるルールを記述する必要があるが、残念ながら私はそれを持っていません。オプション。

私のように良い見ていない書いたルール:

  1. が、その後部分がありif文私は
CreditReportにオブジェクトを型にキャストしています
  • ご協力いただきありがとうございます。

    // Context.java

    public class Context { 
        private Map<String, Object> data = Maps.newHashMap(); 
    
        public <T> T getData(final String key, final Class<T> clazz) { 
         return clazz.cast(data.get(key)); 
        } 
    
        public void putData(final String key, final Object value) { 
         this.data.put(key, value); 
        } 
    } 
    

    // CreditReport.java

    public class CreditReport { 
        private final String name; 
        private final int creditScore; 
    
        public String getName() { 
         return this.name; 
        } 
    
        public int getCreditScore() { 
         return this.creditScore; 
        } 
    
    } 
    

    // Mainメソッド

    context.put("creditReport", new CreditReport("John", 810)); 
    session.insert(Arrays.asList(context)); 
    session.fireAllRules(); 
    

    //ルール

    rule "Excellent_Score" 
    when Context($creditReportObject : getData("creditReport")) 
    then 
        final CreditReport creditReport = (CreditReport) $creditReportObject; 
        if (creditReport.getCreditScore() >= 800) { 
         System.out.println("Excellent"); 
        } 
    end 
    
  • 答えて

    1

    単一のContextオブジェクトを含むList<Context>を挿入するにはどうすればよいですか? Javaコードは、ルールが、今あなたが、もちろん、コンテキストからCreditReportを取得し、挿入することができ

    rule "Excellent_Score" 
    when 
        Context($creditReportObject : getData("creditReport")) 
        CreditReport(creditScore > 800) from $creditReportObject 
    then 
        System.out.println("Excellent"); 
    end 
    

    のように書くことができ

    context.put("creditReport", new CreditReport("John", 810)); 
    session.insert(context); 
    session.fireAllRules(); 
    

    を行う必要があります。 - 私はそれがあなたが示したことがより複雑であると思うが、 "私はその選択肢を持っていない"とにかくコードの匂いである。

    編集この2つのルールは、あなたがメソッドにRHSをラップすることができますことを考慮することをはるかに良いではないかが、「優れた」印刷するための複数の理由のための単一のルールは、以下のいずれかのように書くことができDRL機能。

    rule "Excellent_Score_2" 
    when 
        Context($creditReport : getData("creditReport"), 
          $account: getData("account")) 
        (CreditReport(creditScore > 800) from $creditReport 
        or 
        Account(balance >= 5000) from $account) 
    then 
        System.out.println("Excellent"); 
    end 
    
    +0

    ありがとうございます!あなたはすべての点数が正しいです。 1つのフォローアップの質問。 ContextにAccountという別のオブジェクトが含まれている場合CreditReport.creditScore> = 800またはAccount.balance> = 5000のときに、「Excellent」と表示されるルールを作成したいと思いますか? OR関係ではなくOR関係でルールを書く方法は分かっています。私もevalの使用を避けようとしています – user544192

    +0

    単一のルールではあまり得られません。 – laune

    +0

    もう一度ありがとう! :) – user544192

    関連する問題