;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Experiment file
;;;
;;; This file should be generic enough that it will
;;; run any ACT-R 5.0 model, and therefore should not
;;; contain any model-specific code.
;;;
;;;  File: experiment.lisp
;;;  Author: Andrew R. Freed
;;;  Creation date: 2/July/02
;;;  Last modified: 25/March/03
;;;
;;; This file runs under ACT-R/PM 5.0, RPM v2.17b, Segman v3.1, and ACL 5.0.1
;;;
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Table of Contents
;;;
;;; I. Running the model
;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; I. Running the model
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; collect-data(n num) is the function to be used to run the model.
;; n is a positive integer argument, the number of times the model
;; should run.  num is "Me1" up to "Me10", specifying the telephone
;; number to retrieve from memory.
;; In each run, the model will dial the telephone once.

(defun collect-data (n num)

  ;;This delay gives you enough time to bring the telephone window to
  ;;the foreground, and to note the beginning/end of model runs
  (sleep 2)

  ;;Install to ACT-R/PM the same region Segman is parsing
  (pm-install-window '(0 0 895 689)) ;;specific to phone 52
  
  ;;Initialize Segman/Phone-dialer interface
  (init-segman-for-phone)

  ;; This resets the model to its initial state.  This includes
  ;; erasing its working memory.
  ;(reset)

  ;;This tells ACT-R/PM to process the visual items discovered by
  ;;Segman's segment-screen
  (pm-proc-display)

  ;;This loop runs the model n times, prints running times, and
  ;;finally displays an average time
  (setf *score* 0)
  (setf *all-fixations* 0)
  (loop for i from 1 to n
      do
        (setf *fixations* 0)

	;; a user trial starts with hand at mouse, and mouse at
	;; center of screen
        (pm-start-hand-at-mouse)
        (setf (cursor-position *screen*) (make-position  512 384))

	;;initializes the dialing goal with telephone number
        (mod-chunk phone-goal state init)
        (mod-chunk-fct 'phone-goal (list 'name num))
        (goal-focus phone-goal)

	;;runs the model
        (do-experiment)

	;;report time and update running counts
        (format t "~%~5,3f, ~3,0f" (- (actr-time) *score*) *fixations*)
	(setq *score* (actr-time))
        (setq *all-fixations* (+ *fixations* *all-fixations*))
  )

  (format t "~%Average time: ~5,4fs fixations: ~3,2f~%" (/ *score* n) (/ *all-fixations* n))
)

;; do-experiment makes the actual ACT-R call to start the model
(defun do-experiment ()
  (pm-run 35)       ;; Run the model for up to n seconds
)
