我可以为以下类型规则在规则的LHS中实施if否则吗?

时间:2019-06-21 09:22:20

标签: clips expert-system reasoning

我有诸如

之类的事实
(claim (name Employee) (field 'EmpName' 'Company') (value 'Bob' 'ABC'))
(claim (name Event) (field 'EventName' 'Company') (value 'Conference' 'ABC'))
(drule (id gen1)(name1 'Employee') (field1 'EmpName' 'Company') (value1 'Bob' 'ABC') (name2 'Event')
    (field2 'EventName') (value2 'Conference'))

我尝试了以下规则。但这不能按预期工作。

(defrule drule-rule
    (drule 
        (id ?id))
    (forall
        (drule 
                (id ?id)
                        (name1 ?name1)
                (field1 $?f11 ?field1 $?)
                (value1 $?v11&:(= (length$ ?f11) (length$ ?v11)) ?value1 $?))
                    (name2 ?name2)
                (field2 $?f22 ?field2 $?)
                (value2 $?v22&:(= (length$ ?f22) (length$ ?v22)) ?value2 $?))
        (claim 
            (name ?name1)
                (field $?f1 ?field1 $?)
                (value $?v1&:(= (length$ ?f1) (length$ ?v1)) ?value1 $?))
                (claim (name ?name2)
                (field $?f2 ?field2 $?)
                (value $?v2&:(= (length$ ?f2) (length$ ?v2)) ?value2 $?)))
         (forall
                 (claim 
            (field $?f3 ?field3 $?)
                    (value $?v4&:(= (length$ ?f3) (length$ ?v3)) ?value3 $?))
                 (claim 
            (field $?f4 ?field4 $?)
                    (value $?v4&:(= (length$ ?f4) (length$ ?v4)) ?value3 $?)))

 =>
        (assert (Action allowed)))

我希望以上规则检查第一个claim中匹配的forall中的每个字段以及其他匹配的声明。如果同一领域 名称为找到,然后检查 其他声明中的那个字段。

对于以上事实,断言应该起作用。对于以下情况,断言应该失败,因为Company字段 与其他声明不符。

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

用一条规则可以做到吗?或其他替代方法?

谢谢。

1 个答案:

答案 0 :(得分:1)

除了将第二个 forall 条件元素转换为两个单独的 not 条件元素外,您的数据还存在一些问题(名称和名称之间的单引号用法不一致)将不一致的公司“ xyz”放在已经由 drule 事实检查过的“雇员”声明中,而不是在没有的“事件”声明中)。

         CLIPS (6.31 4/1/19)
CLIPS> 
(deftemplate drule
   (slot id)
   (slot name1)
   (multislot field1)
   (multislot value1)
   (slot name2)
   (multislot field2)
   (multislot value2))
CLIPS>    
(deftemplate claim
   (slot name)
   (multislot field)
   (multislot value))
CLIPS>     
(defrule drule-rule
   (drule (id ?id))
   (forall
      (drule (id ?id)
             (name1 ?name1)
             (field1 $?f11 ?field1 $?)
             (value1 $?v11&:(= (length$ ?f11) (length$ ?v11)) ?value1 $?)
             (name2 ?name2)
             (field2 $?f22 ?field2 $?)
             (value2 $?v22&:(= (length$ ?f22) (length$ ?v22)) ?value2 $?))
      (claim (name ?name1)
             (field $?f1 ?field1 $?)
             (value $?v1&:(= (length$ ?f1) (length$ ?v1)) ?value1 $?))
      (claim (name ?name2)
             (field $?f2 ?field2 $?)
             (value $?v2&:(= (length$ ?f2) (length$ ?v2)) ?value2 $?))
      (not (claim (field $?f3 ?field1 $?)
                  (value $?v3&:(= (length$ ?f3) (length$ ?v3)) ~?value1 $?)))
      (not (claim (field $?f4 ?field2 $?)
                  (value $?v4&:(= (length$ ?f4) (length$ ?v4)) ~?value2 $?))))


   =>
   (assert (Action allowed)))
CLIPS>    
(assert
   (claim (name 'Employee') 
          (field 'EmpName' 'Company')
          (value 'Bob' 'ABC'))
   (claim (name 'Event')
          (field 'EventName' 'Company')
          (value 'Conference' 'ABC'))
   (drule (id gen1)
          (name1 'Employee')
          (field1 'EmpName' 'Company')
          (value1 'Bob' 'ABC')
          (name2 'Event')
          (field2 'EventName')
          (value2 'Conference')))
<Fact-3>
CLIPS> (agenda)
0      drule-rule: f-3,*
For a total of 1 activation.
CLIPS> (reset)
CLIPS>           
(assert
   (claim (name 'Employee')
          (field 'EmpName' 'Company')
          (value 'Bob' 'ABC'))
   (claim (name 'Event')
          (field 'EventName' 'Company')
          (value 'Conference' 'xyz'))
   (drule (id gen1)
          (name1 'Employee')
          (field1 'EmpName' 'Company')
          (value1 'Bob' 'ABC')
          (name2 'Event')
          (field2 'EventName')
          (value2 'Conference')))
<Fact-3>
CLIPS> (agenda)
CLIPS>