2016-01-26 31 views
0

我努力正確評估多字段值的整數值。CLIPS:如何評估作爲整數的多字段值

最終,我需要的是模式匹配一​​個只包含整數的多槽,並能夠判斷「列表」是否按升序排列。

我有什麼至今:

(defclass status 
    (is-a USER) 
    (role concrete) 
    (pattern-match reactive) 
    (multislot numbers 
     (create-accessor write) 
     (type INTEGER) 
     (range 1 ?VARIABLE) 
     (default 1) 
    ) 
) 

(defrule asc 
    ?st <- (object (is-a status) (numbers $?n)) 
    (test (> (length ?n) 2)) 
    (test (< (first$ ?n) (rest$ ?n))) 
=> 
    (printout t "List " ?n " is ascending" crlf) 
) 

(make-instance of status (numbers 1 2 3)) 

我明白,也許這不是擴大多時隙並填充爲(<)參數的方式,但我似乎無法找到正確的方式。 即使參數擴展正確,但它仍然表示,它需要一個整數參數,但

(first$ ?n) 

不計算爲整數。

我的問題是: 如何獲取值列表的值並將其「解析」爲整數? 其次,我該如何擴展這些值以使它們的參數(<)並確定列表是否按升序排列?

回答

1

使用第n個函數從多字段檢索單個值。對於第一個值,你會使用(第n $ 1?n)。但是,在您的規則中,您只需使用展開式$函數將數字槽的值拼接到<函數的參數列表中。

CLIPS> 
(defclass status 
    (is-a USER) 
    (multislot numbers)) 
CLIPS> 
(defrule asc 
    (object (is-a status) (numbers $?n)) 
    (test (> (length ?n) 1)) 
    (test (< (expand$ ?n))) 
    => 
    (printout t "List " ?n " is ascending" crlf)) 
CLIPS> (make-instance of status (numbers 1 2 3)) 
[gen1] 
CLIPS> (make-instance of status (numbers 2 3 1 4)) 
[gen2] 
CLIPS> (run) 
List (1 2 3) is ascending 
CLIPS>