Skip to content

Commit a1de865

Browse files
authored
Merge pull request #194 from VirtualPlantLab/graph-editing
Adds graph editing
2 parents 24b16b5 + 67c7e84 commit a1de865

19 files changed

Lines changed: 4435 additions & 110 deletions

Project.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
1010
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
1111
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
1212
FLoops = "cc61a311-1640-44b5-9fba-1b764f453329"
13+
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
14+
JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
1315
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
1416
MultiScaleTreeGraph = "dd4a991b-8a45-4075-bede-262ee62d5583"
1517
PlantMeteo = "4630fe09-e0fb-4da5-a846-781cb73437b6"
@@ -18,13 +20,22 @@ Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1820
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
1921
Term = "22787eb5-b846-44ae-b979-8e399b8463ab"
2022

23+
[weakdeps]
24+
HTTP = "cd3eb016-35fb-5094-929b-558a96fad6f3"
25+
26+
[extensions]
27+
PlantSimEngineGraphEditorExt = "HTTP"
28+
2129
[compat]
2230
AbstractTrees = "0.4"
2331
CSV = "0.10"
2432
DataAPI = "1.15"
2533
DataFrames = "1"
2634
Dates = "1.10"
2735
FLoops = "0.2"
36+
HTTP = "1"
37+
InteractiveUtils = "1.10"
38+
JSON = "1"
2839
Markdown = "1.10"
2940
MultiScaleTreeGraph = "0.15.1"
3041
PlantMeteo = "0.8.2"

docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ makedocs(;
3434
"Detailed first simulation" => "./step_by_step/detailed_first_example.md",
3535
"Coupling" => "./step_by_step/simple_model_coupling.md",
3636
"Model Switching" => "./step_by_step/model_switching.md",
37+
"Graph visualization and editing" => "./step_by_step/graph_visualization_editor.md",
3738
"Quick examples" => "./step_by_step/quick_and_dirty_examples.md",
3839
"Implementing a process" => "./step_by_step/implement_a_process.md",
3940
"Implementing a model" => "./step_by_step/implement_a_model.md",
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)