pub struct BranchStateMachine { /* private fields */ }Expand description
Branch State Machine for trajectory DAGs.
Provides high-level operations for managing branches, including:
- split: Create a new branch from a subtree (DLM port)
- merge: Combine branches back together
- traverse: Navigate between branches
- recover: Find and reactivate “lost” branches
§Design (from DLM)
The state machine maintains:
- A mapping of branch IDs to Branch structs
- The currently active branch
- A history of all branch operations
- Fork points where branching occurred
§Example
let graph = TrajectoryGraph::from_edges(edges);
let mut machine = BranchStateMachine::from_graph(graph);
// Split at a fork point
let result = machine.split(fork_node_id)?;
println!("Created new branch: {}", result.new_branch);
// Traverse to the new branch
machine.traverse(result.new_branch)?;
// Later, merge back
machine.merge(result.new_branch, result.original_branch)?;Implementations§
Source§impl BranchStateMachine
impl BranchStateMachine
Sourcepub fn from_graph(graph: TrajectoryGraph) -> Self
pub fn from_graph(graph: TrajectoryGraph) -> Self
Create a new state machine from a trajectory graph.
Analyzes the graph to identify branch points and creates the initial branch structure.
Sourcepub fn split(&mut self, node_id: NodeId) -> Result<SplitResult, BranchError>
pub fn split(&mut self, node_id: NodeId) -> Result<SplitResult, BranchError>
Split a branch at a given node, creating a new independent branch.
This is the core operation for solving the “lost branch” problem,
ported from DLM’s StateMachine.split().
§Arguments
node_id- The node at which to split
§Returns
SplitResultcontaining the original and new branch IDs
§Errors
NodeNotFoundif the node doesn’t existCannotSplitRootif trying to split at the root
§Example
let result = machine.split(fork_node_id)?;
// The new branch is now independent and can be explored separately
machine.traverse(result.new_branch)?;Sourcepub fn merge(
&mut self,
from_branch: BranchId,
into_branch: BranchId,
) -> Result<MergeResult, BranchError>
pub fn merge( &mut self, from_branch: BranchId, into_branch: BranchId, ) -> Result<MergeResult, BranchError>
Sourcepub fn traverse(&mut self, target_branch: BranchId) -> Result<(), BranchError>
pub fn traverse(&mut self, target_branch: BranchId) -> Result<(), BranchError>
Traverse from current branch to another branch.
Changes the active branch context.
Sourcepub fn archive(
&mut self,
branch_id: BranchId,
reason: Option<String>,
) -> Result<(), BranchError>
pub fn archive( &mut self, branch_id: BranchId, reason: Option<String>, ) -> Result<(), BranchError>
Archive a branch (preserve but mark inactive).
Sourcepub fn get_branch(&self, branch_id: BranchId) -> Option<&Branch>
pub fn get_branch(&self, branch_id: BranchId) -> Option<&Branch>
Get a branch by ID.
Sourcepub fn active_branches(&self) -> impl Iterator<Item = &Branch>
pub fn active_branches(&self) -> impl Iterator<Item = &Branch>
Get all active branches.
Sourcepub fn all_branches(&self) -> impl Iterator<Item = &Branch>
pub fn all_branches(&self) -> impl Iterator<Item = &Branch>
Get all branches (including inactive).
Sourcepub fn branch_count(&self) -> usize
pub fn branch_count(&self) -> usize
Get the number of branches.
Sourcepub fn fork_points(&self) -> impl Iterator<Item = &ForkPoint>
pub fn fork_points(&self) -> impl Iterator<Item = &ForkPoint>
Get all fork points.
Sourcepub fn history(&self) -> &[BranchOperation]
pub fn history(&self) -> &[BranchOperation]
Get the operation history.
Sourcepub fn context(&self) -> &BranchContext
pub fn context(&self) -> &BranchContext
Get the current context.
Sourcepub fn find_branch_for_node(&self, node_id: NodeId) -> Option<BranchId>
pub fn find_branch_for_node(&self, node_id: NodeId) -> Option<BranchId>
Find which branch contains a node.
Sourcepub fn child_branches(&self, branch_id: BranchId) -> Vec<BranchId> ⓘ
pub fn child_branches(&self, branch_id: BranchId) -> Vec<BranchId> ⓘ
Get child branches of a given branch.
Sourcepub fn graph(&self) -> &TrajectoryGraph
pub fn graph(&self) -> &TrajectoryGraph
Get reference to the underlying graph.
Trait Implementations§
Source§impl Clone for BranchStateMachine
impl Clone for BranchStateMachine
Source§fn clone(&self) -> BranchStateMachine
fn clone(&self) -> BranchStateMachine
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for BranchStateMachine
impl RefUnwindSafe for BranchStateMachine
impl Send for BranchStateMachine
impl Sync for BranchStateMachine
impl Unpin for BranchStateMachine
impl UnwindSafe for BranchStateMachine
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
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<F, W, T, D> Deserialize<With<T, W>, D> for F
impl<F, W, T, D> Deserialize<With<T, W>, D> for F
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