@@ -18,19 +18,44 @@ function _assert_symbol_exists(c::ComponentModel, s::Symbol)
18
18
return nothing
19
19
end
20
20
21
+ function _match_symbol_name (c:: ComponentModel , pattern)
22
+ allsym = Symbol[]
23
+ append! (allsym, sym (c))
24
+ append! (allsym, psym (c))
25
+ hasinsym (c) && append! (allsym, insym_all (c))
26
+ append! (allsym, outsym_flat (c))
27
+ append! (allsym, obssym (c))
28
+ allssym = unique! (allsym)
29
+
30
+ fidx = findall (sym -> contains (string (sym), pattern), allsym)
31
+ if isempty (fidx)
32
+ throw (ArgumentError (" No symbol matching pattern $pattern found in component model." ))
33
+ elseif length (fidx) > 1
34
+ throw (ArgumentError (" Multiple symbols matching pattern $pattern found in component model: $(allsym[fidx]) . Please use a more specific pattern." ))
35
+ end
36
+ return Symbol (allsym[only (fidx)])
37
+ end
38
+
21
39
"""
22
40
has_metadata(c::ComponentModel, sym::Symbol, key::Symbol)
23
41
has_metadata(nw::Network, sni::SymbolicIndex, key::Symbol)
24
42
25
43
Checks if symbol metadata `key` is present for symbol `sym` in a component model,
26
44
or for a symbol referenced by `sni` in a network.
45
+
46
+ `sym` can also be a String or Regex, to address the only symbol containing the pattern, see [`set_metadata!`](@ref) for details.
47
+
27
48
Throws an error if the symbol does not exist in the component model.
28
49
"""
29
50
function has_metadata (c:: ComponentModel , sym:: Symbol , key:: Symbol )
30
51
_assert_symbol_exists (c, sym)
31
52
md = symmetadata (c)
32
53
haskey (md, sym) && haskey (md[sym], key)
33
54
end
55
+ function has_metadata (c:: ComponentModel , pattern:: Union{String,Regex} , key:: Symbol )
56
+ sym = _match_symbol_name (c, pattern)
57
+ has_metadata (c, sym, key)
58
+ end
34
59
function has_metadata (nw:: Network , sym:: SymbolicIndex , key:: Symbol )
35
60
has_metadata (getcomp (nw, sym), sym. subidx, key)
36
61
end
41
66
42
67
Retrieves the metadata `key` for symbol `sym` in a component model,
43
68
or for a symbol referenced by `sni` in a network.
69
+
70
+ `sym` can also be a String or Regex, to address the only symbol containing the pattern, see [`set_metadata!`](@ref) for details.
71
+
44
72
Throws an error if the symbol does not exist in the component model.
45
73
"""
46
74
function get_metadata (c:: ComponentModel , sym:: Symbol , key:: Symbol )
47
75
_assert_symbol_exists (c, sym)
48
76
symmetadata (c)[sym][key]
49
77
end
78
+ function get_metadata (c:: ComponentModel , pattern:: Union{String,Regex} , key:: Symbol )
79
+ sym = _match_symbol_name (c, pattern)
80
+ get_metadata (c, sym, key)
81
+ end
50
82
function get_metadata (nw:: Network , sym:: SymbolicIndex , key:: Symbol )
51
83
get_metadata (getcomp (nw, sym), sym. subidx, key)
52
84
end
59
91
60
92
Sets the metadata `key` for symbol `sym` to `value` in a component model,
61
93
or for a symbol referenced by `sni` in a network.
94
+
95
+ For component models, you can also use a `String` or `Regex` pattern to match symbol names:
96
+ - String patterns use substring matching (e.g., `"δ"` matches `machine₊δ`)
97
+ - Regex patterns use full regex matching (e.g., `r"P\$ "` matches symbols ending with "P")
98
+ This will error if there is none or multile matches.
99
+
100
+ If the pattern matches multiple symbols, an error is thrown. Use a more specific pattern.
62
101
Throws an error if the symbol does not exist in the component model.
63
102
"""
64
103
function set_metadata! (c:: ComponentModel , sym:: Symbol , key:: Symbol , value)
65
104
_assert_symbol_exists (c, sym)
66
105
d = get! (symmetadata (c), sym, Dict {Symbol,Any} ())
67
106
d[key] = value
68
107
end
108
+ function set_metadata! (c:: ComponentModel , pattern:: Union{String,Regex} , key:: Symbol , value)
109
+ sym = _match_symbol_name (c, pattern)
110
+ set_metadata! (c, sym, key, value)
111
+ end
69
112
function set_metadata! (nw:: Network , sym:: SymbolicIndex , key:: Symbol , value)
70
113
set_metadata! (getcomp (nw, sym), sym. subidx, key, value)
71
114
end
72
115
73
116
function set_metadata! (c:: ComponentModel , sym:: Symbol , pair:: Pair )
74
- _assert_symbol_exists (c, sym)
75
117
set_metadata! (c, sym, pair. first, pair. second)
76
118
end
77
119
function set_metadata! (nw:: Network , sym:: SymbolicIndex , pair:: Pair )
84
126
85
127
Removes the metadata `key` for symbol `sym` in a component model,
86
128
or for a symbol referenced by `sni` in a network.
129
+
130
+ `sym` can also be a String or Regex, to address the only symbol containing the pattern, see [`set_metadata!`](@ref) for details.
131
+
87
132
Returns `true` if the metadata existed and was removed, `false` otherwise.
88
133
Throws an error if the symbol does not exist in the component model.
89
134
"""
@@ -99,6 +144,10 @@ function delete_metadata!(c::ComponentModel, sym::Symbol, key::Symbol)
99
144
end
100
145
return false
101
146
end
147
+ function delete_metadata! (c:: ComponentModel , pattern:: Union{String,Regex} , key:: Symbol )
148
+ sym = _match_symbol_name (c, pattern)
149
+ delete_metadata! (c, sym, key)
150
+ end
102
151
delete_metadata! (nw:: Network , sym:: SymbolicIndex , key:: Symbol ) = delete_metadata! (getcomp (nw, sym), sym. subidx, key)
103
152
104
153
"""
@@ -136,6 +185,8 @@ for md in [:default, :guess, :init, :bounds]
136
185
Checks if a `$($ (QuoteNode (md))) ` value is present for symbol `sym` in a component model,
137
186
or for a symbol referenced by `sni` in a network.
138
187
188
+ `sym` can be a String or Regex, to address the only symbol containing the pattern, see [`set_metadata!`](@ref) for details.
189
+
139
190
See also [`get_$($ (QuoteNode (md))) `](@ref), [`set_$($ (QuoteNode (md))) !`](@ref).
140
191
"""
141
192
$ fname_has (c:: Comp_or_NW , sym) = has_metadata (c, sym, $ (QuoteNode (md)))
@@ -147,6 +198,8 @@ for md in [:default, :guess, :init, :bounds]
147
198
Returns the `$($ (QuoteNode (md))) ` value for symbol `sym` in a component model,
148
199
or for a symbol referenced by `sni` in a network.
149
200
201
+ `sym` can be a String or Regex, to address the only symbol containing the pattern, see [`set_metadata!`](@ref) for details.
202
+
150
203
See also [`has_$($ (QuoteNode (md))) `](@ref), [`set_$($ (QuoteNode (md))) !`](@ref).
151
204
"""
152
205
$ fname_get (c:: Comp_or_NW , sym) = get_metadata (c, sym, $ (QuoteNode (md)))
@@ -160,6 +213,8 @@ for md in [:default, :guess, :init, :bounds]
160
213
161
214
If `value` is `nothing` or `missing`, the metadata is removed.
162
215
216
+ `sym` can be a String or Regex, to address the only symbol containing the pattern, see [`set_metadata!`](@ref) for details.
217
+
163
218
See also [`has_$($ (QuoteNode (md))) `](@ref), [`get_$($ (QuoteNode (md))) `](@ref).
164
219
"""
165
220
function $fname_set (c:: Comp_or_NW , sym, val)
@@ -177,6 +232,8 @@ for md in [:default, :guess, :init, :bounds]
177
232
Removes the `$($ (QuoteNode (md))) ` value for symbol `sym` in a component model,
178
233
or for a symbol referenced by `sni` in a network.
179
234
235
+ `sym` can be a String or Regex, to address the only symbol containing the pattern, see [`set_metadata!`](@ref) for details.
236
+
180
237
See also [`has_$($ (QuoteNode (md))) `](@ref), [`set_$($ (QuoteNode (md))) !`](@ref).
181
238
"""
182
239
$ fname_del (c:: Comp_or_NW , sym) = delete_metadata! (c, sym, $ (QuoteNode (md)))
0 commit comments