【问题标题】:How to compare symbols between 2 multifield-variable in CLIPS如何在 CLIPS 中比较 2 个多字段变量之间的符号
【发布时间】:2018-03-14 08:42:27
【问题描述】:

问题是在两个 SYMBOL 类型的多字段变量之间进行比较。 这是我尝试开发的代码示例。

CLIPS>(defrule r       
=>
(printout t "Input A: ")
(bind $?A (explode$ (readline)))

(printout t "Input B: ")
(bind $?B (explode$ (readline)))

(if (member$ $?A $?B) then (printout t " Something ..." crlf)))
CLIPS> (run)
Input A: 1 2 3 4 5
Input B: 7 3 2 1 6
CLIPS> 

我想将 $?A 的每个参数(或值)与 $?B 的每个参数进行比较,如果两者的至少一个参数在 $?A 或 $?B 中,则 if 测试变为 TRUE。

【问题讨论】:

    标签: clips


    【解决方案1】:

    您可以编写一个函数来测试两个多字段值的交集:

    CLIPS> 
    (deffunction intersectionp (?m1 ?m2)
       (foreach ?i1 ?m1
          (foreach ?i2 ?m2
             (if (eq ?i1 ?i2)
                then (return TRUE))))
       (return FALSE))
    CLIPS> 
    (defrule r       
       =>
       (printout t "Input A: ")
       (bind ?A (explode$ (readline)))
    
       (printout t "Input B: ")
       (bind ?B (explode$ (readline)))
    
       (if (intersectionp ?A ?B) then (printout t " Something ..." crlf)))
    CLIPS> (run)
    Input A: 1 2 3 4 5
    Input B: 7 3 2 1 6
     Something ...
    CLIPS> (reset)
    CLIPS> (run)
    Input A: 1 2 3
    Input B: 4 5 6
    CLIPS> 
    

    您也可以使用模式匹配来测试交叉点:

    CLIPS> (clear)
    CLIPS>    
    (defrule r       
       =>
       (printout t "Input A: ")
       (bind ?A (explode$ (readline)))
       (assert (A ?A))
    
       (printout t "Input B: ")
       (bind ?B (explode$ (readline)))
       (assert (B ?B)))
    CLIPS> 
    (defrule intersect
       (exists (A $? ?v $?)
               (B $? ?v $?))
       =>
       (printout t " Something ..." crlf))
    CLIPS> (reset)
    CLIPS> (run)
    Input A: 1 2 3 4 5
    Input B: 7 3 2 1 6
     Something ...
    CLIPS> (reset)
    CLIPS> (run)
    Input A: 1 2 3
    Input B: 4 5 6
    CLIPS> 
    

    【讨论】:

    • 感谢您的好意,非常感谢莱利先生。
    • 在交集规则中有没有办法打印出相交值?因为打印 ?v 会引发错误。
    • exists 条件元素确定是否存在至少一个匹配它包含的模式的匹配项,因此变量 ?v 在条件元素范围之外没有绑定到它的特定值。从包含模式周围移除存在的条件元素,变量 ?v 将在规则的操作范围内,您可以打印其值。
    • 非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多