WoMax Documentation

# Oracle_learn.hpp

Go to the documentation of this file.

    00001 /*-------------------------------------
    00002  * Oracle_learn.hpp
    00003  * Created on 17/03/09 by BenCello
    00004  *-------------------------------------*/
    00005
    00007
    00008 #ifndef __ORACLE_LEARN_HPP__
    00009 #define __ORACLE_LEARN_HPP__
    00010
    00011 #include <vector>
    00012 using namespace std;
    00013
    00014 #include "Oracle_classes.hpp"
    00015 #include "Oracle_data.hpp"
    00016 #include "Oracle_label.hpp"
    00017
    00019 // Prototypes //
    00021
    00025 class O_learner
    00026 {
    00027 protected:
    00029         O_oracle * oracle;
    00031         O_data * data;
    00032 public:
    00034
    00035
    00036         O_learner();
    00038         O_learner(O_oracle &, O_data &);
    00040         ~O_learner(){};
    00042
    00044
    00045
    00046         O_oracle * get_oracle();
    00048         void set_oracle(O_oracle &);
    00050         O_data * get_data();
    00052         void set_data(O_data &);
    00054
    00056
    00057
    00058         template<class O_DataType>
    00059         int add(O_DataType &);
    00061         int lrs(O_state &, O_state &);
    00063         template<class O_DataType>
    00064         int search_trans(O_state &, O_DataType &);
    00066 };
    00067
    00069 // Functions //
    00071
    00073 O_learner::O_learner()
    00074 {
    00075         oracle = NULL;
    00076         data = NULL;
    00077 }
    00078
    00079 O_learner::O_learner(O_oracle & oraclin, O_data & datain)
    00080 {
    00081         oracle = &oraclin;
    00082         data = &datain;
    00083 }
    00084
    00085 O_oracle * O_learner::get_oracle()
    00086 {
    00087         return oracle;
    00088 }
    00089
    00090 void O_learner::set_oracle(O_oracle & oraclin)
    00091 {
    00092         oracle = &oraclin;
    00093 }
    00094
    00095 O_data * O_learner::get_data()
    00096 {
    00097         return data;
    00098 }
    00099
    00100 void O_learner::set_data(O_data & dataIn)
    00101 {
    00102         data = &dataIn;
    00103 }
    00104
    00105 // build
    00107 template<class O_DataType>
    00108 int O_learner::add(O_DataType & dataIn)
    00109 {
    00110         // initialisation if needed
    00111         if (oracle->state_vect.size()<1)
    00112         {
    00113                 oracle->start();
    00114                 data->start<O_DataType>();
    00115         }
    00116
    00117         int j,k,l;
    00118         int newstatenb;
    00119
    00120         O_state * O_it = oracle->state_vect.back(); // old last
    00121         newstatenb = O_it->get_statenb()+1; // new number
    00122
    00123         // create new data
    00124         O_DataType * newdata = new O_DataType(dataIn);
    00125         ((O_label*)newdata)->set_statenb(newstatenb);
    00126         data->state_vect.push_back((O_label*)newdata); // add
    00127         data->size = data->state_vect.size(); // update size
    00128         data->add_state(*newdata); // add state to hashtable
    00129         data->add_date(*newdata); // add date to hashtable
    00130
    00131         // create new state
    00132         O_state * newstate = new O_state(newstatenb);
    00133         oracle->state_vect.push_back(newstate); // add
    00134         oracle->size = oracle->state_vect.size(); // update size
    00135
    00136         // basic transition
    00137         O_it->add_trans(newstate);
    00138
    00139         // suffix algorithm
    00140         j = O_it->get_statenb();
    00141         if(O_it->get_suffix().first)
    00142                 k = O_it->get_suffix().first->get_statenb();
    00143         else
    00144                 k = -1;
    00145         while (k>=0 && search_trans(*oracle->state_vect[k], *newdata)==-1)
    00146         {
    00147                 oracle->state_vect[k]->add_trans(newstate);
    00148                 j = k;
    00149                 if(oracle->state_vect[k]->get_suffix().first)
    00150                         k = oracle->state_vect[k]->get_suffix().first->get_statenb();
    00151                 else
    00152                         k = -1;
    00153         }
    00154         if (k==-1)
    00155                 l = 0;
    00156         else
    00157                 l = search_trans(*oracle->state_vect[k], *newdata);
    00158
    00159         int lrsnew = lrs(*(*oracle)[j],*(*oracle)[l]); // lrs
    00160         oracle->state_vect.back()->set_suffix((*oracle)[l],lrsnew);
    00161
    00162         // reversed suffix
    00163         pair<O_state*,int> * rsuff = new pair<O_state*,int>(newstate,lrsnew);
    00164         oracle->state_vect[l]->add_rsuff(*rsuff);
    00165
    00166         return (newstatenb);
    00167 }
    00168
    00170 template<class O_DataType>
    00171 int O_learner::search_trans(O_state & statein, O_DataType & dataIn)
    00172 {
    00173         int intout = -1;
    00174         bool found = false;
    00175         list<O_state*> thistrans = statein.get_trans();
    00176         list<O_state*>::iterator transit;
    00177         for (transit = thistrans.begin(); (!found) && (transit != thistrans.end()); transit++)
    00178         {
    00179                 if ((*(O_DataType*)(*data)[(*transit)->get_statenb()]) == dataIn)
    00180                 {
    00181                         intout = (*transit)->get_statenb();
    00182                         found = true;
    00183                 }
    00184         }
    00185         return intout;
    00186 }
    00187
    00189 int O_learner::lrs(O_state & state1, O_state & state2)
    00190 {
    00191         if(state2.get_statenb() == 0)
    00192                 return 0;
    00193         else
    00194         {
    00195                 O_state * pi2 = (*oracle)[state2.get_statenb()-1];
    00196                 if (pi2->get_statenb() == state1.get_suffix().first->get_statenb())
    00197                         return(state1.get_suffix().second + 1);
    00198                 else
    00199                 {
    00200                         while (pi2->get_suffix().first != state1.get_suffix().first)
    00201                                 pi2 = pi2->get_suffix().first;
    00202                         if (state1.get_suffix().second> pi2->get_suffix().second)
    00203                                 return(pi2->get_suffix().second + 1);
    00204                         else
    00205                                 return(state1.get_suffix().second + 1);
    00207                 }
    00208         }
    00209 }
    00210
    00211 #endif