专家系统识别球

问题描述 投票:0回答:1

我是剪辑软件的新手,这是我的第一个作业。当我加载并运行程序时它显示错误,请帮助我,我必须在2小时内提交不知道如何解决它们所有的规则是不同的,所以我无法修复它们。请帮我修复所有错误。我在2小时内尝试了很多,我必须提交它们。

我如何解决它们指导我。

 Ball_Identification_Expert_System

;;Ball Identification Expert System
;;This is an expert  system to identify the type of ball    

;;; This is the template definition for the ball. The fact has the following form:
;;; (ball (name ?STRING) (kind ?STRING) (size ?NUMBER) (colors $?SYMBOL) (spots SYMBOL) (Weight $?SYMBOL) (environment $?SYMBOL) (particularity $?SYMBOL) )
 (deftemplate ball "Template holding the characteristics the balll."
             (multislot name (type STRING) (default ?NONE))
    (multislot kind (type STRING) (default ?NONE))
    (slot size (type NUMBER) (default ?NONE))


    (multislot colors (type SYMBOL) (default ?NONE)
        (allowed-symbols brown yellow black grey green red white blue)
    )
    (slot spots (type SYMBOL) (default false) (allowed-symbols false true))
    (multislot body (type SYMBOL) (default ?DERIVE)
        (allowed-symbols nil leather clay)
    )

    ;;; Environment, required field.
    (multislot environment (type SYMBOL) (default ?NONE)
        (allowed-symbols indoors outdoors)
    )

    ;;; Particularities, optional field.
    (multislot particularity (type SYMBOL) (default ?DERIVE)
        (allowed-symbols nil straight-movement rotating curving floating)
    )
)


(deffacts ball-database "Adding the balls database to the facts."
    ;;; Table Tennis
    (ball (name Table Tenis")
            (kind "Hitting with Rachet")
            (size 1.8)
            (colors brown yellow white)
            (spots false)
            (body clay)
            (environment indoors)
            (particularity straight-movement rotating curving)
    )
    ;;; Golf
    (ball (name "Golf ")
            (kind "hitting with golf club")
            (size 1.51)
            (colors white)
            (body clay)
            (environment outdoors indoors)
            (particularity straight-movement)
    )   
    ;;; Tennis
    (ball (name "Tennis")
            (kind "Hitting with ratchet")
            (size 1.90)
            (colors brown yellow grey)
            (body leather)
            (environment outdoors)
            (particularity straight-movement)
    )       
    ;;;Field Hockey
    (ball (name  "Field HOCKEY")
            (kind " hitting with club")
            (size 2.0)
            (colors yellow brown)
            (body leather)
            (environment outdoors)
            (particularity curving)
    )   
    ;;; Basketball
    (ball (name "Basketball")
            (kind "dribbling")
            (size 7.8)
            (colors red brown grey)
            (body leather)
            (environment indoors outdoors)
            (particularity floating curving )
    )       
    ;;; FOOTBALL SOCCER
    (ball (name "FOOTBALL SOCCER")
            (kind "Kicking")
            (size 15)
            (colors blue brown white grey)
                                           (spots true)

            (environment outdoors)
            (particularity rotating straight-movement)
    )   
    ;;; Rugby
    (ball (name "Rugby")
            (kind "Throwing")
            (size 18)
            (colors yellow black)
            (spots false)
            (environment outdoors)
            (particularity rotating curving)
    )
    ;;; Netball
    (ball (name "Netball")
            (kind "slapping")
            (size 30)
            (colors grey green)
            (body leather)
            (environment outdoors indoors)
            (particularity straight-movement)
    )   
    ;;; Volleyball
    (ball (name "Volleyball")
            (kind "slapping")
            (size 17.5)
            (colors green white)
            (body leather)
            (environment outdoors indoors)
            (particularity straight-movement)
    )       
    ;;; Bowling
    (ball (name "bowling")
            (kind "Throwing")
            (size 8)
            (colors red)
            (spots false)
            (environment outdoors)
    )   
    ;;; water polo 
    (ball (name "Water Polo")
            (kind "swimming")
            (size 13.5)
            (colors brown yellow)
            (body leather)
            (environment indoors outdoors)
            (particularity floating)
    )
    ;;; Korfball
    (ball (name "Korfball")
            (kind "hitting")
            (size 22)
            (colors yellow brown red)
            (body clay)
            (environment outdoors)
    )       
)


;;; A global variable that holds the number of avaliable balls.
(defglobal ?*counter* = 12)


;;; The variable above is being modified with this function each time we exclude a ball from
;;; the possible solutions. (minusOne) decreases the global counter by one.
(deffunction minusOne ()
    (bind ?*counter* (- ?*counter* 1))
)


;;; This function is used for every question made to the user.
;;; The question that is printed to the user is broken into three arguments (?qBEG ?qMID ?qEND) for flexibility, as we may need to include a printable in the middle.
    (if (lexemep ?answer)
        then (bind ?answer (lowcase ?answer))
    )
    (while (not (member ?answer ?allowed-values)) do
        (printout t ?qBEG ?qMID ?qEND)
        (bind ?answer (read))
        (if (lexemep ?answer)
            then (bind ?answer (lowcase ?answer)))
    )
?answer)


;;; The first main question made to the user. We ask for the size of the ball, expecting tiny, small or big as the answer.
;;; The result is stored as the following fact: (theScale ?size)
(defrule mainQuestion-Size
    ?x <- (initial-fact)
    =>
    (retract ?x)
    (bind ?size (ask-question "### Is the ball tiny ( <4cm ) , small ( 4cm - 20cm ) or big ( >20cm )? (tiny,small,big) ### " "" "" tiny small big))
    (assert (theScale ?size))   
)


;;; Given that the fact (theScale ?size) exists, this rule gets triggered.
;;; This rule filters the balls by size, and deletes those that are not in the given scale-group.
;;; In addition, every time we retract the ball, we substract one from the global variable ?*counter* calling the (minusOne) function.
(defrule filterBy-Size
    (theScale ?s)
    ?ani <- (ball (size ?size))
    =>
    (if (eq ?s tiny)
        then (if (> ?size 4) then (retract ?ani) (minusOne))
    else (if (eq ?s small)
            then (if (or (<= ?size 4) (>= ?size 20)) then (retract ?ani) (minusOne))
         )
    else (if (eq ?s big)
            then (if (< ?size 20) then (retract ?ani) (minusOne))
         )
    )
)




;;;  The user can type any color from the acceptable list in the parentheses.
;;; The result is stored as the following fact: (theColor ?color)
(defrule mainQuestion-Color
    (theScale ?s)
    =>
    (bind ?color (ask-question "### What color is it? (brown yellow black grey green red white blue) ### " "" "" brown yellow black grey green red white blue))
    (assert (theColor ?color))
)   


;;; every time we retract a ball, we substract one from the global variable ?*counter* calling the (minusOne) function.
(defrule filterBy-Color
    (theColor ?c)
    ?ani <- (ball (colors $?colors))
    =>
    (if (not (member$ ?c $?colors))
        then (retract ?ani) (minusOne)
    )
)


;;; After the Scale and Color filtering process, we check the global variable ?*counter*.
;;; If we have 1 ball left, this is the result and we assert (found true) in order to trigger the success print rule below.
;;; If we have 0 balls left, we assert (found false) as there are no ball that passed the filtering.
;;; If we got more than one, we need more facts to reach a conclusion, so we assert (needMoreFacts ?scale ?color) for the program to continue asking.
(defrule postFilteringEvaluation
    ?scale <- (theScale ?s)
    ?color <- (theColor ?c)
    =>
    (retract ?scale ?color)
    (if (eq ?*counter* 1)
        then (assert (found true))
    else (if (eq ?*counter* 0)
            then (assert (found false))
         ) 
    else (if (> ?*counter* 1)
            then (assert (needMoreFacts ?s ?c))
         ) 
    )   
)   


;;; Given the fact (needMoreFacts ?s ?c) where ?s is the scale and ?c is the color the user has asked, we ask a more specific question about the ball
;;; that we are searching. We then assert a fact in the following form: (ask X Y) where X is a field of the aball template and Y is what we are searching in X.
 (defrule needMoreFacts
    ?q <-(needMoreFacts ?s ?c)
    =>
    (retract ?q)
    (if (and (eq ?s tiny) (eq ?c brown))
        then (assert (ask spots true))
    )
    (if (and (eq ?s tiny) (eq ?c yellow))
        then (assert (ask body tail))
    )
    (if (and (eq ?s tiny) (eq ?c red))
        then (assert (ask environment indoor))
    )
    (if (and (eq ?s small) (eq ?c brown))
        then (assert (ask body leather))
    )
    (if (and (eq ?s big) (eq ?c brown))
        then (assert (ask environment outdoors))
    )
    (if (and (eq ?s big) (eq ?c grey))
        then (assert (ask body leather))
    )
)


;;; Then, based on the user's answer, we retract one of them and we have reached a solution. We then assert the fact (found true).
(defrule askBody
    ?q <-(ask body ?ans)
    ?ani1 <- (ball (body $?content1))
    (test (member$ ?ans $?content1))
    ?ani2 <- (ball (body $?content2))
    (test (neq ?ani1 ?ani2))
    =>
    (retract ?q)
    (bind ?a (ask-question "### Has the ball got " ?ans " on it's body? (yes/no) ### " yes no))
    (if (eq ?a yes)
        then (retract ?ani2) (minusOne)
        else (retract ?ani1) (minusOne)
    )
    (if (eq ?*counter* 1)
        then (assert (found true))
    )
)


;;; This rule follows the same idea as the rule above. Two possible balls, one gets filtered and we got a solution.
(defrule ask environment
    ?q <-(ask environment ?ans)
    ?ani1 <- (ball (environment $?content1))
    (test (member$ ?ans $?content1))
    ?ani2 <- (ball (environment $?content2))
    (test (neq ?ani1 ?ani2))
    =>
    (retract ?q)
    (bind ?a (ask-question "### Does the ball exist in an environment that contains " ?ans "? (yes/no) ### " yes no))
    (if (eq ?a yes)
        then (retract ?ani2) (minusOne)
        else (retract ?ani1) (minusOne)
    )
    (if (eq ?*counter* 1)
        then (assert (found true))
    )
)

;;; in order to trigger the rule that follows.
(defrule ask Spots
    ?q <-(ask spots true)
    ?ani1 <- (ball (spots true))
    ?ani2 <- (ball (spots false))
    ?ani3 <- (ball (spots false))
    (test (neq ?ani2 ?ani3))
    =>
    (retract ?q)
    (bind ?a (ask-question "### Does the ball have " spots "? (yes/no) ### " yes no))
    (if (eq ?a yes)
        then (retract ?ani2) (minusOne) (retract ?ani3) (minusOne)
        else (retract ?ani1) (minusOne) (assert (ask particularity straight-moving))
    )
    (if (eq ?*counter* 1)
        then (assert (found true))
    )
)
;;; One of them gets filtered out and we got a solution, asserting (found true).
(defrule askParticularity
    ?q <-(ask particularity ?ans)
    ?ani1 <- (ball (particularity $?content1))
    (test (member$ ?ans $?content1))
    ?ani2 <- (ball (particularity $?content2))
    (test (neq ?ani1 ?ani2))
    =>
    (retract ?q)
    (bind ?a (ask-question "### Is the ball " rotating "? (yes/no) ### " yes no))
    (if (eq ?a yes)
        then (retract ?ani2) (minusOne)
        else (retract ?ani1) (minusOne)
    )
    (if (eq ?*counter* 1)
        then (assert (found true))
    )
)
 (defrule matchFound
    ?f <- (found true)
    ?ani <- (ball (name ?n) (kind ?k))
    =>
    (retract ?f ?ani)
    (printout t "*********************" crlf)
    (printout t "* ball found!" crlf)
    (printout t "* Name: " ?n crlf)
    (printout t "* Kind: " ?k crlf)
    (printout t "*********************" crlf)
)

;;; Just like the rule above, if the fact (found false) is present, we have no (balls) facts in memory. This means we have
;;; no results with the given criteria. We then print the failure to the user.
(defrule matchNotFound
    ?f <- (found false)
    =>
    (retract ?f)
    (printout t "*********************" crlf)
    (printout t "* No balls match your description!" crlf)
    (printout t "*********************" crlf)
)

这些是我运行它们时显示的错误。

[EXPRNPSR3] Missing function declaration for ask-question.

ERROR:
(defrule MAIN::mainQuestion-Size
   ?x <- (initial-fact)
   =>
   (retract ?x)
   (bind ?size (ask-question
CLIPS> (defrule filterBy-Color
    (theColor ?c)
    ?ani <- (ball (colors $?colors))
    =>
    (if (not (member$ ?c $?colors))
        then (retract ?ani) (minusOne)
    )
)


[EXPRNPSR3] Missing function declaration for minusOne.

ERROR:
(defrule MAIN::filterBy-Color
   (theColor ?c)
   ?ani <- (ball (colors $?colors))
   =>
   (if (not (member$ ?c $?colors))
      then
      (retract ?ani)
      (minusOne
CLIPS> (defrule askBody
    ?q <-(ask body ?ans)
    ?ani1 <- (ball (body $?content1))
    (test (member$ ?ans $?content1))
    ?ani2 <- (ball (body $?content2))
    (test (neq ?ani1 ?ani2))
    =>
    (retract ?q)
    (bind ?a (ask-question "### Has the ball got " ?ans " on it's body? (yes/no) ### " yes no))
    (if (eq ?a yes)
        then (retract ?ani2) (minusOne)
        else (retract ?ani1) (minusOne)
    )
    (if (eq ?*counter* 1)
        then (assert (found true))
    )
)

[EXPRNPSR3] Missing function declaration for ask-question.

ERROR:
(defrule MAIN::askBody
   ?q <- (ask body ?ans)
   ?ani1 <- (ball (body $?content1))
   (test (member$ ?ans $?content1))
   ?ani2 <- (ball (body $?content2))
   (test (neq ?ani1 ?ani2))
   =>
   (retract ?q)
   (bind ?a (ask-question
CLIPS> (defrule ask environment
    ?q <-(ask environment ?ans)
    ?ani1 <- (ball (environment $?content1))
    (test (member$ ?ans $?content1))
    ?ani2 <- (ball (environment $?content2))
    (test (neq ?ani1 ?ani2))
    =>
    (retract ?q)
    (bind ?a (ask-question "### Does the ball exist in an environment that contains " ?ans "? (yes/no) ### " yes no))
    (if (eq ?a yes)
        then (retract ?ani2) (minusOne)
        else (retract ?ani1) (minusOne)
    )
    (if (eq ?*counter* 1)
        then (assert (found true))
    )
)

[PRNTUTIL2] Syntax Error:  Check appropriate syntax for defrule.

ERROR:
(defrule MAIN::ask
   environment
CLIPS> (defrule askParticularity
    ?q <-(ask particularity ?ans)
    ?ani1 <- (ball (particularity $?content1))
    (test (member$ ?ans $?content1))
    ?ani2 <- (ball (particularity $?content2))
    (test (neq ?ani1 ?ani2))
    =>
    (retract ?q)
    (bind ?a (ask-question "### Is the ball " rotating "? (yes/no) ### " yes no))
    (if (eq ?a yes)
        then (retract ?ani2) (minusOne)
        else (retract ?ani1) (minusOne)
    )
    (if (eq ?*counter* 1)
        then (assert (found true))
    )
)

[EXPRNPSR3] Missing function declaration for ask-question.

ERROR:
(defrule MAIN::askParticularity
   ?q <- (ask particularity ?ans)
   ?ani1 <- (ball (particularity $?content1))
   (test (member$ ?ans $?content1))
   ?ani2 <- (ball (particularity $?content2))
   (test (neq ?ani1 ?ani2))
   =>
   (retract ?q)
   (bind ?a (ask-question
CLIPS> (defrule filterBy-Size
    (theScale ?s)
    ?ani <- (ball (size ?size))
    =>
    (if (eq ?s tiny)
        then 
            (if (> ?size 4) then (retract ?ani) (minusOne)
    else  (eq ?s small)
            then  (or (<= ?size 4) (>= ?size 20) then (retract ?ani) (minusOne))
         )
    else (eq ?s big)
            then  (< ?size 20) then (retract ?ani) (minusOne))
         )

[EXPRNPSR3] Missing function declaration for minusOne.

ERROR:
(defrule MAIN::filterBy-Size
   (theScale ?s)
   ?ani <- (ball (size ?size))
   =>
   (if (eq ?s tiny)
      then
      (if (> ?size 4)
         then
         (retract ?ani)
         (minusOne
CLIPS> (defrule filterBy-Size
    (theScale ?s)
    ?ani <- (ball (size ?size))
    =>
    (if (eq ?s tiny)
        then (if (> ?size 4) then (retract ?ani) (minusOne))
    else (if (eq ?s small)
            then (if (or (<= ?size 4) (>= ?size 20)) then (retract ?ani) (minusOne))
         )
    else (if (eq ?s big)
            then (if (< ?size 20) then (retract ?ani) (minusOne))
         )
    )
)

[EXPRNPSR3] Missing function declaration for minusOne.

ERROR:
(defrule MAIN::filterBy-Size
   (theScale ?s)
   ?ani <- (ball (size ?size))
   =>
   (if (eq ?s tiny)
      then
      (if (> ?size 4)
         then
         (retract ?ani)
         (minusOne
CLIPS> 
clips
1个回答
0
投票
;; Fix #1: Added semicolons
;; Ball_Identification_Expert_System

   .
   .
   .

;; Fix #2: Added Quotation Mark 
(deffacts ball-database "Adding the balls database to the facts."
    ;;; Table Tennis
    (ball (name "Table Tenis") 

   .
   .
   .

;; Fix #3: Missing deffunction header and initial answer
(deffunction ask-question (?qBEG ?qMID ?qEND $?allowed-values)
   (printout t ?qBEG ?qMID ?qEND)
   (bind ?answer (read))  
   ;;; This function is used for every question made to the user.

   .
   .
   .

;; Fix #4: Removed space
(defrule askEnvironment 

   .
   .
   .

;; Fix #5: Removed space
(defrule askSpots 
© www.soinside.com 2019 - 2024. All rights reserved.