-
-
Notifications
You must be signed in to change notification settings - Fork 234
feat: add function to parse variable from string #3224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
73daad1
fc13ecd
8712f99
3c728a3
afd55cb
2186298
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3285,6 +3285,86 @@ function dump_unknowns(sys::AbstractSystem) | |
| end | ||
| end | ||
|
|
||
| """ | ||
| $(TYPEDSIGNATURES) | ||
|
|
||
| Return the variable in `sys` referred to by its string representation `str`. | ||
| Roughly supports the following CFG: | ||
|
|
||
| ``` | ||
| varname = "D(" varname ")" | arrvar | maybe_dummy_var | ||
| arrvar = maybe_dummy_var "[idxs...]" | ||
| idxs = int | int "," idxs | ||
| maybe_dummy_var = namespacedvar | namespacedvar "(t)" | | ||
| namespacedvar "(t)" "ˍ" ts | namespacedvar "ˍ" ts | | ||
| namespacedvar "ˍ" ts "(t)" | ||
| ts = "t" | "t" ts | ||
|
||
| namespacedvar = ident "₊" namespacedvar | ident "." namespacedvar | ident | ||
| ``` | ||
| """ | ||
| function parse_variable(sys::AbstractSystem, str::AbstractString) | ||
| # I'd write a regex to validate `str`, but https://xkcd.com/1171/ | ||
| str = strip(str) | ||
| derivative_level = 0 | ||
| while startswith(str, "D(") && endswith(str, ")") | ||
|
||
| derivative_level += 1 | ||
| str = _string_view_inner(str, 2, 1) | ||
| end | ||
|
|
||
| arr_idxs = nothing | ||
| if endswith(str, ']') | ||
| open_idx = only(findfirst('[', str)) | ||
| idxs_range = nextind(str, open_idx):prevind(str, lastindex(str)) | ||
| idxs_str = view(str, idxs_range) | ||
| str = view(str, firstindex(str):prevind(str, open_idx)) | ||
| arr_idxs = map(Base.Fix1(parse, Int), eachsplit(idxs_str, ",")) | ||
| end | ||
|
|
||
| if endswith(str, "(t)") | ||
| str = _string_view_inner(str, 0, 3) | ||
| end | ||
|
|
||
| dummyderivative_level = 0 | ||
| if (dd_idx = findfirst('ˍ', str)) !== nothing | ||
| t_idx = nextind(str, dd_idx) | ||
| while checkbounds(Bool, str, t_idx) | ||
| if str[t_idx] != 't' | ||
| throw(ArgumentError("Dummy derivative must be 'ˍ' followed by one or more 't'.")) | ||
| end | ||
| dummyderivative_level += 1 | ||
| t_idx = nextind(str, t_idx) | ||
| end | ||
| str = view(str, firstindex(str):prevind(str, dd_idx)) | ||
| end | ||
|
|
||
| if endswith(str, "(t)") | ||
| str = _string_view_inner(str, 0, 3) | ||
| end | ||
|
|
||
| cur = sys | ||
| for ident in eachsplit(str, ('.', NAMESPACE_SEPARATOR)) | ||
| ident = Symbol(ident) | ||
| hasproperty(cur, ident) || | ||
| throw(ArgumentError("System $(nameof(cur)) does not have a subsystem/variable named $(ident)")) | ||
| cur = getproperty(cur, ident) | ||
| end | ||
|
|
||
| if arr_idxs !== nothing | ||
| cur = cur[arr_idxs...] | ||
| end | ||
|
|
||
| for i in 1:(derivative_level + dummyderivative_level) | ||
| cur = Differential(get_iv(sys))(cur) | ||
| end | ||
|
|
||
| return cur | ||
| end | ||
|
|
||
| function _string_view_inner(str, startoffset, endoffset) | ||
| view(str, | ||
| nextind(str, firstindex(str), startoffset):prevind(str, lastindex(str), endoffset)) | ||
| end | ||
|
|
||
| ### Functions for accessing algebraic/differential equations in systems ### | ||
|
|
||
| """ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add it to the docs