我有一个简单的愚蠢的例子。我有一个prop模板来存储来自不同服务器的参数及其值。属性值可以是String或numeric(Integer或Float)。目前,从系统读取属性的“收集器”生成事实,其列值为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参数并返回浮动。 CLIPS的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
如果有明显的答案,请道歉。在此先感谢您的帮助。
答案 0 :(得分:2)
您可以重载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>