我可以在某些情况下绑定变量吗?

时间:2019-07-05 08:17:52

标签: clips expert-system

我有一个模板:

(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的索赔与drule中的索赔相同。
  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。

1 个答案:

答案 0 :(得分:0)

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>