Skip to content

Commit 2da1319

Browse files
committed
Add documentation. Close issue #6
1 parent 66053e3 commit 2da1319

File tree

10 files changed

+883
-41
lines changed

10 files changed

+883
-41
lines changed

NEWS.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
We started keeping track of changes in the `NEWS.md` file after version 0.1.0.
44

5-
# PlantGraphs#master
5+
# PlantGraphs 0.1.1 (2025-07-17)
66

77
* Export methods to modify global ID counter (useful when you save the graph and want to
88
restart simulation later on).
99

1010
* Add `apply!` method to fill the nodes that result from a query in-place.
11+
12+
* Document every function and type in the package, including methods and functions not included
13+
in the public API.

src/Algorithms.jl

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
"""
88
traverse(g::Graph; fun = () -> nothing, order = "any", ID = root_id(g))
9+
traverse(g::StaticGraph; fun = () -> nothing, order = "any", ID = root_id(g))
910
1011
Iterates over all the nodes in the graph and execute for the function `fun` on
1112
each node
1213
1314
## Arguments
14-
- `g::Graph`: The graph object that will be traversed.
15+
- `g::Graph` or `g::StaticGraph`: The graph object that will be traversed.
1516
1617
## Keywords
1718
- `fun`: A function or function-like object defined by the user that will be applied to each node.
@@ -77,9 +78,10 @@ julia> traverse(g, fun = f, order = "dfs");
7778
julia> traverse(g, fun = f, order = "bfs");
7879
```
7980
"""
80-
function traverse(g::Graph; fun = () -> nothing, order = "any", ID = root_id(g))
81+
function traverse(g::StaticGraph; fun = () -> nothing, order = "any", ID = root_id(g))
8182
traverse(static_graph(g), fun = fun, order = order, ID = ID)
8283
end
84+
8385
function traverse(g::StaticGraph; fun = () -> nothing, order = "any", ID = root_id(g))
8486
if order == "any"
8587
for val in values(nodes(g))
@@ -93,6 +95,31 @@ function traverse(g::StaticGraph; fun = () -> nothing, order = "any", ID = root_
9395
return nothing
9496
end
9597

98+
99+
100+
"""
101+
traverse_dfs(g::Graph; fun = () -> nothing, ID = root_id(g))
102+
traverse_dfs(g::StaticGraph, fun, ID)
103+
104+
Iterates over all the nodes in the graph in depth-first order and executes the function `fun` on each node.
105+
106+
## Arguments
107+
- `g::Graph` or `g::StaticGraph`: The graph object to be traversed.
108+
109+
## Keywords
110+
- `fun`: A function or function-like object defined by the user that will be applied to each node.
111+
- `ID`: The ID of the node where the traversal should start. By default, traversal starts at the root of the graph.
112+
113+
## Details
114+
Traversal happens in depth-first order: all nodes in a branch are visited until a leaf node
115+
is reached, then the algorithm moves to the next branch. The function provided by the user
116+
should take only one argument corresponding to the data stored in each node. Results
117+
generated by `fun` are not stored by this function; side effects should be managed by the
118+
user.
119+
120+
## Returns
121+
This function returns nothing but `fun` may have side-effects.
122+
"""
96123
function traverse_dfs(g::Graph; fun = () -> nothing, ID = root_id(g))
97124
traverse_dfs(static_graph(g), fun, ID)
98125
end
@@ -114,6 +141,32 @@ function traverse_dfs(g::StaticGraph, fun, ID)
114141
return nothing
115142
end
116143

144+
145+
146+
"""
147+
traverse_bfs(g::Graph; fun = () -> nothing, ID = root_id(g))
148+
traverse_bfs(g::StaticGraph, fun, ID)
149+
150+
Iterates over all the nodes in the graph in breadth-first order and executes the function
151+
`fun` on each node.
152+
153+
## Arguments
154+
- `g::Graph` or `g::StaticGraph`: The graph object to be traversed.
155+
156+
## Keywords
157+
- `fun`: A function or function-like object defined by the user that will be applied to each node.
158+
- `ID`: The ID of the node where the traversal should start. By default, traversal starts at
159+
the root of the graph.
160+
161+
## Details
162+
Traversal happens in breadth-first order: all nodes at a given depth are visited first, then
163+
the algorithm moves to the next level. The function provided by the user should take only
164+
one argument corresponding to the data stored in each node. Results generated by `fun` are
165+
not stored by this function; side effects should be managed by the user.
166+
167+
## Returns
168+
This function returns nothing but `fun` may have side-effects.
169+
"""
117170
function traverse_bfs(g::Graph; fun = () -> nothing, ID = root_id(g))
118171
traverse_bfs(static_graph(g), fun, ID)
119172
end

src/Context.jl

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ Returns the data stored in a node. Intended to be used within a rule or query.
1515
data(c::Context) = data(node(c))
1616

1717
# Return the Graph stored inside the Context object
18+
19+
20+
21+
"""
22+
graph(c::Context)
23+
24+
Returns the dynamic graph stored inside a `Context` object. Intended to be used within a
25+
rule or query.
26+
27+
## Arguments
28+
- `c::Context`: The context associated to a node in a dynamic graph.
29+
30+
## Returns
31+
The `Graph` object contained in the context.
32+
"""
1833
graph(c::Context) = c.graph
1934

2035
"""
@@ -25,6 +40,20 @@ Returns the graph-level variables. Intended to be used within a rule or query.
2540
graph_data(c::Context) = data(graph(c))
2641

2742
# This is needed to traverse graphs within rules
43+
44+
45+
"""
46+
id(c::Context)
47+
48+
Returns the unique identifier (ID) of the node stored in a `Context` object. Intended to be
49+
used within a rule or query.
50+
51+
## Arguments
52+
- `c::Context`: The context associated to a node in a dynamic graph.
53+
54+
## Returns
55+
The integer ID of the node contained in the context.
56+
"""
2857
id(c::Context) = self_id(node(c))
2958

3059
################################################################################
@@ -39,6 +68,20 @@ within a rule or query.
3968
"""
4069
has_parent(c::Context) = has_parent(node(c))
4170

71+
72+
73+
"""
74+
isroot(c::Context)
75+
76+
Check if a node is the root of the graph (i.e., has no parent) and return `true` or `false`.
77+
Intended to be used within a rule or query.
78+
79+
## Arguments
80+
- `c::Context`: The context associated to a node in a dynamic graph.
81+
82+
## Returns
83+
`true` if the node is the root of the graph, otherwise `false`.
84+
"""
4285
isroot(c::Context) = !has_parent(c)
4386

4487
"""
@@ -311,6 +354,27 @@ function children(c::Context)
311354
return out
312355
end
313356

357+
358+
359+
"""
360+
getdescendant(c::Context; condition = x -> true, max_level::Int = typemax(Int))
361+
362+
Returns the first descendant of a node that matches the `condition`. Intended to be used
363+
within a rule or query.
364+
365+
## Arguments
366+
- `c::Context`: Context associated to a node in a dynamic graph.
367+
368+
## Keywords
369+
- `condition`: An user-defined function that takes a `Context` object as input and returns `true` or `false`.
370+
- `max_level::Int`: Maximum number of steps that the algorithm may take when traversing the graph.
371+
372+
## Details
373+
This function traverses the graph from the node associated to `c` towards the leaves of the graph until a node is found for which `condition` returns `true`. If no node meets the condition, then it will return `missing`.
374+
375+
## Returns
376+
Return a `Context` object or `missing`.
377+
"""
314378
function getdescendant(c::Context; condition = x -> true, max_level::Int = typemax(Int))
315379
desc = getdescendant(node(c), graph(c), condition, max_level, 1)
316380
ismissing(desc) && return missing

src/Draw.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,24 @@ Translate a static graph in a DiGraph to be used by GraphMakie. Nodes are
1919
labelled, edges are not. The translation extracts the topological relationships
2020
among nodes and the result of applying `node_label` to each node.
2121
=#
22+
23+
24+
"""
25+
GR.DiGraph(g::StaticGraph)
26+
27+
Translate a static graph into a directed graph (DiGraph) structure for visualization with
28+
GraphMakie. Nodes are labelled using `node_label`, and edges represent parent-child
29+
relationships.
30+
31+
## Arguments
32+
- `g::StaticGraph`: The static graph to be translated into a DiGraph.
33+
34+
## Returns
35+
A tuple `(dg, labels, n)` where:
36+
- `dg`: The constructed DiGraph object.
37+
- `labels`: An array of labels for each node.
38+
- `n`: The number of nodes in the graph.
39+
"""
2240
function GR.DiGraph(g::StaticGraph)
2341
# Create a DiGraph structure
2442
n = length(g)
@@ -46,6 +64,24 @@ function GR.DiGraph(g::StaticGraph)
4664
end
4765

4866
# Forward the DiGraph method of StaticGraph onto Graph
67+
68+
69+
"""
70+
GR.DiGraph(g::Graph)
71+
72+
Translate a dynamic `Graph` object into a directed graph (DiGraph) structure for
73+
visualization with GraphMakie. This method forwards the translation to the underlying
74+
static graph contained in the `Graph` object.
75+
76+
## Arguments
77+
- `g::Graph`: The dynamic graph to be translated into a DiGraph.
78+
79+
## Returns
80+
A tuple `(dg, labels, n)` where:
81+
- `dg`: The constructed DiGraph object.
82+
- `labels`: An array of labels for each node.
83+
- `n`: The number of nodes in the graph.
84+
"""
4985
GR.DiGraph(g::Graph) = GR.DiGraph(g.graph)
5086

5187
"""

src/Graph.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,20 @@ data(g::Graph) = g.data
101101
Returns the StaticGraph stored inside the Graph object (users are not supposed
102102
to interact directly with the StaticGraph)
103103
=#
104+
105+
106+
107+
"""
108+
static_graph(g::Graph)
109+
110+
Return the internal `StaticGraph` stored inside a dynamic `Graph` object.
111+
112+
## Arguments
113+
- `g::Graph`: The dynamic graph from which to retrieve the internal static graph.
114+
115+
## Returns
116+
The `StaticGraph` object contained within the dynamic graph.
117+
"""
104118
static_graph(g::Graph) = g.graph
105119

106120
################################################################################

0 commit comments

Comments
 (0)