Skip to content

Commit 278cefe

Browse files
committed
soft_dependencies : Previous changes didn't account for when the nested hard dep is at the same scale as the soft dependency and not its parent, only hard deps at different scales from the downstream model
1 parent 57d4503 commit 278cefe

1 file changed

Lines changed: 38 additions & 7 deletions

File tree

src/dependencies/soft_dependencies.jl

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,44 @@ function soft_dependencies_multiscale(soft_dep_graphs_roots::DependencyGraph{Dic
171171
# If the process has soft dependencies, then it is not independant
172172
# and we need to add its parent(s) to the node, and the node as a child
173173
for (parent_soft_dep, soft_dep_vars) in pairs(soft_deps_not_hard)
174+
175+
# if the parent isn't registered as a soft dependency, it likely means the soft dependecy should be to an internal hard dependency to the parent
176+
if(!haskey(soft_dep_graph, parent_soft_dep))
177+
178+
roots_at_given_scale = soft_dep_graphs_roots.roots[i.scale][:soft_dep_graph]
179+
if !(parent_soft_dep in keys(roots_at_given_scale))
180+
master_node = ()
181+
for (hd_key, hd) in hard_dep_dict
182+
if parent_soft_dep == hd_key
183+
master_node = hd
184+
depth = 0
185+
# A cleaner way of preventing cycles or infinite loops would be more desirable
186+
while !isa(master_node, SoftDependencyNode) && depth < 50
187+
master_node.parent === nothing && error("Finalised hard dependency has no parent")
188+
master_node = master_node.parent
189+
depth += 1
190+
end
191+
192+
break
193+
end
194+
end
195+
master_node == () && error("Parent is not located in hard deps, nor in roots, which should be the case when initalizing soft dependencies")
196+
end
197+
# NOTE : this may need to be propagated within internal hard dependencies' ancestors of this model... ?
198+
parent_node = soft_dep_graphs_roots.roots[master_node.scale][:soft_dep_graph][master_node.process]
199+
else
200+
parent_node = soft_dep_graph[parent_soft_dep]
201+
end
202+
203+
204+
174205
# preventing a cyclic dependency
175206
if parent_soft_dep == proc
176207
error("Cyclic model dependency detected for process $proc from organ $organ.")
177208
end
178209

179210
# preventing a cyclic dependency: if the parent also has a dependency on the current node:
180-
if soft_dep_graph[parent_soft_dep].parent !== nothing && i in soft_dep_graph[parent_soft_dep].parent
211+
if parent_node.parent !== nothing && i in parent_node.parent
181212
error(
182213
"Cyclic dependency detected for process $proc from organ $organ:",
183214
" $proc depends on $parent_soft_dep, which depends on $proc.",
@@ -186,26 +217,26 @@ function soft_dependencies_multiscale(soft_dep_graphs_roots::DependencyGraph{Dic
186217
end
187218

188219
# preventing a cyclic dependency: if the current node has the parent node as a child:
189-
if i.children !== nothing && soft_dep_graph[parent_soft_dep] in i.children
220+
if i.children !== nothing && parent_node in i.children
190221
error(
191222
"Cyclic dependency detected for process $proc from organ $organ:",
192223
" $proc depends on $parent_soft_dep, which depends on $proc.",
193224
" This is not allowed, but is possible via a hard dependency."
194225
)
195226
end
196227

197-
i in soft_dep_graph[parent_soft_dep].children && error("Cyclic dependency detected for process $proc from organ $organ.")
228+
i in parent_node.children && error("Cyclic dependency detected for process $proc from organ $organ.")
198229

199230
# Add the current node as a child of the node on which it depends
200-
push!(soft_dep_graph[parent_soft_dep].children, i)
231+
push!(parent_node.children, i)
201232

202233
# Add the node on which the current node depends as a parent
203234
if i.parent === nothing
204235
# If the node had no parent already, it is nothing, so we change into a vector
205-
i.parent = [soft_dep_graph[parent_soft_dep]]
236+
i.parent = [parent_node]
206237
else
207-
soft_dep_graph[parent_soft_dep] in i.parent && error("Cyclic dependency detected for process $proc from organ $organ.")
208-
push!(i.parent, soft_dep_graph[parent_soft_dep])
238+
parent_node in i.parent && error("Cyclic dependency detected for process $proc from organ $organ.")
239+
push!(i.parent, parent_node)
209240
end
210241

211242
# Add the soft dependencies (variables) of the parent to the current node

0 commit comments

Comments
 (0)