Skip to content

Commit 749e8ab

Browse files
committed
API changed for consistency
1 parent 839f925 commit 749e8ab

13 files changed

Lines changed: 94 additions & 71 deletions

docs/src/index.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ has_parent(c::Context)
7171
```
7272

7373
```@docs
74-
isroot(c::Context)
74+
is_root
7575
```
7676

7777
```@docs
@@ -87,23 +87,27 @@ ancestor(c::Context; condition = x -> true, maxlevel::Int = typemax(Int))
8787
```
8888

8989
```@docs
90-
haschildren(c::Context)
90+
get_root
9191
```
9292

9393
```@docs
94-
isleaf(c::Context)
94+
has_children(c::Context)
9595
```
9696

9797
```@docs
98-
hasdescendant(c::Context; condition = x -> true, maxlevel::Int = typemax(Int))
98+
is_leaf(c::Context)
99+
```
100+
101+
```@docs
102+
has_descendant(c::Context; condition = x -> true, maxlevel::Int = typemax(Int))
99103
```
100104

101105
```@docs
102106
children(c::Context)
103107
```
104108

105109
```@docs
106-
getdescendant(c::Context; condition = x -> true, maxlevel::Int = typemax(Int))
110+
get_descendant
107111
```
108112

109113
# Traversal algorithms
@@ -113,11 +117,11 @@ traverse(g::Graph; fun = () -> nothing)
113117
```
114118

115119
```@docs
116-
traversedfs(g::Graph; fun = () -> nothing, ID = root_id(g))
120+
traverse_dfs(g::Graph; fun = () -> nothing, ID = root_id(g))
117121
```
118122

119123
```@docs
120-
traversebfs(g::Graph; fun = () -> nothing, ID = root_id(g))
124+
traverse_bfs(g::Graph; fun = () -> nothing, ID = root_id(g))
121125
```
122126

123127
## Graph visualization

src/Algorithms.jl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ applied to each node.
2121
This traveral happens in the order in which the nodes are stored in the graph.
2222
This order is arbitrary and may vary across executions of the code (it does not
2323
correspond to the order in which nodes are created). For algorithms that require
24-
a particular traveral order of the graph, see `traversedfs` and `traversebfs`.
24+
a particular traveral order of the graph, see `traverse_dfs` and `traverse_bfs`.
2525
2626
This function does not store any results generated by `fun`. Hence, if the user
2727
wants to keep track of such results, they should be stored indirectly (e.g., via
@@ -71,15 +71,15 @@ function traverse(g::StaticGraph; fun = () -> nothing, order = "any", ID = root_
7171
fun(data(val))
7272
end
7373
elseif order == "dfs"
74-
traversedfs(g::Graph; fun = fun, ID = ID)
74+
traverse_dfs(g::Graph; fun = fun, ID = ID)
7575
elseif order == "bfs"
76-
traversebfs(g::Graph; fun = fun, ID = ID)
76+
traverse_bfs(g::Graph; fun = fun, ID = ID)
7777
end
7878
return nothing
7979
end
8080

8181
"""
82-
traversedfs(g::Graph; fun = () -> nothing, ID = root_id(g))
82+
traverse_dfs(g::Graph; fun = () -> nothing, ID = root_id(g))
8383
8484
Iterates over all the nodes in the graph (depth-first order, starting at a any
8585
node) and execute for the function `fun` on each node
@@ -98,7 +98,7 @@ This traveral happens in a depth-first order. That is, all nodes in a branch of
9898
the graph are visited until reach a leaf node, then moving to the next branch.
9999
Hence, this algorithm should always generate the same result when applied to the
100100
same graph (assuming the user-defined function is not stochastic). For a version
101-
of this function that us breadth-first order see `traversebfs`.
101+
of this function that us breadth-first order see `traverse_bfs`.
102102
103103
This function does not store any results generated by `fun`. Hence, if the user
104104
wants to keep track of such results, they should be stored indirectly (e.g., via
@@ -129,7 +129,7 @@ julia> let
129129
f = Foo(Int[])
130130
axiom = A1(2) + (B1(1) + A1(3), B1(4))
131131
g = Graph(axiom = axiom)
132-
traversedfs(g, fun = f)
132+
traverse_dfs(g, fun = f)
133133
f.vals
134134
end
135135
4-element Vector{Int64}:
@@ -139,11 +139,11 @@ julia> let
139139
3
140140
```
141141
"""
142-
function traversedfs(g::Graph; fun = () -> nothing, ID = root_id(g))
143-
traversedfs(static_graph(g), fun, ID)
142+
function traverse_dfs(g::Graph; fun = () -> nothing, ID = root_id(g))
143+
traverse_dfs(static_graph(g), fun, ID)
144144
end
145145

146-
function traversedfs(g::StaticGraph, fun, ID)
146+
function traverse_dfs(g::StaticGraph, fun, ID)
147147
# Use LIFO stack to keep track of nodes in traversal
148148
node_stack = Int[]
149149
push!(node_stack, ID)
@@ -161,7 +161,7 @@ function traversedfs(g::StaticGraph, fun, ID)
161161
end
162162

163163
"""
164-
traversebfs(g::Graph; fun = () -> nothing, ID = root_id(g))
164+
traverse_bfs(g::Graph; fun = () -> nothing, ID = root_id(g))
165165
166166
Iterates over all the nodes in the graph (breadth-first order, starting at a any
167167
node) and execute for the function `fun` on each node
@@ -180,7 +180,7 @@ This traveral happens in a breadth-first order. That is, all nodes at a given
180180
depth of the the graph are visited first, then moving on to the next level.
181181
Hence, this algorithm should always generate the same result when applied to the
182182
same graph (assuming the user-defined function is not stochastic). For a version
183-
of this function that us depth-first order see `traversedfs`.
183+
of this function that us depth-first order see `traverse_dfs`.
184184
185185
This function does not store any results generated by `fun`. Hence, if the user
186186
wants to keep track of such results, they should be stored indirectly (e.g., via
@@ -211,7 +211,7 @@ julia> let
211211
f = Foo(Int[])
212212
axiom = A1(2) + (B1(1) + A1(3), B1(4))
213213
g = Graph(axiom = axiom)
214-
traversebfs(g, fun = f)
214+
traverse_bfs(g, fun = f)
215215
f.vals
216216
end
217217
4-element Vector{Int64}:
@@ -221,11 +221,11 @@ julia> let
221221
3
222222
```
223223
"""
224-
function traversebfs(g::Graph; fun = () -> nothing, ID = root_id(g))
225-
traversebfs(static_graph(g), fun, ID)
224+
function traverse_bfs(g::Graph; fun = () -> nothing, ID = root_id(g))
225+
traverse_bfs(static_graph(g), fun, ID)
226226
end
227227

228-
function traversebfs(g::StaticGraph, fun, ID)
228+
function traverse_bfs(g::StaticGraph, fun, ID)
229229
# Use LIFO stack to keep track of nodes in traversal
230230
node_stack = Int[]
231231
prepend!(node_stack, ID)

src/Context.jl

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,18 @@ within a rule or query.
3939
"""
4040
has_parent(c::Context) = has_parent(node(c))
4141

42+
isroot(c::Context) = !has_parent(c)
43+
4244
"""
43-
isroot(c::Context)
45+
is_root(c::Context)
46+
4447
4548
Check if a node is the root of the graph (i.e., has no parent) and return `true` or
4649
`false`. Intended to be used within a rule or query.
50+
51+
`isroot` is an alias for `is_root` for compatibility with AbstractTrees.jl
4752
"""
48-
isroot(c::Context) = !has_parent(c)
53+
const is_root = isroot
4954

5055
"""
5156
has_ancestor(c::Context; condition = x -> true, max_level::Int = typemax(Int))
@@ -107,23 +112,23 @@ function has_ancestor(c::Context; condition = x -> true, max_level::Int = typema
107112
end
108113

109114
"""
110-
haschildren(c::Context)
115+
has_children(c::Context)
111116
112117
Check if a node has at least one child and return `true` or `false`. Intended to be used
113118
within a rule or query.
114119
"""
115-
haschildren(c::Context) = haschildren(node(c))
120+
has_children(c::Context) = has_children(node(c))
116121

117122
"""
118-
isleaf(c::Context)
123+
is_leaf(c::Context)
119124
120125
Check if a node is a leaf in the graph (i.e., has no children) and return `true` or
121126
`false`. Intended to be used within a rule or query.
122127
"""
123-
isleaf(c::Context) = !haschildren(c)
128+
is_leaf(c::Context) = !has_children(c)
124129

125130
"""
126-
hasdescendant(c::Context; condition = x -> true, max_level::Int = typemax(Int))
131+
has_descendant(c::Context; condition = x -> true, max_level::Int = typemax(Int))
127132
128133
Check if a node has a descendant that matches the optional condition. Intended to be used
129134
within a rule or query.
@@ -143,7 +148,7 @@ leaves of the graph until a node is found for which `condition` returns `true`.
143148
If no node meets the condition, then it will return `false`. The defaults values
144149
for this function are such that the algorithm always returns `true`
145150
after one step (unless it is applied to a leaf node) in which case it is
146-
equivalent to calling `haschildren` on the node.
151+
equivalent to calling `has_children` on the node.
147152
148153
The number of levels that the algorithm is allowed to traverse is capped by
149154
`max_level` (mostly to avoid excessive computation, though the user may want to
@@ -165,7 +170,7 @@ julia> let
165170
axiom = A1(2) + (B1(1) + A1(3), B1(4))
166171
g = Graph(axiom = axiom)
167172
function qfun(n)
168-
hasdescendant(n, condition = x -> data(x).val == 1)[1]
173+
has_descendant(n, condition = x -> data(x).val == 1)[1]
169174
end
170175
Q1 = Query(A1, condition = qfun)
171176
R1 = apply(g, Q1)
@@ -176,8 +181,8 @@ julia> let
176181
(A1[A1(2)], B1[])
177182
```
178183
"""
179-
function hasdescendant(c::Context; condition = x -> true, max_level::Int = typemax(Int))
180-
out = hasdescendant(node(c), graph(c), condition, max_level, 1)
184+
function has_descendant(c::Context; condition = x -> true, max_level::Int = typemax(Int))
185+
out = has_descendant(node(c), graph(c), condition, max_level, 1)
181186
return out
182187
end
183188

@@ -296,12 +301,21 @@ function children(c::Context)
296301
return out
297302
end
298303

304+
function getdescendant(c::Context; condition = x -> true, max_level::Int = typemax(Int))
305+
desc = getdescendant(node(c), graph(c), condition, max_level, 1)
306+
ismissing(desc) && return missing
307+
out = Context(graph(c), desc)
308+
return out
309+
end
310+
299311
"""
300-
getdescendant(c::Context; condition = x -> true, max_level::Int = typemax(Int))
312+
get_descendant(c::Context; condition = x -> true, max_level::Int = typemax(Int))
301313
302314
Returns the first descendant of a node that matches the `condition`. Intended to
303315
be used within a rule or query.
304316
317+
`getdescendant` is an alias for `get_descendant` for compatibility with AbstractTrees.jl
318+
305319
## Arguments
306320
- `c::Context`: Context associated to a node in a dynamic graph.
307321
@@ -312,8 +326,8 @@ and returns `true` or `false`.
312326
traversing the graph.
313327
314328
## Details
315-
If `hasdescendant()` returns `false` for the same node and `condition`,
316-
`getdescendant()` will return `missing`, otherwise it returns the `Context`
329+
If `has_descendant()` returns `false` for the same node and `condition`,
330+
`get_descendant()` will return `missing`, otherwise it returns the `Context`
317331
associated to the matching node.
318332
319333
## Return
@@ -327,7 +341,7 @@ julia> let
327341
axiom = A1(1) + (B1(1) + A1(3), B1(4))
328342
g = Graph(axiom = axiom)
329343
function qfun(n)
330-
na = getdescendant(n, condition = x -> (data(x).val == 1))
344+
na = get_descendant(n, condition = x -> (data(x).val == 1))
331345
if !ismissing(na)
332346
data(na) isa B1
333347
else
@@ -343,9 +357,4 @@ julia> let
343357
(A1[A1(1)], B1[])
344358
```
345359
"""
346-
function getdescendant(c::Context; condition = x -> true, max_level::Int = typemax(Int))
347-
desc = getdescendant(node(c), graph(c), condition, max_level, 1)
348-
ismissing(desc) && return missing
349-
out = Context(graph(c), desc)
350-
return out
351-
end
360+
const get_descendant = getdescendant

src/Graph.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ end
151151
@forwardgraph length
152152
@forwardgraph nodetypes
153153
@forwardgraph root_id
154-
@forwardgraph getroot
154+
@forwardgraph get_root
155155
@forwardgraph insertion_id
156156
@forwardgraph insertion
157157
@forwardgraph nodes

src/GraphNode.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,17 @@ end
5757
Check if GraphNode has a child or a descendant that fits a condition with optional
5858
recursive search (with maximum depth)
5959
=#
60-
haschildren(n::GraphNode) = !isempty(n.children_id)
61-
isleaf(n::GraphNode) = !haschildren(n)
60+
has_children(n::GraphNode) = !isempty(n.children_id)
61+
is_leaf(n::GraphNode) = !has_children(n)
6262

63-
function hasdescendant(node::GraphNode, g::Graph, condition, maxlevel::Int,
63+
function has_descendant(node::GraphNode, g::Graph, condition, maxlevel::Int,
6464
level::Int = 1)
6565
for child in children(node, g)
6666
if condition(Context(g, child))
6767
return true, level
6868
else
6969
if level <= maxlevel
70-
if hasdescendant(child, g, condition, maxlevel, level + 1)[1]
70+
if has_descendant(child, g, condition, maxlevel, level + 1)[1]
7171
return true, level + 1
7272
end
7373
end

src/PlantGraphs.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ module PlantGraphs
33
# Public API of PlantGraph
44
export Node, Graph, Rule, Query, rewrite!, apply, data, rules, graph,
55
static_graph, data, graph_data, parent,
6-
has_parent, has_ancestor, ancestor, isroot, getroot, haschildren, hasdescendant,
7-
children, getdescendant, isleaf, traverse, traversedfs, traversebfs, draw,
6+
has_parent, has_ancestor, ancestor, is_root, get_root, has_children, has_descendant,
7+
children, get_descendant, is_leaf, traverse, traverse_dfs, traverse_bfs, draw,
88
calculate_resolution, node_label
99

1010
# API for VPL-style graphs

src/StaticGraph.jl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
### This file does not contain public API ###
1+
### This file contains public API ###
22

33
#=
44
Generate unique IDs for Nodes in graphs to avoid ID clashes when merging graphs
@@ -64,6 +64,16 @@ root_id(g::StaticGraph) = g.root
6464
update_root!(g::StaticGraph, ID) = g.root = ID
6565
getroot(g::StaticGraph) = g[root_id(g)]
6666

67+
"""
68+
get_root(g::Graph)
69+
70+
71+
Extract the root node of a graph.
72+
73+
`getroot` is an alias for `get_root` for compatibility with AbstractTrees.jl
74+
"""
75+
const get_root = getroot
76+
6777
#=
6878
Insertion
6979
=#

test/api/test_graph_2.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let
3030

3131
# Cell transfer bottom to top
3232
function transferUp(context)
33-
if haschildren(context)
33+
if has_children(context)
3434
child = first(children(context))
3535
return (true, (child,))
3636
else

test/api/test_graph_3.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ let
4646

4747
# Extract states from the organism using breadth-first traversal
4848
states = Rational{Int}[]
49-
traversebfs(organism, fun = x -> push!(states, x.state))
49+
traverse_bfs(organism, fun = x -> push!(states, x.state))
5050
@test states == [10 // 1 * 5 // 4 * 1 // 3, 10 // 1 * 5 // 4 * 2 // 3]
5151

5252
# Extract states from the organism using depth-first traversal
5353
states = Rational{Int}[]
54-
traversedfs(organism, fun = x -> push!(states, x.state))
54+
traverse_dfs(organism, fun = x -> push!(states, x.state))
5555
@test states == [10 // 1 * 5 // 4 * 1 // 3, 10 // 1 * 5 // 4 * 2 // 3]
5656

5757
# Extract organism using traversal with arbitrary order
@@ -64,12 +64,12 @@ let
6464

6565
# Extract states from the organism using breadth-first traversal
6666
states = Rational{Int}[]
67-
traversebfs(organism, fun = x -> push!(states, x.state))
67+
traverse_bfs(organism, fun = x -> push!(states, x.state))
6868
@test states == [125 // 72, 125 // 36, 125 // 36, 125 // 18]
6969

7070
# Extract states from the organism using depth-first traversal
7171
states = Rational{Int}[]
72-
traversedfs(organism, fun = x -> push!(states, x.state))
72+
traverse_dfs(organism, fun = x -> push!(states, x.state))
7373
@test sort(states) == [125 // 72, 125 // 36, 125 // 36, 125 // 18]
7474

7575
### Growth & death models ###

0 commit comments

Comments
 (0)