Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

regex - 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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> 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...