You are on page 1of 10

Jess 程式開發 撰寫者:陳志華

Jess 程式開發

一、安裝 Jess
(1) 下載 Jess
Jess 網址:http://herzberg.ca.sandia.gov/jess/

(2) 輸入相關資訊,並點選” I agree with the license; take me to software download


area”

第1頁
Jess 程式開發 撰寫者:陳志華

(3) 點 選 ” Stable:Jess 7.0p1 classes, docs and samples (trial version)”, 下 載


Jess70p1.zip

(4) 解壓縮 Jess70p1.zip,本文件以 C:\Jess 為例

(5) 設定環境變數,新增環境變數 classpath,值為 C:\Jess\lib\jess.jar

第2頁
Jess 程式開發 撰寫者:陳志華

(6) 測試環境
(i) 開啟”命令提示字元”
(ii) 切換到 jess 資料庫,輸入 cd\jess
(iii) Java jess.Main examples/jess/hello.clp
(iv) 成功顯示 Hello, world! 字樣

第3頁
Jess 程式開發 撰寫者:陳志華

二、安裝 Eclipse plugin for Jess


(1) 解壓縮 Jess70p1.zip,將 jess/eclipse 目錄裡的壓縮檔複製到 eclipse 所在目錄
下,然後直接解壓縮

(2) 執行 eclipse,開啟「File\New\Project」

(3) 建立新專案,本文件以 Jess 名稱為例

第4頁
Jess 程式開發 撰寫者:陳志華

(4) 設定專案屬性,於專案名稱點選滑鼠右鍵,並選擇 Properties

(5) 選擇「Java Build Path\Libraries」


,點選「Add External JARs…」加入外部 JAR
檔 jess.jar,完成後點選「OK」

第5頁
Jess 程式開發 撰寫者:陳志華

(6) 新增 hello.clp,並輸入 (printout t "Hello, world!" crlf)

(7) 點選「 」進行編譯和執行,結果如下圖中飲 Console

第6頁
Jess 程式開發 撰寫者:陳志華

三、Jess 基本語法
(1) Symbols
z A Jess symbol 可以包含 letters, numbers,以及下列標點符號: $, *, ., =, =,
/, >, <, _, ?, and #
z A Jess symbol 開頭不能是數字
z ($, ?, and =)這幾個標點符號不能使用在 symbol 開頭
z Valid symbols:
„ first-name
„ Name1
„ name#1
„ name$1
„ _abc

(2) Special symbols


z nil → null
z TRUE, FALSE (uppercase)
z crlf

(3) Numbers
z 3
z 4.
z 2.323

(4) Strings
z “Hello !”
z “\”Nonsense, \” he said firmly.”

(5) Comments
z ;;this is a comment

(6) List
z (+ 3 2)
z (a b c)
z (“Hello, World!”)
z ()
z (1 2 3)
z (deftemplate foo (slot bar))

第7頁
Jess 程式開發 撰寫者:陳志華

(7) Calling functions


z (+ (* 2 2) (- 3 2))
z (printout t “Hello World!”)
z (batch examples/hello.clp)

(8) Variables
z 不需要事先宣告
z are untyped, 意味這個變數可以裝任何資料型態
z 範例:
„ (bind ?x “Hello World!”)
„ (bind ?x 2007)
„ 承續上面 (bind ?x (+ ?x 3)),則?x 為 2010

(9) Global Variables


z (defglobal ?*a* = 2007),宣告全域變數?*a*為 2007
z (bind ?*a* (+ ?*a* 3)),則?*a*變成 2010
z (reset),則?*a*變回 2007

(10) More lists


z (bind ?newlist (create$ eggs bread milk)),則?newlist 為(eggs bread milk)
z (printout t (nth$ 2 ?newlist) crlf),則印出 bread
z (printout t (first$ ?newlist) crlf) ,則印出 eggs
z (bind ?more-list (create$ ?newlist salt soap)),則?more-list 為(eggs bread
milk salt soap)

(11) foreach
z (foreach <variable> <list> <expression>+)
z 範例:
„ (bind ?newlist (create$ eggs bread milk))
„ (foreach ?e ?newlist (printout t ?e crlf)),分行依序印出?newlist 內容

(12) while
z (while <Boolean expression> do <expression>+)
z 範例(計算 1~10 之總合):
„ (bind ?i 1)
„ (bind ?sum 0)
„ (while (<= ?i 10) do (bind ?sum (+ ?sum ?i)) (bind ?i (+ ?i 1))),
則?sum 為 55

第8頁
Jess 程式開發 撰寫者:陳志華

(13) if/then/else
z (if <Boolean expression> then <expression>+ [else <expression>+])
z 範例:
„ (bind ?newlist (create$ eggs bread milk))
„ (if (member$ eggs ?newlist) then
„ (printout t “I need to buy eggs” crlf)
„ else
„ (printout t “No eggs, thanks”)),則最後印出 I need to buy eggs

第9頁
Jess 程式開發 撰寫者:陳志華

四、Jess 事實與規則語法
(1) Jess 程式是由”規則”(rules)和”事實”(facts)所組成的,而“事實”是被”規則”執
行使用的

(2) 事實相關語法
z 存放資料到事實串列
„ (assert (man))
„ (assert (man-is jack))
z 查看事實串列內容
„ (facts)
z 清除記憶體所有內容
„ (clear)

(3) 規則相關語法
z 定義規則
„ 範例
‹ (defrule rule0 ;定義 rule
(man-is jack)
=>
(printout t “jack” crlf))
z 查看規則
„ (rules)
z 執行
„ (run)
z 清除記憶體所有內容
„ (clear)

第 10 頁

You might also like