00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00027 #ifndef _SEARCH_GRAPH_H
00028 #define _SEARCH_GRAPH_H
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include <iostream>
00038 #include <algorithm>
00039 #include <dag.h>
00040 #include <coconut_config.h>
00041 #include <search_node.h>
00042
00043 using namespace vgtl;
00044 using namespace std;
00045
00046 #include <exception>
00047
00048 class search_graph_exception : public exception
00049 {
00050 public:
00051
00052 char const *const message;
00053
00054 search_graph_exception(char const *m):message(m) {}
00055
00056 virtual char const *what() const throw()
00057 {
00058 return message;
00059 }
00060 };
00061
00062 class search_graph : public dag<search_node*>
00063 {
00064 private:
00065 typedef dag<search_node*> _Base;
00066 typedef search_graph _Self;
00067 typedef search_graph::const_walker search_inspector;
00068 typedef search_graph::walker search_focus;
00069
00070
00071 public:
00072 std::list<walker> focus;
00073 std::list<const_walker> inspector;
00074
00075 search_node *root;
00076 search_inspector inspector_for_root;
00077 ptr<search_node> ptr_ground;
00078 ptr<model> ptr_root_model;
00079 list<search_node*> search_nodes_to_deallocate;
00080 list<delta_id> db_deltas_to_remove;
00081 gptr<vdbl::database> *__dbase;
00082 vdbl::userid __dbuser;
00083
00084 private:
00085 search_node_id __maxid;
00086
00087 void _internal_remove(search_focus& _sf);
00088
00089 public:
00090 search_graph(model& root_model, gptr<vdbl::database>& _db)
00091 : _Base(), ptr_ground(NULL), ptr_root_model(NULL),
00092 __dbase(&_db), __dbuser(0), __maxid(0)
00093 {
00094 ptr_root_model = root_model;
00095 *ground() = new search_node(get_node_id(), __dbuser, *__dbase, snr_virtual);
00096 search_nodes_to_deallocate.push_front(*ground());
00097 ptr_ground = **ground();
00098 root = new full_node(get_node_id(), __dbuser, ptr_root_model, ptr_ground,
00099 *__dbase, snr_root);
00100 search_nodes_to_deallocate.push_front(root);
00101 inspector_for_root = insert(ground(), *root);
00102 }
00103
00104 search_graph(model & root_model, gptr<vdbl::database>& _db,
00105 const std::vector<annotation>& _a)
00106 : _Base(), ptr_ground(NULL), ptr_root_model(NULL),
00107 __dbase(&_db), __dbuser(0), __maxid(0)
00108 {
00109 ptr_root_model = root_model;
00110 *ground() = new search_node(get_node_id(), __dbuser, *__dbase, snr_virtual);
00111 search_nodes_to_deallocate.push_front(*ground());
00112 ptr_ground = **ground();
00113 root = new full_node(get_node_id(), __dbuser, ptr_root_model, ptr_ground,
00114 *__dbase, _a, snr_root);
00115 search_nodes_to_deallocate.push_front(root);
00116 inspector_for_root = insert(ground(), *root);
00117 }
00118
00119 ~search_graph()
00120 {
00121 for(list<search_node*>::iterator i = search_nodes_to_deallocate.begin();
00122 i != search_nodes_to_deallocate.end(); ++i)
00123 delete *i;
00124
00125 vdbl::standard_table * dtable = (vdbl::standard_table *)
00126 (*__dbase)->get_table("deltas", __dbuser);
00127 if(dtable == NULL)
00128 throw "Database inconsistency: Programming Error!";
00129 for(list<delta_id>::iterator i = db_deltas_to_remove.begin();
00130 i != db_deltas_to_remove.end(); ++i)
00131 dtable->remove(*i);
00132 }
00133
00134 search_node_id get_node_id() { return __maxid++; }
00135
00136 search_focus& new_focus(const search_inspector& _si);
00137 void destroy_focus(const search_focus& _sf);
00138 search_focus& set_focus(search_focus& _fc, const search_inspector& _si);
00139
00140 search_inspector& new_inspector(const search_inspector& inspector_to_add);
00141
00142 search_inspector& new_inspector()
00143 {
00144 return new_inspector(ground());
00145 }
00146 void destroy_inspector(const search_inspector& inspector_to_destroy);
00147
00148 work_node extract(const search_inspector& where);
00149 work_node extract(const search_focus& where);
00150 search_inspector& insert(const search_focus& where, const search_node& what);
00151 search_inspector& replace(search_focus& where, const search_node& what);
00152 void remove(search_focus& _sf);
00153 void remove_upwards(search_focus& _sf, bool relaxation_kills);
00154
00155 search_focus& promote(search_focus& _sf);
00156
00157 search_inspector child(search_inspector& n, unsigned int i)
00158 {
00159 if(n.n_children() < i || i < 0)
00160 throw search_graph_exception
00161 ("in search_graph :: child (...) : the index of child is out of range");
00162 return new_inspector(n >> (n.child_begin() + i));
00163 }
00164
00165 search_inspector parent(search_inspector& n, unsigned int i)
00166 {
00167 if(n.n_parents() < i || i < 0)
00168 throw search_graph_exception
00169 ("in search_graph :: parent (...) : the index of parent is out of range");
00170 return new_inspector(n >> (n.parent_begin() + i));
00171 }
00172
00173 };
00174
00175 typedef search_graph::const_walker search_inspector;
00176 typedef search_graph::walker search_focus;
00177
00178 #endif // _SEARCH_GRAPH_H