00001 #ifndef SingleSegmentation_H
00002 #define SingleSegmentation_H
00003
00004
00005 #include <TeAdoDB.h>
00006 #include <TeDatabase.h>
00007 #include "Som.h"
00008
00009
00010 class SingleSegmentation {
00011 public:
00012
00013
00014 SingleSegmentation(){};
00015
00016
00017 void Segment( SOM* som, int );
00018
00019 void createField( vector<string> params );
00020
00021
00022
00023
00024 private:
00025
00026 void HorizontalSegmentation();
00027
00028 void VerticalSegmentation();
00029
00030 void Diagonal01Segmentation();
00031
00032 void Diagonal02Segmentation();
00033
00034 private:
00035 SOM * _som;
00036 int m;
00037 TIntVector _clusters;
00038 int _num_clusters;
00039
00040 };
00041
00042
00043 void
00044 SingleSegmentation::createField( vector<string> params ) {
00045
00046 string host = params[0];
00047 string dbname = params[1];
00048 string user = params[2];
00049 string pass = params[3];
00050 string tableName = params[4];
00051 string fieldName = params[5];
00052 string labelField = params[6];
00053
00054 TLabel label = _som->getMapcode()->getLabel();
00055
00056
00057 int i;
00058
00059
00060 TeDatabase* db = new TeAdo();
00061 if (!db->connect(host,user,pass,dbname,0))
00062 {
00063 cout << "Error: " << db->errorMessage() << endl;
00064 return;
00065 }
00066
00067 TeAttribute atr;
00068 atr.rep_.name_= fieldName;
00069 atr.rep_.type_ = TeREAL;
00070 atr.rep_.numChar_ = 15;
00071 atr.rep_.decimals_ = 0;
00072
00073 if (!db->addColumn(tableName, atr.rep_))
00074 {
00075 cout << "Error adding column to table: " << db->errorMessage() << endl;
00076 db->close();
00077 return;
00078 }
00079
00080
00081
00082 TeDatabasePortal* portal = db->getPortal();
00083 if (!portal)
00084 {
00085 cout << "Error getting a portal to dabase: " << db->errorMessage() << endl;
00086 db->close();
00087 return;
00088 }
00089
00090 string sql = "UPDATE " + tableName + " SET " + fieldName + " = 0";
00091
00092
00093 if (portal->query(sql))
00094 {
00095 cout << "Error submiting query: " << db->errorMessage() << endl;
00096 delete portal;
00097 db->close();
00098 return;
00099 }
00100
00101
00102 for (i=0; i< m; i++ ) {
00103 for (int j = 0; j < label[i].size(); j++ ) {
00104
00105 string value = inttostr(_clusters[i+1]);
00106 string id = label[i][j];
00107 sql = "UPDATE " + tableName + " SET " + fieldName + " = " + value + " WHERE " + labelField + " = " + "\""+ id + "\"";
00108
00109 if (portal->query(sql))
00110 {
00111 cout << "Error submiting query: " << db->errorMessage() << endl;
00112 delete portal;
00113 db->close();
00114 return;
00115 }
00116
00117 }
00118 }
00119
00120 delete portal;
00121 db->close();
00122
00123 };
00124
00125 void
00126 SingleSegmentation::HorizontalSegmentation() {
00127
00128 int L = _som->getMapcode()->getDimensions(0);
00129 int C = _som->getMapcode()->getDimensions(1);
00130
00131 _clusters = create_intvector( 1, m );
00132
00133 int count = 1;
00134 for (int j=1; j<= C; j++ )
00135 for (int i=1; i<= L; i++ ) {
00136 _clusters[ ((j - 1)*L + i) ] = count;
00137 count++;
00138 }
00139
00140
00141 };
00142
00143 void
00144 SingleSegmentation::VerticalSegmentation() {
00145
00146 int L = _som->getMapcode()->getDimensions(0);
00147 int C = _som->getMapcode()->getDimensions(1);
00148
00149 _clusters = create_intvector( 1, m );
00150
00151 int count = 1;
00152 for (int i=1; i<= L; i++ )
00153 for (int j=1; j<= C; j++ ) {
00154 _clusters[ ((j - 1)*L + i) ] = count;
00155 ++count;
00156 }
00157
00158
00159 };
00160
00161 void
00162 SingleSegmentation::Diagonal01Segmentation() {
00163
00164 int L = _som->getMapcode()->getDimensions(0);
00165 int C = _som->getMapcode()->getDimensions(1);
00166 int i, j;
00167
00168 _clusters = create_intvector( 1, m );
00169 for (i=1; i<=m; i++ ) _clusters[i] = 0;
00170
00171 TMatrix mat = create_matrix( 1, L, 1, C );
00172
00173 for (i=1; i<=L; i++) for (j=1; j<= C; j++ ) mat[i][j] = 1;
00174
00175 int count = 1;
00176 bool flag = false;
00177
00178 while ( count <= m ) {
00179
00180 flag = false;
00181 for (i=1; i<=L; i++) {
00182 for (j=1; j <= C; j++ )
00183 if ( mat[i][j] == 1 ) {
00184 mat[i][j] = 0;
00185 flag = true;
00186 break;
00187 }
00188 if ( flag ) break;
00189 }
00190
00191 _clusters[ ((j - 1)*L + i ) ] = count;
00192 ++count;
00193 while (( j > 1 ) && ( i < L ) ) {
00194 --j;
00195 ++i;
00196 mat[i][j] = 0;
00197 _clusters[ ((j - 1)*L + i ) ] = count;
00198 ++count;
00199 }
00200
00201 }
00202 }
00203
00204 void
00205 SingleSegmentation::Diagonal02Segmentation() {
00206
00207 int L = _som->getMapcode()->getDimensions(0);
00208 int C = _som->getMapcode()->getDimensions(1);
00209 int i, j;
00210
00211 _clusters = create_intvector( 1, m );
00212 for (i=1; i<=m; i++ ) _clusters[i] = 0;
00213
00214 TMatrix mat = create_matrix( 1, L, 1, C );
00215
00216 for (i=1; i<=L; i++) for (j=1; j<= C; j++ ) mat[i][j] = 1;
00217
00218 int count = 1;
00219 bool flag = false;
00220
00221 while ( count <= m ) {
00222
00223 flag = false;
00224 for (i=1; i<=L; i++) {
00225 for (j=C; j >= 1; j-- )
00226 if ( mat[i][j] == 1 ) {
00227 mat[i][j] = 0;
00228 flag = true;
00229 break;
00230 }
00231 if ( flag ) break;
00232 }
00233
00234 _clusters[ ((j - 1)*L + i ) ] = count;
00235 ++count;
00236 while (( j < C ) && ( i < L ) ) {
00237 ++j;
00238 ++i;
00239 mat[i][j] = 0;
00240 _clusters[ ((j - 1)*L + i ) ] = count;
00241 ++count;
00242 }
00243
00244 }
00245 };
00246
00247 void
00248 SingleSegmentation::Segment( SOM* som, int single_type ) {
00249
00250 _som = som;
00251 m = som->getMapcode()->getMapSize();
00252 _num_clusters = m;
00253
00254 switch( single_type ) {
00255 case 0 : HorizontalSegmentation(); break;
00256 case 1 : VerticalSegmentation(); break;
00257 case 2 : Diagonal01Segmentation(); break;
00258 case 3 : Diagonal02Segmentation(); break;
00259 };
00260
00261 };
00262
00263
00264 #endif