0
votes

I'm trying to implement a basic expert system in the Clips programming language. I have a knowledge base of children with their respective parents. I want to set up a rule so that if two children have the same parents then it asserts the fact that they are siblings.


(deftemplate person "family tree"
          (slot name)
          (slot father)
          (slot mother))

(assert
        (person
                (name "William")
                (father "John")
                (mother "Megan")))
(assert
        (person (name "David")
                (father "John")
                (mother "Megan")))

(defrule sibling
        (person
                (name ?name1)
                (father ?x)
                (mother ?x))
        (person
                (name ?name2)
                (father ?y)
                (mother ?y)))

and when I define the rule I get a syntax error:

Syntax Error:  Check appropriate syntax for defrule.
2

2 Answers

0
votes

The correct syntax for your rule is:

(defrule sibling 
   (person (name ?name1) (father ?x) (mother ?x)) 
   (person (name ?name2) (father ?y) (mother ?y)) 
   => 
   ...)

Within a rule, a template is referred as:

(template_name (slot_name value) (slot_name value)) 

A rule is divided in two sides: the LHS (Left-Hand Side) where you define the conditions satisfying such rule and the RHS (Right-Hand Side) where you define the consequent actions.

In CLIPS, the => operator separates the two sides.

Example:

(defrule animal-is-a-duck
    (animal ?name)
    (quacks)
    (two legs)
    (lay eggs)
    =>
    (assert (animal ?name is a duck)))

You can read more about CLIPS syntax in the basic programming guide.

0
votes

Your rule should be something like this:


    (defrule sibling 
        (person
                (name ?name1)
                (father ?x)
                (mother ?y))
        (person
                (name ?name2)
                (father ?x)
                (mother ?y))
        (test (neq ?name1 ?name2))
    =>
    (assert (siblings ?name1 ?name2)))

The original rule could be satisfied only if the father and the mother were the same person in each fact being matched.

This rule permits duplicates:


    f-3     (siblings "David" "William")
    f-4     (siblings "William" "David")

so you can either catch that in another rule or you can write a more complex rule (or another rule) also matching against currently generated matched siblings facts.