Skip to content

Commit 55f587a

Browse files
committed
Add get_model_nodes
1 parent d72ca56 commit 55f587a

3 files changed

Lines changed: 39 additions & 11 deletions

File tree

src/PlantSimEngine.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ include("dependencies/traversal.jl")
5858
include("dependencies/is_graph_cyclic.jl")
5959
include("dependencies/printing.jl")
6060
include("dependencies/dependencies.jl")
61+
include("dependencies/get_model_in_dependency_graph.jl")
6162

6263
# MTG compatibility:
6364
include("mtg/GraphSimulation.jl")
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
get_model_nodes(dep_graph::DependencyGraph, model)
3+
4+
Get the nodes in the dependency graph implementing a type of model.
5+
6+
# Arguments
7+
8+
- `dep_graph::DependencyGraph`: the dependency graph.
9+
- `model`: the model type to look for.
10+
11+
# Returns
12+
13+
- An array of nodes implementing the model type.
14+
15+
# Examples
16+
17+
```julia
18+
PlantSimEngine.get_model_nodes(dependency_graph, Beer)
19+
```
20+
"""
21+
function get_model_nodes(dep_graph::DependencyGraph, model)
22+
model_node = Union{SoftDependencyNode,HardDependencyNode}[]
23+
24+
traverse_dependency_graph!(dep_graph) do node
25+
if isa(node.value, model)
26+
push!(model_node, node)
27+
end
28+
end
29+
30+
return model_node
31+
end

src/dependencies/traversal.jl

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ function traverse_dependency_graph(
4747
end
4848

4949

50-
function traverse_dependency_graph!(
51-
f::Function,
52-
node::SoftDependencyNode;
53-
visit_hard_dep=true
54-
)
50+
function traverse_dependency_graph!(f::Function, graph::DependencyGraph; visit_hard_dep=true)
51+
for (p, root) in graph.roots
52+
traverse_dependency_graph!(f, root, visit_hard_dep=visit_hard_dep)
53+
end
54+
end
5555

56+
function traverse_dependency_graph!(f::Function, node::SoftDependencyNode; visit_hard_dep=true)
5657
f(node)
5758
# Traverse the hard dependencies of the SoftDependencyNode if any:
5859
if visit_hard_dep && node isa SoftDependencyNode
@@ -67,12 +68,7 @@ function traverse_dependency_graph!(
6768
end
6869
end
6970

70-
function traverse_dependency_graph!(
71-
f::Function,
72-
node::HardDependencyNode;
73-
visit_hard_dep=true
74-
)
75-
71+
function traverse_dependency_graph!(f::Function, node::HardDependencyNode; visit_hard_dep=true)
7672
f(node)
7773
# Traverse all hard dependencies:
7874
for child in node.children

0 commit comments

Comments
 (0)