Skip to content

Commit

Permalink
Hide all the visualization code behind a visualize feature flag.
Browse files Browse the repository at this point in the history
This also makes petgraph an optional dependency.
  • Loading branch information
andriyDev committed Jan 22, 2025
1 parent d3a1f84 commit c23a0a9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
5 changes: 4 additions & 1 deletion bonsai/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ name = "bonsai_bt"
path = "src/lib.rs"

[dependencies]
petgraph = "0.6.2"
petgraph = { version = "0.6.2", optional = true }
serde = { version = "1.0.137", features = ["derive"], optional = true }

[features]
visualize = ["dep:petgraph"]

[dev-dependencies]
serde_json = { version = "1.0.81" }

Expand Down
12 changes: 8 additions & 4 deletions bonsai/src/bt.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::fmt::Debug;

use crate::visualizer::NodeType;
use crate::{state::State, ActionArgs, Behavior, Status, UpdateEvent};
use petgraph::dot::{Config, Dot};
use petgraph::Graph;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -79,6 +76,7 @@ impl<A: Clone, B> BT<A, B> {
}
}

#[cfg(feature = "visualize")]
impl<A: Clone + Debug, B: Debug> BT<A, B> {
/// Compile the behavior tree into a [graphviz](https://graphviz.org/) compatible [DiGraph](https://docs.rs/petgraph/latest/petgraph/graph/type.DiGraph.html).
///
Expand Down Expand Up @@ -113,7 +111,13 @@ impl<A: Clone + Debug, B: Debug> BT<A, B> {
self.get_graphviz_with_graph_instance().0
}

pub(crate) fn get_graphviz_with_graph_instance(&mut self) -> (String, Graph<NodeType<A>, u32>) {
pub(crate) fn get_graphviz_with_graph_instance(
&mut self,
) -> (String, petgraph::Graph<crate::visualizer::NodeType<A>, u32>) {
use crate::visualizer::NodeType;
use petgraph::dot::{Config, Dot};
use petgraph::Graph;

let behavior = self.initial_behavior.to_owned();

let mut graph = Graph::<NodeType<A>, u32, petgraph::Directed>::new();
Expand Down
4 changes: 3 additions & 1 deletion bonsai/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,7 @@ mod event;
mod sequence;
mod state;
mod status;
mod visualizer;
mod when_all;

#[cfg(feature = "visualize")]
mod visualizer;
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ rust-version = "1.56.0"
version = "0.1.0"

[dependencies]
bonsai-bt = { path = "../bonsai" , features = ["serde"]}
bonsai-bt = { path = "../bonsai", features = ["serde", "visualize"] }
futures = "0.3.24"
tokio = { version = "1.21.1", features = [
"rt-multi-thread",
Expand Down

0 comments on commit c23a0a9

Please sign in to comment.