Skip to content

Commit 36b9b67

Browse files
author
Brad Carman
committed
Design read from TOML files
1 parent 07dd03b commit 36b9b67

File tree

6 files changed

+200
-113
lines changed

6 files changed

+200
-113
lines changed

Manifest.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
julia_version = "1.9.0-rc1"
44
manifest_format = "2.0"
5-
project_hash = "713e17a84cfb50a483b6bccd433ea2ac228a9331"
5+
project_hash = "245a12c4a05084897342c6385479733236148032"
66

77
[[deps.AbstractAlgebra]]
88
deps = ["GroupsCore", "InteractiveUtils", "LinearAlgebra", "MacroTools", "Markdown", "Random", "RandomExtensions", "SparseArrays", "Test"]

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
88
FilterHelpers = "d8f44d74-4a63-4059-add9-16c5740a0809"
99
GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a"
1010
ModelingToolkit = "961ee093-0014-501f-94e3-6117800e7a78"
11+
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"

src/ModelingToolkitDesigner.jl

Lines changed: 71 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ using GLMakie
33
using ModelingToolkit
44
using FilterHelpers
55
using FileIO
6+
using TOML
67

78

89
#TODO: write out design code
@@ -33,7 +34,7 @@ end
3334
mutable struct ODESystemDesign
3435
# parameters::Vector{Parameter}
3536
# states::Vector{State}
36-
namespace::Union{Symbol, Nothing}
37+
3738
system::ODESystem
3839
system_color::Symbol
3940
systems::Vector{ODESystemDesign}
@@ -47,91 +48,89 @@ mutable struct ODESystemDesign
4748

4849
end
4950

50-
function ODESystemDesign(rootnamespace::Union{Symbol, Nothing}, system::ODESystem, maps::Vector{DesignMap}, design::Union{Vector{Pair{ODESystem, NamedTuple}}, Dict}; x=0.0, y=0.0, icon=nothing, wall=:E)
51+
function design_file(system::ODESystem, path::String)
52+
@assert !isnothing(system.gui_metadata) "ODESystem must use @component"
5153

52-
#update design to a Dict()
53-
if design isa Vector
54-
pars = design
55-
design = Dict()
56-
for (sys, args) in pars
57-
push!(design, sys.name => args)
58-
end
54+
# path = joinpath(@__DIR__, "designs")
55+
if !isdir(path)
56+
mkdir(path)
5957
end
6058

61-
println("system: $(system.name)")
62-
if !isnothing(rootnamespace)
63-
namespace = Symbol(rootnamespace, "", system.name)
64-
else
65-
namespace = system.name
59+
parts = split(string(system.gui_metadata.type),'.')
60+
for part in parts[1:end-1]
61+
path = joinpath(path, part)
62+
if !isdir(path)
63+
mkdir(path)
64+
end
6665
end
66+
file = joinpath(path, "$(parts[end]).toml")
6767

68-
systems = filter(x-> typeof(x) == ODESystem, ModelingToolkit.get_systems(system))
69-
68+
return file
69+
end
7070

71-
children = ODESystemDesign[]
72-
connectors = ODESystemDesign[]
73-
connections = Tuple{ODESystemDesign, ODESystemDesign}[]
74-
if !isempty(systems)
75-
children_ = filter(x->!ModelingToolkit.isconnector(x), systems)
76-
if !isempty(children_)
71+
function process_children!(children::Vector{ODESystemDesign}, systems::Vector{ODESystem}, design::Dict, maps, design_path, is_connector=false; connectors...)
72+
children_ = filter(x->ModelingToolkit.isconnector(x) == is_connector, systems)
73+
if !isempty(children_)
74+
for (i,c) in enumerate(children_)
75+
# key = Symbol(namespace, "₊", c.name)
76+
key = string(c.name)
77+
kwargs = if haskey(design, key)
78+
design[key]
79+
else
80+
Dict()
81+
end
7782

78-
for (i,c) in enumerate(children_)
79-
key = Symbol(namespace, "", c.name)
80-
kwargs = if haskey(design, key)
81-
design[key]
82-
else
83-
NamedTuple()
84-
end
85-
86-
kwargs_pair = Pair[]
87-
83+
kwargs_pair = Pair[]
84+
85+
if !is_connector
8886
if !haskey(kwargs, :x) && !haskey(kwargs, :y)
8987
push!(kwargs_pair, :x=> i*3*Δh )
9088
push!(kwargs_pair, :y=> i*Δh )
9189
end
92-
93-
for (key, value) in pairs(kwargs)
94-
push!(kwargs_pair, key=>value)
90+
else
91+
if haskey(connectors, c.name)
92+
push!(kwargs_pair, :wall => connectors[c.name])
9593
end
96-
push!(kwargs_pair, :icon=>find_icon(c))
97-
98-
push!(children, ODESystemDesign(namespace, c, maps, design; NamedTuple(kwargs_pair)...))
9994
end
95+
96+
for (key, value) in kwargs
97+
push!(kwargs_pair, Symbol(key)=>value)
98+
end
99+
push!(kwargs_pair, :icon=>find_icon(c))
100+
101+
push!(children, ODESystemDesign(c, maps, design_path; NamedTuple(kwargs_pair)...))
100102
end
103+
104+
end
105+
106+
end
101107

102-
connectors_ = filter(x->ModelingToolkit.isconnector(x), systems)
103-
if !isempty(connectors_)
104-
105-
for (i,c) in enumerate(connectors_)
108+
function ODESystemDesign(system::ODESystem, maps::Vector{DesignMap}, design_path::String; x=0.0, y=0.0, icon=nothing, wall=:E, kwargs...)
106109

107-
key = Symbol(namespace, "", c.name)
108-
kwargs = if haskey(design, key)
109-
design[key]
110-
else
111-
NamedTuple()
112-
end
110+
file = design_file(system, design_path)
111+
design = if isfile(file)
112+
TOML.parsefile(file)
113+
else
114+
Dict()
115+
end
113116

114-
kwargs_pair = Pair[]
115-
# style = get_style(c)
116-
# if !isnothing(style)
117-
# println("found style! $style")
118-
# if haskey(kwargs, :wall)
119-
# kwargs[:wall] = style
120-
# else
121-
# push!(kwargs_pair, :wall=>style)
122-
# end
123-
# end
124-
125-
for (key, value) in pairs(kwargs)
126-
push!(kwargs_pair, key=>value)
127-
end
128-
push!(kwargs_pair, :icon=>find_icon(c))
117+
println("system: $(system.name)")
118+
# if !isnothing(rootnamespace)
119+
# namespace = Symbol(rootnamespace, "₊", system.name)
120+
# else
121+
# namespace = system.name
122+
# end
129123

130-
131-
push!(connectors, ODESystemDesign(namespace, c, maps, design; NamedTuple(kwargs_pair)...))
132-
end
133-
end
124+
systems = filter(x-> typeof(x) == ODESystem, ModelingToolkit.get_systems(system))
125+
134126

127+
children = ODESystemDesign[]
128+
connectors = ODESystemDesign[]
129+
connections = Tuple{ODESystemDesign, ODESystemDesign}[]
130+
if !isempty(systems)
131+
132+
process_children!(children, systems, design, maps, design_path, false; kwargs...)
133+
process_children!(connectors, systems, design, maps, design_path, true; kwargs...)
135134

136135
for eq in equations(system)
137136

@@ -163,16 +162,17 @@ function ODESystemDesign(rootnamespace::Union{Symbol, Nothing}, system::ODESyste
163162

164163

165164
end
166-
167-
168-
165+
169166
end
170167

171168
xy= Observable((x,y))
172169
color = get_color(system, maps)
170+
if wall isa String
171+
wall = Symbol(wall)
172+
end
173173

174174

175-
return ODESystemDesign(rootnamespace, system, color, children, connectors, connections, xy, icon, Observable(color), Observable(wall))
175+
return ODESystemDesign(system, color, children, connectors, connections, xy, icon, Observable(color), Observable(wall))
176176
end
177177

178178
function find_icon(sys::ODESystem)

test/designs/Main/system.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[fluid]
2+
x = 0.5
3+
y = -0.5
4+
5+
[res]
6+
port_a = "W"
7+
x = 1.0
8+
9+
[src]
10+
input = "W"
11+
x = 0.5
12+
13+
[stp]
14+
x = 0.0
15+
16+
[vol]
17+
port = "W"
18+
x = 1.5
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
[p1]
2+
port_a = "W"
3+
x = 0.67
4+
y = 0.3
5+
6+
[p2]
7+
port_a = "W"
8+
x = 0.84
9+
y = 0.3
10+
11+
[p3]
12+
port_a = "W"
13+
x = 1.02
14+
y = 0.3
15+
16+
[p4]
17+
port_a = "W"
18+
x = 1.2
19+
y = 0.3
20+
21+
[port_a]
22+
x = 0.54
23+
y = 0.14
24+
25+
[port_b]
26+
x = 1.31
27+
y = 0.15
28+
29+
[v1]
30+
port = "S"
31+
x = 0.58
32+
y = 0.46
33+
34+
[v2]
35+
port = "S"
36+
x = 0.76
37+
y = 0.46
38+
39+
[v3]
40+
port = "S"
41+
x = 0.93
42+
y = 0.46
43+
44+
[v4]
45+
port = "S"
46+
x = 1.12
47+
y = 0.46
48+
49+
[v5]
50+
port = "S"
51+
x = 1.3
52+
y = 0.46

0 commit comments

Comments
 (0)