pub struct NodeRegistry { /* private fields */ }Expand description
Registry for behavior node types.
The NodeRegistry provides a factory pattern for creating behavior nodes from
string identifiers and JSON configuration. This enables loading behavior trees
from JSON files without hardcoding node types.
§Thread Safety
The registry is thread-safe and can be shared across multiple threads using Arc.
Internally it uses RwLock to allow concurrent reads while serializing writes.
§Example
use mecha10_behavior_runtime::prelude::*;
use mecha10_behavior_runtime::NodeRegistry;
use serde_json::json;
// Create a registry
let mut registry = NodeRegistry::new();
// Register a node type
registry.register("my_behavior", |config| {
Ok(Box::new(MyBehavior))
});
// Create an instance from the registry
let node = registry.create("my_behavior", json!({})).unwrap();Implementations§
Source§impl NodeRegistry
impl NodeRegistry
Sourcepub fn register<F>(&mut self, node_type: impl Into<String>, factory: F)
pub fn register<F>(&mut self, node_type: impl Into<String>, factory: F)
Register a node type with a factory function.
The factory function will be called each time a node of this type is created. It receives the JSON configuration and should return a boxed behavior node.
§Arguments
node_type- String identifier for this node type (e.g., “move_to_goal”)factory- Function that creates instances of this node type
§Example
use mecha10_behavior_runtime::NodeRegistry;
use serde_json::json;
let mut registry = NodeRegistry::new();
registry.register("move", |config| {
let speed = config.get("speed")
.and_then(|v| v.as_f64())
.unwrap_or(1.0);
Ok(Box::new(MoveBehavior { speed }))
});Sourcepub fn create(&self, node_type: &str, config: Value) -> Result<BoxedBehavior>
pub fn create(&self, node_type: &str, config: Value) -> Result<BoxedBehavior>
Create a node instance from a registered type.
This looks up the factory function for the given node type and calls it with the provided configuration to create a new instance.
§Arguments
node_type- The type identifier that was used during registrationconfig- JSON configuration to pass to the factory function
§Returns
A boxed behavior node instance, or an error if:
- The node type is not registered
- The factory function returns an error
§Example
use mecha10_behavior_runtime::NodeRegistry;
use serde_json::json;
let config = json!({ "speed": 2.0 });
let node = registry.create("move", config).unwrap();Sourcepub fn has_node(&self, node_type: &str) -> bool
pub fn has_node(&self, node_type: &str) -> bool
Check if a node type is registered.
§Arguments
node_type- The type identifier to check
§Returns
true if the node type is registered, false otherwise
§Example
use mecha10_behavior_runtime::NodeRegistry;
let mut registry = NodeRegistry::new();
registry.register("move", |_| Ok(Box::new(mecha10_behavior_runtime::SequenceNode::new(vec![]))));
assert!(registry.has_node("move"));
assert!(!registry.has_node("unknown"));Sourcepub fn contains(&self, node_type: &str) -> bool
pub fn contains(&self, node_type: &str) -> bool
Check if a node type is registered.
§Example
use mecha10_behavior_runtime::NodeRegistry;
let mut registry = NodeRegistry::new();
registry.register("move", |_| Ok(Box::new(mecha10_behavior_runtime::SequenceNode::new(vec![]))));
assert!(registry.contains("move"));
assert!(!registry.contains("unknown"));Sourcepub fn registered_types(&self) -> Vec<String>
pub fn registered_types(&self) -> Vec<String>
Get a list of all registered node types.
§Example
use mecha10_behavior_runtime::NodeRegistry;
let mut registry = NodeRegistry::new();
registry.register("move", |_| Ok(Box::new(mecha10_behavior_runtime::SequenceNode::new(vec![]))));
registry.register("rotate", |_| Ok(Box::new(mecha10_behavior_runtime::SequenceNode::new(vec![]))));
let types = registry.registered_types();
assert!(types.contains(&"move".to_string()));
assert!(types.contains(&"rotate".to_string()));Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the number of registered node types.
§Example
use mecha10_behavior_runtime::NodeRegistry;
let mut registry = NodeRegistry::new();
assert_eq!(registry.len(), 0);
registry.register("move", |_| Ok(Box::new(mecha10_behavior_runtime::SequenceNode::new(vec![]))));
assert_eq!(registry.len(), 1);Trait Implementations§
Source§impl Clone for NodeRegistry
impl Clone for NodeRegistry
Source§fn clone(&self) -> NodeRegistry
fn clone(&self) -> NodeRegistry
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for NodeRegistry
impl Debug for NodeRegistry
Auto Trait Implementations§
impl Freeze for NodeRegistry
impl RefUnwindSafe for NodeRegistry
impl Send for NodeRegistry
impl Sync for NodeRegistry
impl Unpin for NodeRegistry
impl UnwindSafe for NodeRegistry
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more