@@ -58,28 +58,65 @@ find_first_period(comp_def::AbstractComponentDef) = @or(first_period(comp_def),
58
58
find_last_period (comp_def:: AbstractComponentDef ) = @or (last_period (comp_def), last_period (get_root (comp_def)))
59
59
60
60
"""
61
- delete!(obj::AbstractCompositeComponentDef, component ::Symbol)
61
+ delete!(md::ModelDef, comp_name ::Symbol; deep::Bool=false )
62
62
63
- Delete a `component` by name from composite `ccd`.
63
+ Delete a `component` by name from `md`.
64
+ If `deep=true` then any external model parameters connected only to
65
+ this component will also be deleted.
64
66
"""
65
- function Base. delete! (ccd :: AbstractCompositeComponentDef , comp_name:: Symbol )
66
- if ! has_comp (ccd , comp_name)
67
+ function Base. delete! (md :: ModelDef , comp_name:: Symbol ; deep :: Bool = false )
68
+ if ! has_comp (md , comp_name)
67
69
error (" Cannot delete '$comp_name ': component does not exist." )
68
70
end
69
71
70
- comp_def = compdef (ccd , comp_name)
71
- delete! (ccd . namespace, comp_name)
72
+ comp_def = compdef (md , comp_name)
73
+ delete! (md . namespace, comp_name)
72
74
73
75
# Remove references to the deleted comp
74
76
comp_path = comp_def. comp_path
75
77
76
- # TBD: find and delete external_params associated with deleted component? Currently no record of this.
77
-
78
+ # Remove internal parameter connections
78
79
ipc_filter = x -> x. src_comp_path != comp_path && x. dst_comp_path != comp_path
79
- filter! (ipc_filter, ccd. internal_param_conns)
80
+ filter! (ipc_filter, md. internal_param_conns)
81
+
82
+ # Remove external parameter connections
83
+
84
+ if deep # Find and delete external_params that were connected only to the deleted component if specified
85
+ # Get all external parameters this component is connected to
86
+ comp_ext_params = map (x -> x. external_param, filter (x -> x. comp_path == comp_path, md. external_param_conns))
87
+
88
+ # Identify which ones are not connected to any other components
89
+ unbound_filter = x -> length (filter (epc -> epc. external_param == x, md. external_param_conns)) == 1
90
+ unbound_comp_params = filter (unbound_filter, comp_ext_params)
91
+
92
+ # Delete these parameters (the delete_param! function also deletes the associated external_param_conns)
93
+ [delete_param! (md, param_name) for param_name in unbound_comp_params]
94
+
95
+ else # only delete the external connections for this component but leave all external parameters
96
+ epc_filter = x -> x. comp_path != comp_path
97
+ filter! (epc_filter, md. external_param_conns)
98
+ end
99
+ dirty! (md)
100
+ end
101
+
102
+ """
103
+ delete_param!(md::ModelDef, external_param_name::Symbol)
104
+
105
+ Delete `external_param_name` from `md`'s list of external parameters, and also
106
+ remove all external parameters connections that were connected to `external_param_name`.
107
+ """
108
+ function delete_param! (md:: ModelDef , external_param_name:: Symbol )
109
+ if external_param_name in keys (md. external_params)
110
+ delete! (md. external_params, external_param_name)
111
+ else
112
+ error (" Cannot delete $external_param_name , not found in external parameter list." )
113
+ end
114
+
115
+ # Remove external parameter connections
116
+ epc_filter = x -> x. external_param != external_param_name
117
+ filter! (epc_filter, md. external_param_conns)
80
118
81
- epc_filter = x -> x. comp_path != comp_path
82
- filter! (epc_filter, ccd. external_param_conns)
119
+ dirty! (md)
83
120
end
84
121
85
122
@delegate Base. haskey (comp:: AbstractComponentDef , key:: Symbol ) => namespace
0 commit comments