Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

search_graph.h

Go to the documentation of this file.
00001 // Search Graph implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2003 Hermann Schichl, Evgueni Petrov, Brice Pajot
00004 //
00005 // This file is part of the COCONUT API.  This library
00006 // is free software; you can redistribute it and/or modify it under the
00007 // terms of the Library GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // Library GNU General Public License for more details.
00015 
00016 // As a special exception, you may use this file as part of a free software
00017 // library without restriction.  Specifically, if other files instantiate
00018 // templates or use macros or inline functions from this file, or you compile
00019 // this file and link it with other files to produce an executable, this
00020 // file does not by itself cause the resulting executable to be covered by
00021 // the Library GNU General Public License.  This exception does not however
00022 // invalidate any other reasons why the executable file might be covered by
00023 // the Library GNU General Public License.
00024 
00027 #ifndef _SEARCH_GRAPH_H
00028 #define _SEARCH_GRAPH_H
00029 
00030 // FIXED by IRIN as of 08/11/2002
00031 // + re-ordered and/or completed some initialization lists
00032 // + made some methods of search_node, full_node etc. "const"
00033 // + suppressed some empty declarations
00034 // + made the destructors of search_node & Co. "virtual" 
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   // private:
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; // the actual initialization of ptr_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();    // the actual initialization of ptr_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;  // the actual initialization of ptr_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();    // the actual initialization of ptr_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

Generated on Tue Nov 4 01:57:58 2003 for COCONUT API by doxygen1.2.18