;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;; from SPL lisp code
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defvar *experiment_contexts* nil)
(defvar *current_context* nil)
(defvar *experiment_nlevels* nil)
(defvar *experiment_settings* nil)
(defvar *nvariables* 6)
(defvar *max_levels* 3)
(defvar *experiment_contents* nil)   ;varied variables, their values, cell-contents, non-varied vars+values
(defvar *ct* nil)
(defvar *at* nil)
(defvar *et* t)
(defvar *search_path* nil)
(defvar *runs*)
(defvar *which-model-page* 'f11)
(defvar *first-experiment* t)
(defvar *model-results* nil)
(defvar *previous-experiment* nil)
(defvar *max-changes* 0)
(defvar *exper-count* 0)
(defvar *text*)
(defvar *graphic*)
(defvar *overlay*)
(setf *text* t)
(setf *graphic* nil)
(setf *overlay* nil)

(defparameter *verbal_numbers-spl*
  '(one two three four five six seven eight nine ten))

(defparameter *verbal_numbers_to_value-spl*
   (pairlis *verbal_numbers-spl* '(1 2 3 4 5 6 7 8 9 10)))

(defparameter *dim_locations-spl*  '(dim1 dim2 dim3 dim4))

(defparameter *external_dim_names-spl* '(e-dim1 e-dim2 e-dim3 e-dim4))

(defparameter *external_nv_names-spl* '(e-nv-dim1 e-nv-dim2 e-nv-dim3 e-nv-dim4))

(defparameter *external_dim_value_names-spl* 
  '((e-dim1-v1 e-dim1-v2 e-dim1-v3)
    (e-dim2-v1 e-dim2-v2 e-dim2-v3)
    (e-dim3-v1 e-dim3-v2 e-dim3-v3)
    (e-dim4-v1 e-dim4-v2 e-dim4-v3)))

(defparameter *external_constant_var_names-spl* '(e-cv1 e-cv2 e-cv3 e-cv4 e-cv5 e-cv6))

(defparameter *ecell_names-spl*
 '((((e-cell-1111 e-cell-1112) (e-cell-1121 e-cell-1122))
     ((e-cell-1211 e-cell-1212) (e-cell-1221 e-cell-1222)))
    (((e-cell-2111 e-cell-2112) (e-cell-2121 e-cell-2122))
     ((e-cell-2211 e-cell-2212) (e-cell-2221 e-cell-2222)))))

(defparameter *internal_variable_names-spl*
  '(repetitions spacing source_context test_task delay test_context))

(defparameter *variable_options-spl*
  '((two three four five)
    (one_minute five_minutes ten_minutes)
    (same diff_room diff_mood) 
    (recall recog stem)
    (one_minute five_minutes ten_minutes) 
    (same diff_room diff_mood)))

(defparameter *external_option_names-spl*
  '((e_two e_three e_four e_five) (e_one_minute e_five_minutes e_ten_minutes) (e_same e_diff_room e_diff_mood)
    (e_recall e_recog e_stem) (e_one_minute e_five_minutes e_ten_minutes) (e_same e_diff_room e_diff_mood)))

(defparameter *variable_option_locations-spl*
  '(top middle bottom))

(defparameter *main_button_locations-spl*
  '(1left 2left 3left 1right 2right 3right))

(defparameter *main_button_names-spl*
  '(rep_button sp_button sc_button tt_button del_button tc_button))

(defparameter *level_ids-spl*
  '(level1 level2 level3))

(defparameter *level_scrollers-spl*
  '(scroller_level1 scroller_level2 scroller_level3))

(defparameter *level_sliders-spl*
  '(slider_level1 slider_level2 slider_level3))

(defparameter *option_locations-spl*
  '(1right 2right 3right))

(defparameter *main_button_to_variable-spl*
  (pairlis *main_button_locations-spl* *internal_variable_names-spl*))

(defparameter *variable_to_number-spl*
  (pairlis *internal_variable_names-spl* '(0 1 2 3 4 5)))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; This section contains the interface for the WWW using the
;;; ACT-R on the Web application by Elmar Schwarz


(defvar *WWW-interface*)
(setf *WWW-interface*
      '((:heading "SPL Model (figure 11.12)" 2)
        (:table)
        (:table)
        "Number runs (1-20)" (:string :sy *runs* 2) 
        (:table-end)
        (:table)
        (:checkbox "Cycle Trace" :sy *ct* nil) (:new-row) 
        (:checkbox "Action Trace" :sy *at* nil) (:new-row) 
        (:checkbox "Experiment Trace" :sy *et* nil) (:new-row) 
        (:checkbox "Text output" :sy *text*  t) (:new-row)
        (:checkbox "Graphic output" :sy *graphic*  nil) (:new-row)
        (:checkbox "Show simulation and experiment data on one graph" :sy *overlay*  nil)
        (:table-end)
        (:table-end)
        (:new-para)
        
        (:button "Show Experiment Results" "(output-spl *fig12-data-spl*  nil)")
       
        (:new-para)

        (:button "Run Model" "(if (and  (numberp *runs*))
                                  (run-model-for-graph-spl (min 20 (max 1 *runs*)))
                                  (format *standard-output* \"Both addents must be numbers~%\"))") 
        (:button "Production Rules" "(let ((prods (no-output (pp))))
                                         (dolist (x prods)
                                           (pp-fct (list x))
                                           (spp-fct (list x))
                                           (format *standard-output* \"~%\")))")
        (:button "Chunk types" "(chunk-type)")
        (:button "Chunks" "(dm)")
        
        (:new-para)
        (:hidden :sy *which-model-page* f12)

         "TIME and SIZE:"
        (:new-para)
        "- It usually takes about 1 minute for 1 run of the model"
        (:new-line)
        "- The cycle trace of 1 run is approximatly 180k (120 pages) in size"
        (:new-line)
        "- The action trace of 1 run is approximatly 160k (100 pages) in size"
        (:new-line)
        "- The experiment trace of 1 run is approximatly 6k (4 page) in size"))
        


(defun run-model-for-graph-spl (n)
  (setf *runs* n)

  (let ((results nil))
    (dolist (x (case *which-model-page*
                 (f11 '(with without))
                 (f12 '(with without))
                 (f13 '(with without-i without-t))))
      
      (run-spl n x)
      (push *model-results* results))
    
    (output-spl results t)))

(defun output-spl (results sim)
  (when sim
    (format *standard-output* "~%~S Runs of the model.~%" *runs*))

  (case *which-model-page*
    (f11 (display-output-f11-spl results sim))
    (f12 (display-output-f12-spl results sim))
    (f13 (display-output-f13-spl results sim))))

(defparameter *fig11-data-spl* '(((.783 .478 .391 .217 .345 .261)) ((.143 .571 .714 .286 .429 .143))))
(defparameter *factor-names-spl* '("Repetitions" "Spacing" "Source cntxt" "Test task" "Delay" "Test cntxt"))
(defparameter *fig12-data-spl* '((0 33 36 65) (0 0 15 36) (0 0 7 28)))
(defparameter *fig12-names-spl* '("Domain Experts" "Task Experts" "High Ugrads" "Mid Ugrads"))
(defparameter *fig13-data-spl* '((2.8 1.9 2.5 2.7) (2.8 1.7 2.2 2.6)))
(defparameter *fig13-names-spl* '("With Interaction" "Without interaction" "Without Test Hyp"))

(defun draw-graph-f13-sim-only-spl (res)

    (format *standard-output* 
       "<PARAM name=\"longestline\" value=\"3\">
        <PARAM name=\"numlines\" value=\"2\">
        <PARAM name=\"xmin\" value=\"0\">
        <PARAM name=\"xmax\" value=\"4\">
        <PARAM name=\"ymax\" value=\"3.0\">
        <PARAM name=\"ymin\" value=\"0\">
        <PARAM name=\"ydiv\" value=\".5\">
        <PARAM name=\"numxlabels\" value=\"5\">
        <PARAM name=\"yspacing\" value=\"1\">
        <PARAM name=\"xlabels\" value=\" ;With Interaction;Without Interaction;Without Test Hyp; ;\">
        <PARAM name=\"widestxlabel\" value=\"WWWWWWWWWWWWWWWWWWW\">
        <PARAM name=\"xval0\" value=\"1;2;3;\">
        <PARAM name=\"xval1\" value=\"1;2;3;\">
        <PARAM name=\"xname\" value=\"Who\">
        <PARAM name=\"yname\" value=\"# Factors per Experiment\">
        <PARAM name=\"name0\" value=\"model All experiments\">
        <PARAM name=\"name1\" value=\"model First experiment\">
        <PARAM name=\"lcolor0\" value=\"0\">
        <PARAM name=\"lcolor1\" value=\"1\">
        <PARAM name=\"lstyle0\" value=\"6555\">
        <PARAM name=\"lstyle1\" value=\"9831\">
 ")
   
    (format *standard-output* 
            "<PARAM name=\"yval0\" value=\"~3,2f;~3,2f;~3,2f;\">
             <PARAM name=\"yval1\" value=\"~3,2f;~3,2f;~3,2f;\">"
            (first (first res)) (second (first res)) (third (first res))
            (first (second res)) (second (second res)) (third (second res))))
            

(defun draw-graph-f13-sim-and-data-spl (res)

    (format *standard-output* 
       "<PARAM name=\"longestline\" value=\"4\">
        <PARAM name=\"numlines\" value=\"4\">
        <PARAM name=\"xmin\" value=\"0\">
        <PARAM name=\"xmax\" value=\"9\">
        <PARAM name=\"ymax\" value=\"3.0\">
        <PARAM name=\"ymin\" value=\"0\">
        <PARAM name=\"ydiv\" value=\".5\">
        <PARAM name=\"numxlabels\" value=\"10\">
        <PARAM name=\"yspacing\" value=\"1\">
        <PARAM name=\"xlabels\" value=\" ;Domain;Task;High;Mid; ;W/ Inter;W/o Inter;W/o Hypo; ;\">
        <PARAM name=\"widestxlabel\" value=\"WWWWWWWWW\">
        <PARAM name=\"xval0\" value=\"1;2;3;4;\">
        <PARAM name=\"xval1\" value=\"1;2;3;4;\">
        <PARAM name=\"xval2\" value=\"6;7;8;\">
        <PARAM name=\"xval3\" value=\"6;7;8;\">
        <PARAM name=\"xname\" value=\"Who\">
        <PARAM name=\"yname\" value=\"# Factors per Experiment\">
        <PARAM name=\"name0\" value=\"data All experiments\">
        <PARAM name=\"name1\" value=\"data First experiment\">
        <PARAM name=\"name2\" value=\"model All experiments\">
        <PARAM name=\"name3\" value=\"model First experiment\">
        <PARAM name=\"lcolor0\" value=\"0\">
        <PARAM name=\"lcolor1\" value=\"1\">
        <PARAM name=\"lcolor2\" value=\"0\">
        <PARAM name=\"lcolor3\" value=\"1\">
        <PARAM name=\"lstyle0\" value=\"0\">
        <PARAM name=\"lstyle1\" value=\"0\">
        <PARAM name=\"lstyle2\" value=\"6555\">
        <PARAM name=\"lstyle3\" value=\"9831\">
 ")
    (dotimes (i 2)
      (format *standard-output* "<PARAM name=\"yval~s\" value=\"" i)
      
      (dotimes (j 4)
        (format *standard-output* "~5,2f;" (nth j (nth i *fig13-data-spl*))))
      (format *standard-output* "\">"))
    
    (format *standard-output* 
            "<PARAM name=\"yval2\" value=\"~3,2f;~3,2f;~3,2f;\">
             <PARAM name=\"yval3\" value=\"~3,2f;~3,2f;~3,2f;\">"
            (first (first res)) (second (first res)) (third (first res))
            (first (second res)) (second (second res)) (third (second res))))



(defun draw-graph-f13-data-only-spl ()


    (format *standard-output* 
       "<PARAM name=\"longestline\" value=\"4\">
        <PARAM name=\"numlines\" value=\"2\">
        <PARAM name=\"xmin\" value=\"0\">
        <PARAM name=\"xmax\" value=\"5\">
        <PARAM name=\"ymax\" value=\"3.0\">
        <PARAM name=\"ymin\" value=\"0\">
        <PARAM name=\"ydiv\" value=\".5\">
        <PARAM name=\"numxlabels\" value=\"6\">
        <PARAM name=\"yspacing\" value=\"1\">
        <PARAM name=\"xlabels\" value=\" ;Domain experts;Task experts;High ugrads;Mid ugrads; ;\">
        <PARAM name=\"widestxlabel\" value=\"WWWWWWWWWWWWWW\">
        <PARAM name=\"xval0\" value=\"1;2;3;4;\">
        <PARAM name=\"xval1\" value=\"1;2;3;4;\">
        <PARAM name=\"xname\" value=\"Who\">
        <PARAM name=\"yname\" value=\"# Factors per Experiment\">
        <PARAM name=\"name0\" value=\"data All experiments\">
        <PARAM name=\"name1\" value=\"data First experiment\">
        <PARAM name=\"lcolor0\" value=\"0\">
        <PARAM name=\"lcolor1\" value=\"1\">
        <PARAM name=\"lstyle0\" value=\"0\">
        <PARAM name=\"lstyle1\" value=\"0\">
 ")
   
     (dotimes (i 2)
      (format *standard-output* "<PARAM name=\"yval~s\" value=\"" i)
      
      (dotimes (j 4)
        (format *standard-output* "~5,2f;" (nth j (nth i *fig13-data-spl*))))
      (format *standard-output* "\">")))


(defun display-f13-sim-spl (res)
  (format *standard-output* "~%~%Simulation results:~%~%")
  
  (format *standard-output* "Who                      Factors per Experiment~%")
  (format *standard-output* "                 First Experiment     All Experiments~%")
  (dotimes (i 3)
    (format *standard-output* "~20A    ~9,1f             ~9,1f~%" (nth i *fig13-names-spl*)
            (nth i (second res)) (nth i (first res))))
  (format *standard-output* "~%"))

(defun display-f13-data-spl ()
  
    (format *standard-output* "~%~%Experiment results:~%~%")
  
  (format *standard-output* "Who                      Factors per Experiment~%")
  (format *standard-output* "                 First Experiment     All Experiments~%")
  (dotimes (i 4)
    (format *standard-output* "~20A    ~9,1f             ~9,1f~%" (nth i *fig12-names-spl*)
            (nth i (second *fig13-data-spl*)) (nth i (first *fig13-data-spl*))))
  (format *standard-output* "~%"))


(defun display-output-f13-spl (results sim)
  (let ((d (list nil nil)))
    (when sim
      (dotimes (i 2)
        (dotimes (j 3)
          (push 0 (nth i d))))
    (dotimes (i 3)
      (let ((first-sum 0)
            (total-sum 0)
            (total-count 0))
        (dolist (x (nth i results))
          (incf first-sum (third x))
          (incf total-sum (/ (second x) (first x))))
        (setf (nth (- 2 i) (first d)) (/ total-sum (length (first results))))
        (setf (nth (- 2 i) (second d)) (/ first-sum (length (first results))))))
    (setf results d))
    
    (when *text*
      (if sim
          (display-f13-sim-spl results)
          (display-f13-data-spl))
      (when (and sim *overlay*)
        (display-f13-data-spl))
      
      (unless *graphic* (format *standard-output*
                                "~%</pre>If your browser supports JAVA, you
                               can display the data in a graph by checking
                               the Graphic output box on the interface page.<pre>~%~%")))
    (when *graphic*
      (format *standard-output* "
        <applet
        code = \"DansGraphs.class\"
        width = 700
        height = 400>

        <PARAM name=\"title\" value=\"Figure 11.13\">")
      
      (if (and sim *overlay*)
          (draw-graph-f13-sim-and-data-spl results)
          (if sim
              (draw-graph-f13-sim-only-spl results)
              (draw-graph-f13-data-only-spl )))
      
      
    (format *standard-output* "
             <HR> Your browser does not support JAVA, so you cannot view the graphs.~%
             </HR></applet>"))
    
    ))


(defun display-f12-sim-spl (res)
  (format *standard-output* "~%~%Simulation results:~%~%")
  
  (format *standard-output* "Who                           %Varied~%")
  (format *standard-output* "                 change >1    change >2    change >3~%")
  (dotimes (i 2)
    (format *standard-output* "~14A    ~9,1f    ~9,1f    ~9,1f~%" (if (= i 0) "With" "Without")
            (* 100 (/ (nth i (first res)) (third (first res))))
            (* 100 (/ (nth i (second res)) (third (first res))))
            (* 100 (/ (nth i (third res)) (third (first res))))))
  (format *standard-output* "~%"))

(defun display-f12-data-spl ()
  
    (format *standard-output* "~%~%Experiment results:~%~%")

    (format *standard-output* "Who                           %Varied~%")
    (format *standard-output* "                change >1    change >2    change >3~%")
    (dotimes (i 4)
      (format *standard-output* "~14A    ~9,1f    ~9,1f    ~9,1f~%" (nth i *fig12-names-spl*)
              (nth i (first *fig12-data-spl*)) (nth i (second *fig12-data-spl*)) (nth i (third *fig12-data-spl*))))
    (format *standard-output* "~%"))

(defun display-output-f12-spl (results sim)
  (let ((d (list nil nil nil)))
    (when sim
    (dotimes (i 3)
        (dotimes (j 3)
          (push 0 (nth i d))))
    (dolist (x (car results))
      (when (> x 1) (incf (second (first d))) )
      (when (> x 2) (incf (second (second d))) )
      (when (> x 3) (incf (second (third d))) ))
    (dolist (x (second results))
      (when (> x 1) (incf (first (first d))) )
      (when (> x 2) (incf (first (second d))) )
      (when (> x 3) (incf (first (third d))) ))
    (setf (third (first d)) (length (car results)))
    
    
    (setf results d))

     (when *text*
      (if sim
          (display-f12-sim-spl results)
          (display-f12-data-spl))
    (when (and sim *overlay*)
      (display-f12-data-spl))

    (unless *graphic* (format *standard-output*
                              "~%</pre>If your browser supports JAVA, you
                               can display the data in a graph by checking
                               the Graphic output box on the interface page.<pre>~%~%")))
  (when *graphic*
    (format *standard-output* "
        <applet
        code = \"DansGraphs.class\"
        width = 700
        height = 400>

        <PARAM name=\"title\" value=\"Figure 11.12\">")

    (if (and sim *overlay*)
        (draw-graph-f12-sim-and-data-spl results)
        (if sim
            (draw-graph-f12-sim-only-spl results)
            (draw-graph-f12-data-only-spl )))
    

    (format *standard-output* "
             <HR> Your browser does not support JAVA, so you cannot view the graphs.~%
             </HR></applet>"))

))

(defun draw-graph-f12-sim-only-spl (res)

    (format *standard-output* 
       "<PARAM name=\"longestline\" value=\"2\">
        <PARAM name=\"numlines\" value=\"3\">
        <PARAM name=\"xmin\" value=\"0\">
        <PARAM name=\"xmax\" value=\"3\">
        <PARAM name=\"ymax\" value=\"100\">
        <PARAM name=\"ymin\" value=\"0\">
        <PARAM name=\"ydiv\" value=\"5\">
        <PARAM name=\"numxlabels\" value=\"4\">
        <PARAM name=\"yspacing\" value=\"10\">
        <PARAM name=\"xlabels\" value=\" ;With;Without; ;\">
        <PARAM name=\"widestxlabel\" value=\"WWWWWWW\">
        <PARAM name=\"xval0\" value=\"1;2;\">
        <PARAM name=\"xval1\" value=\"1;2;\">
        <PARAM name=\"xval2\" value=\"1;2;\">
        <PARAM name=\"xname\" value=\"Who\">
        <PARAM name=\"yname\" value=\"% Varied\">
        <PARAM name=\"name0\" value=\"model any change > 1\">
        <PARAM name=\"name1\" value=\"model any change > 2\">
        <PARAM name=\"name2\" value=\"model any change > 3\">
        <PARAM name=\"lcolor0\" value=\"0\">
        <PARAM name=\"lcolor1\" value=\"1\">
        <PARAM name=\"lcolor2\" value=\"2\">
        <PARAM name=\"lstyle0\" value=\"6555\">
        <PARAM name=\"lstyle1\" value=\"9831\">
        <PARAM name=\"lstyle2\" value=\"13107\">
 ")
   
    (format *standard-output* 
            "<PARAM name=\"yval0\" value=\"~s;~s;\">
             <PARAM name=\"yval1\" value=\"~s;~s;\">
             <PARAM name=\"yval2\" value=\"~s;~s;\">"

            (* 100 (/ (first (first res)) (third (first res))))
            (* 100 (/ (second (first res)) (third (first res))))
            (* 100 (/ (first (second res)) (third (first res))))
            (* 100 (/ (second (second res)) (third (first res))))
            (* 100 (/ (first (third res)) (third (first res))))
            (* 100 (/ (second (third res)) (third (first res))))))

(defun draw-graph-f12-sim-and-data-spl (res)

    (format *standard-output* 
       "<PARAM name=\"longestline\" value=\"4\">
        <PARAM name=\"numlines\" value=\"6\">
        <PARAM name=\"xmin\" value=\"0\">
        <PARAM name=\"xmax\" value=\"8\">
        <PARAM name=\"ymax\" value=\"100\">
        <PARAM name=\"ymin\" value=\"0\">
        <PARAM name=\"ydiv\" value=\"5\">
        <PARAM name=\"numxlabels\" value=\"9\">
        <PARAM name=\"yspacing\" value=\"10\">
        <PARAM name=\"xlabels\" value=\" ;Domain;Task;High;Mid; ;With;Without; ;\">
        <PARAM name=\"widestxlabel\" value=\"WWWWWWW\">
        <PARAM name=\"xval0\" value=\"1;2;3;4;\">
        <PARAM name=\"xval1\" value=\"1;2;3;4;\">
        <PARAM name=\"xval2\" value=\"1;2;3;4;\">
        <PARAM name=\"xval3\" value=\"6;7;\">
        <PARAM name=\"xval4\" value=\"6;7;\">
        <PARAM name=\"xval5\" value=\"6;7;\">
        <PARAM name=\"xname\" value=\"Who\">
        <PARAM name=\"yname\" value=\"% Varied\">
        <PARAM name=\"name0\" value=\"data any change > 1\">
        <PARAM name=\"name1\" value=\"data any change > 2\">
        <PARAM name=\"name2\" value=\"data any change > 3\">
        <PARAM name=\"name3\" value=\"model any change > 1\">
        <PARAM name=\"name4\" value=\"model any change > 2\">
        <PARAM name=\"name5\" value=\"model any change > 3\">
        <PARAM name=\"lcolor0\" value=\"0\">
        <PARAM name=\"lcolor1\" value=\"1\">
        <PARAM name=\"lcolor2\" value=\"2\">
        <PARAM name=\"lcolor3\" value=\"0\">
        <PARAM name=\"lcolor4\" value=\"1\">
        <PARAM name=\"lcolor5\" value=\"2\">
        <PARAM name=\"lstyle0\" value=\"0\">
        <PARAM name=\"lstyle1\" value=\"0\">
        <PARAM name=\"lstyle2\" value=\"0\">
        <PARAM name=\"lstyle3\" value=\"6555\">
        <PARAM name=\"lstyle4\" value=\"9831\">
        <PARAM name=\"lstyle5\" value=\"13107\">
 ")
    (dotimes (i 3)
      (format *standard-output* "<PARAM name=\"yval~s\" value=\"" i)
      
      (dotimes (j 4)
        (format *standard-output* "~5,2f;" (nth j (nth i *fig12-data-spl*))))
      (format *standard-output* "\">"))
    
    (format *standard-output* 
            "<PARAM name=\"yval3\" value=\"~s;~s;\">
             <PARAM name=\"yval4\" value=\"~s;~s;\">
             <PARAM name=\"yval5\" value=\"~s;~s;\">"

            (* 100 (/ (first (first res)) (third (first res))))
            (* 100 (/ (second (first res)) (third (first res))))
            (* 100 (/ (first (second res)) (third (first res))))
            (* 100 (/ (second (second res)) (third (first res))))
            (* 100 (/ (first (third res)) (third (first res))))
            (* 100 (/ (second (third res)) (third (first res))))))



(defun draw-graph-f12-data-only-spl ()


    (format *standard-output* 
       "<PARAM name=\"longestline\" value=\"4\">
        <PARAM name=\"numlines\" value=\"3\">
        <PARAM name=\"xmin\" value=\"0\">
        <PARAM name=\"xmax\" value=\"5\">
        <PARAM name=\"ymax\" value=\"100\">
        <PARAM name=\"ymin\" value=\"0\">
        <PARAM name=\"ydiv\" value=\"5\">
        <PARAM name=\"numxlabels\" value=\"6\">
        <PARAM name=\"yspacing\" value=\"10\">
        <PARAM name=\"xlabels\" value=\" ;Domain experts;Task experts;High ugrads;Mid ugrads; ;\">
        <PARAM name=\"widestxlabel\" value=\"WWWWWWWWWWWWWW\">
        <PARAM name=\"xval0\" value=\"1;2;3;4;\">
        <PARAM name=\"xval1\" value=\"1;2;3;4;\">
        <PARAM name=\"xval2\" value=\"1;2;3;4;\">
        <PARAM name=\"xname\" value=\"Who\">
        <PARAM name=\"yname\" value=\"% Varied\">
        <PARAM name=\"name0\" value=\"Data any change > 1\">
        <PARAM name=\"name1\" value=\"Data any change > 2\">
        <PARAM name=\"name2\" value=\"Data any change > 3\">
        <PARAM name=\"lcolor0\" value=\"0\">
        <PARAM name=\"lcolor1\" value=\"1\">
        <PARAM name=\"lcolor2\" value=\"2\">
        <PARAM name=\"lstyle0\" value=\"0\">
        <PARAM name=\"lstyle1\" value=\"0\">
        <PARAM name=\"lstyle2\" value=\"0\">
 ")
   
    (dotimes (i 3)
      (format *standard-output* "<PARAM name=\"yval~s\" value=\"" i)
      
      (dotimes (j 4)
        (format *standard-output* "~5,2f;" (nth j (nth i *fig12-data-spl*))))
      (format *standard-output* "\">")))



(defun display-output-f11-spl (results sim)
  
  (let ((res1 (make-list 6 :initial-element 0))
        (res2 (make-list 6 :initial-element 0)))
    (dotimes (i 6)
      (setf (nth i res1) (apply #'+ (mapcar (lambda (x) (nth i x)) (car results))))
      (setf (nth i res2) (apply #'+ (mapcar (lambda (x) (nth i x)) (second results)))))

    (when *text*
    (format *standard-output* "~%~%~a data:~%" (if sim "Simulation" "Experimental"))
    (format *standard-output* "~%Factor                        % Varied") 
    (format *standard-output* "~%             ~15@A  ~15@A~%" (if sim "With" "Test theory") (if sim "Without" "Explore factors"))
    (dotimes (i 6)
      (format *standard-output* "~12A ~15,1f  ~15,1f~%" (nth i *factor-names-spl*) (* 100 (/ (nth i res2) (length (car results))))
              (* 100 (/ (nth i res1) (length (car results))))))
    (format *standard-output* "~%")

    (when (and sim *overlay*)
      (format *standard-output* "~%~%Experimental data:~%")
      (format *standard-output* "~%Factor                        % Varied~%") 
      (format *standard-output* "~%             ~15@A  ~15@A~%" "Test theory" "Explore factors")
      (dotimes (i 6)
        (format *standard-output* "~12A ~15,1f  ~15,1f~%" (nth i *factor-names-spl*) (* 100  (nth i (first (second *fig11-data-spl*)) ))
                (* 100  (nth i (first (car *fig11-data-spl*))))))
      (format *standard-output* "~%"))

    (unless *graphic* (format *standard-output*
                              "~%</pre>If your browser supports JAVA, you
                               can display the data in a graph by checking
                               the Graphic output box on the interface page.<pre>~%~%")))
  (when *graphic*
    (format *standard-output* "
        <applet
        code = \"DansGraphs.class\"
        width = 650
        height = 400>

        <PARAM name=\"title\" value=\"Figure 11.11\">
        <PARAM name=\"longestline\" value=\"6\">
        <PARAM name=\"numlines\" value=\"~S\">
        <PARAM name=\"xmin\" value=\"0\">
        <PARAM name=\"xmax\" value=\"5\">
        <PARAM name=\"ymax\" value=\"100\">
        <PARAM name=\"ymin\" value=\"0\">
        <PARAM name=\"ydiv\" value=\"5\">
        <PARAM name=\"numxlabels\" value=\"6\">
        <PARAM name=\"yspacing\" value=\"10\">
        <PARAM name=\"xlabels\" value=\"Repetitions;Spacing;Source cntxt;Test task;Delay;Test cntxt;\">
        <PARAM name=\"widestxlabel\" value=\"WWWWWWWWWWWW\">
        <PARAM name=\"xval0\" value=\"0;1;2;3;4;5;\">
        <PARAM name=\"xval1\" value=\"0;1;2;3;4;5;\">
        <PARAM name=\"xname\" value=\"Factor\">
        <PARAM name=\"yname\" value=\"% Varied\">
        <PARAM name=\"name0\" value=\"~a\">
        <PARAM name=\"name1\" value=\"~a\">
        <PARAM name=\"lcolor0\" value=\"0\">
        <PARAM name=\"lcolor1\" value=\"1\">
        <PARAM name=\"lstyle0\" value=\"~s\">
        <PARAM name=\"lstyle1\" value=\"~s\">
 "
            (if (and sim *overlay*) 4 2)
            (if sim "With" "Test theory")
            (if sim "Without" "Explore factors")
            (if sim 6555 0)
            (if sim 9831 0)
)

    (format *standard-output* "<PARAM name=\"yval0\" value=\"")

    (dotimes (i 6)
      (format *standard-output* "~5,2f;" (* 100 (/ (nth i res2) (length (car results))))))

    (format *standard-output* "\">")

    (format *standard-output* "<PARAM name=\"yval1\" value=\"")

    (dotimes (i 6)
      (format *standard-output* "~5,2f;" (* 100 (/ (nth i res1) (length (car results))))))

    (format *standard-output* "\">")
    

    (when (and *overlay* sim)
      (format *standard-output* "
        <PARAM name=\"xval2\" value=\"0;1;2;3;4;5;\">
        <PARAM name=\"xval3\" value=\"0;1;2;3;4;5;\">
        <PARAM name=\"name2\" value=\"Test theory\">
        <PARAM name=\"name3\" value=\"Explore factors\">
        <PARAM name=\"lcolor2\" value=\"0\">
        <PARAM name=\"lcolor3\" value=\"1\">
        <PARAM name=\"lstyle2\" value=\"0\">
        <PARAM name=\"lstyle3\" value=\"0\">
        <PARAM name=\"yval2\" value=\"")

 

     (dotimes (i 6)
        (format *standard-output* "~5,2f;" (* 100  (nth i (first (second *fig11-data-spl*)) ))))

      )

    (format *standard-output* "\"><PARAM name=\"yval3\" value=\"")

    (dotimes (i 6)
      (format *standard-output* "~5,2f;" (* 100  (nth i (first (car *fig11-data-spl*))))))

    (format *standard-output* "\">")

    (format *standard-output* "
             <HR> Your browser does not support JAVA, so you cannot view the graphs.~%
             </HR></applet>"))

))
                      
(defun set-up-spl ()
  (setf *experiment_contexts* nil)
  (setf *current_context* nil)
  (setf *experiment_nlevels* nil)
  (setf *experiment_settings* nil)
  (setf *nvariables* 6)
  (setf *max_levels* 3)
  (setf *experiment_contents* nil)   
  (setf *search_path* nil)
  (setf *first-experiment* t)
  (setf *previous-experiment* nil)
  (setf *max-changes* 0)
  (setf *exper-count* 0))


(defun run-spl (n condition)

  (setf *model-results* nil)
  
  (dotimes (i n)
    (reset)
    ;; randomize goal order
    (if (< (random 1.0) .5)
        (progn
          (Goal-focus start2)
          (push-goal start))
        (progn
          (Goal-focus start)
          (push-goal start2)))
    (set-up-spl)
    (start_screen-spl)
    (sgp-fct (list :ct *ct* :ot *at* :v (or *ct* *at*)))
    (setup-condition-based-on-model-page-spl condition)
    (run)
    (when (equal *which-model-page* 'f12)
      (push *max-changes* *model-results*))))

(defun setup-condition-based-on-model-page-spl (condition)
  (cond 
   ((equal *which-model-page* 'f11)
    (sdp Spacing_x_Delay :creation-time -100000 :references 1)
    (sdp Delay_x_Test_task :creation-time -100000 :references 1)
    (sdp Spacing_x_Test_task :creation-time -100000 :references 1)
    (if (equal condition 'with)
        (parameters discovery_goal-test_the_hypothesis :r .8)
        (parameters discovery_goal-test_the_hypothesis :r .2)))
   ((equal *which-model-page* 'f12)
    (if (equal condition 'with)
        (progn
          (parameters variable_setting-retrieve_previous_experiment_value :r .8)
          (parameters variable_setting-pick_previous_second_value :r .8))
        (progn
          (parameters variable_setting-retrieve_previous_experiment_value :r .2)
          (parameters variable_setting-pick_previous_second_value :r .2))))
   ((equal *which-model-page* 'f13)
    (cond
     ((equal condition 'with)
      (parameters discovery_goal-test_the_hypothesis :r .8)
      (sdp Spacing_x_Delay :creation-time -100 :references 100)
      (sdp Delay_x_Test_task :creation-time -100 :references 100)
      (sdp Spacing_x_Test_task :creation-time -100 :references 100))
     
     ((equal condition 'without-i)
      (parameters discovery_goal-test_the_hypothesis :r .8)
      (sdp Spacing_x_Delay :creation-time -100000 :references 1)
      (sdp Delay_x_Test_task :creation-time -100000 :references 1)
      (sdp Spacing_x_Test_task :creation-time -100000 :references 1))
     ((equal condition 'without-t)
      (parameters discovery_goal-test_the_hypothesis :r .2)
      (sdp Spacing_x_Delay :creation-time -100000 :references 1)
      (sdp Delay_x_Test_task :creation-time -100000 :references 1)
      (sdp Spacing_x_Test_task :creation-time -100000 :references 1))))))



;-------------------------------------------

(defun location_below-spl (location)
  (+ location 1000))

(defun location_right-spl (location)
  (+ location 100))

(defun location_table_below-spl (location)
  (+ location 10))

(defun location_table_right-spl (location)
  (+ location 1))


(defun get_ecell_name-spl (i j k l)
  (nth (1- l) (nth (1- k) (nth (1- j) (nth (1- i) *ecell_names-spl*)))))

(defun var_nlevels-spl (id)
  (case (aref *experiment_nlevels* id)
    (one 1) (two 2) (three 3)))

(defun variables_varied-spl ()
  (do ((i 0 (1+ i))
       max (values_list nil) (varied_list nil))
      ((= i *nvariables*)
       (if (null varied_list) (when *at* (format *standard-output* "~%~%     nothing was varied!!!!! ~%~%~%~%"))
           (list (no-output (dm-fct (reverse varied_list)))
                 (reverse values_list))))
    (setf max (var_nlevels-spl i))
    (unless (= max 1)
      (push (nth i *internal_variable_names-spl*) varied_list)
      (do ((j 0 (1+ j))
           (vals nil))
          ((= j max) (push (no-output (dm-fct (reverse vals))) values_list))
        (push (aref *experiment_settings* i j) vals)))))

(defun variables_not_varied-spl ()
  (do ((i 0 (1+ i))
       (not_varied_list nil))
      ((= i *nvariables*)
       (if (null not_varied_list) (when *at* (format *standard-output* "~%~%     everything was varied!!!!! ~%~%~%~%"))
           (reverse not_varied_list)))
    (when (= 1 (var_nlevels-spl i))
      (push (no-output (dm-fct (list (nth i *internal_variable_names-spl*) 
                                     (aref *experiment_settings* i 0))))
            not_varied_list))))


(defun build_experiment_contents-spl ()
  (setf *experiment_contents*
        (append (variables_varied-spl)
                (list (make_outcome_cells-spl))
                (list (variables_not_varied-spl))))
  t)

(defun makelocationid-spl (a b c d)
  (+ (* a 1000) (* b 100) (* c 10) d))

(defun compute_dim_number-spl (dimension tag)
  (case dimension 
    (dim1 (truncate tag 1000))
    (dim2 (mod (truncate tag 100) 10))
    (dim3 (mod (truncate tag 10) 10))
    (dim4 (mod tag 10))))

#|
(defun find_adjacent_tag-spl (dimension tag)
  (case dimension 
    (dim1 (- tag 1000))
    (dim2 (- tag 100))
    (dim3 (- tag 10))
    (dim4 (- tag 1))))
|#

(defun next_table_dimension-spl (location_dme)
  (let ((next_location
         (second (member location_dme *dim_locations-spl* ))))
    (if next_location
      (first (no-output (dm-fct (list next_location))))
      'done)))


(defun create_external_table_misc_objects-spl ()
  (add-dm-fct
   '((e_main_screen
      ISA external_object
      type screen_type
      label outcome_table)
     (e_next
      ISA external_object
      type button
      label next_experiment
      location bottom_left)))
  (sdp-fct (list 'e_main_screen :creation-time -100 :references 100))
  (sdp-fct (list 'e_next :creation-time -100 :references 100))
  t)


(defun create_external_table_dimensions-spl (nvars variable_names variable_levels)
  (dotimes (i nvars)
    (add-dm-fct   
     `((,(nth i *external_dim_names-spl*)
        ISA external_object
        type dimension
        location ,(nth i *dim_locations-spl* )
        label ,(nth i variable_names))
       (,(nth i *external_nv_names-spl*)
        ISA e_dim_nvalues
        variable ,(nth i variable_names)
        nvalues ,(length (nth i variable_levels)))))
    (sdp-fct (list (nth i *external_dim_names-spl*) :creation-time -100 :references 100))
    (sdp-fct (list (nth i *external_nv_names-spl*) :creation-time -100 :references 100))
    (dotimes (j (length (nth i variable_levels)))
      (add-dm-fct
       `((,(nth j (nth i *external_dim_value_names-spl*))
          ISA  e_dim_value
          dim_loc ,(nth i *dim_locations-spl* )
          dim_number ,(1+ j)
          value ,(nth j (nth i variable_levels)))))
      (sdp-fct (list (nth j (nth i *external_dim_value_names-spl*)) :creation-time -100 :references 100))))
  t)

(defun create_external_table_other_vars-spl (not_varied_vars)
  (dotimes (i (length not_varied_vars))
    (add-dm-fct   
     `((,(nth i *external_constant_var_names-spl*)
        ISA e_other_settings
        variable ,(first (nth i not_varied_vars))
        setting ,(second (nth i not_varied_vars)))
       ))
    (sdp-fct (list (nth i *external_constant_var_names-spl*) :creation-time -100 :references 100))
    )
  t)

(defun create_external_table_outcomes-spl (outcomes)
  (do ((outcomelist outcomes (rest outcomelist))
       (entry nil) (dmelist nil))
      ((null outcomelist)
       (add-dm-fct dmelist)
       (mapcar #'(lambda (x)
                   (sdp-fct (list (car x) :creation-time -100 :references 100)))
               dmelist))
    (setf entry (first outcomelist))
    (push  `(,(get_ecell_name-spl (first entry) (second entry)
                              (third entry) (fourth entry))
             ISA e_cell   ;(1 1 1 1 50)
             outcome ,(fifth entry)
             x_loc ,(first entry)
             y_loc ,(second entry)
             x_table ,(third entry)
             y_table ,(fourth entry)
             locationid ,(makelocationid-spl (first entry) (second entry)
                                         (third entry) (fourth entry)))
           dmelist))
  t)

(defun create_external_table-spl ()
  (let ((nvars (length (first *experiment_contents*)))
        (variable_names (first *experiment_contents*))
        (variable_levels (second *experiment_contents*))
        (outcomes (third *experiment_contents*))
        (not_varied_vars (fourth *experiment_contents*)))
    (create_external_table_misc_objects-spl)
    (create_external_table_dimensions-spl nvars variable_names variable_levels)
    (create_external_table_outcomes-spl outcomes)
    (create_external_table_other_vars-spl not_varied_vars)
  (values)))


(defun delete_external_table-spl ()
  (let ((nvars (length (first *experiment_contents*)))
        (variable_levels (second *experiment_contents*))
        (outcomes (third *experiment_contents*))
        (not_varied_vars (fourth *experiment_contents*)))
    (no-output (delete-chunk-fct '(e_main_screen e_next)))
    (dotimes (i nvars)
      (no-output (delete-chunk-fct
       `(,(nth i *external_dim_names-spl*)
         ,(nth i *external_nv_names-spl*))))
      (dotimes (j (length (nth i variable_levels)))
        (no-output (delete-chunk-fct
         `(,(nth j (nth i *external_dim_value_names-spl*)))))))
    (do ((outcomelist outcomes (rest outcomelist))
         (entry nil) (dmelist nil))
        ((null outcomelist) (no-output (delete-chunk-fct dmelist)))
      (setf entry (first outcomelist))
      (push (get_ecell_name-spl (first entry) (second entry)
                                (third entry) (fourth entry))
            dmelist))
    (dotimes (i (length not_varied_vars))
      (no-output (delete-chunk-fct   
       `(,(nth i *external_constant_var_names-spl*)))))
  (values)))

;(print_designed_experiment-spl)
(defun print_designed_experiment-spl ()
  (when (and (equal *which-model-page* 'f11)); *first-experiment*)
    (setf *first-experiment* nil)
    (let ((res nil))
      (dotimes (i *nvariables*)
        (push (1- (var_nlevels-spl i)) res))
      (push (reverse res) *model-results*)))

  (when (equal *which-model-page* 'f12)
    (if (not *previous-experiment*)
        (setf *previous-experiment* (experiment-stats-spl))
        (progn
          (let ((cur-exp (experiment-stats-spl))
                (changes 0))
            (mapcar (lambda (x y) (when (and (not (equal x y)) (not (equal x -1)) (not (equal y -1))) (incf changes))) *previous-experiment* cur-exp)
            (when (> changes *max-changes*) (setf *max-changes* changes))
            (setf *previous-experiment* cur-exp)))))
 
  (when (equal *which-model-page* 'f13)
    (incf *exper-count*)
    (if *first-experiment*
        (progn
          (setf *first-experiment* nil)
          (push (list 1 (count-factors-spl) (count-factors-spl)) *model-results*))
        (progn
          (incf (first (car *model-results*)))
          (incf (second (car *model-results*)) (count-factors-spl)))))

  (when *et*
    (format *standard-output* "~%")
    (let* ((goal (no-output (goal-focus)))
           (current_context (chunk-slot-value-fct goal 'context)))
      (format *standard-output* "~@(~S~)~%" current_context)
      (dotimes (i *nvariables*)
        (format *standard-output* "  ~@(~S~) ---" (nth i *internal_variable_names-spl*))
        (dotimes (j (var_nlevels-spl i))
          (format *standard-output* " ~@(~S~)" (aref *experiment_settings* i j)))
        (format *standard-output* "~%"))))
  t)


(defun factor-indices-spl ()
  (let ((x nil))
    (dotimes (i *nvariables* x)
      (unless (= (var_nlevels-spl i) 1)
        (push i x)))))

(defun experiment-stats-spl ()
  (let ((x nil))
    (dotimes (i *nvariables* x)
      (if (= (var_nlevels-spl i) 1)
          (push (aref *experiment_settings* i 0) x)
          (push -1 x)))))

(defun count-factors-spl ()
  (let ((count 0))
    (dotimes (i *nvariables* count)
      (when (> (var_nlevels-spl i) 1)
        (incf count)))))

;(print_experiment_outcome-spl)
(defun print_experiment_outcome-spl ()
  (case (length (first *experiment_contents*))
    (1 (print_experiment_outcome-spl_1d))
    (2 (print_experiment_outcome-spl_2d))
    (3 (print_experiment_outcome-spl_3d))
    (t (default_print_experiment_outcome-spl)))
  t)


(defun default_print_experiment_outcome-spl ()
  (when *et*
    (format *standard-output* "~%")
    (format *standard-output* "~S~%" (first *experiment_contents*))
    
    (when (< 1 (length (first *experiment_contents*)))
      (format *standard-output* "~S~%" (rest (reverse (second *experiment_contents*)))))
    (do ((outcomes_list (third *experiment_contents*) (rest outcomes_list))
         (i 0 (1+ i)))
        ((null outcomes_list) (format *standard-output* "~%"))
      (format *standard-output* "  ~S ~2D"
              (nth (mod i 2) (first (last (second *experiment_contents*))))
              (fifth (first outcomes_list)))
      (when (= 1 (mod i 2))
        (format *standard-output* "~%"))))
    t)


(defun print_experiment_outcome-spl_1d ()
  (when *et*
    (let ((variable (first (first *experiment_contents*)))
          (levels (first (second *experiment_contents*)))
          (contents (third *experiment_contents*)))
      (format *standard-output* "~%~S~%" variable)
      (dotimes (i (length levels))
        (format *standard-output* "~15S ~3D~%" (nth i levels) (fifth (nth i contents)))))
    t))

(defun print_experiment_outcome-spl_2d ()
  (when *et*
    (let ((variables (first *experiment_contents*))
          (levels (second *experiment_contents*))
          (contents (third *experiment_contents*)))
      (format *standard-output* "~%~37@A~%~16A" (second variables) (first variables))
      (dolist (el (second levels))
        (format *standard-output* "~13@S" el))
      (format *standard-output* "~%")
      (dotimes (i (length (first levels)))
        (format *standard-output* "   ~13S" (nth i (first levels)))
        (dotimes (j (length (second levels)))
          (format *standard-output* "~13D" (find_entry_in_contents_list-spl (1+ i) (1+ j) 1 1 contents)))
        ;(fifth (nth (+ (* i (length (second levels))) j)
        ;                        contents))))
        (format *standard-output* "~%"))))
    t)
;(print_experiment_outcome-spl_2d)


(defun contents_equal_test-spl (a b)
  (and (eq (first a) (first b))
       (eq (second a) (second b))
       (eq (third a) (third b))
       (eq (fourth a) (fourth b))))

(defun find_entry_in_contents_list-spl (i j k l contents)
  (fifth (first (member (list i j k l) contents :test 'contents_equal_test-spl))))


(defun print_experiment_outcome-spl_3d ()
  (when *et*
    (let ((variables (first *experiment_contents*))
          (levels (second *experiment_contents*))
          (contents (third *experiment_contents*)))
      (format *standard-output* "~%~A" (third variables))
      (dotimes (k (length (third levels)))
        (format *standard-output* "~% ~15A~24@A~%   ~16A" (nth k (third levels))
                (second variables) (first variables))
        (dolist (el (second levels))
          (format *standard-output* "~13@S" el))
        (format *standard-output* "~%")
        (dotimes (i (length (first levels)))
          (format *standard-output* "      ~13S" (nth i (first levels)))
          (dotimes (j (length (second levels)))
            (format *standard-output* "~13D" (find_entry_in_contents_list-spl (1+ i) (1+ j) (1+ k) 1 contents)
                    ))
          (format *standard-output* "~%")))))
  t)
;(print_experiment_outcome-spl_3d)

;separate print functions for each number of total variables
;1 ->down
;2 ->right
;3 ->down
;4 ->right

;--------------------------------------
;compute experiment results

(defun base_state-spl ()
  (make-array (list *nvariables*)
              :initial-contents 
              (list (case (aref *experiment_settings* 0 0)
                      (two 2) (three 3) (four 4) (five 5))
                    (case (aref *experiment_settings* 1 0)
                      (one_minute 1)(five_minutes 5) (ten_minutes 10))
                    (aref *experiment_settings* 2 0)
                    (aref *experiment_settings* 3 0)
                    (case (aref *experiment_settings* 4 0)
                      (one_minute 1)(five_minutes 5) (ten_minutes 10))
                    (aref *experiment_settings* 5 0))))


(defun change_state-spl (state var i)
  (let (new)
    (setf new
        (case var
          (0 (case (aref *experiment_settings* 0 i)
               (two 2) (three 3) (four 4) (five 5)))
          (1 (case (aref *experiment_settings* 1 i)
                      (one_minute 1)(five_minutes 5) (ten_minutes 10)))
          (2 (aref *experiment_settings* 2 i))
          (3 (aref *experiment_settings* 3 i))
          (4 (case (aref *experiment_settings* 4 i)
               (one_minute 1)(five_minutes 5) (ten_minutes 10)))
          (5 (aref *experiment_settings* 5 i))))
    (setf (aref state var) new))
  state)


(defun ids_of_vars_varied-spl ()
  (let ((idlist nil))
    (dotimes (i *nvariables*)
      (unless (equal (aref *experiment_nlevels* i) 'one)
        (push i idlist)))
    (reverse idlist)))


(defun 1d_cells-spl (var)
  (let ((new_state (base_state-spl))
        (cells nil))
    (dotimes (i (var_nlevels-spl var))
      (if (= i 0)
        (push (list 1 1 1 1 (compute_outcomes-spl new_state)) cells)
        (push (list (1+ i) 1 1 1 (compute_outcomes-spl (change_state-spl new_state var i))) cells)))
    (reverse cells)))


(defun 2d_cells-spl (varlist)
  (let ((state (base_state-spl))
        (cells nil))
    (dotimes (i (var_nlevels-spl (first varlist)))
      (change_state-spl state (first varlist) i)
      (dotimes (j (var_nlevels-spl (second varlist)))
        (change_state-spl state (second varlist) j)
        (push (list (1+ i) (1+ j) 1 1 (compute_outcomes-spl state)) cells)))
    (reverse cells)))


(defun 3d_cells-spl (varlist)
  (let ((state (base_state-spl))
        (cells nil))
    (dotimes (i (var_nlevels-spl (first varlist)))
      (change_state-spl state (first varlist) i)
      (dotimes (j (var_nlevels-spl (second varlist)))
        (change_state-spl state (second varlist) j)
        (dotimes (k (var_nlevels-spl (third varlist)))
          (change_state-spl state (third varlist) k)
          (push (list (1+ i) (1+ j) (1+ k) 1 (compute_outcomes-spl state)) cells))))
    (reverse cells)))


(defun 4d_cells-spl (varlist)
  (let ((state (base_state-spl))
        (cells nil))
    (dotimes (i (var_nlevels-spl (first varlist)))
      (change_state-spl state (first varlist) i)
      (dotimes (j (var_nlevels-spl (second varlist)))
        (change_state-spl state (second varlist) j)
        (dotimes (k (var_nlevels-spl (third varlist)))
          (change_state-spl state (third varlist) k)
          (dotimes (l (var_nlevels-spl (fourth varlist)))
            (change_state-spl state (fourth varlist) l)
            (push (list (1+ i) (1+ j) (1+ k) (1+ l) (compute_outcomes-spl state)) cells)))))
    (reverse cells)))


(defun make_outcome_cells-spl ()
  (let ((varied_list (ids_of_vars_varied-spl)))
    (case (length varied_list)
      (1 (1d_cells-spl (first varied_list)))
      (2 (2d_cells-spl varied_list))
      (3 (3d_cells-spl varied_list))
      (4 (4d_cells-spl varied_list)))))
;(1d_cells-spl (first (ids_of_vars_varied-spl)))


(defun compute_outcomes-spl (state)
  (let (val)
    (setf val
          (case (aref state 3) ;task
            (recall
             (+ (* .9 (expt (aref state 4) (/ -1 (expt (aref state 1) .4))))
                (* (aref state 0) .01)))
            (recog
             (+ (expt (aref state 4) (/ -1 (expt (aref state 1) .4)))
                (* (aref state 0) .01)))
            (stem
             (expt (aref state 4) -0.2))))
    (decf val
          (case (aref state 5) (same 0) (diff_room .07) (diff_mood .15)))
    (setf val (* val 100))
    (case (random 2) 
      (0 (incf val (- (random 5) 2)))
      (1 (incf val (- (random 3) 1))))
    (setf val (round val))
    (cond ((> val 100) (setf val 100))
          ((< val 0) (setf val 0)))
    val))

;--------------------------------------


(defun start_off_experiment-spl ()
  ;(zero_exper_results_list)
  (setf *experiment_contexts*
   '(exper1 exper2 exper3 exper4 exper5 exper6 exper7 exper8 exper9 exper10 exper11 exper12 exper13 exper14 exper15 exper16 exper17 exper18 exper19 exper20 exper21 ))
  t)

(defun zero_experiment_settings-spl ()
  (setf *experiment_nlevels* (make-array (list *nvariables*) :initial-element 'one))
  (setf *experiment_settings* (make-array (list *nvariables* *max_levels*) :initial-element nil))
  t)

(defun create_main_design_screen-spl ()
  (add-dm-fct
   '((e_main_screen
      ISA external_object
      type screen_type
      label main_screen)
     (e_run
      ISA external_object
      type button
      label run
      location bottom_left)))
  (sdp-fct (list 'e_main_screen :creation-time -100 :references 100))
  (sdp-fct (list 'e_run :creation-time -100 :references 100))
  (dotimes (i (length *internal_variable_names-spl*))
    (add-dm-fct
     `((,(nth i *main_button_names-spl*)
        ISA external_object 
        type button
        label ,(nth i *internal_variable_names-spl*)
        location ,(nth i *main_button_locations-spl*))))
    (sdp-fct (list (nth i *main_button_names-spl*) :creation-time -100 :references 100)))
  t)

(defun delete_main_design_screen-spl ()
  (no-output (delete-chunk-fct '(e_main_screen e_run)))
  (no-output (delete-chunk-fct *main_button_names-spl*))
  t)

(defun add_value_slider-spl (i var)
  (add-dm-fct
   `((,(nth  i *level_sliders-spl*)
      ISA slider
      type value
      label ,(nth i *level_ids-spl*)
      location ,(nth i *option_locations-spl*)
      setting ,(aref *experiment_settings* var i))))
  (sdp-fct (list (nth i *level_sliders-spl*) :creation-time -100 :references 100))
  t)

(defun add_value_scroller-spl (i var)
  (add-dm-fct 
   `((,(nth i *level_scrollers-spl*)
      ISA scrolling_list
      type value
      label ,(nth i *level_ids-spl*)
      location ,(nth i *option_locations-spl*)
      setting ,(aref *experiment_settings* var i))))
  (sdp-fct (list (nth i *level_scrollers-spl*) :creation-time -100 :references 100))
  t)

(defun add_n_value_objects-spl (n var)
  (dotimes (i n)
    (case var
      ((1 2 5) (add_value_slider-spl i var))
      ((3 4 6) (add_value_scroller-spl i var))
      ))
  t)

(defun add_variable_options-spl (var)
  (let ((external_names (nth var *external_option_names-spl*))
        (option_list (nth var *variable_options-spl*)))
    (dotimes (i (length option_list))
      (add-dm-fct
       `((,(nth i external_names)
          ISA external_object
          type scroller_option
          label ,(nth i option_list)
          location ,(nth i *variable_option_locations-spl*))))
      (sdp-fct (list (nth i external_names) :creation-time -100 :references 100)))
    t))


(defun create_pick_values_screen-spl (variable)
  (add-dm-fct
   `((e_main_screen
      ISA external_object
      type screen_type
      label ,variable)
     (e_done
      ISA external_object
      type button
      label done
      location bottom_left)))
  (sdp-fct (list 'e_main_screen :creation-time -100 :references 100))
  (sdp-fct (list 'e_done :creation-time -100 :references 100))
  (let* ((var (cdr (assoc variable *variable_to_number-spl*)))  ;get variable index number
         (nlevels (aref *experiment_nlevels* var))
         (numeric_nlevels (case nlevels (one 1) (two 2) (three 3))))
    (add-dm-fct
     `((nlevels_slider
        ISA slider
        type nlevels
        label ,variable
        setting ,nlevels
        location left)))
    (sdp-fct (list 'nlevels_slider :creation-time -100 :references 100))
    (add_n_value_objects-spl numeric_nlevels var)
    (add_variable_options-spl var))
  t)

(defun delete_n_value_objects-spl (n var)
  (case var 
    ((1 2 5) (dotimes (i n) (no-output (delete-chunk-fct `(,(nth i *level_sliders-spl*))))))
    ((3 4 6) (dotimes (i n) (no-output (delete-chunk-fct `(,(nth i *level_scrollers-spl*)))))))
  t)

(defun delete_variable_options-spl (var)
  (let ((option_list (nth var *external_option_names-spl*)))
    (no-output (delete-chunk-fct option_list)))
  t)

(defun delete_pick_values_screen-spl (variable)
  (no-output (delete-chunk-fct '(e_main_screen nlevels_slider e_done)))
  (let* ((var (cdr (assoc variable *variable_to_number-spl*)))  ;get variable index number
         (nlevels (aref *experiment_nlevels* var))
         (numeric_nlevels (case nlevels (one 1) (two 2) (three 3))))
    (delete_n_value_objects-spl numeric_nlevels var)
    (delete_variable_options-spl var))
  t)

(defun change_slider_setting-spl (variable newvalue)
  (let* ((var (cdr (assoc variable *variable_to_number-spl*)))
         (old_nlevels (aref *experiment_nlevels* var))
         (numeric_old (case old_nlevels (one 1) (two 2) (three 3)))
         (numeric_new (case newvalue (one 1) (two 2) (three 3))))
    (setf (aref *experiment_nlevels* var) newvalue)
    (delete_n_value_objects-spl numeric_old var)
    (add_n_value_objects-spl numeric_new var)
    (mod-chunk-fct 'nlevels_slider `(setting ,newvalue))
    )
  t)

(defun initialize_context-spl ()
  (mod-chunk-fct (no-output (goal-focus)) `(context ,(first *experiment_contexts*)))
  (setf *current_context* (first *experiment_contexts*)) 
  t)

(defun update_context-spl ()
  "Get current focus and change it's context slot to next context value."
  (let ((goal (no-output (goal-focus))))
    (setf *current_context* (second (member *current_context* *experiment_contexts*)))
    (mod-chunk-fct goal `(context ,*current_context*))
  t))

(defun get_current_context-spl ()
  (first (no-output (dm-fct (list *current_context*)))))

(defun update_looking_at-spl ()
  "Get current focus and change it's looking_at slot to nil."
  (mod-chunk-fct (no-output (goal-focus)) '(looking_at nil))
  t)


(defun physical_action-spl (action looking_at &optional param1 param2 param3)
  (let ((looking looking_at) 
        p1 p2 p3)
    (when param1 (setf p1 param1))
    (when param2 (setf p2 param2))
    (when param3 (setf p3 param3))
    (cond ((equal looking 'main_screen)
           (case action
             (press_button
              (cond ((equal p1 'bottom_left)           ;run
                     (when *at*
                       (format *standard-output* "~%ACTION: Click Run ~%NEW SCREEN: Outcome Table"))
                     (when (> (count-factors-spl) 4)
                       ;; randomly remove so that only 4 left.....
                       ;; a hack, since the model does not respond
                       ;; to feedback like the experiment provided
                       ;; subjects were not allowed to run with more than 4 factors
                       (do* ((numfactors (count-factors-spl) (1- numfactors))
                            (factors (factor-indices-spl) (remove index factors))
                            (index (nth (random (length factors)) factors)))
                           ((= numfactors 4))
                            (when *at* (format *standard-output* "Removing a factor (~S)~%" index))
                            (setf (aref *experiment_nlevels* index) 'one)))

                     (print_designed_experiment-spl)
                     (delete_main_design_screen-spl)
                     (build_experiment_contents-spl)
                     (print_experiment_outcome-spl)
                     (create_external_table-spl)
                     (update_looking_at-spl)
                     (when (or (and (equal *which-model-page* 'f13) (= *exper-count* 11)) (equal *which-model-page* 'f11) )
                       (when *at*
                         (if (equal *which-model-page* 'f11)
                             (format *standard-output* "Stopping model after first experiment (since that is all that is required).~%")
                             (format *standard-output* "Stopping model after 10 experiments (forcing it to give up).~%")))
                       (cleargoalstack) 
                       (pop-goal )))
                    (t                                 ;otherwise click on variable button
                     (when *at*
                       (format *standard-output* "~%ACTION: Click button ~S~%NEW SCREEN: Choose Variable" p1))
                     (delete_main_design_screen-spl)
                     (create_pick_values_screen-spl (cdr (assoc p1 *main_button_to_variable-spl*)))
                     (update_looking_at-spl))))
             (t (format *standard-output* "~%unknown action"))))
          ((member looking *internal_variable_names-spl*)
           (case action
             (press_button
              (cond ((equal p1 'bottom_left)          ;done setting variable
                     (when *at*
                       (format *standard-output* "~%ACTION: Click Done ~%NEW SCREEN: Main Screen"))
                     (delete_pick_values_screen-spl looking)
                     (create_main_design_screen-spl)
                     (update_looking_at-spl))
                    (t (format *standard-output* "~%unknown action"))))
             (change_slider
              (when *at*
                (format *standard-output* "~%ACTION: Update nlevels for ~S to ~S" param1 param2))
              (change_slider_setting-spl p1 p2))
             (make_value_selection-spl
              (when *at*
                (format *standard-output* "~%ACTION: Update value ~S for ~S to be ~S" param2 param1 param3))
              (make_value_selection-spl p1 (1- (cdr (assoc p2 *verbal_numbers_to_value-spl*))) p3))
             (t (format *standard-output* "~%unknown action"))))
          ((equal looking 'outcome_table)
           (case action
             (press_button
              (cond ((equal p1 'bottom_left)          ;next experiment
                     (when *at*
                       (format *standard-output* "~%ACTION: Click Next_experiment~%NEW SCREEN: Main Screen"))
                     ;(update_exper_results_list)
                     (delete_external_table-spl)
                     (zero_experiment_settings-spl)
                     (create_main_design_screen-spl)
                     (update_context-spl)
                     (update_looking_at-spl))
                    (t (format *standard-output* "~%unknown action"))))
             (t (format *standard-output* "~%unknown action"))))
          (t (format *standard-output* "~%unknown action")))
    (values)))


(defun start_screen-spl ()
  (start_off_experiment-spl)
  (zero_experiment_settings-spl)
  (create_main_design_screen-spl)
  (initialize_context-spl)
  t)


(defun next_scroller_location-spl (location)
  (let* ((location_name location) ;(first (get-name-fct (list location))))
         (next_location_name (second (member location_name *variable_option_locations-spl*))))
    (if next_location_name
      (first (no-output (dm-fct (list next_location_name))))
      (first (no-output (dm-fct (list 'done)))))))


(defun experiment_complete?-spl ()
  (do ((i 0 (1+ i))
       (done nil))
      ((or done (= i *nvariables*)) (not done))
    (do ((j 0 (1+ j))
         (max (case (aref *experiment_nlevels* i) (one 1) (two 2) (three 3))))
        ((or done (= j max)) t)
      (when (null (aref *experiment_settings* i j))
        (setf done t)))))


(defun first_unselected_variable-spl ()
  (do ((i 0 (1+ i))
       (first nil))
      ((or first (= i *nvariables*)) 
       (first (no-output (dm-fct (list (nth first *internal_variable_names-spl*))))))
    (do ((j 0 (1+ j))
         (max (case (aref *experiment_nlevels* i) (one 1) (two 2) (three 3))))
        ((or first (= j max)) t)
      (when (null (aref *experiment_settings* i j))
        (setf first i)))))


(defun make_value_selection-spl (variable n value)
  (let ((variable-n (cdr (assoc variable *variable_to_number-spl*))))
    (setf (aref *experiment_settings* variable-n n) value)
    t))


;--------------------------------

(defun nth_in_series-spl (value series)
  (let ((membership (member value (reverse series))))
    (when membership (length membership))))


(defun convert_value_to_number-spl (value)
  (or (nth_in_series-spl value '(one two three four five))
      (nth_in_series-spl value '(one_minute five_minutes ten_minutes))))


(defun close_together-spl (value1 value2)
  (let ((num1 (convert_value_to_number-spl value1))
        (num2 (convert_value_to_number-spl value2)))
    (when (and num1 num2)
      (= 1 (abs (- num1 num2))))))


(defun delay_too_long-spl (value)
  (equal value 'ten_minutes))


(defun delay_too_short-spl (value)
  (equal value 'one_minute))


(defun less_than-spl (value1 value2)
  (let ((num1 (convert_value_to_number-spl value1))
        (num2 (convert_value_to_number-spl value2)))
    (when (and num1 num2)
          (< num1 num2))))

(defun greater_than-spl (value1 value2)
  (let ((num1 (convert_value_to_number-spl value1))
        (num2 (convert_value_to_number-spl value2)))
    (when (and num1 num2)
          (> num1 num2))))


;create search list for retrieve where to go next



(defun convert_array_to_location-spl (array)
  (+ (aref array 3)
     (* (aref array 2) 10)
     (* (aref array 1) 100)
     (* (aref array 0) 1000)))


(defun compute_ndims-spl ()
  (let ((n 0))
    (dotimes (i 6)
      (unless (equal (aref *experiment_nlevels* i) 'one)
        (incf n)))
    n))
;(compute_ndims-spl)


(defun current_dimension_ns-spl ()
  (let ((ns (make-array '(4) :initial-element 1)))
    (dotimes (i (compute_ndims-spl))
      (setf (aref ns i) 2))
    ns))
;(current_dimension_ns-spl)


(defun dimension_name_to_number-spl (dimname)
  (let ((num nil))
    (dotimes (i 4)
      (when (equal (nth i *dim_locations-spl* ) dimname)
        (setf num i)))
    num))


(defun create_search_list-spl (dimname)
  (let ((cur (dimension_name_to_number-spl dimname))
        (mx (current_dimension_ns-spl))
        (cell (make-array '(4) :initial-element 1))
        (slist nil)
        (p 0))
    (push (convert_array_to_location-spl cell) slist)
    (if (equal (compute_ndims-spl) 1)
      (setf *search_path* slist)
      (do ((i 1))
          ((= p 4) (setf *search_path* (reverse slist)))
        (cond ((= p cur) (incf p))
              ((= (aref cell p) (aref mx p))
               (setf (aref cell p) 1)
               (incf p))
              ((= (aref cell p) 1) 
               (incf (aref cell p))
               (incf i)
               (push (convert_array_to_location-spl cell) slist)
               (setf p 0))
              (t (format *standard-output* "~%bad")))
        );do
      );if
    ))
;(create_search_list-spl (first (no-output (dm-fct (list 'dim1)))))



(defun nil_to_failure-spl (val)
  (if val val (first (no-output (dm-fct '(failure))))))


(defun undo_dimension_shift-spl (current dimension)
  (let ((dim (dimension_name_to_number-spl dimension)))
    (case dim
      (0 (- current 1000))
      (1 (- current 100))
      (2 (- current 10))
      (3 (- current 1)))))


(defun next_search_location-spl (current dimension)
  (let ((prev_cell (undo_dimension_shift-spl current dimension)))
    (nil_to_failure-spl (second (member prev_cell *search_path* :test 'equal)))))


(defun dimension_diff-spl (cell1 cell2)
  (let ((diff (abs (- cell1 cell2))))
    (first (no-output (dm-fct (list
                               (cond ((>= diff 500) 'dim1)
                                     ((>= diff 50) 'dim2)
                                     ((>= diff 5) 'dim3)
                                     (t 'dim4))))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(Clearall)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; from SPL init
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(sgp 
     :ANS nil ;activation noise
     :G 50.0  ;value of the goal
     :RT -0.5  ;retrieval threshold
     :ERA t   ;rational analysis on 
     :ER t    ;randomness on
     :ANS .1  ;activation noise s on

     :EGS .1 ;.5 ;expected gain noise off
     :BLL .5   ;base-level learning on
     :OL  t    ;optimized learning on
     :AL nil    ;associate learning off
     :SL nil    ;strength learning off
     :PL nil    ;parameter learning off
     :PM nil  ;partial matching off

     :OT t   ;output trace on

     :CRT nil ;conflict resolution trace off
     :CST nil ;conflict set trace
     :CT t    ;cycle trace on
     :MT nil  ;matches trace
     :LT nil  ;latency trace off
     :DMT nil ;declarative memory trace off
     :ACT nil ;activation trace off
     )


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; from SPL Chunk-Types
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(Chunk-Type generic_object name context)
  (Chunk-Type (button (:include generic_object)))
  (Chunk-Type (hypothesis (:include generic_object))
              cause effect conditional)
  (Chunk-Type (consequence (:include generic_object))
              hypothesis variable effect)
  (Chunk-Type (complex_variable (:include generic_object))
              component)
  (Chunk-Type (manipulable_variable  (:include generic_object))
              number_of_levels type)
  (Chunk-Type (experiment (:include generic_object)))
  (Chunk-Type (variable_option (:include generic_object)))
  (Chunk-Type (threshold (:include generic_object))
              value)

(Chunk-Type generic_goal context looking_at)
  (Chunk-Type (discovery_goal (:include generic_goal)) 
              hypothesis done)
  (Chunk-Type (evaluate_evidence (:include generic_goal))
              hypothesis variable effect done)
  (Chunk-Type (experiment_goal (:include generic_goal))
              designed_exper ran_exper analyzed_exper)
    (Chunk-Type (hyp_testing_goal (:include experiment_goal))
                hypothesis)
    (Chunk-Type (explore_system_goal (:include experiment_goal))
                under_consideration)
    (Chunk-Type (test_one_factor_goal (:include experiment_goal))
                factor)
  (Chunk-Type (vary_hypothesis_factors (:include generic_goal))
              hypothesis under_consideration)
  (Chunk-Type (fix_remaining_variables (:include generic_goal))
              under_consideration)
  (Chunk-Type (pick_one_to_vary (:include generic_goal))
              factor)
  (Chunk-Type (consider_factor_goal (:include generic_goal))
              factor)
    (Chunk-Type (vary_factor_goal (:include consider_factor_goal)))
    (Chunk-Type (hold_factor_constant (:include consider_factor_goal)))
  (Chunk-Type (experiment_problem (:include generic_goal))
              factor problem index index2 active)
  (Chunk-Type (visual_search_goal (:include generic_goal))
              object index result)
  (Chunk-Type (variable_setting (:include generic_goal))
              name units nlevels value1 value2 know_options interaction)
  (Chunk-Type (possible_selection (:include generic_goal))
              name variable)
  (Chunk-Type (encode_table (:include generic_goal))
              current_dimension searched_dim start_cell method)
              ;search_dim current_cell current_value prev_value)
  (Chunk-Type (encode_dimension (:include generic_goal))
              dimension current_cell current_value prev_value
              feature prev_feature amount variable value1 value2)
  (Chunk-Type (dimension_value (:include generic_goal))
              value_name value_loc dim_name)
  (Chunk-Type (test_generality (:include generic_goal))
              variable feature)
  (Chunk-Type (interaction (:include generic_goal))
              variable1 variable2 feature1 feature2 cell prev_cell)
  (Chunk-Type (physical_action-spl (:include generic_goal))
              object location)
    (Chunk-Type (click_button (:include physical_action-spl)))
    (Chunk-Type (click_slider (:include physical_action-spl))
                new_setting)
    (Chunk-Type (click_value_selection (:include physical_action-spl))
                new_setting level)
  (Chunk-Type (solve_value_constraint (:include generic_goal))
              variable relation value value2 result)
  (Chunk-Type (rehearse_problem (:include generic_goal))
              rehearsed factor factor_rehearsed)
  (Chunk-Type (rehearse_hypothesis (:include generic_goal))
              rehearsed factor)
  (Chunk-Type (memory_search (:include generic_goal))
              type object1 object2 result)

              

(Chunk-Type external_object type label location)
  (Chunk-Type (slider (:include external_object))
              setting)
  (Chunk-Type (scrolling_list (:include external_object))
              setting)
(Chunk-Type e_cell outcome prediction x_loc y_loc x_table y_table locationid)
(Chunk-Type e_dim_value dim_loc dim_number value)
(Chunk-Type e_dim_nvalues variable nvalues)
(Chunk-Type e_other_settings variable setting)

(Chunk-Type location)
(Chunk-Type dummytype)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; from SPL setDM
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(Add-DM
 (1left ISA location)(2left ISA location)(3left ISA location)
 (1right ISA location)(2right ISA location)(3right ISA location)
 (button ISA location) (buttonY ISA location) (left ISA location)
 (top ISA location)(middle ISA location)(bottom ISA location)
 (dim1 ISA location)(dim2 ISA location)(dim3 ISA location)(dim4 ISA location)
 (top_left ISA location)(bottom_left ISA location)

 (exper1 ISA experiment name exper1)(exper2 ISA experiment name exper2)
 (exper3 ISA experiment name exper3)(exper4 ISA experiment name exper4)
 (exper5 ISA experiment name exper5)(exper6 ISA experiment name exper6)
 (exper7 ISA experiment name exper7)(exper8 ISA experiment name exper8)
 (exper9 ISA experiment name exper9)(exper10 ISA experiment name exper10)

 (same ISA variable_option)(diff_room ISA variable_option)(diff_mood ISA variable_option)
 (recall ISA variable_option)(recog ISA variable_option)(stem ISA variable_option)
 (one_minute ISA variable_option)(five_minutes ISA variable_option)(ten_minutes ISA variable_option)

 (button_label ISA dummytype) (first_unselected ISA dummytype) (variable ISA dummytype)
 (failure ISA dummytype) (dimension ISA dummytype) (outcome_table ISA dummytype)
 (one ISA dummytype)(two ISA dummytype)(three ISA dummytype)(four ISA dummytype)(five ISA dummytype)
 (done ISA dummytype)(run ISA dummytype)(next_experiment ISA dummytype)
 (true ISA dummytype) (maybe ISA dummytype)
 (cell_location ISA dummytype)(search_dimension ISA dummytype)(cell_outcome ISA dummytype)
 (scroller_option ISA dummytype) (slider ISA dummytype)
 (screen_type ISA dummytype)(nlevels ISA dummytype)(blocked ISA dummytype)
 (too_close_together ISA dummytype)(ceiling_effect ISA dummytype)(floor_effect ISA dummytype)
 (too_high ISA dummytype)(too_low ISA dummytype)
 (value ISA dummytype)(level1 ISA dummytype)(level2 ISA dummytype)(level3 ISA dummytype)
 (separating_dimension ISA dummytype)(yes ISA dummytype)(interaction ISA dummytype)

 (LtR_scan ISA dummytype)(test_variable ISA dummytype)
 (equal_to ISA dummytype)(different_than ISA dummytype)
 (greater_than ISA dummytype)(less_than ISA dummytype)

 (threshold ISA threshold value 5)

 (main_screen ISA external_object)
 
 (repetitions ISA manipulable_variable name repetitions)
 (spacing ISA manipulable_variable name spacing)
 (source_context ISA manipulable_variable name source_context)
 (test_task ISA manipulable_variable name test_task)
 (delay ISA manipulable_variable name delay)
 (test_context ISA manipulable_variable name test_context)

 (start ISA discovery_goal hypothesis shifting_context)
 (start2 ISA discovery_goal hypothesis frequency_regularity)
 
 (ShCt ISA hypothesis cause shifting_context effect spacing_effect conditional maybe)
 (shifting_context ISA complex_variable name shifting_context component source_context)
 (spacing_effect ISA complex_variable name spacing_effect component spacing)


 (FRT ISA hypothesis cause frequency_regularity effect spacing_effect conditional maybe)
 (frequency_regularity ISA complex_variable name frequency_regularity component spacing)

 (Sh_Ct_consequence ISA consequence
                    hypothesis shifting_context
                    variable source_context
                    effect greater_than)
 (FR_consequence ISA consequence
                 hypothesis frequency_regularity
                 variable spacing
                 effect greater_than)

 (Spacing_x_Test_task ISA interaction variable1 spacing variable2 test_task)
 (Delay_x_Test_task ISA interaction variable1 delay variable2 test_task)
 (Spacing_x_Delay ISA interaction variable1 spacing variable2 delay)
)

(sdp Spacing_x_Delay :creation-time -100 :references 100)
(sdp Delay_x_Test_task :creation-time -100 :references 100)
(sdp Spacing_x_Test_task :creation-time -100 :references 100)
(sdp Sh_Ct_consequence :creation-time -100 :references 100)
(sdp FR_consequence :creation-time -100 :references 100)
(sdp FRT :creation-time -100 :references 100)
(sdp ShCt :creation-time -100 :references 100)
(sdp shifting_context :creation-time -100 :references 100)
(sdp spacing_effect :creation-time -100 :references 100)
(sdp frequency_regularity :creation-time -100 :references 100)

(sdp threshold :creation-time -100 :references 100)
(sdp repetitions :creation-time -100 :references 100)
(sdp spacing :creation-time -100 :references 100)
(sdp source_context :creation-time -100 :references 100)
(sdp test_task :creation-time -100 :references 100)
(sdp test_context :creation-time -100 :references 100)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; from SPL visual prods
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(p generic_goal-look_around
  =goal>
     ISA generic_goal
     context =context
     looking_at nil
==>
  =subgoal>
     ISA visual_search_goal
     context =context
     object screen_type
     result =object
     looking_at blocked
  =goal>
     looking_at =object
  !output! ("Looking for a screen_type")
  !push! =subgoal
)
   

(p generic_goal-add_context
   =goal>
     ISA generic_goal
     context nil
 ==>
   !bind! =context (get_current_context-spl)
   =goal>
     context =context
   !output! ("Update context for ~S to be ~S" =goal =context)
   )

;----------------------------------------------

;external search routines

(p visual_search-find_screen_type
   =visual_search_goal>
     ISA visual_search_goal
     object screen_type
     result nil
   =external_object>
     ISA external_object
     type screen_type
     label =var
==>
   =visual_search_goal>
     result =var
   !output! ("Found screen_type object ~S" =var)
   !pop!
   )

(p visual_search-find_button_label
   =visual_search_goal>
     ISA visual_search_goal
     object button_label
     index =var
     result nil
   =external_object>
     ISA external_object
     type button
     label =var
     location =location
 ==>
   =visual_search_goal>
     result =location
   !output! ("Found button ~S" =var)
   !pop!
   )

(p visual_search-find_unselected_variable
   =visual_search_goal>
      ISA visual_search_goal
      object variable
      looking_at main_screen
      index first_unselected
      result nil
   !eval! (not (experiment_complete?-spl))
 ==>
   !bind! =variable (first_unselected_variable-spl)
   =visual_search_goal>
     result =variable
   !output! ("Found variable ~S" =variable)
   !pop!
   )

(p visual_search-find_nlevels
   =visual_search_goal>
     ISA visual_search_goal
     object slider
     index nlevels
     result nil
   =external_object>
     ISA slider
     type nlevels
     setting =setting
 ==>
   =visual_search_goal>
     result =setting
   !output! ("Found nlevels ~S" =setting)
   !pop!
   )

(p visual_search_for_selections-iterate
   =search_goal>
     ISA possible_selection
     name nil
     variable =var
     looking_at =location
     context =context
   =external_object>
     ISA external_object
     type scroller_option
     label =option
     location =location
 ==>
   =search_goal>
     name =option
   !bind! =new_location (next_scroller_location-spl =location)
   =new_option>
     ISA possible_selection
     variable =var
     looking_at =new_location
     context =context
   !focus-on! =new_option
   !output! ("Visually found option ~S" =option)
   )

;probably need different production to create options
 ;for slider (rep, sp, del)

(p visual_search_for_selections-done
   =search_goal>
     ISA possible_selection
     looking_at done
 ==>
   =search_goal>
     looking_at nil
   !pop!
   )


(p visual_search-find_variable_dimension
   =visual_search_goal>
      ISA visual_search_goal
      object variable
      context =context
      looking_at outcome_table
      index =variable
      result nil
   =external_object>
      ISA external_object
      type dimension
      location =location
      label =variable
 ==>
   =visual_search_goal>
      result =location
   !output! ("~S is on dimension ~S" =variable =location)
   !pop!
  )

(p visual_search-find_dimension_variable
   =visual_search_goal>
      ISA visual_search_goal
      object dimension
      context =context
      index =dimension
      result nil
   =external_object>
      ISA external_object
      type dimension
      location =dimension
      label =variable
 ==>
    =visual_search_goal>
      result =variable
   !output! ("Found dimension name ~S" =variable)
   !pop!
   )

(P dimension_value-retrieve_visually
   =dimension_value>
      ISA dimension_value
      dim_name =dimension
      looking_at =tag
      value_name nil
  !bind! =dim_number (compute_dim_number-spl =dimension =tag)
   =e_dim_value>
      ISA e_dim_value
      dim_loc =dimension
      dim_number =dim_number
      value =value_name
 ==>
   =dimension_value>
      value_name =value_name
      value_loc =dim_number
   !output! ("Found dimension value ~S" =value_name)
   !pop!
   )

(p visual_search-find_top_left_cell
   =visual_search_goal>
      ISA visual_search_goal
      object top_left
      looking_at outcome_table
      result nil
   =e-cell>
      ISA             e_cell
      x_loc           1
      y_loc           1
      x_table         1
      y_table         1
      locationid      =locationid
 ==>
   =visual_search_goal>
      result =locationid
   !output! ("Found top-left cell ~S" =locationid)
   !pop!
   )


(p visual_search-find_search_dimension-start_search
   =visual_search_goal>
      ISA visual_search_goal
      object search_dimension
      looking_at outcome_table
      context =context
      result nil
 ==>
   =visual_search_goal>
      looking_at dim1
   !output! ("Start with dimension 1.")
   )

(p visual_search-find_search_dimension-already_searched_this_one
   =visual_search_goal>
      ISA visual_search_goal
      object search_dimension
     - looking_at outcome_table
      looking_at =location
      context =context
      result nil
   =encode_dimension>
      ISA encode_dimension
      context =context
      dimension =location
 ==>
   !bind! =new_location (next_table_dimension-spl =location)
   =visual_search_goal>
      looking_at =new_location
   !output! ("Consider next dimension (~S)" =new_location)
   )


(p visual_search-find_search_dimension-return_dimension
   =visual_search_goal>
      ISA visual_search_goal
      object search_dimension
     - looking_at outcome_table
      looking_at =location
      context =context
      result nil
   =external_object>
      ISA external_object
      type dimension
      location =location
 ==>
   =visual_search_goal>
      result =location
   !output! ("Found search dimension ~S." =location)
   !pop!
   )
(parameters visual_search-find_search_dimension-return_dimension :r .8)


(p visual_search-find_cell_outcome
   =visual_search_goal>
      ISA visual_search_goal
      object cell_outcome
      looking_at outcome_table
      index =locationid
      result nil
   =e_cell>
      ISA e_cell
      outcome =outcome
      locationid =locationid
==>
   =visual_search_goal>
      result =outcome
   !output! ("Found value ~S" =outcome)
   !pop!
   )

(p visual_search-cell_below
   =visual_search_goal>
      ISA visual_search_goal
      object cell_location
      looking_at =current_cell
      index dim1
      result nil
   !bind! =next_location (location_below-spl =current_cell)
   =e_cell>
      ISA e_cell
      locationid =next_location
 ==>
   =visual_search_goal>
      result =next_location
   !output! ("Found location below, ~S" =next_location)
   !pop!
   )

(p visual_search-cell_right
   =visual_search_goal>
      ISA visual_search_goal
      object cell_location
      looking_at =current_cell
      index dim2
      result nil
   !bind! =next_location (location_right-spl =current_cell)
   =e_cell>
      ISA e_cell
      locationid =next_location
 ==>
   =visual_search_goal>
      result =next_location
   !output! ("Found location right, ~S" =next_location)
   !pop!
   )

(p visual_search-table_below
   =visual_search_goal>
      ISA visual_search_goal
      object cell_location
      looking_at =current_cell
      index dim3
      result nil
   !bind! =next_location (location_table_below-spl =current_cell)
   =e_cell>
      ISA e_cell
      locationid =next_location
 ==>
   =visual_search_goal>
      result =next_location
   !output! ("Found location table below, ~S" =next_location)
   !pop!
   )


(p visual_search-table_right
   =visual_search_goal>
      ISA visual_search_goal
      object cell_location
      looking_at =current_cell
      index dim4
      result nil
   !bind! =next_location (location_table_right-spl =current_cell)
   =e_cell>
      ISA e_cell
      locationid =next_location
 ==>
   =visual_search_goal>
      result =next_location
   !output! ("Found location table right, ~S" =next_location)
   !pop!
   )

(p visual_search_goal-find_separating_dimension
   =visual_search_goal>
      ISA visual_search_goal
      context =context
      looking_at =current_cell
      object separating_dimension
      index =prev_cell
   !bind! =dimension (dimension_diff-spl =current_cell =prev_cell)
   =external_object>
      ISA external_object
      type dimension
      location =dimension
      label =variable
 ==>
   =visual_search_goal>
      result =variable
   !output! ("Found separating dimension, ~S" =variable)
   !pop!
   )



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; from SPL motor prods
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;motor action productions

;------------------------------------------------------------

(p click_button-look_for_button
   =click_button>
      ISA click_button
      object =object
      looking_at =something
      location nil
      context =context
 ==>
   =visual_search_goal>
      ISA visual_search_goal
      looking_at =something
      object button_label
      index =object
      result =location
      context =context
   =click_button>
      location =location
   !output! ("Look for the button labeled ~S" =object)
   !push! =visual_search_goal
   )

(p click_button-click_on_location
   =click_button>
      ISA click_button
      object =object
      looking_at =something
   =visual_search_goal>
      ISA visual_search_goal
      looking_at =something
      object button_label
      index =object
      result =button
     - result failure
 ==>
   =click_button>
      location =button
   !output! ("Clicking on location ~S" =button)
   !pop! 
   !eval! (physical_action-spl 'press_button =something =button)
   )

;------------------------------------------------------------
;this is still a simple hack...

(p click_slider-make_nlevels_change
   =click_slider>
      ISA click_slider
      object nlevels
      looking_at =something
      new_setting =setting
     - new_setting failure
 ==>
   !output! ("Clicking slider ~S" =something)
   !pop! 
   !eval! (physical_action-spl 'change_slider =something =something =setting)
   )


;------------------------------------------------------------
;this is still a simple hack...

(p click_value_selection-make_change
   =click_slider>
      ISA click_value_selection
      object =object
      looking_at =object
      level =level
      new_setting =setting
    - new_setting failure
 ==>
   !output! ("Making selection ~S for ~S" =level =object)
   !pop! 
   !eval! (physical_action-spl 'make_value_selection-spl =object =object =level =setting)
   )


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; from SPL table productions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(p encode_table-pick_LtR_scan_method
   =encode_table>
      ISA encode_table
      method nil
 ==>
   =encode_table>
      method LtR_scan
   !output! ("Try scanning left-to-right.")
   )

(p encode_table-pick_upper_left_cell
   =encode_table>
      ISA encode_table
      method LtR_scan
      start_cell nil
      context =context
      looking_at =screen
 ==>
   =visual_search_goal>
      ISA visual_search_goal
      object top_left
      context =context
      looking_at =screen
      result =locationid
   =encode_table>
      start_cell =locationid
   !output! ("Visually find starting place--top left.")
   !push! =visual_search_goal
   )

(p encode_table-start_with_dim1
   =encode_table>
      ISA encode_table
      method LtR_scan
      start_cell =cell
      context =context
      looking_at =screen
      current_dimension nil
  ==>
   =encode_table>
      current_dimension dim1
   !output! ("Start with dimension dim1.")
   )

(p encode_table-go_to_next_dimension
   =encode_table>
      ISA encode_table
      method LtR_scan
      start_cell =cell
      context =context
      looking_at =screen
      current_dimension =dim
     - current_dimension failure
      searched_dim t
  ==>
   =visual_search_goal>
      ISA visual_search_goal
      object search_dimension
      looking_at outcome_table
      context =context
      result =next_dim
   =encode_table>
      current_dimension =next_dim
      searched_dim nil
   !output! ("Find next search dimension.")
   !push! =visual_search_goal
   )


(p encode_table-encode_dimension
   =encode_table>
      ISA encode_table
      method LtR_scan
      start_cell =cell
      context =context
      looking_at =screen
      current_dimension =dim
     - current_dimension failure
      searched_dim nil
 ==>
   !eval! (create_search_list-spl =dim)
   =encode_table>
      searched_dim t
   =encode_dimension>
      ISA encode_dimension
      dimension =dim
      current_cell =cell
      context =context
      looking_at =screen
   !output! ("Push goal to encode dimension ~S" =dim)
   !push! =encode_dimension
   )


(p encode_table-done_encoding_table
   =encode_table>
      ISA encode_table
      context =context
      looking_at =screen
      current_dimension failure
 ==>
   =rehearse_problem>
      ISA rehearse_problem
      context =context
      looking_at =screen
      rehearsed nil
  !output! ("Finished encoding the table. Now go over problems if any.")
  !focus-on! =rehearse_problem
  )

;------------------------------------------------------------
;encode_dimension

(p encode_dimension-find_variable_visually
   =encode_dimension>
      ISA encode_dimension
      context =context
      looking_at =screen
      dimension =dimension
      variable nil
 ==>
   =visual_search_goal>
      ISA visual_search_goal
      object dimension
      context =context
      index =dimension
      result =dim_name
      looking_at blocked
   =encode_dimension>
      variable =dim_name
   !output! ("Visually locate dimension variable.")
   !push! =visual_search_goal
   )


(p encode_dimension-find_variable_value2_visually
   =encode_dimension>
      ISA encode_dimension
      dimension =dimension
      current_cell =cell
      current_value =val1
      prev_value =val2     ;therefore looking at 2nd cell
      value2 nil
      context =context
 ==>
  =dim_value>
      ISA dimension_value
      dim_name =dimension
      context =context
      looking_at =cell
      value_loc 2
      value_name =value_name
   =encode_dimension>
      value2 =value_name
   !output! ("Look for value2 visually")
   !push! =dim_value
   )


(p encode_dimension-retrieve_variable_value2_from_memory
   =encode_dimension>
      ISA encode_dimension
      dimension =dimension
      current_cell =cell
      current_value =val1
      prev_value =val2    ;therefore looking at 2nd cell
      value2 nil
      context =context
   =dimension_value>
      ISA dimension_value
      dim_name =dimension
      value_loc 2
      value_name =value_name
      context =context
 ==>
   =encode_dimension>
      value2 =value_name
   !output! ("Retrieved ~S for value2" =value_name)
   )


(p encode_dimension-find_variable_value1_visually
   =encode_dimension>
      ISA encode_dimension
      dimension =dimension
      current_cell =cell
      current_value =val1
      prev_value nil     ;therefore looking at 1st cell
      value1 nil
      context =context
 ==>
  =dim_value>
      ISA dimension_value
      dim_name =dimension
      context =context
      looking_at =cell
      value_loc 1
      value_name =value_name
   =encode_dimension>
      value1 =value_name
   !output! ("Look for value1 visually")
   !push! =dim_value
   )


(p encode_dimension-retrieve_variable_value1_from_memory
   =encode_dimension>
      ISA encode_dimension
      dimension =dimension
      current_cell =cell
      current_value =val1
      prev_value nil    ;therefore looking at 1st cell
      value1 nil
      context =context
   =dimension_value>
      ISA dimension_value
      dim_name =dimension
      value_loc 1
      value_name =value_name
      context =context
 ==>
   =encode_dimension>
      value1 =value_name
   !output! ("Retrieved ~S for value2" =value_name)
   )


(p encode_dimension-visually_get_cell_outcome
   =encode_dimension>
      ISA encode_dimension
      current_value nil
      current_cell =locationid
     - current_cell failure
      context =context
 ==>
   =visual_search_goal>
      ISA visual_search_goal
      object cell_outcome
      looking_at outcome_table
      index =locationid
      result =outcome
      context =context
   =encode_dimension>
      current_value =outcome
   !output! ("Visually get cell value")
   !push! =visual_search_goal
   )


(p encode_dimension-finished_encoding_dimension
   =encode_dimension>
      ISA encode_dimension
      current_cell failure
      context =context
      looking_at =screen
      variable =variable
      prev_feature =feature
 ==>
   =test_generality>
      ISA test_generality
      variable =variable
      feature =feature
      context =context
      looking_at =screen
   !output! ("Finished encoding dimension. See if main effect generalizes.")
   !focus-on! =test_generality
   )


(p encode_dimension-move_in_desired_direction
   =encode_dimension>
      ISA encode_dimension
      current_value =current_value
      prev_value nil
      current_cell =current_cell
      dimension =search_dim
      context =context
 ==>
   =visual_search_goal>
      ISA visual_search_goal
      object cell_location
      looking_at =current_cell
      index =search_dim
      result =new_cell
      context =context
   =encode_dimension>
      current_value nil
      current_cell =new_cell
      prev_value =current_value
   !output! ("Move in desired direction.")
   !push! =visual_search_goal
   )
(parameters encode_dimension-move_in_desired_direction :r .8)


(p encode_dimension-notice_equal_to
   =encode_dimension>
      ISA encode_dimension
      current_value =current_value
      prev_value =current_value
      feature nil
 ==>
   =encode_dimension>
      feature equal_to
   !output! ("Notice equal to pattern.")
   )


(p encode_dimension-notice_greater_than
   =encode_dimension>
      ISA encode_dimension
      current_value =current_value
      prev_value =prev_value
      feature nil
   !eval! (> =current_value =prev_value)
 ==>
   !bind! =difference (- =current_value =prev_value)
   =encode_dimension>
      feature greater_than
      amount =difference
   !output! ("Notice greater than pattern.")
   )


(p encode_dimension-notice_less_than
   =encode_dimension>
      ISA encode_dimension
      current_value =current_value
      prev_value =prev_value
      feature nil
   !eval! (< =current_value =prev_value)
 ==>
   !bind! =difference (- =current_value =prev_value)
   =encode_dimension>
      feature less_than
      amount =difference
   !output! ("Notice less than pattern.")
   )


(p encode_dimension-difference_not_significant
   =encode_dimension>
      ISA encode_dimension
      dimension =dimension
     - feature equal_to
      amount =difference
   =threshold>
      ISA threshold
      value =amount
   !eval! (< (abs =difference) =amount)
 ==>
   =encode_dimension>
      feature equal_to
   !output! ("Difference ~S in ~S is not significant." =difference =dimension)
   )


(p encode_dimension-finish_first_row
   =encode_dimension>
      ISA encode_dimension
      context =context
      looking_at =screen
      feature =feature
      prev_feature nil
      dimension =dimension
      variable =variable
      value1 =value1
      value2 =value2
      current_cell =current_cell
 ==>
   !bind! =next_cell (next_search_location-spl =current_cell =dimension)
   =next_encode_dimension>
      ISA encode_dimension
      context =context
      looking_at =screen
      dimension =dimension
      variable =variable
      value1 =value1
      value2 =value2
      current_cell =next_cell
      prev_feature =feature
   !output! ("Finished first row of dimension ~S." =dimension)
   !focus-on! =next_encode_dimension
   )
(parameters encode_dimension-finish_first_row :r .8)


(p encode_dimension-finish_row_consistent_effect
   =encode_dimension>
      ISA encode_dimension
      context =context
      looking_at =screen
      feature =feature
      prev_feature =feature
      dimension =dimension
      variable =variable
      value1 =value1
      value2 =value2
      current_cell =current_cell
 ==>
   !bind! =next_cell (next_search_location-spl =current_cell =dimension)
   =next_encode_dimension>
      ISA encode_dimension
      context =context
      looking_at =screen
      dimension =dimension
      variable =variable
      value1 =value1
      value2 =value2
      current_cell =next_cell
      prev_feature =feature
   !output! ("Finished next row of dimension ~S. Same effect found." =dimension)
   !focus-on! =next_encode_dimension
   )
(parameters encode_dimension-finish_row_consistent_effect :r .8)


(p encode_dimension-finish_dimension_inconsistent_effect
   =encode_dimension>
      ISA encode_dimension
      context =context
      looking_at =screen
      feature =feature
      prev_feature =prev_feature
     - prev_feature =feature
      variable =variable
      current_cell =current_cell
 ==>
   =interaction>
      ISA interaction
      variable1 =variable
      variable2 nil     ;don't know what this dimension is 
      context =context
      looking_at =screen
      feature1 =prev_feature
      feature2 =feature
      cell =current_cell
   =test_generality>
      ISA test_generality
      variable =variable
      feature equal_to
      context =context
      looking_at =screen
   !output! ("Found interaction. No main effect? Investigate interaction.")
   !focus-on! =test_generality
   !push! =interaction
   )
(parameters encode_dimension-finish_dimension_inconsistent_effect :r .8)
 

;----------------------------------------------------------

(p interaction-retrieve_cell_with_other_effect
   =interaction>
      ISA interaction
      variable1 =variable
      variable2 nil 
      context =context
      looking_at =screen
      feature1 =prev_feature
      cell =current_cell
      prev_cell nil
   =encode_dimension>
      ISA encode_dimension
      context =context
      looking_at =screen
      feature =prev_feature
      variable =variable
      current_cell =prev_cell
 ==>
   =interaction>
      prev_cell =prev_cell
   !output! ("Found the cell ~S with the ~S effect." =prev_cell =prev_feature)
   )


(p interaction-search_for_separating_dimension
   =interaction>
      ISA interaction
      variable2 nil 
      context =context
      looking_at =screen
      cell =current_cell
      prev_cell =prev_cell
 ==>
   =visual_search_goal>
      ISA visual_search_goal
      context =context
      looking_at =current_cell
      object separating_dimension
      index =prev_cell
      result =variable
   =interaction>
      variable2 =variable
   !output! ("Search for variable which separates ~S from ~S." =current_cell =prev_cell)
   !push! =visual_search_goal
   )


(p interaction-done
   =interaction>
      ISA interaction
      variable1 =variable1
      variable2 =variable2 
      context =context
      looking_at =screen
 ==>
   !output! ("Done investigating interaction of ~S with ~S." =variable1 =variable2)
   !pop!
   )


;----------------------------------------------------------

#|
need to keep track of direction of effect!!!
also need to change termination conditions
  need to have direction expectations (add to discovery goal)
  if direction expectations met, then done
  if direction expectations opposed then done

hypothesis can have multiple consequences
  consequence is an expectation of an effect (variable and direction)
if a consequence is violated then done
if a consequence is upheld then done

this replaces component stuff
  -may still need it for design, though


*if expect feature1 and observe feature1
 then change conditional to true

*if expect feature1 with conditional true and observe feature2
 then change conditional to maybe, and look for experiment problem

*if expect feature1 with conditional maybe and observe feature2
 then change to feature2, and look for experiment problem

*if have no expecations then create hypothesis that feature1 with conditional maybe
  -weak strength for this 
  -build experiment problem if feature1 is equal_to

|#

(p test_generality-not_finding_known_effect
   =test_generality>
      ISA test_generality
      variable =variable
      feature =feature1
      context =context
      looking_at =something
   =hypothesis>
      ISA hypothesis
      cause =variable
     - effect =feature1
      conditional true
 ==>
   =hypothesis>
      conditional maybe
   =experiment_problem>
      ISA experiment_problem
      context =context
      looking_at =something
      factor =variable
   !focus-on! =experiment_problem
   !output! ("Effect of ~S not as expected. Experiment problem?" =variable)
   )


(p test_generality-not_finding_expected_effect
   =test_generality>
      ISA test_generality
      variable =variable
      feature =feature1
      context =context
      looking_at =something
   =hypothesis>
      ISA hypothesis
      cause =variable
      effect =feature2
     - effect =feature1
      conditional maybe
 ==>
   =hypothesis>
      effect =feature1
   =experiment_problem>
      ISA experiment_problem
      context =context
      looking_at =something
      factor =variable
   !focus-on! =experiment_problem
   !output! ("Effect of ~S not ~S as expected. Suspect ~S and check for experiment problem" 
             =variable =feature2 =feature1)
   )


(p test_generality-definite_effect
   =test_generality>
      ISA test_generality
      variable =variable
      feature =feature
   =hypothesis>
      ISA hypothesis
      cause =variable
      effect =feature
 ==>
   =hypothesis>
      conditional true
   !output! ("Effect of ~S is definitely ~S" =variable =feature)
   !pop!
   )


(p test_generality-build_new_hypothesis_of_no_effect
   =test_generality>
      ISA test_generality
      variable =variable
      feature equal_to
      context =context
      looking_at =something
 ==>
   =hypothesis>
      ISA hypothesis
      cause =variable
      conditional maybe
      effect equal_to
   =experiment_problem>
      ISA experiment_problem
      context =context
      looking_at =something
      factor =variable
   !output! ("Hypothesize that ~S may have no effect and check for problems." =variable)
   !focus-on! =experiment_problem
   !push! =hypothesis
   )
(parameters test_generality-build_new_hypothesis_of_no_effect :r .5)


(p test_generality-build_new_hypothesis_of_effect
   =test_generality>
      ISA test_generality
      variable =variable
      feature =feature
     - feature equal_to
      context =context
 ==>
   =hypothesis>
      ISA hypothesis
      cause =variable
      effect =feature
      conditional maybe
   !output! ("Hypothesize that ~S may have effect ~S." =variable =feature)
   !focus-on! =hypothesis
   )
(parameters test_generality-build_new_hypothesis_of_effect :r .5)


#|
once pattern filled-in, then push new encode dimension 
  with same: dimension, variable, value1, value2
  and with new: current_cell  (blank or new value calculated by lisp call)
  and with blank: current_value prev_value, feature, amount
  maybe copy over feature to prev_feature???


encode dim name and val1 and val2
set effect to nil
for dims 4 vals (unless current_dim)
  for dim 3 vals (unless current_dim)
    for dim 2 vals (unless current_dim)
      for dim 1 vals (unless current_dim)
         goto desired cell (lowest on current_dim)
         compute effect of current_dim (pos, neg, none)
            store pattern (and details), but don't reason about until main effect and interaction stage
         if nil effect store effect
         else if same as prev, then continue
         else different and conclude interaction
              and push goal to figure out interaction
when done and no interaction then conclude main effect and test generality
  -or maybe test_generality does memory test for interactions as well
   as comparison to memory...
|#


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; from SPL productions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
#| GOAL STRUCTURE

discovery_goal
     -evaluate-evidence
     |test_one_factor_goal
    -|explore_system_goal
     |hypothesis_testing_goal
          -vary_hypothesis_factors
               -vary_factor_goal
                    -variable_setting
                         -possible_selection
          -fix_remaining_variables
               -consider_factor_goal
    -encode_table
         -encode_dimension
              -interaction
              -test_generality
                   -experiment_problem
  
note. visual and motor goals are not included in this structure
|#

;------------------------------------------------

;3 main experiment paradigms

(p discovery_goal-explore_system
   =discovery_goal>
      ISA discovery_goal
      context =context
      looking_at =screen
 ==>
   =discovery_goal>
      done nil
   =explore_system_goal>
      ISA explore_system_goal
      context =context
      looking_at =screen
   !output! ("Design an experiment to explore the system")
   !push! =explore_system_goal
   )
(parameters discovery_goal-explore_system :r .5)


(p discovery_goal-test_one_factor
   =discovery_goal>
      ISA discovery_goal
      context =context
      looking_at =screen
 ==>
   =discovery_goal>
      done nil
   =test_one_factor_goal>
      ISA test_one_factor_goal
      context =context
      looking_at =screen
   !output! ("Design an experiment to test one factor")
   !push! =test_one_factor_goal
   )
(parameters discovery_goal-test_one_factor :r .5)


(p discovery_goal-test_the_hypothesis
   =discovery_goal>
      ISA discovery_goal
      context =context
      looking_at =something
      hypothesis =hypothesis
      done failure
 ==>
   =discovery_goal>
      done nil
   =hyp_testing_goal>
      ISA hyp_testing_goal
      context =context
      looking_at =something
      hypothesis =hypothesis
   !output! ("Design an experiment to test hypothesis ~S" =hypothesis)
   !push! =hyp_testing_goal
   )
(parameters discovery_goal-test_the_hypothesis :r .8)  ;.8 or .2


(p discovery_goal-done_testing_the_hypothesis
   =discovery_goal>
      ISA discovery_goal
      context =context
      looking_at =something
      hypothesis =hypothesis
      done yes
 ==>
   !output! ("Examined consequence. Therefore done testing hypothesis ~S." =hypothesis)
   !pop!
   )

(p discovery_goal-evaluate_evidence
   =discovery_goal>
      ISA discovery_goal
      context =context
      looking_at =screen
      done nil
      hypothesis =hypothesis
 ==>
   =evaluate_evidence>
      ISA evaluate_evidence
      context =context
      looking_at =screen
      hypothesis =hypothesis
      done =done
   =discovery_goal>
      done =done
   !output! ("Evaluate evidence for hypothesis ~S." =hypothesis)
   !push! =evaluate_evidence
   )

;------------------------------------------------

(p evaluate_evidence-retrieve_consequence
   =evaluate_evidence>
      ISA evaluate_evidence
      hypothesis =hypothesis
      variable nil
      effect nil
   =hypothesis_consequence>
      ISA consequence
      hypothesis =hypothesis
      variable =variable
      effect =effect
 ==>
   =evaluate_evidence>
      variable =variable
      effect =effect
   !output! ("Found consequence ~S ~S." =variable =effect)
   )


(p evaluate_evidence-consequence_supported
   =evaluate_evidence>
      ISA evaluate_evidence
      variable =variable
      effect =effect
      hypothesis =hypothesis
   =hyp>
      ISA hypothesis
      cause =variable
      effect =effect
      conditional true
 ==>
   =evaluate_evidence>
      done yes
   !output! ("Found consequence to be true. Therefore accept hypothesis ~S." =hypothesis)
   !pop!
   )

(p evaluate_evidence-consequence_not_supported
   =evaluate_evidence>
      ISA evaluate_evidence
      variable =variable
      effect =effect
      hypothesis =hypothesis
   =hyp>
      ISA hypothesis
      cause =variable
     - effect =effect
      conditional true
 ==>
   =evaluate_evidence>
      done yes
   !output! ("Found consequence to be false. Therefore reject hypothesis ~S." =hypothesis)
   !pop!
   )




;------------------------------------------------
;steps common to all 3 experiment paradigms

(p experiment_goal-done_designing
   =experiment_goal>
      ISA experiment_goal
      designed_exper t
      ran_exper nil
      context =context
      looking_at main_screen
 ==>
   =experiment_goal>
      ran_exper t
   =click_button>
      ISA click_button
      object run
      context =context
      looking_at main_screen
   !output! ("Finished designing the experiment, click on run")
   !push! =click_button
   )

(P experiment_goal-encode_table
   =experiment_goal>
      ISA experiment_goal
      designed_exper t
      ran_exper t
      analyzed_exper nil
      context =context
      looking_at =screen
 ==>
   =encode_table>
      ISA encode_table
      context =context
      looking_at =screen
   =experiment_goal>
      analyzed_exper t
   !output! ("Encode table.")
   !push! =encode_table
   )

(p experiment_goal-go_to_next_experiment
   =experiment_goal>
      ISA experiment_goal
      designed_exper t
      ran_exper t
      analyzed_exper t
      context =context
 ==>
   =click_button>
      ISA click_button
      object next_experiment
      context =context
      looking_at outcome_table
   !output! ("Work on next experiment.")
   !focus-on! =click_button
   )
(parameters experiment_goal-go_to_next_experiment :r .8)


(p experiment_goal-retest_factor
   =experiment_goal>
      ISA experiment_goal
      context =context
      designed_exper t
      ran_exper t
      analyzed_exper t
      looking_at outcome_table
   =experiment_problem>
      ISA experiment_problem
      context =context
      factor =variable
      problem too_close_together 
 ==>
   =test_one_factor_goal>
      ISA test_one_factor_goal
      context =context
      looking_at outcome_table
      factor =variable
   =click_button>
      ISA click_button
      object next_experiment
      context =context
      looking_at outcome_table
   !output! ("Work on next experiment---retest factor ~S." =variable)
   !focus-on! =test_one_factor_goal
   !push! =click_button
   )


;------------------------------------------------

(p hyp_testing_goal-set_subgoals
   =hyp_testing_goal>
      ISA hyp_testing_goal
      context =context
      looking_at =something
      hypothesis =hypothesis
      designed_exper nil
 ==>
   =hyp_testing_goal>
      designed_exper t
   =vary_hypothesis_factors>
      ISA vary_hypothesis_factors
      context =context
      looking_at =something
      hypothesis =hypothesis
   =fix_remaining_variables>
      ISA fix_remaining_variables
      context =context
      looking_at =something
   !output! ("Vary hypothesis factors and fix remaining variables.")
   !push! =fix_remaining_variables
   !push! =vary_hypothesis_factors
   )

;--------------------------------------

(p vary_hypothesis_factors-vary_consequent_variable
   =vary_hypothesis_factors>
      ISA vary_hypothesis_factors
      hypothesis =hypothesis
      under_consideration nil
   =hypothesis_consequence>
      isa consequence
      hypothesis =hypothesis
      variable =variable
 ==>
   =vary_hypothesis_factors>
      under_consideration =variable
   !output! ("Found consequent variable ~S of the hypothesis ~S" =variable =hypothesis)
   )


(p vary_hypothesis_factors-first_unselected    ;if can't retrieve a relevant consequent
   =vary_hypothesis_factors>
      ISA vary_hypothesis_factors
      context =context
      looking_at =something
      hypothesis =hypothesis
 ==>
   =visual_search_goal>
      ISA visual_search_goal
      object variable
      looking_at =something
      index first_unselected
      context =context
      result =result
   =vary_hypothesis_factors>
      under_consideration =result
   !output! ("Look first unselected variable as back-up.")
   !push! =visual_search_goal
   )
(parameters vary_hypothesis_factors-first_unselected :r .5)


(p vary_hypothesis_factors-vary_selected_variable
   =vary_hypothesis_factors>
      ISA vary_hypothesis_factors
      context =context
      looking_at =something
      hypothesis =hypothesis
      under_consideration =variable
 ==>
   =vary_factor_goal>
      ISA vary_factor_goal
      context =context
      looking_at =something
      factor =variable
   !focus-on! =vary_factor_goal
   !output! ("Vary variable ~S" =variable)
   )

;--------------------------------------

(p fix_remaining_variables-first_unselected
   =fix_remaining_variables>
      ISA fix_remaining_variables
      context =context
      looking_at =something
      under_consideration nil
 ==>
   =visual_search_goal>
      ISA visual_search_goal
      object variable
      looking_at =something
      index first_unselected
      context =context
      result =result
   =fix_remaining_variables>
      under_consideration =result
   !output! ("Look for first unselected variable.")
   !push! =visual_search_goal
   )


(p fix_remaining_variables-click_on_factor
   =fix_remaining_variables>
      ISA fix_remaining_variables
      context =context
      looking_at =screen
      under_consideration =variable
     - under_consideration failure
 ==>
    =fix_remaining_variables>
      under_consideration nil
    =consider_factor_goal>
      ISA consider_factor_goal
      context =context
      looking_at main_screen
      factor =variable
   !output! ("Consider factor ~S" =variable)
   !push! =consider_factor_goal
   )


(p fix_remaining_variables-done_designing
   =fix_remaining_variables>
      ISA fix_remaining_variables
      context =context
      looking_at =screen
      under_consideration failure
 ==>
   !output! ("Finished selecting variable values.")
   !pop!
   )

;--------------------------------------

(p explore_system-first_unselected
   =explore_system_goal>
      ISA explore_system_goal
      context =context
      looking_at =something
      designed_exper nil
      under_consideration nil
 ==>
   =visual_search_goal>
      ISA visual_search_goal
      object variable
      looking_at =something
      index first_unselected
      context =context
      result =result
   =explore_system_goal>
      under_consideration =result
   !output! ("Look for first unselected variable.")
   !push! =visual_search_goal
   )

(p explore_system-consider_factor
   =explore_system_goal>
      ISA explore_system_goal
      context =context
      looking_at =screen
      designed_exper nil
      under_consideration =variable
     - under_consideration failure           
 ==>
    =explore_system_goal>
      under_consideration nil
    =consider_factor_goal>
      ISA consider_factor_goal
      context =context
      looking_at main_screen
      factor =variable
   !output! ("Consider factor ~S" =variable)
   !push! =consider_factor_goal
   )

(p explore_system-vary_factor
   =explore_system_goal>
      ISA explore_system_goal
      context =context
      looking_at =screen
      designed_exper nil
      under_consideration =variable
   - under_consideration failure           
 ==>
    =explore_system_goal>
      under_consideration nil
    =vary_factor_goal>
      ISA vary_factor_goal
      context =context
      looking_at main_screen
      factor =variable
   !output! ("Vary factor ~S" =variable)
   !push! =vary_factor_goal
   )

(p explore_system-click_run
   =explore_system_goal>
      ISA explore_system_goal
      context =context
      looking_at main_screen
      designed_exper nil
      ran_exper nil
      under_consideration failure
 ==>
   =explore_system_goal>
      designed_exper t
      ran_exper t
   =click_button>
      ISA click_button
      object run
      context =context
      looking_at main_screen
   !output! ("Finished designing the experiment, click on run")
   !push! =click_button
   )

;--------------------------------------

(p test_one_factor_goal-pick_one_to_vary
   =test_one_factor_goal>
      ISA test_one_factor_goal
      context =context
      looking_at =something
      designed_exper nil
      factor nil
 ==>
   =pick_one_to_vary>
      ISA pick_one_to_vary
      context =context
      looking_at =something
      factor =factor
   =test_one_factor_goal>
      factor =factor
   !output! ("Pick a factor to vary.")
   !push! =pick_one_to_vary
   )

(p test_one_factor_goal-factor_already_selected
   =test_one_factor_goal>
      ISA test_one_factor_goal
      context =context
      looking_at =something
      designed_exper nil
      factor =variable
 ==>
   =test_one_factor_goal>
      designed_exper t
   =vary_factor_goal>
      ISA vary_factor_goal
      context =context
      looking_at =something
      factor =variable
   =fix_remaining_variables>
      ISA fix_remaining_variables
      context =context
      looking_at =something
   !output! ("Vary factor ~S and fix remaining variables." =variable)
   !push! =fix_remaining_variables
   !push! =vary_factor_goal
   )

;--------------------------------------

(p pick_one_to_vary-default_first_unselected
   =pick_one_to_vary>
      ISA pick_one_to_vary
      context =context
      looking_at =something
      factor nil
 ==>
   =visual_search_goal>
      ISA visual_search_goal
      object variable
      looking_at =something
      index first_unselected
      context =context
      result =result
   =pick_one_to_vary>
      factor =result
   !output! ("Just pick first unselected variable.")
   !push! =visual_search_goal
   )

(p pick_one_to_vary-done
   =pick_one_to_vary>
      ISA pick_one_to_vary
      context =context
      looking_at =something
      factor =factor
 ==>
   !output! ("Found one (~S)." =factor)
   !pop!
   )

;----------------------------------

(p consider_factor_goal-click_factor_button
   =consider_factor_goal>
      ISA consider_factor_goal
      context =context
      looking_at main_screen
      factor =variable
 ==>
   =click_button>
      ISA click_button
      object =variable
      context =context
      looking_at main_screen
   !output! ("Click on the button relating to factor ~S" =variable)
   !push! =click_button
   )
(parameters consider_factor_goal-click_factor_button :r .8)


(p consider_factor_goal-choose_settings
   =consider_factor_goal>
      ISA consider_factor_goal
      context =context
      looking_at =variable
      factor =variable
 ==>
   =variable_setting>
      ISA variable_setting
      context =context
      looking_at =variable
      name =variable
   !output! ("On the right screen to pick ~S" =variable)
   !focus-on! =variable_setting
   )


;---------------------------------------------

(p variable_setting-how_many_levels_currently
   =variable_setting>
      ISA variable_setting
      context =context
      looking_at =variable
      name =variable
      nlevels nil
 ==>
   =visual_search_goal>
      ISA visual_search_goal
      context =context
      looking_at blocked
      object slider
      index nlevels
      result =nlevels
   =variable_setting>
      nlevels =nlevels
   !output! ("How many levels are we currently at for ~S?" =variable)
   !push! =visual_search_goal
   )


(p variable_setting-want_to_change_this_one
   =variable_setting>
      ISA variable_setting
      context =context
      looking_at =variable
      name =variable
      nlevels one
   =vary_factor_goal>
      ISA vary_factor_goal
      context =context
      looking_at =variable
      factor =variable
 ==>
   =variable_setting>
      nlevels two
   =click_slider>
      ISA click_slider
      object nlevels
      context =context
      looking_at =variable
      new_setting two
   !output! ("~S is at 1, but we want 2, so change it" =variable)
   !push! =click_slider
   )


(p variable_setting-search_for_interaction
   =variable_setting>
      ISA variable_setting
      context =context
      looking_at =variable2
      name =variable2
      nlevels one
      interaction nil
   =vary_factor_goal>
      ISA vary_factor_goal
      context =context
      factor =variable1
     - factor =variable2
 ==>
   =memory_search>
      ISA memory_search
      context =context
      looking_at =variable2
      type interaction
      object1 =variable1
      object2 =variable2
      result =result
   =variable_setting>
      interaction =result
   !output! ("Search memory to see if ~S is an interacting variable with ~S" =variable1 =variable2)
   !push! =memory_search
   )

(p variable_setting-vary_interacting_variable
   =variable_setting>
      ISA variable_setting
      context =context
      looking_at =variable2
      name =variable2
      nlevels one
      interaction =variable1
     - interaction failure
   =vary_factor_goal>
      ISA vary_factor_goal
      context =context
      looking_at =variable1
      factor =variable1
 ==>
   =variable_setting>
      nlevels two
   =click_slider>
      ISA click_slider
      object nlevels
      context =context
      looking_at =variable2
      new_setting two
   !output! ("~S is an interacting variable with ~S, so set nlevels to 2" =variable2 =variable1)
   !push! =click_slider
   )


(p variable_setting-look_for_options
   =variable_setting>
      ISA variable_setting
      context =context
      looking_at =variable
      name =variable
      value1 nil
      know_options nil
 ==>
   =possible_selection>
      ISA possible_selection
      context =context
      looking_at top
      name nil
      variable =variable
   =variable_setting>
      know_options t
   !output! ("Look for options for variable ~S" =variable)
   !push! =possible_selection
   )
(parameters variable_setting-look_for_options :r .8)


(p variable_setting-use_options_in_memory
   =variable_setting>
      ISA variable_setting
      looking_at =variable
      name =variable
      know_options nil
   =selection>
      ISA possible_selection
      name =name
      variable =variable
 ==>
   =variable_setting>
      know_options t
   !output! ("Already know options for ~S" =variable)
   )
(parameters variable_setting-use_options_in_memory :r .8)


(p variable_setting-value1_memory_failure
   =variable_setting>
      ISA variable_setting
      name =variable
      looking_at =variable
      context =context
      value1 failure
      know_options t
 ==>
   =possible_selection>
      ISA possible_selection
      context =context
      looking_at top
      name nil
      variable =variable
   =variable_setting>
      value1 nil
   !output! ("Failed finding something for value1. Look for more options.")
   !push! =possible_selection
   )


(p variable_setting-value2_memory_failure
   =variable_setting>
      ISA variable_setting
      name =variable
      looking_at =variable
      context =context
      value2 failure
      know_options t
 ==>
   =possible_selection>
      ISA possible_selection
      context =context
      looking_at top
      name nil
      variable =variable
   =variable_setting>
      value2 nil
   !output! ("Failed finding something for value2. Look for more options.")
   !push! =possible_selection
   )


(p variable_setting-only_know_one_value_look_again
   =variable_setting>
      ISA variable_setting
      name =variable
      looking_at =variable
      context =context
      nlevels two
      value2 nil
      know_options t
 ==>
   =possible_selection>
      ISA possible_selection
      context =context
      looking_at top
      name nil
      variable =variable
   !output! ("Failed finding something for value2 in memory. Look for more options.")
   !push! =possible_selection
   )
(parameters variable_setting-only_know_one_value_look_again :r .3)


(p variable_seting-pick_top_value
   =variable_setting>
      ISA variable_setting
      looking_at =variable
      name =variable
      value1 nil
      know_options t
      context =context
   =some_option>
      ISA possible_selection
      looking_at top
      name =selection
      variable =variable
 ==>
   =variable_setting>
      value1 =selection
   =click_value_selection>
      ISA click_value_selection
      context =context
      object =variable
      looking_at =variable
      level one
      new_setting =selection
   !output! ("Pick the top value (~S) as a default." =selection)
   !push! =click_value_selection
   )
(parameters variable_seting-pick_top_value :r .5)


(p variable_seting-pick_most_active_value
   =variable_setting>
      ISA variable_setting
      looking_at =variable
      name =variable
      value1 nil
      know_options t
      context =context
   =some_option>
      ISA possible_selection
      name =selection
      variable =variable
 ==>
   =variable_setting>
      value1 =selection
   =click_value_selection>
      ISA click_value_selection
      context =context
      object =variable
      looking_at =variable
      level one
      new_setting =selection
   !output! ("Pick active value (~S)." =selection)
   !push! =click_value_selection
   )
(parameters variable_seting-pick_most_active_value :r .5)


(p variable_seting-pick_something_smaller
   =variable_setting>
      ISA variable_setting
      looking_at =variable
      name =variable
      value1 nil
      know_options t
      context =context
   =experiment_problem>
      ISA experiment_problem
      problem too_high
      factor =variable
      index =value
 ==>
   =solve_value_constraint>
      ISA solve_value_constraint
      looking_at =variable
      context =context
      variable =variable
      relation less_than
      value =value
      result =result
   =variable_setting>
      value1 =result
   =click_value_selection>
      ISA click_value_selection
      context =context
      object =variable
      looking_at =variable
      level one
      new_setting =result
   !output! ("Look for something less than ~S" =value)
   !push! =click_value_selection
   !push! =solve_value_constraint
   )


(p variable_setting-pick_something_bigger
   =variable_setting>
      ISA variable_setting
      looking_at =variable
      name =variable
      value1 nil
      know_options t
      context =context
   =experiment_problem>
      ISA experiment_problem
      problem too_low
      factor =variable
      index =value
 ==>
   =solve_value_constraint>
      ISA solve_value_constraint
      looking_at =variable
      context =context
      variable =variable
      relation greater_than
      value =value
      result =result
   =variable_setting>
      value1 =result
   =click_value_selection>
      ISA click_value_selection
      context =context
      object =variable
      looking_at =variable
      level one
      new_setting =result
   !output! ("Look for something greater than ~S" =value)
   !push! =click_value_selection
   !push! =solve_value_constraint
   )


(p variable_setting-retrieve_previous_experiment_value
   =variable_setting>
      ISA variable_setting
      looking_at =variable
      name =variable
      value1 nil
      know_options t
      context =context
   =previous_setting>
      ISA variable_setting
      name =variable
      value1 =value
 ==>
   =variable_setting>
      value1 =value
   =click_value_selection>
      ISA click_value_selection
      context =context
      object =variable
      looking_at =variable
      level one
      new_setting =value
   !output! ("Pick previous value ~S." =value)
   !push! =click_value_selection
   )
(parameters variable_setting-retrieve_previous_experiment_value :r .8)   ;.8 or .2


(p variable_setting-pick_some_second_value
   =variable_setting>
      ISA variable_setting
      looking_at =variable
      name =variable
      nlevels two
      value1 =value1
      value2 nil
      know_options t
      context =context
   =some_option>
      ISA possible_selection
      name =selection
     - name =value1
      variable =variable
 ==>
   =variable_setting>
      value2 =selection
   =click_value_selection>
      ISA click_value_selection
      object =variable
      context =context
      looking_at =variable
      level two
      new_setting =selection
   !output! ("Picking ~S as second value." =selection)
   !push! =click_value_selection
   )
(parameters variable_setting-pick_some_second_value :r .5)


(p variable_setting-pick_wider_value2_range
   =variable_setting>
      ISA variable_setting
      looking_at =variable
      name =variable
      nlevels two
      value1 =value1
      value2 nil
      know_options t
      context =context
   =experiment_problem>
      ISA experiment_problem
      problem too_close_together
      factor =variable
      index2 =value2
      index =value1
 ==>
   =solve_value_constraint>
      ISA solve_value_constraint
      looking_at =variable
      context =context
      variable =variable
      relation different_than
      value =value1
      value2 =value2
      result =result
   =variable_setting>
      value2 =result
   =click_value_selection>
      ISA click_value_selection
      context =context
      object =variable
      looking_at =variable
      level two
      new_setting =result
   !output! ("Look for value2 wider apart than ~S and ~S" =value2 =value1)
   !push! =click_value_selection
   !push! =solve_value_constraint
   )


(p variable_setting-pick_previous_second_value
   =variable_setting>
      ISA variable_setting
      looking_at =variable
      name =variable
      nlevels two
      value1 =value1
      value2 nil
      context =context
   =previous_variable_setting>
      ISA variable_setting
      name =variable
      value2 =selection
     - value2 =value1
 ==>
   =variable_setting>
      value2 =selection
   =click_value_selection>
      ISA click_value_selection
      context =context
      object =variable
      looking_at =variable
      level two
      new_setting =selection
   !output! ("Picking previous value2 (~S)" =selection)
   !push! =click_value_selection
   )
(parameters variable_setting-pick_previous_second_value :r .8)   ;.8 or .2


(p variable_setting-done_picking_one
   =variable_setting_goal>
      ISA variable_setting
      name =variable
      looking_at =variable
      context =context
      nlevels one
     - value1 failure
      value1 =something
 ==>
   =click_button>
      ISA click_button
      object done
      context =context
      looking_at =variable
   !output! ("Done picking values for ~S" =variable)
   !focus-on! =click_button
   )
(Parameters variable_setting-done_picking_one :r .3)


(p variable_setting-done_picking_two
   =variable_setting_goal>
      ISA variable_setting
      name =variable
      looking_at =variable
      context =context
      nlevels two
     - value1 failure
     - value2 failure
      value1 =something
      value2 =something2
 ==>
   =click_button>
      ISA click_button
      object done
      context =context
      looking_at =variable
   !output! ("Done picking values for ~S" =variable)
   !focus-on! =click_button
   )

;-------------------------------------------------


(p memory_search-found_interacting_variable
   =memory_search>
      ISA memory_search
      type interaction
      object1 =variable1
      object2 =variable2
   =interaction>
      ISA interaction
      variable1 =variable1
      variable2 =variable2
 ==>
   =memory_search>
      result =variable1
   !output! ("Found interaction between ~S and ~S." =variable1 =variable2)
   !pop!
   )

;-------------------------------------------------


(p solve_value_constraint-less_than
   =solve_value_constraint>
      ISA solve_value_constraint
      looking_at =variable
      context =context
      variable =variable
      relation less_than
      value =value
      result nil
   =some_option>
      ISA possible_selection
      name =new_value
     - name =value
      variable =variable
   !eval! (less_than-spl =new_value =value)
 ==>
   =solve_value_constraint>
      result =new_value
   !output! ("Found ~S to be less than ~S" =new_value =value)
   !pop!
   )


(p solve_value_constraint-greater_than
   =solve_value_constraint>
      ISA solve_value_constraint
      looking_at =variable
      context =context
      variable =variable
      relation greater_than
      value =value
      result nil
   =some_option>
      ISA possible_selection
      name =new_value
     - name =value
      variable =variable
   !eval! (greater_than-spl =new_value =value)
 ==>
   =solve_value_constraint>
      result =new_value
   !output! ("Found ~S to be greater than ~S" =new_value =value)
   !pop!
   )


(p solve_value_constraint-different_than
   =solve_value_constraint>
      ISA solve_value_constraint
      looking_at =variable
      context =context
      variable =variable
      relation different_than
      value =value1
      value2 =value2
      result nil
   =some_option>
      ISA possible_selection
      name =new_value
     - name =value1
     - name =value2
      variable =variable
 ==>
   =solve_value_constraint>
      result =new_value
   !output! ("Found ~S to be different than ~S and ~S" =new_value =value1 =value2)
   !pop!
   )


;----------------------------------------------------------

(p hypothesis-pop
   =hypothesis>
      ISA hypothesis
      cause =variable
      conditional =conditional
 ==>
   =rehearse_hypothesis>
      ISA rehearse_hypothesis
      factor =variable
      rehearsed nil
      context blocked
      looking_at blocked
   !focus-on! =rehearse_hypothesis
   !output! ("Pop hypothesis.")
   )

;----------------------------------------------------------

(p rehearse_hypothesis-retrieve_hypothesis
   =rehearse_hypothesis>
      ISA rehearse_hypothesis
      factor =factor
      rehearsed nil
   =hypothesis>
      ISA hypothesis
      cause =factor
 ==>
   =rehearse_hypothesis>
      rehearsed =factor
   !output! ("Rehearsing hypothesis involving ~S" =factor)
   !pop!
   )


;----------------------------------------------------------

(p experiment_problem-insufficient_range
   =experiment_problem>
      ISA experiment_problem
      contex