seq_instance.h

00001 /*
00002  *  Copyright (C) 2005 M.J. Zaki <zaki@cs.rpi.edu> Rensselaer Polytechnic Institute
00003  *  Written by parimi@cs.rpi.edu
00004  *  Updated by chaojv@cs.rpi.edu, alhasan@cs.rpi.edu, salems@cs.rpi.edu
00005  *  Modifications: 
00006  *        added separate induced and embedded sequence instances -- zaki, 5/11/06
00007  *        fixed bug with induced and embbeded sequence mining -- zaki, 5/11/06
00008  *
00009  *  This program is free software; you can redistribute it and/or
00010  *  modify it under the terms of the GNU General Public License
00011  *  as published by the Free Software Foundation; either version 2
00012  *  of the License, or (at your option) any later version.
00013  *
00014  *  This program is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License along
00020  *  with this program; if not, write to the Free Software Foundation, Inc.,
00021  *  59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
00022  */
00023 #ifndef _SEQ_INSTANCE_H_
00024 #define _SEQ_INSTANCE_H_
00025 
00026 #include "typedefs.h"
00031 template <class MP>
00032 class seq_instance{};
00033 
00034 template <class MP>
00035 ostream& operator<< (ostream& ostr, const seq_instance<V_Fkk_EMB_MINE_PROP>& rhs);
00036 
00041 template <class MP>
00042 class seq_instance<V_Fkk_EMB_MINE_PROP>
00043 {
00044   
00045 public:
00046   
00047   seq_instance(): ts(-1) {} //default defunct constructor
00048   
00049   seq_instance(const int timestamp, const int position): ts(timestamp) {} 
00051   seq_instance(const int timestamp): ts(timestamp) {} 
00053   int get_ts() const {return ts;}
00054   
00064   template<template<typename, typename > class ST, template <typename> class ALLOC>
00065   static void seq_join_seq(const ST<seq_instance, ALLOC<seq_instance> >& i1,             const ST<seq_instance, ALLOC<seq_instance> >& i2,
00066                ST<seq_instance, ALLOC<seq_instance> >& seqi_inst, 
00067                ST<seq_instance, ALLOC<seq_instance> >& seqj_inst, 
00068                bool& seqi_ind, bool& seqj_ind, 
00069                bool& seqi_emb, bool& seqj_emb, 
00070                            const bool self_join_only) 
00071   {
00072     typedef ST<seq_instance, ALLOC<seq_instance> > INSTANCES;
00073     typedef typename INSTANCES::const_iterator CONST_INST_IT;
00074       CONST_INST_IT it1, it2, it3;
00075 
00076       // Results in seq atom with seqi prefix
00077       it1 = i1.begin();
00078       it2 = i2.end()-1;
00079       if (it1->ts < it2->ts){
00080         for(it1=i1.begin(), it2=i2.begin(); it1 != i1.end() && it2 != i2.end();) {
00081           //search for it2 after it1
00082           if (it1->ts < it2->ts){
00083             //copy all remaining i2 values since they must be < it1
00084             for (it3 = it2; it3 != i2.end(); ++it3){
00085               seqi_inst.push_back(seq_instance(it3->ts));
00086             }
00087             seqi_emb = true;
00088             break; //we are done with this sequence.
00089           }
00090           else ++it2;
00091         }
00092       }
00093       
00094       
00095       // Only if the join is going to result in 2 different vats.
00096       if(!self_join_only) {
00097         it1 = i1.end()-1;
00098         it2 = i2.begin();
00099         if (it2->ts < it1->ts){
00100           for(it1=i1.begin(), it2=i2.begin(); it1 != i1.end() && it2 != i2.end();) {
00101             //search for it1 after it2
00102             if (it2->ts < it1->ts){
00103               //copy all remaining i1 values since they must be < it2
00104               for (it3 = it1; it3 != i1.end(); ++it3){
00105                 seqj_inst.push_back(seq_instance(it3->ts));
00106               }
00107               seqj_emb = true;
00108               break; //we are done with this sequence.
00109             }
00110             else ++it1;        
00111           }
00112         }
00113       }
00114       
00115   }//end seq_join_seq()    
00116 
00117   friend ostream& operator<< <>(ostream& ostr, const seq_instance& rhs); 
00118 private:
00119   int ts; 
00120 }; //end of seq_instance
00121 
00122 template<class MP>
00123 ostream& operator<< (ostream& ostr, const seq_instance<V_Fkk_EMB_MINE_PROP>& rhs){
00124   ostr << "[ " << rhs.ts << " ] ";
00125   return ostr;
00126 }
00127 
00128 template <class MP>
00129 ostream& operator<< (ostream& ostr, const seq_instance<V_Fkk_IND_MINE_PROP>& rhs);
00130 
00135 template <class MP>
00136 class seq_instance<V_Fkk_IND_MINE_PROP>
00137 {
00138   
00139 public:
00140   
00141   seq_instance(): pos(-1), induced(false) {} //default defunct constructor
00142   
00143   seq_instance(const int timestamp, const int position): pos(position), induced(true) {} 
00145   seq_instance(const int position, const bool ind): pos(position), induced(ind) {} 
00157   template<template<typename, typename > class ST, template <typename> class ALLOC>
00158   static void seq_join_seq(const ST<seq_instance, ALLOC<seq_instance> >& i1, const ST<seq_instance, ALLOC<seq_instance> >& i2,
00159                ST<seq_instance, ALLOC<seq_instance> >& seqi_inst, 
00160                ST<seq_instance, ALLOC<seq_instance> >& seqj_inst, 
00161                bool& seqi_ind, bool& seqj_ind, 
00162                bool& seqi_emb, bool& seqj_emb, 
00163                            const bool self_join_only, const bool skip0=false) 
00164   {
00165     typedef ST<seq_instance, ALLOC<seq_instance> > INSTANCES;
00166     typedef typename INSTANCES::const_iterator CONST_INST_IT;
00167       CONST_INST_IT it1, it2;
00168       
00169       if (!skip0){
00170         bool seen_valid_occurrence = false;
00171         // Results in seq atom with seqi prefix
00172         for(it1=i1.begin(), it2=i2.begin(); it1 != i1.end() && it2 != i2.end();) {
00173           
00174           if (it1->pos < it2->pos){
00175             seen_valid_occurrence = true;
00176             if (it1->is_induced() && it2->pos - it1->pos == 1){
00177               seqi_inst.push_back(seq_instance(it2->pos, true));
00178               seqi_ind = true;    
00179               ++it2;
00180             }
00181             ++it1;
00182           }
00183           else{
00184             if (seen_valid_occurrence){
00185               seqi_inst.push_back(seq_instance(it2->pos, false));    
00186               seqi_emb=true;
00187             }
00188             ++it2;
00189           }
00190         }
00191         //copy over any remaining occurrences in it2
00192         for (; it2 != i2.end(); ++it2){
00193           if (seen_valid_occurrence){
00194             seqi_inst.push_back(seq_instance(it2->pos, false));    
00195             seqi_emb=true;
00196           }
00197         }
00198       }
00199       
00200       // Only if the join is going to result in 2 different vats.
00201       if(!self_join_only) {
00202         bool seen_valid_occurrence = false;
00203         // Results in seq atom with seqj prefix
00204         for(it1=i1.begin(), it2=i2.begin(); it1 != i1.end() && it2 != i2.end();) {
00205           
00206           if (it2->pos < it1->pos){
00207             seen_valid_occurrence = true;
00208             if (it2->is_induced() && it1->pos - it2->pos == 1){
00209               seqj_inst.push_back(seq_instance(it1->pos, true));
00210               seqj_ind = true;    
00211               ++it1;
00212             }
00213             ++it2;
00214           }
00215           else{
00216             if (seen_valid_occurrence){
00217               seqj_inst.push_back(seq_instance(it1->pos, false));    
00218               seqj_emb=true;
00219             }
00220             ++it1;
00221           }
00222         }
00223         //copy over any remaining occurrences in it1
00224         for (; it1 != i1.end(); ++it1){
00225           if (seen_valid_occurrence){
00226             seqj_inst.push_back(seq_instance(it1->pos, false));    
00227             seqj_emb=true;
00228           }
00229         }        
00230       }
00231       
00232   }//end seq_join_seq()    
00233   
00234   friend ostream& operator<< <>(ostream& ostr, const seq_instance& rhs); 
00235   int get_pos() const {return pos;}
00236   
00237   bool get_induced() const {
00238     return induced;
00239   } // Returns true if this instance is an induced occurrence
00240   
00241 private:
00242   int pos; 
00243   bool induced; 
00244   bool is_induced() const{
00245     return induced;
00246   }
00247 }; //end of seq_instance
00248 
00249 template <class MP>
00250 ostream& operator<< (ostream& ostr, const seq_instance<V_Fkk_IND_MINE_PROP>& rhs){
00251   ostr << "[ " << rhs.pos << ", " << rhs.induced << " ] ";
00252   return ostr;
00253 }
00254 
00255 
00256 #endif
00257 

Generated on Wed Jul 26 14:01:08 2006 for DMTL by  doxygen 1.4.7