1 | // ================================================================ // |
---|
2 | // // |
---|
3 | // File : ap_main_type.hxx // |
---|
4 | // Purpose : // |
---|
5 | // // |
---|
6 | // Institute of Microbiology (Technical University Munich) // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // ================================================================ // |
---|
10 | |
---|
11 | #ifndef AP_MAIN_TYPE_HXX |
---|
12 | #define AP_MAIN_TYPE_HXX |
---|
13 | |
---|
14 | #ifndef AP_BUFFER_HXX |
---|
15 | #include "AP_buffer.hxx" |
---|
16 | #endif |
---|
17 | #ifndef PARS_DTREE_HXX |
---|
18 | #include "pars_dtree.hxx" |
---|
19 | #endif |
---|
20 | |
---|
21 | class UserFrame { |
---|
22 | Level frameLevel; |
---|
23 | // @@@ store more information here (e.g. comment, parsimony value, timestamp) |
---|
24 | public: |
---|
25 | UserFrame(Level level) : frameLevel(level) {} |
---|
26 | Level get_level() const { return frameLevel; } |
---|
27 | }; |
---|
28 | |
---|
29 | typedef AP_STACK<UserFrame> UserFrameStack; |
---|
30 | |
---|
31 | class AP_main : virtual Noncopyable { |
---|
32 | NodeStack *currFrame; |
---|
33 | FrameStack frames; |
---|
34 | UserFrameStack user_frames; |
---|
35 | Level frameLevel; |
---|
36 | AWT_graphic_parsimony *agt; // provides access to tree! |
---|
37 | StackFrameData *frameData; // saved/restored by remember/revert |
---|
38 | GBDATA *gb_main; |
---|
39 | |
---|
40 | void push_nodes_changed_since(Level wanted_frameLevel); |
---|
41 | void rollback_to(Level wanted_frameLevel); |
---|
42 | |
---|
43 | public: |
---|
44 | AP_main() |
---|
45 | : currFrame(NULp), |
---|
46 | frameLevel(0), |
---|
47 | agt(NULp), |
---|
48 | frameData(NULp), |
---|
49 | gb_main(NULp) |
---|
50 | {} |
---|
51 | ~AP_main() { |
---|
52 | if (gb_main) GB_close(gb_main); |
---|
53 | ap_assert(!frameData); |
---|
54 | delete currFrame; |
---|
55 | } |
---|
56 | |
---|
57 | void set_tree_root(AWT_graphic_parsimony *agt_); |
---|
58 | AWT_graphic_parsimony *get_graphic_tree() { return agt; } |
---|
59 | AP_pars_root *get_tree_root() const { return agt->get_tree_root(); } |
---|
60 | |
---|
61 | DEFINE_READ_ACCESSORS(AP_tree_nlen*, get_root_node, agt->get_root_node()); |
---|
62 | |
---|
63 | GBDATA *get_gb_main() const { |
---|
64 | ap_assert(gb_main); // you need to call open() before you can use get_gb_main() |
---|
65 | return gb_main; |
---|
66 | } |
---|
67 | const char *get_aliname() const; |
---|
68 | size_t get_user_push_counter() const { return user_frames.count_elements(); } |
---|
69 | Level get_frameLevel() const { return frameLevel; } |
---|
70 | |
---|
71 | GB_ERROR open(const char *db_server); |
---|
72 | |
---|
73 | bool push_node(AP_tree_nlen *node, AP_STACK_MODE); |
---|
74 | |
---|
75 | void remember_user_state(); |
---|
76 | void revert_user_state(); |
---|
77 | // @@@ add accept_user_state (#632) |
---|
78 | |
---|
79 | void remember(); |
---|
80 | void revert(); |
---|
81 | void accept(); |
---|
82 | void accept_all() { while (currFrame) accept(); } |
---|
83 | |
---|
84 | #if defined(UNIT_TESTS) |
---|
85 | void remember_whole_tree(); // dont use in production |
---|
86 | #endif |
---|
87 | |
---|
88 | void accept_if(bool cond) { if (cond) accept(); else revert(); } |
---|
89 | void revert_if(bool cond) { accept_if(!cond); } |
---|
90 | |
---|
91 | bool remember_and_rollback_to(Level wanted_frameLevel) { |
---|
92 | if (wanted_frameLevel>=frameLevel) { |
---|
93 | return false; |
---|
94 | } |
---|
95 | remember(); |
---|
96 | push_nodes_changed_since(wanted_frameLevel); |
---|
97 | rollback_to(wanted_frameLevel); |
---|
98 | return true; |
---|
99 | } |
---|
100 | bool remember_and_rollback_to_previous() { |
---|
101 | /*! tree is modified into the same (or equiv) state as it would be done by calling pop(), |
---|
102 | * but the current state is pushed onto the stack. |
---|
103 | * Calling pop() afterwards will undo this operation. |
---|
104 | */ |
---|
105 | return remember_and_rollback_to(frameLevel-1); |
---|
106 | } |
---|
107 | |
---|
108 | #if defined(PROVIDE_PRINT) |
---|
109 | void print(std::ostream& out); |
---|
110 | void print2file(const char *file_in_ARBHOME); |
---|
111 | void dump2file(const char *name) { |
---|
112 | static int counter = 0; |
---|
113 | |
---|
114 | if (counter == 0) { |
---|
115 | system("rm $ARBHOME/[0-9][0-9]_*.log"); |
---|
116 | } |
---|
117 | |
---|
118 | char *numbered_name = GBS_global_string_copy("%02i_%s.log", ++counter, name); |
---|
119 | print2file(numbered_name); |
---|
120 | free(numbered_name); |
---|
121 | } |
---|
122 | |
---|
123 | #endif |
---|
124 | |
---|
125 | #if defined(ASSERTION_USED) || defined(UNIT_TESTS) |
---|
126 | Validity revert_will_produce_valid_tree() { |
---|
127 | ASSERT_VALID_TREE(get_root_node()); |
---|
128 | ASSERT_RESULT(bool, true, remember_and_rollback_to_previous()); // otherwise stack is empty |
---|
129 | Validity valid(tree_is_valid(get_root_node(), false)); |
---|
130 | revert(); // undo remember_and_rollback_to_previous |
---|
131 | ASSERT_VALID_TREE(get_root_node()); |
---|
132 | return valid; |
---|
133 | } |
---|
134 | Validity all_available_reverts_will_produce_valid_trees() { |
---|
135 | ASSERT_VALID_TREE(get_root_node()); |
---|
136 | Level pops_avail = get_frameLevel(); |
---|
137 | Validity valid; |
---|
138 | for (Level rollback = 1; rollback<=pops_avail && valid; ++rollback) { |
---|
139 | Level wanted_level = pops_avail-rollback; |
---|
140 | ASSERT_RESULT(bool, true, remember_and_rollback_to(wanted_level)); // otherwise stack is empty |
---|
141 | |
---|
142 | // dump2file(GBS_global_string("after_remember_and_rollback_to_%lu", wanted_level)); |
---|
143 | valid = Validity(tree_is_valid(get_root_node(), false)); |
---|
144 | |
---|
145 | revert(); // undo remember_and_rollback_to |
---|
146 | // dump2file("after_pop_to_undo__remember_and_rollback_to"); |
---|
147 | |
---|
148 | ASSERT_VALID_TREE(get_root_node()); |
---|
149 | } |
---|
150 | return valid; |
---|
151 | } |
---|
152 | #endif |
---|
153 | |
---|
154 | inline AP_tree_nlen *makeNode(AP_pars_root *proot); |
---|
155 | inline AP_tree_edge *makeEdge(AP_tree_nlen *n1, AP_tree_nlen *n2); |
---|
156 | inline void destroyNode(AP_tree_nlen *node); |
---|
157 | inline void destroyEdge(AP_tree_edge *edge); |
---|
158 | }; |
---|
159 | |
---|
160 | #else |
---|
161 | #error ap_main_type.hxx included twice |
---|
162 | #endif // AP_MAIN_TYPE_HXX |
---|
163 | |
---|