Biorithm  1.1
 All Classes Functions Variables Typedefs Friends
Miner.h
00001 /**************************************************************************
00002  * Copyright (c) 2007 Clifford Conley Owens III                           *
00003  *                                                                        *
00004  * This file is part of Biorithm.                                         *
00005  *                                                                        *
00006  * Biorithm is free software: you can redistribute it and/or modify       *
00007  * it under the terms of the GNU General Public License as published by   *
00008  * the Free Software Foundation, either version 3 of the License, or      *
00009  * (at your option) any later version.                                    *
00010  *                                                                        *
00011  * Biorithm 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  * GNU General Public License for more details.                           *
00015  *                                                                        *
00016  * You should have received a copy of the GNU General Public License      *
00017  * along with Biorithm.  If not, see <http://www.gnu.org/licenses/>.      *
00018  *                                                                        *
00019  **************************************************************************/
00020 
00031 #ifndef MINER_H
00032 #define MINER_H
00033 
00034 #include<cstring>
00035 #include<ctime>
00036 using namespace std;
00037 
00038 // ---------------------------- Internal Headers ---------------------------- //
00039 
00040 #include "cnull.h"
00041 #include "Monitorable.h"
00042 #include "BinaryMatrix.h"
00043 
00050 class Miner: public Monitorable
00051 {
00052     protected:
00053         // The truth matrix
00054         BinaryMatrix* bm;
00055         
00056         // The stream to print positive borders to
00057         vector<ofstream*> pborder_out;
00058 
00059         // The stream to print negative borders to
00060         //ahsanur: commented out for it has never been written in
00061 //        ofstream nborder_out;
00062         
00063         // The stream to print time to
00064         ofstream time_out;
00065         
00066         // The stream to print number of tts found at each level
00067         ofstream level_out;
00068 
00069         // The start time
00070         double starttime;
00071 
00072         // The error message
00073         char errmsg[512];
00074 
00075         // Helper methods
00076 
00078         // Gets an ostream for printing information on the positive border at //
00079         // the given level                                                    //
00080         //                                                                    //
00081         // --------------------------- Parameters --------------------------- //
00082         // unsigned int level                                                 //
00083         //     The level for the pborder                                      //
00084         //                                                                    //
00085         // ----------------------------- Return ----------------------------- //
00086         // A reference to the ostream that we want                            //
00088         virtual ofstream& pborder(unsigned int level)
00089         {
00090             // TODO: it's a pain to hit this logic every time
00091             // Add all the missing ofstreams
00092             while (pborder_out.size() <= level)
00093             {
00094                 char filename[512];
00095                 sprintf(filename, "%s_pborders_%d.dat",
00096                     bm->getFilename().c_str(), pborder_out.size() + 1);
00097                 pborder_out.push_back(new ofstream(filename, ofstream::out));
00098             }
00099 
00100             // Return the ofstream that we want
00101             return *pborder_out[level];
00102         }
00103 
00105         // Prints an item set to an ostream                                   //
00106         //                                                                    //
00107         // --------------------------- Parameters --------------------------- //
00108         // Itemset& is                                                        //
00109         //     The itemset to print                                           //
00110         // ostream& out                                                       //
00111         //     the stream to print to                                         //
00113         virtual void print(const Itemset& is, ostream& out) const
00114         {
00115             if (is.isEmpty())
00116             {
00117                 return;
00118             }
00119 
00120             out << is[0] << ':' << bm->getColname(is[0]);
00121             for (unsigned int i = 1; i < is.getSize(); i++)
00122             {
00123                 out << ", " << is[i] << ':' << bm->getColname(is[i]);
00124             }
00125         }
00126 
00128         // Prints an item set to an ostream                                   //
00129         //                                                                    //
00130         // --------------------------- Parameters --------------------------- //
00131         // Itemset* is                                                        //
00132         //     The itemset to print                                           //
00133         // ostream& out                                                       //
00134         //     the stream to print to                                         //
00136         virtual void print(const Itemset* is, ostream& out) const
00137         {
00138             print(*is, out);
00139         }
00140 
00141         virtual void analyzeHelper() = 0;
00142 
00143     public:
00145         // Constructor                                                        //
00146         //                                                                    //
00147         // --------------------------- Parameters --------------------------- //
00148         // TruthTable* bm                                                     //
00149         //     The truth table                                                //
00151         Miner(BinaryMatrix* bm = NULL): Monitorable()
00152         {
00153             this->bm = bm;
00154             errmsg[0] = '\0';
00155         }
00156 
00158         // The copy constructor                                               //
00159         //                                                                    //
00160         // --------------------------- Parameters --------------------------- //
00161         // const Miner& src                                                   //
00162         //     The source Miner                                               //
00164         Miner(const Miner& src)
00165         {
00166 
00167         }
00168 
00170         // The destructor                                                     //
00172         virtual ~Miner()
00173         {
00174 
00175         }
00176 
00178         // The = operator                                                     //
00179         //                                                                    //
00180         // --------------------------- Parameters --------------------------- //
00181         // const eMiner& rhs                                                  //
00182         //     The right hand side                                            //
00183         //                                                                    //
00184         // ----------------------------- Return ----------------------------- //
00185         // A copy of the Miner                                                //
00187         virtual Miner& operator =(const Miner& rhs)
00188         {
00189             bm = rhs.bm;
00190             status_out = rhs.status_out;
00191             Monitorable::operator =(rhs);
00192             strcpy(errmsg, rhs.errmsg);
00193             starttime = rhs.starttime;
00194             return *this;
00195         }
00196  
00198         // Analyzes the truth matrix                                          //
00199         //                                                                    //
00200         // ----------------------------- Return ----------------------------- //
00201         // A pointer to an error message                                      //
00203         virtual const char* analyze()
00204         {
00205             // Make sure we have a good stream
00206             if (!bm)
00207             {
00208                 sprintf(errmsg, "blah!");
00209                 return errmsg;
00210             }
00211             if (!bm || !bm->isOpen())
00212             {
00213                 sprintf(errmsg, "No file is open for reading");
00214                 return errmsg;
00215             }
00216  
00217             // Create the output and get a start time
00218 //            nborder_out.open((bm->getFilename() + "_nborders.dat").c_str(),
00219 //                ofstream::out);
00220             time_out.open((bm->getFilename() + "_time.dat").c_str(),
00221                 ofstream::out);
00222             level_out.open((bm->getFilename() + "_level.dat").c_str(),
00223                 ofstream::out);
00224             *status_out << "There are " << bm->getHeight() <<
00225                 " rows of data." << endl;
00226             *status_out << "There are " << bm->getWidth() <<
00227                 " columns of data." << endl;
00228             starttime = clock();
00229 
00230             // This is only an abstract class, so we will let a subclass worry
00231             // about the real analysis
00232             analyzeHelper();
00233 
00234             // Record the final time
00235             double totaltime = (clock() - starttime)/CLOCKS_PER_SEC;
00236             time_out << totaltime << endl;
00237             *status_out << "Finished in " << totaltime << " miliseconds." <<
00238                 endl;
00239 
00240             // Close all of our streams
00241             for (unsigned int i = 0; i < pborder_out.size(); i++)
00242             {
00243                 pborder_out[i]->close();
00244                 delete pborder_out[i];
00245             }
00246             pborder_out.clear();
00247             //nborder_out.close();
00248             time_out.close();
00249             level_out.close();
00250             if (strlen(errmsg))
00251             {
00252                 return errmsg;
00253             }
00254             return NULL;
00255         }
00256         
00258         // Sets the truth matrix                                              //
00259         //                                                                    //
00260         // --------------------------- Parameters --------------------------- //
00261         // BinaryMatrix* bm                                                   //
00262         //     The truth matrix                                               //
00264         virtual void setBinaryMatrix(BinaryMatrix* bm)
00265         {
00266             this->bm = bm;
00267         }
00268 
00270         // Transposes the matrix if coltype is not null                       //
00271         //                                                                    //
00272         // --------------------------- Parameters --------------------------- //
00273         // const char* coltype                                                //
00274         //     The name for the new column type.  Ie: if it was bills before, //
00275         //     it should now be senators                                      //
00277         virtual void transpose(const char* coltype)
00278         {
00279             bm->transpose(coltype);
00280         }
00281 };
00282 
00283 #endif
 All Classes Functions Variables Typedefs Friends