1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::{
	collections::HashMap,
	sync::{PoisonError, RwLockWriteGuard},
};

use crate::{
	cluster_objects::kinematic_data_errors::AddTransmissionError,
	joint::{Joint, JointBuilder},
	link::{builder::LinkBuilder, Link},
	material::{data::MaterialData, Material},
	transmission::{
		transmission_builder_state::{WithActuator, WithJoints},
		Transmission, TransmissionBuilder,
	},
	ArcLock, Chained, WeakLock,
};

pub mod kinematic_data_errors;
pub(crate) mod kinematic_data_tree;
mod kinematic_tree;
mod robot;

pub use kinematic_tree::KinematicTree;
pub use robot::Robot;

type PoisonWriteIndexError<'a, K, V> = PoisonError<RwLockWriteGuard<'a, HashMap<K, V>>>;

pub trait KinematicInterface: Sized {
	// NOTE: THIS IS NOT FINAL;

	/// Returns the root link of the Kinematic Tree
	///
	/// # Example
	/// ```
	/// # use robot_description_builder::{KinematicInterface, Link, JointBuilder, JointType, linkbuilding::LinkBuilder};
	/// let tree = Link::builder("the root link").build_tree();
	///
	/// /// This is equivalent to `get_root_link` in this case, since this is a new tree/Link.
	/// tree.get_newest_link().try_write().unwrap().try_attach_child(
	///     LinkBuilder::new("his one and only child"),
	///     JointBuilder::new("just a joint", JointType::Fixed)
	/// ).unwrap();
	///
	/// assert_eq!(tree.get_root_link().try_read().unwrap().name(), "the root link")
	/// ```
	fn get_root_link(&self) -> ArcLock<Link>;

	/// Returns the newest link of the Kinematic Tree
	///
	/// # Example
	/// ```
	/// # use robot_description_builder::{KinematicInterface, Link, JointBuilder, JointType, linkbuilding::LinkBuilder};
	/// let tree = Link::builder("the root link").build_tree();
	///
	/// assert_eq!(tree.get_newest_link().try_read().unwrap().name(), "the root link");
	///
	/// tree.get_newest_link().try_write().unwrap().try_attach_child(
	///     LinkBuilder::new("his one and only child"),
	///     JointBuilder::new("just a joint", JointType::Fixed)
	/// ).unwrap();
	///
	/// assert_eq!(tree.get_newest_link().try_read().unwrap().name(), "his one and only child");
	///
	/// let long_sub_tree = LinkBuilder::new("the other child").build_tree();
	///
	/// long_sub_tree.get_newest_link().try_write().unwrap().try_attach_child(
	///     Link::builder("the latest child"),
	///     JointBuilder::new("second joint", JointType::Fixed)
	/// ).unwrap();
	///
	/// tree.get_root_link().try_write().unwrap().try_attach_child(long_sub_tree,
	///     JointBuilder::new("third joint", JointType::Fixed)
	/// ).unwrap();
	///
	/// assert_eq!(tree.get_newest_link().try_read().unwrap().name(), "the latest child");
	/// ```
	fn get_newest_link(&self) -> ArcLock<Link>;

	fn get_links(&self) -> ArcLock<HashMap<String, WeakLock<Link>>>;
	fn get_joints(&self) -> ArcLock<HashMap<String, WeakLock<Joint>>>;
	/// FIXME: This not Ok end-user should not interact wiht MaterialData
	fn get_materials(&self) -> ArcLock<HashMap<String, ArcLock<MaterialData>>>;
	fn get_transmissions(&self) -> ArcLock<HashMap<String, ArcLock<Transmission>>>;

	fn get_link(&self, name: &str) -> Option<ArcLock<Link>>;
	fn get_joint(&self, name: &str) -> Option<ArcLock<Joint>>;
	fn get_material(&self, name: &str) -> Option<Material>;
	fn get_transmission(&self, name: &str) -> Option<ArcLock<Transmission>>;

	/// TODO: NOT FINAL
	/// TODO: Maybe remove rcrefcell from transmission parameter
	fn try_add_transmission(
		&self,
		transmission: TransmissionBuilder<WithJoints, WithActuator>,
	) -> Result<(), AddTransmissionError>;

	/// Cleans up orphaned/broken `Link` entries from the `links` HashMap.
	///
	/// This mostly happens automatically, but is exposed for use in other methods.
	///
	/// TODO: DOCTEST/EXAMPLE
	fn purge_links(&self) -> Result<(), PoisonWriteIndexError<String, WeakLock<Link>>>;

	/// Cleans up orphaned/broken `Joint` entries from the `joints` HashMap.
	///
	/// This mostly happens automatically, but is exposed for use in other methods.
	///
	/// TODO: DOCTEST/EXAMPLE
	fn purge_joints(&self) -> Result<(), PoisonWriteIndexError<String, WeakLock<Joint>>>;

	/// Cleans up orphaned/unused `Material` entries from `material_index` HashMap
	fn purge_materials(&self) -> Result<(), PoisonWriteIndexError<String, ArcLock<MaterialData>>>;

	/// Cleans up orphaned/broken `Transmission` entries from the `transmissions` HashMap
	fn purge_transmissions(
		&self,
	) -> Result<(), PoisonWriteIndexError<String, ArcLock<Transmission>>>;

	/// TODO: DOCS
	///
	/// NOTE: after yanking the joints parent link is the `newest_link`
	fn yank_link(&self, name: &str) -> Option<Chained<LinkBuilder>> {
		let builder = self
			.get_link(name)
			.map(|link| link.read().unwrap().yank()) // FIXME: Is unwrap ok here?
			.map(Chained);
		self.purge_joints().unwrap(); // FIXME: Is unwrap ok here?
		self.purge_links().unwrap(); // FIXME: Is unwrap ok here?
		builder
	}

	/// Cosumes the `KinematicInterface` implementor and creates a `Chained<LinkBuilder>` to rebuild it.
	///
	/// This has the same result as yanking the `root_link`, with the additional effect that the current tree is consumed.
	///
	/// # Example
	///
	/// ```
	/// # use robot_description_builder::{prelude::*, Link};
	///
	/// let builder = Link::builder("root-link");
	///
	/// assert_eq!(*builder.clone().build_tree().yank_root(), builder);
	///
	/// /// It is equivalent to yanking the "root_link"
	/// assert_eq!(builder.clone().build_tree().yank_root(), builder.build_tree().yank_link("root-link").unwrap())
	/// ```
	fn yank_root(self) -> Chained<LinkBuilder> {
		let builder = self.get_root_link().read().unwrap().yank();
		Chained(builder)
	}

	fn yank_joint(&self, name: &str) -> Option<Chained<JointBuilder>> {
		let builder = self
			.get_joint(name)
			.map(|joint| joint.read().unwrap().yank()) // FIXME: Is unwrap ok here?
			.map(Chained);
		self.purge_joints().unwrap(); // FIXME: Is unwrap ok here?
		self.purge_links().unwrap(); // FIXME: Is unwrap ok here?
		builder
	}

	// TODO: or a rebuild?
}