【问题标题】:Something like OR operator in CLIPS (CLIPS Rule Based Programming Language)类似于 CLIPS 中的 OR 运算符(基于 CLIPS 规则的编程语言)
【发布时间】:2021-03-28 23:40:57
【问题描述】:

我需要这个程序的帮助,我不知道如何让用户输入一个是/否的字符,我已经定义了一种类似动物的哺乳动物 - 对于哺乳动物来说,牛奶字符必须是是,腿(腿数)可以是 2 或 4,另一个字符可以是是或否,例如 - 哺乳动物 - 牛奶是的,腿 2 或 4,但骨干可以是或否,捕食者是或否....但我不知道该怎么做(或条件或 idk 类似的东西),用户可以输入其中之一这些它找出了效果中定义的动物类型,感谢您的帮助:)

;*********** DEFTEMPLATE ***********;
(deftemplate animal_type    
    (slot type (type SYMBOL) (allowed-symbols mammal bird fish))    
    (slot milk (type SYMBOL) (allowed-symbols yes no))  
    (slot feathers (type SYMBOL) (allowed-symbols yes no))  
    (slot fins (type SYMBOL) (allowed-symbols yes no))  
    (slot backbone (type SYMBOL) (allowed-symbols yes no))  
    (slot fly (type SYMBOL) (allowed-symbols yes no))   
    (slot predator (type SYMBOL) (allowed-symbols yes no))  
    (multislot legs (type INTEGER) (allowed-integers 0 2 4 6 8))
)

(deftemplate finding_type   
    (slot milk (type SYMBOL) (allowed-symbols yes no))  
    (slot feathers (type SYMBOL) (allowed-symbols yes no))  
    (slot fins (type SYMBOL) (allowed-symbols yes no))  
    (slot backbone (type SYMBOL) (allowed-symbols yes no))  
    (slot fly (type SYMBOL) (allowed-symbols yes no))   
    (slot predator (type SYMBOL) (allowed-symbols yes no))  
    (multislot legs (type INTEGER) (allowed-integers 0 2 4 6 8))
)

;*********** DEFFACTS ***********;
(deffacts characters_type   
    (animal_type (typ mammal) (milk yes) (legs 2 4))    
    (animal_type (typ bird) (milk no) (feathers yes) (fly yes)) 
    (animal_type (typ fish) (milk no) (feathers no) (fins yes) (legs 0))
)     
;something like this (animal_type (typ mammal) (milk yes) (backbone or(yes no)) ... (legs 2 4))

(deffacts temp_fact 
    (next_search)
)

(defrule input_characters   
    ?gone<-(next_search)
=>  (retract ?gone) 
    (printout t " " crlf)   
    (printout t "Enter - yes/no, legs - 0/2/4/6/8" crlf)    
    (printout t "==================================================================" crlf)
    (printout t "Milk:")    
    (bind ?o1 (read))
    (printout t "Feathers:")    
    (bind ?o2 (read))
    (printout t "Fins:")    
    (bind ?o3 (read))
    (printout t "Backbone:")    
    (bind ?o4 (read))
    (printout t "Fly:") 
    (bind ?o5 (read))
    (printout t "Predator:")    
    (bind ?o6 (read))
    (printout t "Legs:")    
    (bind ?o7 (read))

    (assert (finding_type (milk ?o1) (feathers ?o2) (fins ?o3) (backbone ?o4) (fly ?o5) (predator ?o6) (legs ?o7)) )
)

(defrule find_out_type  
    (finding_type (milk ?o1) (feathers ?o2) (fins ?o3) (backbone ?o4) (fly ?o5) (predator ?o6) (legs ?o7))
    (animal_type (type ?type) (milk ?o1) (feathers ?o2) (fins ?o3) (backbone ?o4) (fly ?o5) (predator ?o6) (legs ?o7))
=>  
    (printout t " " crlf)   
    (printout t "Type of animal is: " ?type crlf)
)   

(defrule not_found  
    (finding_type (milk ?o1) (feathers ?o2) (fins ?o3) (backbone ?o4) (fly ?o5) (predator ?o6) (legs ?o7))
    (not (animal_type (type ?type) (milk ?o1) (feathers ?o2) (fins ?o3) (backbone ?o4) (fly ?o5) (predator ?o6) (legs ?o7)) )
=>  
    (printout t " " crlf)   
    (printout t "Nothing found!" crlf)
)

(defrule cancel (declare (salience -10))    
    ?gone<-(finding_type (milk ?o1) (feathers ?o2) (fins ?o3) (backbone ?o4) (fly ?o5) (predator ?o6) (legs ?o7))
=>  
    (retract ?gone)
)

【问题讨论】:

    标签: conditional-statements conditional-formatting conditional-operator clips


    【解决方案1】:

    修改规则中的 animal_type 模式以使用多字段通配符来匹配指定腿数左右的无关值。由于您目前的规则是 animal_type 模式,因此只会与包含腿槽中的一个值的事实匹配。

    (animal_type (type ?type) 
                 (milk ?o1) 
                 (feathers ?o2) 
                 (fins ?o3) 
                 (backbone ?o4) 
                 (fly ?o5) 
                 (predator ?o6)
                 (legs $? ?o7 $?))
    

    【讨论】:

      猜你喜欢
      • 2019-03-28
      • 1970-01-01
      • 2017-04-05
      • 1970-01-01
      • 2014-09-28
      • 2016-09-22
      • 2020-09-16
      • 2021-02-26
      • 1970-01-01
      相关资源
      最近更新 更多