-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmod.rs
377 lines (301 loc) · 12 KB
/
mod.rs
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
mod branch;
mod iter;
#[cfg(feature = "serde_support")]
mod serde_support;
pub use branch::*;
pub use iter::*;
#[cfg(feature = "serde_support")]
pub use serde_support::*;
use id_tree::InsertBehavior::*;
use id_tree::*;
use std::iter::once;
/// The ID of some branch node or some branch in a truth tree. This ID is guaranteed to be unique for the lifetime
/// of the process.
///
/// **Serialization of this struct requires the feature `serde_support` to be enabled.**
#[derive(PartialEq, Eq, Debug, Clone, Hash)]
#[cfg_attr(feature = "serde_support", derive(Serialize))]
pub struct TreeId(NodeId);
/// A truth tree generated by the truth tree algorithm.
pub struct TruthTree {
tree: Tree<Branch>,
}
impl<'a> TruthTree {
pub(in crate::validity) fn new(main_branch: Branch) -> Self {
TruthTree {
tree: TreeBuilder::new().with_root(Node::new(main_branch)).build(),
}
}
/// Returns the ID of the root branch of the tree.
pub fn main_trunk_id(&self) -> TreeId {
TreeId(self.tree.root_node_id().unwrap().clone())
}
/// Returns an Iterator over the ancestors' ID's from some branch `branch_id`. Includes `branch_id`
/// as well.
///
/// # Panics
/// Panics if the ID provided does not represent a branch from this truth tree.
pub fn traverse_upwards_branch_ids(&'a self, branch_id: &'a TreeId) -> UpwardsBranchesIdsIter {
UpwardsBranchesIdsIter {
iter: once(&branch_id.0).chain(
self.tree
.ancestor_ids(&branch_id.0)
.expect("invalid branch_id"),
),
}
}
/// Returns an Iterator over the ancestor branches of some branch `branch_id`. Includes `branch_id`
/// as well.
///
/// # Panics
/// Panics if the ID provided does not represent a branch from this truth tree.
pub fn traverse_upwards_branches(&'a self, branch_id: &'a TreeId) -> UpwardsBranchesIter {
UpwardsBranchesIter {
tree: &self.tree,
iter: self.traverse_upwards_branch_ids(&branch_id),
}
}
/// Returns an Iterator over the IDs of the descendant branches of some branch `branch_id`. It implements a
/// pre-order traversal algorithm. It includes `branch_id` as well.
pub fn traverse_downwards_branches_ids(
&'a self,
branch_id: &'a TreeId,
) -> DownwardsBranchesIdsIter {
DownwardsBranchesIdsIter {
iter: IdsIter {
tree: &self.tree,
stack: vec![&branch_id.0],
},
}
}
/// Returns an Iterator over the descendant branches and their IDs of some branch `branch_id`. It implements
/// a pre-order traversal algorithm. It includes `branch_id` as well.
pub fn traverse_downwards_branches(&'a self, branch_id: &'a TreeId) -> DownwardsBranchesIter {
DownwardsBranchesIter {
tree: &self.tree,
iter: self.traverse_downwards_branches_ids(&branch_id),
}
}
/// Returns an Iterator over the direct descendants' IDs of some branch `branch_id`.
pub fn traverse_branch_direct_descendants_ids(
&'a self,
branch_id: &'a TreeId,
) -> BranchDirectDescendantsIdsIter {
BranchDirectDescendantsIdsIter {
iter: self.tree.children_ids(&branch_id.0).unwrap(),
}
}
/// Returns an Iterator over the direct descendants of some branch `branch_id`.
pub fn traverse_branch_direct_descendants(
&'a self,
branch_id: &'a TreeId,
) -> BranchDirectDescendantsIter {
BranchDirectDescendantsIter {
tree: &self.tree,
iter: self.traverse_branch_direct_descendants_ids(&branch_id),
}
}
/// Returns true if the branch has no children, false if it does.
///
/// # Panics
/// Panics if the ID provided does not represent a branch from this truth tree.
pub fn branch_is_last_child(&'a self, branch_id: &'a TreeId) -> bool {
self.tree
.get(&branch_id.0)
.expect("invalid branch_id")
.children()
.is_empty()
}
pub(in crate::validity) fn branch_from_id_mut(&mut self, branch_id: &TreeId) -> &mut Branch {
self.tree
.get_mut(&branch_id.0)
.expect("invalid branch_id")
.data_mut()
}
/// Returns a reference to the `Branch` specified by `branch_id`.
///
/// # Panics
/// Panics if the ID provided does not represent a branch from this truth tree.
pub fn branch_from_id(&self, branch_id: &TreeId) -> &Branch {
self.tree
.get(&branch_id.0)
.expect("invalid branch_id")
.data()
}
pub(in crate::validity) fn append_branch_at(
&mut self,
branch: Branch,
as_child_of_branch_id: &TreeId,
) -> TreeId {
assert!(
!self.branch_from_id(&as_child_of_branch_id).is_closed(),
"attempt to add child to closed branch"
);
TreeId(
self.tree
.insert(Node::new(branch), UnderNode(&as_child_of_branch_id.0))
.expect("invalid branch_id"),
)
}
/// Returns true if there is at least one open branch in the entire tree, false if not.
pub fn is_open(&self) -> bool {
self.traverse_downwards_branches_ids(&self.main_trunk_id())
.filter(|x| self.branch_is_last_child(&x) && !self.branch_from_id(&x).is_closed())
.count()
> 0
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::parser::{SimpleStatementLetter, Statement, Subscript};
static BRANCH_NODE_1: BranchNode = BranchNode {
statement: Statement::Simple(SimpleStatementLetter('A', Subscript(None))),
derived_from: None,
};
static BRANCH_NODE_2: BranchNode = BranchNode {
statement: Statement::Simple(SimpleStatementLetter('B', Subscript(None))),
derived_from: None,
};
static BRANCH_NODE_3: BranchNode = BranchNode {
statement: Statement::Simple(SimpleStatementLetter('C', Subscript(None))),
derived_from: None,
};
#[test]
fn truth_tree_main_trunk_id() {
let truth_tree = TruthTree::new(Branch::new(vec![BRANCH_NODE_1.clone()]));
let root_id = truth_tree.main_trunk_id();
assert_eq!(
root_id,
TreeId(truth_tree.tree.root_node_id().unwrap().clone())
);
}
#[test]
fn truth_tree_traverse_upwards_branch_ids() {
let mut truth_tree = TruthTree::new(Branch::new(vec![BRANCH_NODE_1.clone()]));
let root_id = truth_tree.main_trunk_id();
let child_branch_1_id =
truth_tree.append_branch_at(Branch::new(vec![BRANCH_NODE_2.clone()]), &root_id);
let mut iter = truth_tree.traverse_upwards_branch_ids(&child_branch_1_id);
assert_eq!(iter.next(), Some(TreeId(child_branch_1_id.0.clone())));
assert_eq!(iter.next(), Some(truth_tree.main_trunk_id()));
assert_eq!(iter.next(), None);
}
#[test]
fn truth_tree_traverse_upwards_branches() {
let mut truth_tree = TruthTree::new(Branch::new(vec![BRANCH_NODE_1.clone()]));
let root_id = truth_tree.main_trunk_id();
let child_branch_1_id =
truth_tree.append_branch_at(Branch::new(vec![BRANCH_NODE_2.clone()]), &root_id);
let mut iter = truth_tree.traverse_upwards_branches(&child_branch_1_id);
let (_, first_branch) = iter.next().unwrap();
assert_eq!(
first_branch
.statement_from_id(&first_branch.statement_ids().next().unwrap())
.statement,
BRANCH_NODE_2.statement
);
let (_, second_branch) = iter.next().unwrap();
assert_eq!(
second_branch
.statement_from_id(&second_branch.statement_ids().next().unwrap())
.statement,
BRANCH_NODE_1.statement
);
match iter.next() {
Some(_) => assert!(false),
_ => {}
}
}
#[test]
fn truth_tree_traverse_downwards_branch_ids() {
let mut truth_tree = TruthTree::new(Branch::new(vec![BRANCH_NODE_1.clone()]));
let root_id = truth_tree.main_trunk_id();
let child_branch_1_id =
truth_tree.append_branch_at(Branch::new(vec![BRANCH_NODE_2.clone()]), &root_id);
let child_branch_2_id = truth_tree
.append_branch_at(Branch::new(vec![BRANCH_NODE_3.clone()]), &child_branch_1_id);
let mut iter = truth_tree.traverse_downwards_branches_ids(&root_id);
assert_eq!(iter.next(), Some(root_id.clone()));
assert_eq!(iter.next(), Some(child_branch_1_id));
assert_eq!(iter.next(), Some(child_branch_2_id));
assert_eq!(iter.next(), None);
}
#[test]
fn truth_tree_traverse_branch_direct_descendants_ids() {
let mut truth_tree = TruthTree::new(Branch::new(vec![BRANCH_NODE_1.clone()]));
let root_id = truth_tree.main_trunk_id();
let child_branch_1_id =
truth_tree.append_branch_at(Branch::new(vec![BRANCH_NODE_2.clone()]), &root_id);
truth_tree.append_branch_at(Branch::new(vec![BRANCH_NODE_3.clone()]), &child_branch_1_id);
let mut iter = truth_tree.traverse_branch_direct_descendants_ids(&root_id);
assert_eq!(iter.next(), Some(child_branch_1_id));
assert_eq!(iter.next(), None);
}
#[test]
fn truth_tree_branch_is_last_child() {
let mut truth_tree = TruthTree::new(Branch::new(vec![BRANCH_NODE_1.clone()]));
let root_id = truth_tree.main_trunk_id();
let child_branch_1_id =
truth_tree.append_branch_at(Branch::new(vec![BRANCH_NODE_2.clone()]), &root_id);
assert!(
!truth_tree.branch_is_last_child(&root_id),
"root is not last child"
);
assert!(
truth_tree.branch_is_last_child(&child_branch_1_id),
"child_branch_1 is last child"
);
}
#[test]
fn truth_tree_branch_from_id_mut() {
let mut truth_tree = TruthTree::new(Branch::new(vec![BRANCH_NODE_1.clone()]));
let root_id = truth_tree.main_trunk_id();
let branch = truth_tree.branch_from_id_mut(&root_id);
branch.append_statement(BRANCH_NODE_2.clone());
let mut statements_iter = branch.statement_ids();
assert_eq!(
branch
.statement_from_id(&statements_iter.next().unwrap())
.statement,
BRANCH_NODE_1.statement
);
assert_eq!(
branch
.statement_from_id(&statements_iter.next().unwrap())
.statement,
BRANCH_NODE_2.statement
);
}
#[test]
fn truth_tree_branch_from_id() {
let truth_tree = TruthTree::new(Branch::new(vec![BRANCH_NODE_1.clone()]));
let root_id = truth_tree.main_trunk_id();
let branch = truth_tree.branch_from_id(&root_id);
let first_statement_id = branch.statement_ids().next().unwrap();
assert_eq!(
branch.statement_from_id(&first_statement_id).statement,
BRANCH_NODE_1.statement
);
}
#[test]
fn truth_tree_append_branch_at() {
let mut truth_tree = TruthTree::new(Branch::new(vec![BRANCH_NODE_1.clone()]));
let root_id = truth_tree.main_trunk_id();
let child_branch_1_id =
truth_tree.append_branch_at(Branch::new(vec![BRANCH_NODE_2.clone()]), &root_id);
let mut iter = truth_tree.traverse_downwards_branches_ids(&root_id);
assert_eq!(iter.next(), Some(TreeId(root_id.0.clone())));
assert_eq!(iter.next(), Some(TreeId(child_branch_1_id.0.clone())));
assert_eq!(iter.next(), None);
}
#[test]
fn truth_tree_is_open() {
let mut truth_tree = TruthTree::new(Branch::new(vec![BRANCH_NODE_1.clone()]));
assert!(truth_tree.is_open(), "returned not open but tree is open");
truth_tree
.branch_from_id_mut(&truth_tree.main_trunk_id())
.close();
assert!(!truth_tree.is_open(), "returned open but tree is not open");
}
}