2017-07-07 86 views
0

我有一個簡單的虛擬示例。我有一個道具模板來存儲來自不同服務器的參數及其值。屬性值可以是字符串或數字(整型或浮點型)。目前,從系統讀取屬性的「收集器」會生成列值爲String的事實。如何將字符串轉換爲浮點數

(deftemplate prop (slot serverid) (slot name) (slot value)) 
(assert (prop (serverid "ppn45r07vm_0") (name "email.encoding") (value "utf-8"))) 
(assert (prop (serverid "ppn45r07vm_0") (name "inventory.safety.threshold") (value "99.0"))) 
(assert (prop (serverid "ppn45r55vm_0") (name "inventory.safety.threshold") (value "993.1"))) 

(defrule check-range 
    (prop (serverid ?s) (name "inventory.safety.threshold") (value ?v)) 
    (test (> (float ?v) 100.0)) 
=> 
    (printout t "safety threshold on server " ?s " is set too high at " ?v crlf) 
)  

我的問題 - 如何將字符串值轉換成任何一個浮點數或整型值,這樣我可以執行一系列檢查,等上面的示例代碼工作在JESS因爲JESS的浮動功能發生在一個String參數並返回一個Float。 CLIPS的浮點函數需要一個數字並返回一個浮點數。我找不到將String轉換爲Float的類似CLIPS函數。

只是爲了保持完整性,在剪輯,我得到以下錯誤

CLIPS> (defrule check-range 
    (prop (serverid ?s) (name "inventory.safety.threshold") (value ?v)) 
    (test (> (float ?v) 100.0)) 
=> 
    (printout t "safety threshold on server " ?s " is set too high at " ?v crlf) 
)  
[ARGACCES5] Function float expected argument #1 to be of type float 

[DRIVE1] This error occurred in the join network 
    Problem resides in associated join 
     Of pattern #1 in rule check-range 

[ARGACCES5] Function float expected argument #1 to be of type float 

[DRIVE1] This error occurred in the join network 
    Problem resides in associated join 
     Of pattern #1 in rule check-range 

道歉,如果有一個明顯的答案。預先感謝您的幫助。

回答

2

可以重載浮動功能獲得相似的行爲與傑斯字符串:

CLIPS> (float "3") 
[ARGACCES5] Function float expected argument #1 to be of type integer or float 
CLIPS> 
(defmethod float ((?s STRING)) 
    (float (string-to-field ?s))) 
CLIPS> (float "3") 
3.0 
CLIPS> 
+0

真棒 - 作品!謝謝。 – Bernie