tutorial-2-model-with-time.lisp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. (defstruct model
  2. clock
  3. response ; hold response from model
  4. )
  5. (defun evaluate-program (program)
  6. (let* ((result (run-experiment program))
  7. (result-response (first result))
  8. (result-time (second result)))
  9. (if (and (equalp "hello" result-response)
  10. (< result-time 800)
  11. (< 400 result-time))
  12. 0
  13. 1)))
  14. (defun run-experiment (program)
  15. (let ((md (make-model :clock 0 :response "")))
  16. (interpret program md)
  17. (list (model-response md)
  18. (model-clock md))))
  19. (defun interpret (operator md)
  20. (case (operator-label operator)
  21. (:respond-hello
  22. (incf (model-clock md) 500)
  23. (setf (model-response md) "hello"))
  24. (:respond-bye
  25. (incf (model-clock md) 500)
  26. (setf (model-response md) "bye"))
  27. (:seq
  28. (interpret (first (operator-children operator)) md)
  29. (interpret (second (operator-children operator)) md))
  30. (otherwise
  31. (error "interpret: unknown operator ~a" (operator-label operator)))))
  32. (defstruct operator
  33. label
  34. children
  35. )
  36. ;; Example models - each model has a unique control program
  37. (defun run-and-show (program name)
  38. (let* ((result (run-experiment program))
  39. (result-response (first result))
  40. (result-time (second result)))
  41. (format t "Model 1~&")
  42. (format t "-- response is ~a at ~a ms~&" result-response result-time)
  43. (format t "-- evaluation is ~a~&" (evaluate-program program))))
  44. ;; - simplest models just respond in one way
  45. (run-and-show (make-operator :label :respond-hello) "Model 1")
  46. (run-and-show (make-operator :label :seq
  47. :children (list (make-operator :label :respond-bye)
  48. (make-operator :label :respond-hello)))
  49. "Model 2")