|
| 1 | +# Graph visualization and editing |
| 2 | + |
| 3 | +`PlantSimEngine` can export a dependency graph view from a [`ModelMapping`](@ref). The static viewer is available from the core package and does not require any web server dependency. |
| 4 | + |
| 5 | +```julia |
| 6 | +using PlantSimEngine |
| 7 | +using PlantSimEngine.Examples |
| 8 | + |
| 9 | +mapping = ModelMapping( |
| 10 | + ToyLAIModel(), |
| 11 | + Beer(0.5); |
| 12 | + status=(TT_cu=1.0:200.0,), |
| 13 | +) |
| 14 | + |
| 15 | +write_graph_view("dependency_graph.html", mapping) |
| 16 | +``` |
| 17 | + |
| 18 | +The same serialization path is used by the interactive editor. The editor is implemented as a Julia package extension, so the HTTP/WebSocket stack is loaded only when [`HTTP.jl`](https://github.com/JuliaWeb/HTTP.jl) is available and loaded in the active session. |
| 19 | + |
| 20 | +```julia |
| 21 | +using PlantSimEngine |
| 22 | +using PlantSimEngine.Examples |
| 23 | +using HTTP |
| 24 | + |
| 25 | +mapping = ModelMapping( |
| 26 | + ToyLAIModel(), |
| 27 | + Beer(0.5); |
| 28 | + status=(TT_cu=1.0:200.0,), |
| 29 | +) |
| 30 | + |
| 31 | +session = edit_graph(mapping) |
| 32 | +session.url |
| 33 | +session |
| 34 | +``` |
| 35 | + |
| 36 | +To start from a blank graph and build a mapping from scratch, omit the mapping: |
| 37 | + |
| 38 | +```julia |
| 39 | +session = edit_graph() |
| 40 | +``` |
| 41 | + |
| 42 | +By default, `edit_graph` opens `session.url` in the system default browser. Pass `open_browser=false` to keep the session headless, for example in scripts or tests: |
| 43 | + |
| 44 | +```julia |
| 45 | +session = edit_graph(mapping; open_browser=false) |
| 46 | +``` |
| 47 | + |
| 48 | +The browser sends edit commands to Julia over a WebSocket. Julia remains the source of truth: it applies the edit, rebuilds the [`ModelMapping`](@ref), recompiles graph diagnostics, and sends the updated graph back to the browser. |
| 49 | + |
| 50 | +To stop the HTTP/WebSocket session, run: |
| 51 | + |
| 52 | +```julia |
| 53 | +close(session) |
| 54 | +``` |
| 55 | + |
| 56 | +Use [`current_mapping`](@ref) to recover the latest mapping from the session: |
| 57 | + |
| 58 | +```julia |
| 59 | +edited_mapping = current_mapping(session) |
| 60 | +close(session) |
| 61 | +``` |
| 62 | + |
| 63 | +The web editor also exposes a dedicated "Mapping code" panel. It shows the current [`ModelMapping`](@ref) as Julia code, and can write that code to a `.jl` file so it can be copied/pasted or reused in scripts. The generated file is intentionally plain Julia: it imports the packages needed by the selected models and defines a top-level `mapping` variable: |
| 64 | + |
| 65 | +```julia |
| 66 | +using PlantSimEngine |
| 67 | +using PlantSimEngine.Examples |
| 68 | + |
| 69 | +mapping = ModelMapping( |
| 70 | + # ... |
| 71 | +) |
| 72 | +``` |
| 73 | + |
| 74 | +After writing a file once, every successful edit, undo, redo, or recent-file load automatically rewrites that same file. The session also keeps a recovery autosave in the temporary directory. The top-left "Open" button can reopen a mapping script from a file path or from the recent mapping list. Use git or another version-control system for mapping scripts that matter for a simulation workflow. |
| 75 | + |
| 76 | +The editor extension currently supports the same edit operations as the Julia API: |
| 77 | + |
| 78 | +- add, remove, and replace a model at a scale; |
| 79 | +- update an existing model's parameter values, scale, or rate from the inspector; |
| 80 | +- keep the model default rate, or set a custom `ClockSpec(dt, phase)` when adding a model; |
| 81 | +- set a mapped input variable, either from the inspector or by drawing a connection from an output port to an input port; |
| 82 | +- map a scalar source value or a vector of values from one or several source scales; |
| 83 | +- mark or unmark a variable as [`PreviousTimeStep`](@ref); |
| 84 | +- undo and redo edits inside the live session. |
| 85 | + |
| 86 | +If `HTTP` is not loaded, `edit_graph(mapping)` throws an error explaining that the interactive editor requires `using HTTP`. Static graph visualization through [`write_graph_view`](@ref), `graph_view`, and [`graph_view_json`](@ref) remains available without loading `HTTP`. |
0 commit comments