如何将字符串转换为浮点数

How to convert String to Float

我有一个简单的简化示例。我有一个道具模板来存储来自不同服务器的参数及其值。 属性 值可以是字符串或数字(整数或浮点数)。当前,从系统读取属性的 "collector" 生成列值为 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)
)       

我的问题 -- 我如何将 String 值转换为 Float 或 Integer 值,以便我可以执行范围检查等。上面的示例代码在 JESS 中有效,因为 JESS 的 float 函数接受一个 String 参数,并且returns 一个浮点数。 CLIPS 的 float 函数接受一个数字和 returns 一个浮点数。我找不到将 String 转换为 Float 的类似 CLIPS 函数。

为了完整起见,在 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

如果有明显的答案,我们深表歉意。在此先感谢您的帮助。

您可以重载 float 函数以获得与使用字符串的 Jess 类似的行为:

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>