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

Compare one value to another to test if file exists in CLIPS

我正在尝试让用户输入书名,然后测试该书是否存在于图书馆中。如果不是,程序应该要求他输入书籍的详细信息。但是该程序将所有输入视为一本新书。我比较这两个值是错误的还是我的 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))

您提供了代码,但没有提供运行它所采取的步骤,因此我将不得不猜测您的问题的原因。最简单的解释是您没有发出 reset 命令来断言 current-lib defacts 中的事实。

我对你的代码做了一些修改。在您的 current-lib defacts 中,作者姓名应该是字符串,因为您在 add-book[ 中使用 readline =28=] 规则来获取名字。在您的 add-book 规则的条件中也有不必要的代码。

         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> 

如果您尝试添加一本不存在的书,add-book 规则将不会执行。

CLIPS> (reset)
CLIPS> (run)
Enter the name of the book:
Alice in Wonderland
CLIPS>