Search code examples
declarativeclips

Misunderstanding CLIPS functions. Can't find the right answer


I'm having troubles understanding how CLIPS works. I need to get an aswer like "mooo -> cow". And this is the code I have.

(deftemplate animal (slot name)(slot sound))

(deffacts Input_animal 
    (animal(name cow)(sound mooo))
    (animal(name dog)(sound barf))
    (animal(name cat)(sound meuw))
    (animal(name sheep)(sound me-e-e))
    (animal(name duck)(sound cuack))
    )

(defrule sound_animal 
    (sound ?x)
    (animal(name ?animal)(sound ?x))
    =>
    (printout t ?animal crlf)
)

(defrule no_sound_animal 
    (sound ?x)
    (not(animal(name ?animal)(sound ?x)))
    =>
    (printout t ?x => "the animal doesn't exist" crlf)
)
.

Then I put this on the console: (watch rules) (watch facts) (watch invocations) (reset) (run) (sound_animal (sound mooo))

And I get this answer: [EXPRNPSR3] Miising function declaration for sound

well... I was specting somethng like "animal -> cow" Can anybody hep me with this? I know this should be simple but I got stuck... Thank you!!


Solution

  • You haven't defined a function named sound_animal or sound, so attempting to call these functions will generate an error.

    CLIPS> (sound_animal (sound mooo))
    
    [EXPRNPSR3] Missing function declaration for sound_animal.
    CLIPS> (deffunction sound_animal ())
    CLIPS> (sound_animal (sound mooo))
    
    [EXPRNPSR3] Missing function declaration for sound.
    CLIPS>
    

    Use the assert command to create a sound fact to trigger the sound_animal rule: (assert (sound moo)). The assert command is a special form in that the parentheses that follow the assert function name are used to delimit the fact relation and its slots rather than representing a function call to the sound function with an argument of moo.

    CLIPS> 
    (deftemplate animal (slot name)(slot sound))
    CLIPS> 
    (deffacts Input_animal 
        (animal(name cow)(sound mooo))
        (animal(name dog)(sound barf))
        (animal(name cat)(sound meuw))
        (animal(name sheep)(sound me-e-e))
        (animal(name duck)(sound cuack))
        )
    CLIPS> 
    (defrule sound_animal 
        (sound ?x)
        (animal(name ?animal)(sound ?x))
        =>
        (printout t ?animal crlf))
    CLIPS> 
    (defrule no_sound_animal 
        (sound ?x)
        (not(animal(name ?animal)(sound ?x)))
        =>
        (printout t ?x => "the animal doesn't exist" crlf))
    CLIPS> (watch rules)
    CLIPS> (watch facts)
    CLIPS> (reset)
    <== f-0     (initial-fact)
    ==> f-0     (initial-fact)
    ==> f-1     (animal (name cow) (sound mooo))
    ==> f-2     (animal (name dog) (sound barf))
    ==> f-3     (animal (name cat) (sound meuw))
    ==> f-4     (animal (name sheep) (sound me-e-e))
    ==> f-5     (animal (name duck) (sound cuack))
    CLIPS> (assert (sound mooo))
    ==> f-6     (sound mooo)
    <Fact-6>
    CLIPS> (run)
    FIRE    1 sound_animal: f-6,f-1
    cow
    CLIPS>