1
- # Global component registry: @defcomp stores component definitions here
2
- global const _compdefs = Dict {ComponentId, ComponentDef} ()
3
-
4
- compdefs () = collect (values (_compdefs))
5
-
6
- compdef (comp_id:: ComponentId ) = _compdefs[comp_id]
7
-
8
- function compdef (comp_name:: Symbol )
9
- matches = collect (Iterators. filter (obj -> name (obj) == comp_name, values (_compdefs)))
10
- count = length (matches)
11
-
12
- if count == 1
13
- return matches[1 ]
14
- elseif count == 0
15
- error (" Component $comp_name was not found in the global registry" )
16
- else
17
- error (" Multiple components named $comp_name were found in the global registry" )
18
- end
19
- end
1
+ compdef (comp_id:: ComponentId ) = getfield (getfield (Main, comp_id. module_name), comp_id. comp_name)
20
2
21
3
compdefs (md:: ModelDef ) = values (md. comp_defs)
22
4
@@ -27,10 +9,8 @@ hascomp(md::ModelDef, comp_name::Symbol) = haskey(md.comp_defs, comp_name)
27
9
compdef (md:: ModelDef , comp_name:: Symbol ) = md. comp_defs[comp_name]
28
10
29
11
function reset_compdefs (reload_builtins= true )
30
- empty! (_compdefs)
31
-
32
12
if reload_builtins
33
- compdir = joinpath (dirname ( @__FILE__ ) , " .." , " components" )
13
+ compdir = joinpath (@__DIR__ , " .." , " components" )
34
14
load_comps (compdir)
35
15
end
36
16
end
@@ -43,11 +23,14 @@ last_period(md::ModelDef, comp_def::ComponentDef) = last_period(comp_def) === no
43
23
44
24
# Return the module object for the component was defined in
45
25
compmodule (comp_id:: ComponentId ) = comp_id. module_name
46
-
47
26
compname (comp_id:: ComponentId ) = comp_id. comp_name
48
27
28
+ compmodule (comp_def:: ComponentDef ) = compmodule (comp_def. comp_id)
29
+ compname (comp_def:: ComponentDef ) = compname (comp_def. comp_id)
30
+
31
+
49
32
function Base. show (io:: IO , comp_id:: ComponentId )
50
- print (io, " $(comp_id. module_name) .$(comp_id. comp_name) " )
33
+ print (io, " <ComponentId $(comp_id. module_name) .$(comp_id. comp_name) > " )
51
34
end
52
35
53
36
"""
@@ -61,40 +44,6 @@ number_type(md::ModelDef) = md.number_type
61
44
62
45
numcomponents (md:: ModelDef ) = length (md. comp_defs)
63
46
64
-
65
- function dump_components ()
66
- for comp in compdefs ()
67
- println (" \n $(name (comp)) " )
68
- for (tag, objs) in ((:Variables , variables (comp)), (:Parameters , parameters (comp)), (:Dimensions , dimensions (comp)))
69
- println (" $tag " )
70
- for obj in objs
71
- println (" $(obj. name) = $obj " )
72
- end
73
- end
74
- end
75
- end
76
-
77
- """
78
- new_comp(comp_id::ComponentId, verbose::Bool=true)
79
-
80
- Add an empty `ComponentDef` to the global component registry with the given
81
- `comp_id`. The empty `ComponentDef` must be populated with calls to `addvariable`,
82
- `addparameter`, etc.
83
- """
84
- function new_comp (comp_id:: ComponentId , verbose:: Bool = true )
85
- if verbose
86
- if haskey (_compdefs, comp_id)
87
- @warn " Redefining component $comp_id "
88
- else
89
- @info " new component $comp_id "
90
- end
91
- end
92
-
93
- comp_def = ComponentDef (comp_id)
94
- _compdefs[comp_id] = comp_def
95
- return comp_def
96
- end
97
-
98
47
"""
99
48
delete!(m::ModelDef, component::Symbol
100
49
@@ -529,13 +478,14 @@ function _add_anonymous_dims!(md::ModelDef, comp_def::ComponentDef)
529
478
end
530
479
531
480
"""
532
- add_comp!(md::ModelDef, comp_def::ComponentDef; first=nothing, last=nothing, before=nothing, after=nothing)
481
+ add_comp!(md::ModelDef, comp_def::ComponentDef, comp_name::Symbol=comp_def.comp_id.comp_name;
482
+ first=nothing, last=nothing, before=nothing, after=nothing)
533
483
534
484
Add the component indicated by `comp_def` to the model indcated by `md`. The component is added at the
535
485
end of the list unless one of the keywords, `first`, `last`, `before`, `after`. If the `comp_name`
536
486
differs from that in the `comp_def`, a copy of `comp_def` is made and assigned the new name.
537
487
"""
538
- function add_comp! (md:: ModelDef , comp_def:: ComponentDef , comp_name:: Symbol ;
488
+ function add_comp! (md:: ModelDef , comp_def:: ComponentDef , comp_name:: Symbol = comp_def . comp_id . comp_name ;
539
489
first:: NothingInt = nothing , last:: NothingInt = nothing ,
540
490
before:: NothingSymbol = nothing , after:: NothingSymbol = nothing )
541
491
0 commit comments