Hey Doug,
So we don't have an officially documented API for NetLogo Web, unfortunately. Part of that is up until now we haven't had many outside projects consuming NetLogo Web as a dependency. Another part is that the API has been changing as we continue to bring NetLogo Web closer to feature parity with NetLogo desktop, especially with extensions, error handling, and internationalization.
To very briefly breakdown the architecture of NetLogo Web:
Tortoise contains the Compiler and Engine for NLW.
- The main entry point for the compiler is the `BrowserCompiler` class. The compiler turns NetLogo code into JavaScript code. It depends on the engine to do the work of running the simulation.
- The entry point for the engine is the `MiniWorkspace` class. The compiler takes care of making sure everything the `MiniWorkspace` needs is setup and then passed in at runtime when the JS is executed.
- The runtime engine is stand-alone. It can run models "headlessly" anywhere you run JS; we use Node.js to run models like that for testing, for instance. If you want to actually run the model, you'll need to execute widgets (especially forever buttons), and then check the `Updater` to see what happened in the model since the last command was run. As an example, here is how Galapagos runs the event loop for a NetLogo model. Note that the loop runs continuously, executing widgets in order (where forever buttons will be run) and then updating the UI from those results. Even if no buttons are pressed, the loop is running.
Galapagos contains the model view for NLW (called beak) as well as the pages and content you see on
netlogoweb.org.
- The view is technically standalone (all JavaScript), although at this point is pretty heavily integrated into NLW.
- The two most important places to check here are the `tortoise.coffee` file which creates the `BrowserCompiler` instance and turns a loaded nlogo source file into a `SessionLite` instance. The `SessionLite` is then the top-level piece that sets up and runs the model with the view.
- Ractive.js is the UI framework we use. It's very simplistic, but it has just enough to do what we need - events, observables, computed values, and templating.
- The `WidgetController` is also important, as it stores the widget state and UI state as the model is live in Ractive.js.
- The NetTango Web application does "bundle" NetLogo Web, using NetLogo models as the underlying runtime for its blocks-based programming interface. It has the `RactiveNetLogoModel` to try to isolate the code for creating and running a NetLogo model as a standalone UI component. That might be a useful example to look at if you are setting up your own NetLogo Web models.
Most information you might be looking for is then found through the `session` or `workspace` globals. For example, here is some code you might write to get the current global variables of a model:
```
const observer = workspace.world.observer
const globals = observer.varNames().map( (global) => observer.getGlobal(global) )
```
One thing I should add based on your described use case: there is
a pull request in flight now to add UI events and basic data queries to NetLogo Web. This would allow you to quite easily do things like listen for certain button presses and respond to them in whatever way you choose. You can also run arbitrary NetLogo reporters to get data from the model, or get metadata about the model, too.
I hope to continue to expand the API of the events and query as time permits. I'm also looking to make UI customization more easy to customize for embedding scenarios (disable authoring, disable just code tab, etc).
I hope that's helpful! And at some point (perhaps once the extension API is complete and error handling is more solid) I can use this as a basis for some real API docs.
Let me know if you have specific questions and I'll try to answer as I'm able.
-Jeremy