2012-02-12 6 views
0

ルールエンジンとしてジェスを使用して、我々はいくつかの証人は、いくつかの場所で、人を見て、そして時間に関連付けられた事実を主張することができます:ルールでJessで同じ値のファクトを数えることはできますか?

(deffacts witnesses 
    (witness Batman Gotham 18) 
    (witness Hulk NYC 19) 
    (witness Batman Gotham 2) 
    (witness Superman Chicago 22) 
    (witness Batman Gotham 10) 
) 

を、私はいくつかの目撃者が持っているかどうかを知りたいです同じ場所で同じ人を見て、時間を考慮しなかった。

ジェスのドキュメントでは、我々は100Kとよりのsalrariesを行う従業員をカウントするため、この例を得た:

(defrule count-highly-paid-employees 
    ?c <- (accumulate (bind ?count 0)      ;; initializer 
    (bind ?count (+ ?count 1))     ;; action 
    ?count          ;; result 
    (employee (salary ?s&:(> ?s 100000)))) ;; CE 
    => 
    (printout t ?c " employees make more than $100000/year." crlf)) 

だから私は、前の例で私のコードをベース:「(deffacts)で

(defrule count-witnesses 
    (is-lost ?plost) 
    (witness ?pseen ?place ?time) 
    ?c <- (accumulate (bind ?count 0) 
    (bind ?count (+ ?count 1)) 
    ?count 
    (test()) ; conditional element of accumulate 
    (test (= ?plost ?pseen)) 
    (test (>= ?count 3)) 
    => 
    (assert (place-seen ?place)) 
) 

を上記のルールとルールに従って、エンジンはその事実を主張しなければならない。

(place-seen Gotham) 

バットマンはゴッサムで3回見たので。

'accumulate'の条件付き要素(CE)部分の使い方はわかりません。同じ人と場所で事実を保持するために「テスト」を使うことはできますか?

どのようにこれを達成するためのアイデアですか?

ありがとうございました!


注:「蓄積」のsynthaxは、私はあなたがまさに、それが何であるかを説明していないとして、?plostについてのものを除外するつもりです

(accumulate <initializer> <action> <result> <conditional element>) 

答えて

1

です。必要に応じて自分で追加することができます。

あなたが望む(ほとんど)基本的なルールは次のとおりです。あなたが得ていなかったCEの部分は、我々が蓄積したいパターンです。ここでは、それが最初の人にマッチした事実と同じ場所で目撃同じ人との事実と一致します。

(defrule count-witnesses 
    ;; Given that some person ?person was seen in place ?place 
    (witness ?person ?place ?) 
    ;; Count all the sightings of that person in that place 
    ?c <- (accumulate (bind ?count 0) 
        (bind ?count (+ ?count 1)) 
        ?count 
        (witness ?person ?place ?)) 
    ;; Don't fire unless the count is at least 3 
    (test (>= ?c 3)) 
    => 
    (assert (place-seen ?person ?place)) 
) 

さて、このルールの唯一の問題は、それがあなたのdeffactsについて3回発射するだろうということです、バットマン/ゴッサムの事実ごとに1回ずつ。最初のパターンを変更して、指定された場所で最も早い人物の照準と一致するようにして、これをやめることができます。

(defrule count-witnesses 
    ;; Given that some person ?person was seen in place ?place, and there is no other 
    ;; sighting of the same person at the same place at an earlier time 
    (witness ?person ?place ?t1)  
    (not (witness ?person ?place ?t2&:(< ?t2 ?t1))) 
    ;; Count all the sightings of that person in that place 
    ?c <- (accumulate (bind ?count 0) 
        (bind ?count (+ ?count 1)) 
        ?count 
        (witness ?person ?place ?)) 
    ;; Don't fire unless the count is at least 3 
    (test (>= ?c 3)) 
    => 
    (assert (place-seen ?person ?place)) 
) 
関連する問題