1 (in-package #:clnl-model)
3 (defvar *separator* "@#$#@#$#@")
5 (defvar *current-interface* nil)
6 (defvar *current-callback* nil)
8 ; At this time, this is the only stateful part of the model. If more get added,
9 ; a more general concept can be introduced.
10 (defvar *enabled-forever-buttons* nil)
26 (defun set-callback (callback)
27 "SET-CALLBACK CALLBACK => RESULT
31 CALLBACK: a function that can take netlogo code
36 Sets the means by which the interface can call arbitrary netlogo code."
37 (setf *current-callback* callback))
39 (defun set-current-interface (interface)
40 "SET-CURRENT-INTERFACE INTERFACE => RESULT
44 INTERFACE: a list of widgets for display
49 Sets the currently running model to INTERFACE.
51 The widgets set here are comprised of the bare necessary
52 to run the engine with or without an actual visual component."
53 (setf *current-interface* interface))
55 (defun interface (model)
56 "INTERFACE MODEL => INTERFACE
60 MODEL: an object representing the model
61 INTERFACE: a list of widgets for display
65 INTERFACE returns the widgets in MODEL, used for display, or
66 setting with SET-CURRENT-INTERFACE."
67 (model-interface model))
69 (defun default-model ()
70 "DEFAULT-MODEL => MODEL
74 MODEL: an object representing the model
78 Returns the default startup model."
82 (make-view :left 10 :top 10 :min-pxcor -5 :max-pxcor 5 :min-pycor -5 :max-pycor 5 :patch-size 13d0))))
84 (defun read-from-nlogo (str)
85 "READ-FROM-NLOGO STR => MODEL
89 STR: a readable stream
90 MODEL: an object representing the model
94 Takes a stream STR, reads in a nlogo file, parses it, and then
95 returns the model object."
99 ((read-sections (&optional section)
101 ((line (read-line str nil)))
103 (if (string= *separator* line)
104 (cons section (read-sections))
105 (read-sections (append section (list line))))))))
108 :code (format nil "~{~A~^~%~}" (nth 0 sections))
109 :interface (parse-interface (nth 1 sections))
110 :info (nth 2 sections)
111 :turtle-shapes (nth 3 sections)
112 :version (nth 4 sections)
113 :preview-commands (nth 5 sections)
114 :system-dynamics (nth 6 sections)
115 :behavior-space (nth 7 sections)
116 :hub-net-client (nth 8 sections)
117 :link-shapes (nth 9 sections)
118 :model-settings (nth 10 sections)
119 :delta-tick (nth 11 sections))))
123 (defparameter *widget-parsers* nil)
125 (defmacro defwidget-definition (type &rest definitions)
134 (find (car def) (list :int :double :tnil-boolean :inverted-boolean :boolean :choice :string :option :code))
139 (lambda (,lines) ; Validator
145 ((line `(nth ,n ,lines)))
147 (:specified `(string= ,(second def) ,line))
148 (:int `(parse-integer ,line :junk-allowed t))
149 (:double `(ignore-errors (coerce (read-from-string ,line) 'double-float)))
150 (:boolean `(or (string= "1" ,line) (string= "0" ,line)))
151 (:inverted-boolean `(or (string= "0" ,line) (string= "1" ,line)))
152 (:tnil-boolean `(or (string= "T" ,line) (string= "NIL" ,line)))
153 (:choice `(find ,line ',(mapcar #'car (third def)) :test #'string=)))))
154 definitions (loop for i to (length definitions) collect i)))))
155 (lambda (,lines) ; Parser
156 (,(read-from-string (format nil "make-~A" type))
161 ((line `(nth ,n ,lines))
164 (:int `(parse-integer ,line))
165 (:double `(coerce (read-from-string ,line) 'double-float))
166 (:boolean `(string= "1" ,line))
167 (:inverted-boolean `(string= "0" ,line))
168 (:tnil-boolean `(string/= "NIL" ,line))
169 (:choice `(cadr (find ,line ',(third def) :key #'car :test #'string=)))
170 (:option `(when (string/= ,line ,(third def)) ,line))
171 (:code `(unescape-code ,line))
173 (when val-getter (list (intern (symbol-name (cadr def)) :keyword) val-getter))))
174 definitions (loop for i to (length definitions) collect i))))))
177 (defwidget-definition view
178 (:specified "GRAPHICS-WINDOW")
192 (:boolean wrapping-allowed-in-x)
193 (:boolean wrapping-allowed-in-y)
199 (:choice update-mode (("0" :continuous) ("1" :tick-based)))
201 (:boolean show-tick-counter)
202 (:string tick-counter-label)
203 (:double frame-rate 30))
205 (defwidget-definition slider
206 (:specified "SLIDER")
218 (:option units "NIL")
219 (:choice direction (("HORIZONTAL" :horizontal) ("VERTICAL" :vertical))))
221 (defwidget-definition switch
222 (:specified "SWITCH")
229 (:inverted-boolean on)
233 (defwidget-definition button
234 (:specified "BUTTON")
239 (:option display "NIL")
241 (:tnil-boolean forever)
244 (:string button-type)
249 (:boolean go-time)) ; should it wait for ticks to be initialized
251 (defun parse-interface (interface-as-strings)
255 ((separate-widgets-as-strings (lines &optional widget-as-strings)
257 (if (string= "" (car lines))
258 (cons widget-as-strings (separate-widgets-as-strings (cdr lines)))
259 (separate-widgets-as-strings (cdr lines) (append widget-as-strings (list (car lines))))))))
260 (separate-widgets-as-strings interface-as-strings))))
264 (lambda (widget-as-strings)
266 ((parser (find-if (lambda (validator) (funcall validator widget-as-strings)) *widget-parsers* :key #'car)))
267 (when parser (funcall (cadr parser) widget-as-strings))))
268 widgets-as-strings))))
270 (defun find-button (name idx)
274 (lambda (widget) (and (button-p widget) (string= (button-display-name widget) name)))
275 *current-interface*)))
277 ; With authoring, idx here needs to be looked at again.
278 (defun execute-button (name &optional (idx 0))
279 "EXECUTE-BUTTON NAME &optional IDX => RESULT
281 ARGUMENTS AND VALUES:
283 NAME: the name of the button
284 IDX: the instance of the button, defaults to 0
289 Executes the code in the button referenced by NAME and IDX.
291 NAME refers to the display name for the button, which is usually
292 set by the model, but sometimes defaults to the code inside.
294 Because NAME is not guaranteed to be unique, IDX is available
295 as a specifier. The index is in the order that the buttons are
296 loaded, and cannot be guaranteed to be stable from run to run."
297 (when *current-callback*
299 ((button (find-button name (round idx))))
301 ((not button) (error "Couldn't find button with name ~A (idx: ~A)" name idx))
302 ((and (button-forever button) (find button *enabled-forever-buttons* :test #'equal))
303 (setf *enabled-forever-buttons* (remove button *enabled-forever-buttons* :test #'equal)))
304 ((button-forever button)
305 (setf *enabled-forever-buttons* (cons button *enabled-forever-buttons*))
306 (sb-thread:make-thread
309 :while (find button *enabled-forever-buttons* :test #'equal)
310 ; The sleep is necessary so that it gives other threads time
313 ((result (funcall *current-callback* (button-code button))))
314 (when (eql :stop result)
315 (setf *enabled-forever-buttons* (remove button *enabled-forever-buttons* :test #'equal)))
317 :name (format nil "Forever button: ~A" (button-display button))))
318 (t (funcall *current-callback* (button-code button)))))))
320 (defun forever-button-on (name &optional (idx 0))
321 "FOREVER-BUTTON-ON NAME &optional IDX => ON
323 ARGUMENTS AND VALUES:
325 NAME: the name of the button
326 IDX: the instance of the button, defaults to 0
331 Returns whether the button identified by NAME and IDX is currently on.
333 NAME refers to the display name for the button, which is usually
334 set by the model, but sometimes defaults to the code inside.
336 Because NAME is not guaranteed to be unique, IDX is available
337 as a specifier. The index is in the order that the buttons are
338 loaded, and cannot be guaranteed to be stable from run to run."
339 (and (find (find-button name (round idx)) *enabled-forever-buttons* :test #'equal) t))
341 ;; INFORMATION ABOUT MODEL
343 (defun world-dimensions (model)
344 "WORLD-DIMENSIONS MODEL => DIMS
346 DIMS: (:xmin XMIN :xmax XMAX :ymin YMIN :ymax YMAX)
348 ARGUMENTS AND VALUES:
350 MODEL: A valid model containing a view
351 XMIN: An integer representing the minimum patch coord in X
352 XMAX: An integer representing the maximum patch coord in X
353 YMIN: An integer representing the minimum patch coord in Y
354 YMAX: An integer representing the maximum patch coord in Y
358 Returns the dimensions of MODEL. MODEL must be a valid model
359 as parsed by CLNL, and have a valid view in it."
361 ((view (find-if #'view-p (model-interface model))))
363 :xmin (view-min-pxcor view)
364 :xmax (view-max-pxcor view)
365 :ymin (view-min-pycor view)
366 :ymax (view-max-pycor view)
367 :patch-size (view-patch-size view))))
369 (defun widget-globals (model)
370 "WIDGET-GLOBALS MODEL => GLOBALS
373 GLOBAL: (NAME DEFAULT)
375 ARGUMENTS AND VALUES:
378 NAME: A symbol interned in the keyworkd package
379 DEFAULT: The widget default value
383 Returns the globals that get declared in the model from widgets.
384 They are interned in the keyword package package set for clnl, so
385 that they can later be used for multiple purposes."
390 (slider (list (intern (string-upcase (slider-varname widget)) :keyword) (slider-default widget)))
391 (switch (list (intern (string-upcase (switch-varname widget)) :keyword) (switch-on widget)))))
392 (model-interface model))))
394 (defun buttons (model)
395 "BUTTONS MODEL => BUTTON-DEFS
397 BUTTON-DEFS: BUTTON-DEF*
398 BUTTON-DEF: (:left LEFT :top TOP :height HEIGHT :width WIDTH :forever FOREVER :display DISPLAY)
400 ARGUMENTS AND VALUES:
403 LEFT: An integer representing the left position
404 TOP: An integer representing the top position
405 HEIGHT: An integer representing height
406 WIDTH: An integer representing width
407 FOREVER: A boolean representing whether this button is a forever button
408 DISPLAY: A string representing display name
412 Returns button definitions that get declared in the buttons of the
413 MODEL. This is used to initialize the interface."
420 :left (button-left widget)
421 :top (button-top widget)
422 :width (- (button-right widget) (button-left widget))
423 :height (- (button-bottom widget) (button-top widget))
424 :forever (button-forever widget)
425 :display (button-display-name widget)))))
426 (model-interface model))))
429 "VIEW MODEL => VIEW-DEF
431 VIEW-DEF: (:left LEFT :top TOP)
433 ARGUMENTS AND VALUES:
436 LEFT: An integer representing the left position
437 TOP: An integer representing the top position
441 Returns the view definition that get declared in the view of the
442 MODEL. This is used to initialize the interface."
444 ((view (find-if #'view-p (model-interface model))))
445 (list :left (view-left view) :top (view-top view))))
450 ARGUMENTS AND VALUES:
453 CODE: The string representing the netlogo code in this model
457 Returns the code from the model."
460 ; This should get cached eventually, though maybe just cached via a display list is good enough
461 (defun button-display-name (button)
463 (button-display button)
464 (cl-ppcre:regex-replace-all "\\s+" (button-code button) " ")))
466 (defun unescape-code (code)
467 (with-output-to-string (out)
468 (with-input-from-string (in code)
470 :for c := (read-char in nil)
472 :for aux := (when (eql #\\ c)
479 (t (error "Invalid escape sequence"))))
480 :do (write-char (or aux c) out)))))