Search code examples
regexrulesclipsexpert-system

How to assert the opposite value from that of the matching?


I have a rule that has a slot that does a matching with two colours, and it doesn't matter which one it will match as long as after the matching it changes to its opposite colour. However when I write this, I get a syntax error:

(defrule colour
?col <- (colorTemp(color ?color&white|black))
=>
(modify ?col (color ?colorOpposite&~?color))
)

Thank you


Solution

  • The syntax ?colorOpposite&~?color is not valid in the actions of a rule and the variable ?colorOpposite is not bound anywhere in the rule so both of these issues will generate errors. You also need to add some code that specifies the color opposites. To prevent looping from one opposite to the other, you will need to indicate some way that the color has been changed.

             CLIPS (6.31 6/12/19)
    CLIPS> 
    (deftemplate colorTemp
       (slot color)
       (slot changed (default no)))
    CLIPS>    
    (deffacts opposites
       (opposite white black))
    CLIPS> 
    (defrule colour
       ?col <- (colorTemp (color ?color&white|black)
                          (changed no))
       (or (opposite ?color ?opposite)
           (opposite ?opposite ?color))
       =>
       (modify ?col (color ?opposite)
                    (changed yes)))
    CLIPS> (reset)
    CLIPS> (assert (colorTemp (color white)))
    <Fact-2>
    CLIPS> (facts)
    f-0     (initial-fact)
    f-1     (opposite white black)
    f-2     (colorTemp (color white) (changed no))
    For a total of 3 facts.
    CLIPS> (agenda)
    0      colour: f-2,f-1
    For a total of 1 activation.
    CLIPS> (run)
    CLIPS> (facts)
    f-0     (initial-fact)
    f-1     (opposite white black)
    f-3     (colorTemp (color black) (changed yes))
    For a total of 3 facts.
    CLIPS>