'clips rule incongruent matching with several conditional elements

I have this rule:

(defrule tio-varon
    (hermano-de (persona1 ?tio)(persona2 ?padremadre));
    (or
        (padre-de (padre ?padremade)(hijo ?hijo))
        (madre-de (madre ?padremade)(hijo ?hijo))
    )
    (varon (persona ?tio))
    (not (tio-de(tio ?tio)(sobrino ?hijo)))
    =>
    (assert (tio-de(tio ?tio)(sobrino ?hijo)))
)

This rule is matching with facts like these:

(hermano-de (persona1 <b>John</b>)(persona2 Maria))
(padre-de (padre <b>John</b>)(hijo Michael))
(varon John)

giving as result the fact (tio-de (John)(Michael))

Why, if John and Maria are matching with ?tio and ?padremade respectively over the (hermano-de) fact, later it's John the value that acts like ?padremadre over the (padre-de) fact? I hoped it was the value Maria the matched value with the fact (madre-de) or (padre-de)



Solution 1:[1]

Your variable names don't match. You use ?padremadre in the hermano-de fact, but ?padremade in the padre-de and padre-me facts. The second 'r' is missing.

When you load your code, you can see that there's an unexpected activation.

         CLIPS (6.4 2/9/21)
CLIPS> 
(deftemplate hermano-de
   (slot persona1)
   (slot persona2))
CLIPS> 
(deftemplate padre-de
   (slot padre)
   (slot hijo))
CLIPS> 
(deftemplate madre-de
   (slot madre)
   (slot hijo))
CLIPS>    
(deftemplate varon
   (slot persona))
CLIPS>    
(deftemplate tio-de
   (slot tio)
   (slot sobrino))
CLIPS>    
(deffacts initial
   (hermano-de (persona1 John) (persona2 Maria))
   (padre-de (padre John) (hijo Michael))
   (varon (persona John)))
CLIPS> 
(defrule tio-varon
   (hermano-de (persona1 ?tio)(persona2 ?padremadre))
   (or (padre-de (padre ?padremade)(hijo ?hijo))
       (madre-de (madre ?padremade)(hijo ?hijo)))
   (varon (persona ?tio))
   (not (tio-de(tio ?tio)(sobrino ?hijo)))
   =>
   (assert (tio-de(tio ?tio)(sobrino ?hijo))))
CLIPS> (reset)
CLIPS> (agenda)
0      tio-varon: f-1,f-2,f-3,*
For a total of 1 activation.
CLIPS> (facts)
f-1     (hermano-de (persona1 John) (persona2 Maria))
f-2     (padre-de (padre John) (hijo Michael))
f-3     (varon (persona John))
For a total of 3 facts.
CLIPS> 

For the padre-de disjunct of the rule, you can see from the matches command that f-1 and f-2 are successfully matching the first two patterns of the rule, which should not be happening. So excluding a bug in the CLIPS pattern matching algorithm, there must be something wrong in the first two patterns of the rule. The ?padremadre variable is the only variable shared by the first two patterns, so that's the one to examine since it should be preventing a match for the first two patterns.

CLIPS> (matches tio-varon)
Matches for Pattern 1
f-1
Matches for Pattern 2
f-2
Matches for Pattern 3
f-3
Matches for Pattern 4
 None
Partial matches for CEs 1 - 2
f-1,f-2
Partial matches for CEs 1 - 3
f-1,f-2,f-3
Partial matches for CEs 1 - 4
f-1,f-2,f-3,*
Matches for Pattern 1
f-1
Matches for Pattern 2
 None
Matches for Pattern 3
f-3
Matches for Pattern 4
 None
Partial matches for CEs 1 - 2
 None
Partial matches for CEs 1 - 3
 None
Partial matches for CEs 1 - 4
 None
Activations
f-1,f-2,f-3,*
(5 3 1)
CLIPS> 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1