我可以在某些条件下绑定变量吗?

Can I bind a variable on some condition?

我有一个模板:

(deftemplate drule
         (slot name1)
         (slot id)
         (multislot field1)
             (multislot value1)
         (slot name2)
         (multislot field2)
         (multislot value2))

(deftemplate claim
         (slot name)
         (multislot field)
         (multislot value))

我可以有规则吗

  1. 查看是否有name与规则中相同的claim
  2. 检查 claim:field 是否有值 'EmpName',如果找到则将其绑定到 ?name.

目前我有如下情况:

(defrule drule
    (drule (id ?id)
        (name1 ?name1)
        (name2 ?name2))
    (claim  (name ?name1)
        (field $?pf1 'EmpName' $?)
        (value $?pv1&:(= (length$ ?pf1) (length$ ?pv1)) ?name $?))
    (claim  (name ?name2)
        (field $?pf2 'EmpName' $?)
        (value $?pv2&:(= (length$ ?pf2) (length$ ?pv2)) ?name $?))
     ...
=>
    (assert (success ?name))

但只有当所有选定的声明都有字段 EmpName 时,它才会绑定。我只想检查那些带有名称的声明 规则中提到。如果这些声明中的任何一个具有字段 'EmpName' ,那么它应该被绑定。如果不是就继续。我的 如果某些 claims 根本没有 EmpName 字段,规则将失败。 假设 某些声明将具有 EmpName 字段 和价值。

所需的输入和输出:

(assert
   (claim (name 'Employee') 
          (field 'Company')
          (value 'ABC'))
   (claim (name 'Event')
          (field 'EmpName' 'EventName' 'Company')
          (value 'Bob' 'Conference' 'ABC'))
   (drule (id '001')
          (name1 'Employee')
          (field1 'Company')
          (value1 'ABC')
          (name2 'Event')
          (field2 'EventName')
          (value2 'Conference')))

输出:

(success 'Bob')

抱歉,如果它非常愚蠢,我尝试在 field 上使用 test$ 和 member$。但想不通。我在空闲时间自学CLIPS。

CLIPS> 
(deftemplate drule
         (slot name1)
         (slot id)
         (multislot field1)
             (multislot value1)
         (slot name2)
         (multislot field2)
         (multislot value2))
CLIPS> 
(deftemplate claim
         (slot name)
         (multislot field)
         (multislot value))
CLIPS>          
(defrule drule
   (drule (id ?id)
          (name1 ?name1)
          (name2 ?name2))
   (claim (name ?name1))
   (claim (name ?name2))
   (claim (name ?name1 | ?name2)
          (field $?pf1 'EmpName' $?)
          (value $?pv1&:(= (length$ ?pf1) (length$ ?pv1)) ?name $?))
   =>
   (assert (success ?name)))
CLIPS>    
(deffacts initial
   (claim (name 'Employee') 
          (field 'Company')
          (value 'ABC'))
   (claim (name 'Event')
          (field 'EmpName' 'EventName' 'Company')
          (value 'Bob' 'Conference' 'ABC'))
   (drule (id '001')
          (name1 'Employee')
          (field1 'Company')
          (value1 'ABC')
          (name2 'Event')
          (field2 'EventName')
          (value2 'Conference')))
CLIPS> (reset)
CLIPS> (agenda)
0      drule: f-3,f-1,f-2,f-2
For a total of 1 activation.
CLIPS> (run)
CLIPS> (facts)
f-0     (initial-fact)
f-1     (claim (name 'Employee') (field 'Company') (value 'ABC'))
f-2     (claim (name 'Event') (field 'EmpName' 'EventName' 'Company') (value 'Bob' 'Conference' 'ABC'))
f-3     (drule (name1 'Employee') (id '001') (field1 'Company') (value1 'ABC') (name2 'Event') (field2 'EventName') (value2 'Conference'))
f-4     (success 'Bob')
For a total of 5 facts.
CLIPS>