将一个值与另一个值进行比较以测试CLIPS中是否存在文件

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

我正在尝试让用户输入书名,然后测试该书是否存在于库中。如果不是,程序应该要求他输入书籍详细信息。但该计划将所有输入视为一本新书。我比较两个值是错误的还是我的readline?

代码到目前为止:

(deftemplate book (slot name) (slot author) (slot code))

(deffacts current-lib
  (book (name "Alice in Wonderland") (author Lewis-Carroll) (code CAR))
  (book (name "The Bourne Supremacy") (author Robert-Ludlum) (code LUD)))

(defrule readnew "inputs potential new book details"
=>


(printout t "Enter the name of the book:")
  (bind ?b_name (readline))
  (assert (potential ?b_name)))

(defrule add-book "determine if book already exists otherwise add"
  ?out <- (potential ?newname)
  (and (potential ?newname)
       (not (book (name ?b_name&?newname) (author $?))))
=>
  (printout t "Book is new, please enter the author's name:" crlf)
  (bind ?auth (readline))
  (printout t "Please enter a three letter code for the book:" crlf)
  (bind ?coode (read))
  (assert (book (name ?newname) (author ?auth) (code ?coode)))
  (retract ?out))
clips
1个回答
0
投票

您提供了代码,但没有提供运行它的步骤,所以我必须猜测问题的原因。最简单的解释是你没有发出reset命令来断言current-lib deffacts中的事实。

我对您的代码进行了一些更改。在你的current-lib deffacts中,作者名称应该是字符串,因为你在你的add-book规则中使用readline来获取名字。您的添加订单规则的条件中也有不必要的代码。

         CLIPS (6.31 2/3/18)
CLIPS> 
(deftemplate book 
   (slot name)
   (slot author) (slot code))
CLIPS> 
(deffacts current-lib
   (book (name "Alice in Wonderland") (author "Lewis Carroll") (code CAR))
   (book (name "The Bourne Supremacy") (author "Robert Ludlum") (code LUD)))
CLIPS> 
(defrule readnew
   =>
   (printout t "Enter the name of the book:" crlf)
   (bind ?b_name (readline))
   (assert (potential ?b_name)))
CLIPS> 
(defrule add-book
  ?out <- (potential ?newname)
  (not (book (name ?newname)))
  =>
  (printout t "Book is new, please enter the author's name:" crlf)
  (bind ?auth (readline))
  (printout t "Please enter a three letter code for the book:" crlf)
  (bind ?coode (read))
  (assert (book (name ?newname) (author ?auth) (code ?coode)))
  (retract ?out))
CLIPS>

现在,如果您添加一本不存在的图书,您将需要其他信息。

CLIPS> (reset)
CLIPS> (run)
Enter the name of the book:
Ringworld
Book is new, please enter the author's name:
Larry Niven
Please enter a three letter code for the book:
RNG
CLIPS> 

如果您尝试添加不存在的图书,则不会执行添加图书规则。

CLIPS> (reset)
CLIPS> (run)
Enter the name of the book:
Alice in Wonderland
CLIPS>
© www.soinside.com 2019 - 2024. All rights reserved.