跳轉到內容

專家系統/Jess

來自華夏公益教科書

關於 Jess

[編輯 | 編輯原始碼]

Jess 是一個用於 Java 平臺的規則引擎,它是 CLIPS 的超集,由桑迪亞國家實驗室的歐內斯特·弗裡德曼-希爾開發。它最初是在 1995 年後期編寫的。

它提供了基於規則的程式設計,適用於自動化專家系統,通常被稱為 專家系統外殼。近年來,智慧代理系統也得到發展,它們依賴於類似的功能。

Jess 不使用過程式正規化,其中一個程式只有一個迴圈,並且該迴圈只啟用一次,而是使用 Jess 使用的宣告式正規化,透過稱為 模式匹配 的過程,持續將規則集合應用於事實集合。規則可以修改事實集合,或者可以執行任何 Java 程式碼。

Jess 可用於構建 Java servlet、EJB、applet 和完整的應用程式,這些應用程式使用宣告性規則形式的知識來得出結論和進行推斷。由於許多規則可能匹配許多輸入,因此幾乎沒有有效的通用匹配演算法。Jess 規則引擎使用 Rete 演算法。

雖然 CLIPS 作為開源軟體獲得許可,但 Jess 不是開源軟體。

程式碼示例

  ; is a comment

 (bind ?x 100)
 
 ; x = 100

 (deffunction max (?a ?b)
   (if (> ?a ?b) then ?a else ?b))

 (deffacts myroom
    (furniture chair)
    (furniture table)
    (furniture bed)
 )

 (deftemplate car
    (slot color)
    (slot mileage)
    (slot value)
 )

 (assert (car (color red) (mileage 10000) (value 400)))

示例程式碼

 (clear)
 (deftemplate blood-donor (slot name) (slot type))
 (deffacts blood-bank ; put names & their types into [[working memory(jess)|working memory]]
       (blood-donor (name "Alice")(type "A"))
       (blood-donor (name "Agatha")(type "A"))
       (blood-donor (name "Bob")(type "B"))
       (blood-donor (name "Barbara")(type "B"))
       (blood-donor (name "Jess")(type "AB"))
       (blood-donor (name "Karen")(type "AB"))
       (blood-donor (name "Onan")(type "O"))
       (blood-donor (name "Osbert")(type "O"))
 )
 (defrule can-give-to-same-type-but-not-self ; handles A > A, B > B, O > O, AB > AB, but not N1 > N1
       (blood-donor (name ?name)(type ?type))
       (blood-donor (name ?name2)(type ?type2 &:(eq ?type ?type2) &: (neq ?name ?name2)  ))
       =>
       (printout t ?name " can give blood to " ?name2 crlf)
 )
 (defrule O-gives-to-others-but-not-itself ; O to O cover in above rule
       (blood-donor (name ?name)(type ?type &:(eq ?type "O")))
       (blood-donor (name ?name2)(type ?type2 &: (neq ?type ?type2) &: (neq ?name ?name2)  ))
       =>
       (printout t ?name " can give blood to " ?name2 crlf)
 )
 (defrule A-or-B-gives-to-AB ; case O gives to AB and AB gives to AB already dealt with
       (blood-donor (name ?name)(type ?type &:(or (eq ?type "A") (eq ?type "B" ))))
       (blood-donor (name ?name2)(type ?type2 &: (eq ?type2 "AB")  &: (neq ?name ?name2)  ))
       =>
       (printout t ?name " can give blood to " ?name2 crlf)
 )
 ;(watch all)
 (reset)
 (run)
華夏公益教科書