#!/usr/bin/clisp
;
; Copyright (c) 2004 Matthew W. Coan.  All rights reserved.
;
; Author : Matthew W. Coan
; Date   : Tue Oct 12 21:35:22 EST 2004
;
;

; aks a question to the user
(defun ask (question question_table visited)
   (setq quit '(QUITAI))
   (setq true_list '(TRUE))
   (setq false_list '(FALSE))
   (setq answer nil)
   (cond ((not (equal question quit))
         (cond ((null (gethash question visited)) 
               (setf (gethash question visited) 'true)
               (cond ((null (gethash question question_table))
                     (cond ( (equal (gethash quit question_table) false_list)
                           (print question)
                           (print '?)
                           (setq answer (read))
                           (cond ((equal quit answer)
                                 (print '(quit ai))
                                 (setf (gethash quit question_table) true_list))
                           (t (setf (gethash question question_table) answer)
                              (askquestions answer question_table visited)
                              (askquestions question question_table 
                               visited))))))
                      (t (setq answer (gethash question question_table))
                         (cond ( (equal (gethash quit question_table) 
                                         false_list)
                               (print question)
                               (print answer)
                               (askquestions question question_table visited)
                               (askquestions answer question_table visited
)))))))))
)

; ask questions
(defun askquestions (text question_table visited)
   (cond ((not (null text)) 
          (askquestions (cdr text) question_table visited)
          (ask (cons 'who (list (car text))) question_table visited)
          (ask (cons 'what (list (car text))) question_table visited)
          (ask (cons 'when (list (car text))) question_table visited)
          (ask (cons 'where (list (car text))) question_table visited)
          (ask (cons 'why (list (car text))) question_table visited)
          (ask (cons 'how (list (car text))) question_table visited)
)))

; read memory bank file
(defun readfile (infile question_table)
   (setq question (read infile nil 'eof))
   (setq answer (read infile nil 'eof))
   (cond ((not (equal question 'eof)) 
         (setf (gethash question question_table) answer)))
   (print question)
   (print answer) 
   (cond ((not (equal question 'eof)) (readfile infile question_table)))
)

; write memory bank file
(defun writefile (outfile question_table)
   (setq temp '())
   (maphash #'(lambda (key val)
               (print key)
               (print val)
               (print key outfile)
               (cond ((null val)
                      (print temp outfile))
                      (t (print val outfile)))
              ) question_table)
)

; main function
(defun main ()
   (setq question nil)
   (setq answer nil)
   (print '(loading data))
   (setq question_table (make-hash-table :test #'equal))
   (setq visited (make-hash-table :test #'equal))
   (with-open-file (infile "memory_bank.lisp")
      (readfile infile question_table))
   (print '(written by matthew william coan)) 
   (print '(type (quitai) to exit))
   (print '(please ask a question)) 
   (print '?)
   (setq question (read)) 
   (ask question question_table visited)
   (setq quit '(QUITAI))
   (setq false_list '(FALSE))
   (setf (gethash quit question_table) false_list)
   (with-open-file (outfile "memory_bank.lisp" :direction :output)
      (writefile outfile question_table)))

(main)
