1 # Package CLNL-INTERFACE
5 The NetLogo view interface using opengl. This is responsible for taking the current state of the enging and displaying it. Will not house any interface components.
9 * **function [export-view](#function-export-view)** - _export-view_ returns the current view in raw data of RGBA pixels.
10 * **function [initialize](#function-initialize)** - This is where the initialization of the interface that sits behind the interface lives. From here, one can go into headless or running mode, but for certain things this interface will still need to act, and also allows for bringing up and taking down of visual elements.
11 * **function [run](#function-run)** - _run_ runs the view in an external window.
13 ## Function **EXPORT-VIEW**
17 **export-view** => _image-data_
19 #### Arguments and Values:
21 _image-data_---A vector, pixel data as returned by opengls readPixels
25 _export-view_ returns the current view in raw data of RGBA pixels.
27 Each pixel is made up of 4 bytes of data, which an be walked over. The number of pixels is the current width x height. Converting to some other image format is a matter of pulling that information out and putting it into whatever format you like.
29 This requires opengl to run, but can be used with xvfb in a headless mode.
31 ## Function **INITIALIZE**
35 **initialize** _&key_ _dims_ _view_ _buttons_ _switches_ => _result_
37 ```dims::= (:xmin xmin :xmax xmax :ymin ymin :ymax ymax :patch-size patch-size)```
38 ```view::= (:left left :top top)```
39 ```buttons::= button-def*```
40 ```switches::= switch-def*```
41 ```button-def::= (:left left :top top :height height :width width :forever forever :display display)```
42 ```switch-def::= (:left left :top top :width width :var var :display display :initial-value initial-value)```
44 #### Arguments and Values:
47 _xmin_---An integer representing the minimum patch coord in X
48 _xmax_---An integer representing the maximum patch coord in X
49 _ymin_---An integer representing the minimum patch coord in Y
50 _ymax_---An integer representing the maximum patch coord in Y
51 _patch-size_---A double representing the size of the patches in pixels
52 _height_---An integer representing height
53 _forever_---A boolean representing the forever status
54 _left_---An integer representing the left position
55 _top_---An integer representing the top position
56 _width_---An integer representing width
57 _var_---A string representing the variable name
58 _display_---A string representing display name
59 _initial-value_---The initial value
63 This is where the initialization of the interface that sits behind the interface lives. From here, one can go into headless or running mode, but for certain things this interface will still need to act, and also allows for bringing up and taking down of visual elements.
71 #### Arguments and Values:
73 _result_---undefined, should never get here
77 _run_ runs the view in an external window.
79 This should be run inside another thread as it starts the glut main-loop. Closing this window will then cause the entire program to terminate.
84 The primary code responsible for tokenizing NetLogo code.
88 * **function [lex](#function-lex)** - _lex_ lexes NetLogo code.
94 **lex** _text_ => _ast_
96 #### Arguments and Values:
98 _text_---Some NetLogo code
99 _ast_---An ambigious _ast_ that can later be parsed
103 _lex_ lexes NetLogo code.
105 _lex_ checks for some things, in as much as it can without knowing anything about some of the backgrounds of NetLogo. However, it does the first pass with as much as it can.
106 # Package CLNL-PARSER
110 All the code to convert the list of tokens coming from the lexer into an ast that can be transpiled later.
114 * **function [parse](#function-parse)** - _parse_ takes a ambigious _lexed-ast_ and converts it to an unambigious one.
116 ## Function **PARSE**
120 **parse** _lexed-ast_ _&optional_ _dynamic-prims_ => _ast_
122 ```dynamic-prims::= dynamic-prim*```
123 ```dynamic-prim::= (:name name :args args :infix infix :precedence precedence)```
126 #### Arguments and Values:
128 _lexed-ast_---An ambigious ast
129 _ast_---An unambigious ast that can be transpiled
130 _name_---A symbol in the keyword package
131 _infix_---Boolean denoting whether the prim is infix, defaulting to NIL
132 _precedence_---A number, usually 10 for reporters, and 0 for commands
133 _arg_---A list of symbols denoting the type of argument
137 _parse_ takes a ambigious _lexed-ast_ and converts it to an unambigious one.
139 _dynamic-prims_ that are passed in are used to avoid compilation errors on things not statically defined by the NetLogo language, be they user defined procedures or generated primitives from breed declarations. _name_ and _precedence_ are required for all dynamic prims.
141 _precedence_ is a number used to calculate the order of operations. Higher numbers have more precedence than lower ones. Generally all commands should have the lowest precedence, and all reporters should have 10 as the precedence.
143 The possible values for _arg_ are :agentset, :boolean, :number, :command-block, :string, or t for wildcard. For optional arguments, _arg_ can be a list of the form (_arg_ :optional) where _arg_ is one of the aforementioned values.
145 The need for a parser between the lexer and the transpiler is because NetLogo needs two passes to turn into something that can be used. This is the only entry point into this module, and should probably remain that way.
147 There's also a lot of error checking that the _lexed-ast_ even makes sense, even though the lexer obviously thought it did.
149 Examples are too numerous and varied, but by inserting an output between the lexer and this code, a good idea of what goes on can be gotten.
150 # Package CLNL-TRANSPILER
154 The transpiler is responsible for taking an ast and turning it into valid CL code targeting the nvm. Here is where start to care about commands versus reporters and ensuring that things are in the right place. The reason we wait until here is because we want to allow someone else to play with the AST before handing it off to us. For instance, the command center wants to add "show" to reporters, and the users dictate based on entry point whether they are expecting a command or a reporter. So monitors can say "hey, transpile this reporter" and we'll check to make sure it actually is.
156 Furthermore, the lisp code that any netlogo code would be transpiled to should use exported symbols, such that anyone writing NetLogo code in lisp could use the nvm in the same way that comes out of this transpiler All the code to convert the list of tokens coming from the lexer into an ast that can be transpiled later.
160 * **function [command-list-p](#function-command-list-p)** - _command-list-p_ returns whether the parsed-ast is a valid list of commands.
161 * **function [reporter-p](#function-reporter-p)** - _reporter-p_ returns whether the parsed-ast is a valid reporter.
162 * **function [transpile](#function-transpile)** - _transpile_ takes a unambigious _parsed-ast_ and converts it to Common Lisp code. The _parsed-ast_ must be either a list of commands, or a single reporter.
164 ## Function **COMMAND-LIST-P**
168 **command-list-p** _parsed-ast_ => _result_
170 #### Arguments and Values:
172 _parsed-ast_---An ast as returned by the parser
177 _command-list-p_ returns whether the parsed-ast is a valid list of commands.
179 ## Function **REPORTER-P**
183 **reporter-p** _parsed-ast_ => _result_
185 #### Arguments and Values:
187 _parsed-ast_---An ast as returned by the parser
192 _reporter-p_ returns whether the parsed-ast is a valid reporter.
194 ## Function **TRANSPILE**
198 **transpile** _parsed-ast_ _&optional_ _dynamic-prims_ => _ast_
200 ```dynamic-prims::= dynamic-prim*```
201 ```dynamic-prim::= (:name name :type type :macro macro :func func)```
202 ```type::= :reporter | :command```
204 #### Arguments and Values:
206 _parsed-ast_---An ast as returned by the parser
207 _ast_---An common lisp _ast_ that can be actually run in a common lisp instance
208 _name_---A symbol in the keyword package
209 _macro_---A macro that will be called with the arguments ast
210 _func_---A function that will be called with the transpiled arguments
214 _transpile_ takes a unambigious _parsed-ast_ and converts it to Common Lisp code. The _parsed-ast_ must be either a list of commands, or a single reporter.
216 When a set of _dynamic-prims_ is included, external language constructs can be also transpiled. The provided functions will be inserted into the returned _ast_ with a call to _func_ALL. If :macro is included, instead of having a call to _func_ALL provided, the macro will be run at netlogo transpile time, with the arguments it should have specified to the parser. The result of that function call will then be dropped into the ast.
218 Calling eval on that code should work correctly as long as you have a running engine.
219 # Package CLNL-CODE-PARSER
223 A parser specifically for code from NetLogo models, that turns the lexed ast from an entire structured file into something more defined.
225 This is different from the general parser (in clnl-parser) in that it's made for parsing the code section of nlogo files, and so works outside of the constraints. In NetLogo, I believe this is analagous to the StructureParser, but I'm guessing there's weird overlap with other things.
229 * **function [breeds](#function-breeds)** - Returns the breeds that get declared in the code.
230 * **function [globals](#function-globals)** - Returns the globals that get declared in the code.
231 * **function [parse](#function-parse)** - _parse_ takes a ambigious _lexed-ast_ and converts it to an unambigious one. It also returns the primitives that are defined in the code file, including ones generated from the _external-globals_, that can then be passed to both the parser and the transpiler.
232 * **function [patches-own-vars](#function-patches-own-vars)** - Returns the turtles own variables that get declared in the code.
233 * **function [procedures](#function-procedures)** - Returns the procedures that were defined in the code. These can then be translated into common lisp by using mapcar on the _body_, and set to some function defined by _name_
234 * **function [turtles-own-vars](#function-turtles-own-vars)** - Returns the turtles own variables that get declared in the code.
236 ## Function **BREEDS**
240 **breeds** _code-parsed-ast_ => _breeds_
242 ```breeds::= breed*```
244 #### Arguments and Values:
246 _code-parsed-ast_---An ast as created by clnl-code-parse:parse
247 _breed_---A symbol interned in :keyword
251 Returns the breeds that get declared in the code.
253 ## Function **GLOBALS**
257 **globals** _code-parsed-ast_ => _globals_
259 ```globals::= global*```
261 #### Arguments and Values:
263 _code-parsed-ast_---An ast as created by clnl-code-parse:parse
264 _global_---A symbol interned in :keyword
268 Returns the globals that get declared in the code.
270 ## Function **PARSE**
274 **parse** _lexed-ast_ _&optional_ _external-globals_ => _ast_, _prims_
276 #### Arguments and Values:
278 _lexed-ast_---An ambigious ast
279 _external-globals_---A list of symbols in keyword package
280 _ast_---An unambigious ast that represents the code block of a model
281 _prims_---Primitives that can be sent to the parser and transpiler
285 _parse_ takes a ambigious _lexed-ast_ and converts it to an unambigious one. It also returns the primitives that are defined in the code file, including ones generated from the _external-globals_, that can then be passed to both the parser and the transpiler.
287 _external-globals_ is a list of symbols representing global variables that are not defined within the code. Normally these come from widgets defined in the model file, but could arguably come from elsewhere.
289 This parser, unlike CLNL-_parse_:_parse_, should not be fed into the transpiler.
291 Rather, the ast that's returned can be queried with other functions included in the CLNL-CODE-_parse_R package to tease out necessary information. Some of those things will involve code blocks that can then be transpiled.
293 ## Function **PATCHES-OWN-VARS**
297 **patches-own-vars** _code-parsed-ast_ => _patches-own-vars_
299 ```patches-own-vars::= patches-own-var*```
301 #### Arguments and Values:
303 _code-parsed-ast_---An ast as created by clnl-code-parse:parse
304 _patches-own-var_---A symbol interned in :keyword
308 Returns the turtles own variables that get declared in the code.
310 ## Function **PROCEDURES**
314 **procedures** _code-parsed-ast_ => _procedures_
316 ```procedures::= procedure*```
317 ```procedure::= (name body)```
319 #### Arguments and Values:
321 _code-parsed-ast_---An ast as created by clnl-code-parse:parse
322 _name_---A symbol interned in :keyword
323 _body_---A list of lexed forms
327 Returns the procedures that were defined in the code. These can then be translated into common lisp by using mapcar on the _body_, and set to some function defined by _name_
329 ## Function **TURTLES-OWN-VARS**
333 **turtles-own-vars** _code-parsed-ast_ => _turtles-own-vars_
335 ```turtles-own-vars::= turtles-own-var*```
337 #### Arguments and Values:
339 _code-parsed-ast_---An ast as created by clnl-code-parse:parse
340 _turtles-own-var_---A symbol interned in :keyword
344 Returns the turtles own variables that get declared in the code.
349 The representation, parsing, and serializing of NetLogo model files, including all of the sections, and subsections held within. This package houses not only the code to read and write .nlogo files, but also the living state of the model as clnl runs.
353 * **function [buttons](#function-buttons)** - Returns button definitions that get declared in the buttons of the _model_. This is used to initialize the interface.
354 * **function [code](#function-code)** - Returns the code from the model.
355 * **function [default-model](#function-default-model)** - Returns the default startup model.
356 * **function [execute-button](#function-execute-button)** - Executes the code in the button referenced by _name_ and _idx_.
357 * **function [forever-button-on](#function-forever-button-on)** - Returns whether the button identified by _name_ and _idx_ is currently on.
358 * **function [interface](#function-interface)** - _interface_ returns the widgets in _model_, used for display, or setting with SET-CURRENT-_interface_.
359 * **function [read-from-nlogo](#function-read-from-nlogo)** - Takes a stream _str_, reads in a nlogo file, parses it, and then returns the model object.
360 * **function [set-callback](#function-set-callback)** - Sets the means by which the interface can call arbitrary netlogo code.
361 * **function [set-current-interface](#function-set-current-interface)** - Sets the currently running model to _interface_.
362 * **function [sliders](#function-sliders)** - Returns slider definitions that get declared in the sliders of the _model_. This is used to initialize the interface.
363 * **function [switches](#function-switches)** - Returns switch definitions that get declared in the switches of the _model_. This is used to initialize the interface.
364 * **function [textboxes](#function-textboxes)** - Returns textbox definitions that get declared in the textboxes of the _model_. This is used to initialize the interface.
365 * **function [view](#function-view)** - Returns the view definition that get declared in the view of the _model_. This is used to initialize the interface.
366 * **function [widget-globals](#function-widget-globals)** - Returns the globals that get declared in the model from widgets. They are interned in the keyword package package set for clnl, so that they can later be used for multiple purposes.
367 * **function [world-dimensions](#function-world-dimensions)** - Returns the dimensions of _model_. _model_ must be a valid model as parsed by CLNL, and have a valid view in it.
369 ## Function **BUTTONS**
373 **buttons** _model_ => _button-defs_
375 ```button-defs::= button-def*```
376 ```button-def::= (:left left :top top :height height :width width :forever forever :display display)```
378 #### Arguments and Values:
380 _model_---A valid model
381 _left_---An integer representing the left position
382 _top_---An integer representing the top position
383 _height_---An integer representing height
384 _width_---An integer representing width
385 _forever_---A boolean representing whether this button is a forever button
386 _display_---A string representing display name
390 Returns button definitions that get declared in the buttons of the _model_. This is used to initialize the interface.
396 **code** _model_ => _code_
398 #### Arguments and Values:
400 _model_---A valid model
401 _code_---The string representing the netlogo code in this model
405 Returns the code from the model.
407 ## Function **DEFAULT-MODEL**
411 **default-model** => _model_
413 #### Arguments and Values:
415 _model_---an object representing the model
419 Returns the default startup model.
421 ## Function **EXECUTE-BUTTON**
425 **execute-button** _name_ _&optional_ _idx_ => _result_
427 #### Arguments and Values:
429 _name_---the name of the button
430 _idx_---the instance of the button, defaults to 0
435 Executes the code in the button referenced by _name_ and _idx_.
437 _name_ refers to the display name for the button, which is usually set by the model, but sometimes defaults to the code inside.
439 Because _name_ is not guaranteed to be unique, _idx_ is available as a specifier. The index is in the order that the buttons are loaded, and cannot be guaranteed to be stable from run to run.
441 ## Function **FOREVER-BUTTON-ON**
445 **forever-button-on** _name_ _&optional_ _idx_ => _on_
447 #### Arguments and Values:
449 _name_---the name of the button
450 _idx_---the instance of the button, defaults to 0
455 Returns whether the button identified by _name_ and _idx_ is currently on.
457 _name_ refers to the display name for the button, which is usually set by the model, but sometimes defaults to the code inside.
459 Because _name_ is not guaranteed to be unique, _idx_ is available as a specifier. The index is in the order that the buttons are loaded, and cannot be guaranteed to be stable from run to run.
461 ## Function **INTERFACE**
465 **interface** _model_ => _interface_
467 #### Arguments and Values:
469 _model_---an object representing the model
470 _interface_---a list of widgets for display
474 _interface_ returns the widgets in _model_, used for display, or setting with SET-CURRENT-_interface_.
476 ## Function **READ-FROM-NLOGO**
480 **read-from-nlogo** _str_ => _model_
482 #### Arguments and Values:
484 _str_---a readable stream
485 _model_---an object representing the model
489 Takes a stream _str_, reads in a nlogo file, parses it, and then returns the model object.
491 ## Function **SET-CALLBACK**
495 **set-callback** _callback_ => _result_
497 #### Arguments and Values:
499 _callback_---a function that can take netlogo code
504 Sets the means by which the interface can call arbitrary netlogo code.
506 ## Function **SET-CURRENT-INTERFACE**
510 **set-current-interface** _interface_ => _result_
512 #### Arguments and Values:
514 _interface_---a list of widgets for display
519 Sets the currently running model to _interface_.
521 The widgets set here are comprised of the bare necessary to run the engine with or without an actual visual component.
523 ## Function **SLIDERS**
527 **sliders** _model_ => _slider-defs_
529 ```slider-defs::= slider-def*```
530 ```slider-def::= (:left left :top top :width width :var var :display display :initial-value initial-value)```
532 #### Arguments and Values:
534 _model_---A valid model
535 _left_---An integer representing the left position
536 _top_---An integer representing the top position
537 _width_---An integer representing width
538 _var_---A symbole representing variable
539 _display_---A string representing variable name
540 _initial-value_---The initial value
544 Returns slider definitions that get declared in the sliders of the _model_. This is used to initialize the interface.
546 ## Function **SWITCHES**
550 **switches** _model_ => _switch-defs_
552 ```switch-defs::= switch-def*```
553 ```switch-def::= (:left left :top top :width width :var var :display display :initial-value initial-value)```
555 #### Arguments and Values:
557 _model_---A valid model
558 _left_---An integer representing the left position
559 _top_---An integer representing the top position
560 _width_---An integer representing width
561 _var_---A symbole representing variable
562 _display_---A string representing variable name
563 _initial-value_---The initial value
567 Returns switch definitions that get declared in the switches of the _model_. This is used to initialize the interface.
569 ## Function **TEXTBOXES**
573 **textboxes** _model_ => _textbox-defs_
575 ```textbox-defs::= textbox-def*```
576 ```textbox-def::= (:left left :top top :height height :width width :display display)```
578 #### Arguments and Values:
580 _model_---A valid model
581 _left_---An integer representing the left position
582 _top_---An integer representing the top position
583 _height_---An integer representing height, in characters
584 _width_---An integer representing width, in characters
585 _display_---A string representing display name
589 Returns textbox definitions that get declared in the textboxes of the _model_. This is used to initialize the interface.
595 **view** _model_ => _view-def_
597 ```view-def::= (:left left :top top)```
599 #### Arguments and Values:
601 _model_---A valid model
602 _left_---An integer representing the left position
603 _top_---An integer representing the top position
607 Returns the view definition that get declared in the view of the _model_. This is used to initialize the interface.
609 ## Function **WIDGET-GLOBALS**
613 **widget-globals** _model_ => _globals_
615 ```globals::= global*```
616 ```global::= (name default)```
618 #### Arguments and Values:
620 _model_---A valid model
621 _name_---A symbol interned in the keyworkd package
622 _default_---The widget default value
626 Returns the globals that get declared in the model from widgets. They are interned in the keyword package package set for clnl, so that they can later be used for multiple purposes.
628 ## Function **WORLD-DIMENSIONS**
632 **world-dimensions** _model_ => _dims_
634 ```dims::= (:xmin xmin :xmax xmax :ymin ymin :ymax ymax)```
636 #### Arguments and Values:
638 _model_---A valid model containing a view
639 _xmin_---An integer representing the minimum patch coord in X
640 _xmax_---An integer representing the maximum patch coord in X
641 _ymin_---An integer representing the minimum patch coord in Y
642 _ymax_---An integer representing the maximum patch coord in Y
646 Returns the dimensions of _model_. _model_ must be a valid model as parsed by CLNL, and have a valid view in it.
647 # Package CLNL-RANDOM
649 Wrapper around mt19937.
651 mt19937 implements a merseinne twister that must be adapted a little in order to match the implementation in the main NetLogo codebase which tries to match how java.util.Random works. Turtles, all the way down.
655 * **function [export](#function-export)** - _export_ dumps out the random state to be export world ready.
656 * **function [next-double](#function-next-double)** - _next-double_ returns the next randomly generated double.
657 * **function [next-int](#function-next-int)** - _next-int_ returns the next randomly generated integer.
658 * **function [next-long](#function-next-long)** - _next-long_ returns the next randomly generated long.
659 * **function [set-seed](#function-set-seed)** - _set-seed_ sets the seed on the RNG.
661 ## Function **EXPORT**
665 **export** => _random-state_
667 #### Arguments and Values:
669 _random-state_---A dump of the current random state
673 _export_ dumps out the random state to be export world ready.
675 When NetLogo dumps out the current state of the engine, the state of the RNG also gets dumped out so that it can be reinitialized later. This accomplishes that.
677 This isn't really useful for regular use.
679 ## Function **NEXT-DOUBLE**
683 **next-double** _&optional_ _n_ => _double_
685 #### Arguments and Values:
687 _n_---A double representing the upper bound
692 _next-double_ returns the next randomly generated double.
694 It does so in a way that's in accordance with java.util.Random and the MerseinneTwisterFast that's in _n_etLogo. It also advances the R_n_G and is bounded by _n_.
696 ## Function **NEXT-INT**
700 **next-int** _n_ => _int_
702 #### Arguments and Values:
704 _n_---An integer representing the upper bound
709 _next-int_ returns the next randomly generated integer.
711 It does so in a way that's in accordance with java.util.Random and the MerseinneTwisterFast that's in _n_etLogo. It also advances the R_n_G and is bounded by _n_.
713 ## Function **NEXT-LONG**
717 **next-long** _n_ => _long_
719 #### Arguments and Values:
721 _n_---A long representing the upper bound
726 _next-long_ returns the next randomly generated long.
728 It does so in a way that's in accordance with java.util.Random and the MerseinneTwisterFast that's in _n_etLogo. It also advances the R_n_G and is bounded by _n_.
730 ## Function **SET-SEED**
734 **set-seed** => _result_
736 #### Arguments and Values:
742 _set-seed_ sets the seed on the RNG.