1 22 23 package org.xquark.extractor.algebra; 24 25 import java.util.ArrayList ; 26 import java.util.List ; 27 28 import org.xquark.extractor.common.Debug; 29 93 94 public final class Numbering implements Constants{ 95 private static final String RCSRevision = "$Revision: 1.4 $"; 96 private static final String RCSName = "$Name: $"; 97 98 99 100 List _idList = null; 101 102 103 List _sortSpecListList = null; 104 int _subBueryNumber; 106 int _matrix[][] ; 107 int _maxRow; 108 int _maxCol; 109 110 111 112 Mapper _mapper = null; 113 114 public Numbering(List idList, int subBueryNumber) { 115 _idList = idList; 116 _subBueryNumber = subBueryNumber; 117 } 118 119 124 private void initializeMatrix() 125 { 126 128 129 long bitMap ; 130 _maxCol = _idList.size() ; 131 _maxRow = _subBueryNumber ; 132 _matrix = new int[_maxRow+1][_maxCol+2]; 133 134 135 long ruler ; 136 for (int col = 1; col <= _maxCol ; col++) { 137 MapItem mapItem = (MapItem)_idList.get(col-1); 138 bitMap = mapItem.getPart(); 139 ruler = 1; 140 for (int row = 1; row <= _maxRow; row++) { 141 142 _matrix[row ][col] = ((bitMap >> (row-1)) & ruler) > 0 ? 1 : 0; 143 } 144 } 145 146 for (int i = 1; i <=_maxCol; i++) { 147 _matrix[0][i] = i; 148 } 149 for (int i = 1; i <=_maxRow; i++) { 150 _matrix[i][0] = i; 151 } 152 } 155 private void simplifyMatrix() 156 { 157 159 for (int col = 1; col < _maxCol ; col++) { 160 if ( colEquals(col, col + 1)) { 161 removeCol(col); 162 } 163 } 164 166 } 168 169 private boolean colEquals(int col1 , int col2) 170 { 171 boolean retVal = true; 173 for (int row = 1; row <= _maxRow; row++) { 174 if (_matrix[row][col1] != _matrix[row][col2]) { 175 retVal = false; 176 break; 177 } 178 } 179 return retVal; 181 } 182 183 private void removeCol(int col) 184 { 185 for (int row = 0; row <= _maxRow; row++) { 187 _matrix[row][col] = 0; 188 } 189 } 191 192 private int slave(int query, int variable) 193 { 194 int retVal = 0; 196 for (int col = variable + 1; col <= _maxCol; col++) { 197 if (1 == _matrix[query][col] ) { 198 retVal = col; 199 break; 200 } 201 } 202 return retVal; 204 } 205 206 public void number() 207 { 208 210 initializeMatrix(); 211 212 eliminationGuide(); 213 214 simplifyMatrix(); 215 216 int curNum = 0; 217 int oldSlave; 218 int slave; 219 220 221 for (int col = 1; col <= _maxCol; col++) { 222 oldSlave = 0; 223 curNum = 1; 224 for (int row = 1; row <= _maxRow; row++) { 225 if (0 != _matrix[row][col]) { 226 slave = slave(row, col); 227 if (oldSlave == slave) { 228 _matrix[row][col] = curNum; 229 } 230 else { 231 oldSlave = slave; 232 curNum ++; 233 _matrix[row][col] = curNum; 234 } 235 } 236 } 237 } 238 removeEmptyColumn(); 240 } 243 244 private void removeEmptyColumn() 245 { 246 for (int col = 1; col <= _maxCol; col++) { 248 if (isEmpty(col)) { 249 _matrix[0][col] = 0; 250 } 251 } 252 } 254 255 private boolean isEmpty(int col) 256 { 257 boolean retVal = true; 259 int counter = 0; 260 for (int row = 1; row <= _maxRow; row++) { 261 if (0 != _matrix[row][col]) { 262 counter ++; 263 } 264 } 265 retVal= counter < 2; 266 return retVal; 268 } 269 270 public int[] getNumbersForQuery(int query) 271 { 272 int[] retVal = new int[_maxCol+1]; 274 for (int col = 0; col < _maxCol; col++) { 275 retVal[col] = _matrix[query][col]; 276 } 277 return retVal; 279 } 280 281 void setSortSpecListList(List sortSpecListList) 282 { 283 _sortSpecListList = sortSpecListList; 284 } 285 286 List getSortList() 287 { 288 290 List retVal = new ArrayList (); 291 292 List sortSpecList = null; 293 MapItem mapItem; 294 List subIdList; 295 AttributeExpression numberColumn; 296 297 LitInteger pseudoNumberColomn = new LitInteger(0); 298 299 AttributeExpression sortExpr = null; 300 SortSpecification sortSpec = null; 301 302 int idCursor = 0; int lastIdOfCurrentQuery = 0; 304 for (int query = 1; query <= _subBueryNumber; query++) { 305 306 307 if (null != _sortSpecListList) { 308 sortSpecList = (List )_sortSpecListList.get(query-1); 309 if (null != sortSpecList) { 310 retVal.addAll(sortSpecList); 311 } 312 } 313 314 315 lastIdOfCurrentQuery = lastIdColumnIn(query); 316 for (int j = idCursor; j < lastIdOfCurrentQuery; j++) { 317 mapItem = (MapItem)_idList.get(j); 321 if (mapItem.isLeafPath()) { 322 sortExpr = new AttributeExpression(null, mapItem.getItemName()); 323 sortExpr.setUnderlyingExpr(mapItem.getRelatedExpression()); 324 sortSpec = new SortSpecification(sortExpr, ASCENDING_ORDER); 325 retVal.add(sortSpec); 326 } 327 else { 328 String childPath = null; 329 MapItem subItem = null; 330 subIdList = mapItem.getChildren(); 331 for (int k = 0; k < subIdList.size(); k++) { 332 333 childPath = (String )subIdList.get(k); 334 subItem = _mapper.map(childPath); 335 sortExpr = new AttributeExpression(null, subItem.getItemName()); 336 sortExpr.setUnderlyingExpr(subItem.getRelatedExpression()); 337 sortSpec = new SortSpecification(sortExpr,ASCENDING_ORDER); 338 retVal.add(sortSpec); 339 } 340 } 341 } 342 343 idCursor = lastIdOfCurrentQuery; 344 345 346 if (0 < _matrix[0][idCursor]) { 347 numberColumn = new AttributeExpression(null, 348 GenAlgebraVisitor.NUMBER_COLUMN_PREFIX+Integer.toString(_matrix[0][idCursor])); 349 350 numberColumn.setUnderlyingExpr(pseudoNumberColomn); 351 sortSpec = new SortSpecification(numberColumn, ASCENDING_ORDER); 352 retVal.add(sortSpec); 353 } 354 } 355 356 return retVal; 358 } 359 360 private void eliminationGuide () 361 { 362 int lastId; 363 int lastPresence; 364 for (int query = 1; query <= _maxRow; query++) { 365 lastId = lastIdColumnIn(query); 366 lastPresence = lastPresenceOf(query,lastId); 367 _matrix[query][_maxCol+1] = lastPresence; 368 } 369 } 370 371 public int[] getEliminationGuide() 372 { 373 int[] retVal = new int[_maxRow]; 374 for (int query = 1; query <= _maxRow; query++) { 375 retVal[query-1] = _matrix[query][_maxCol+1]; 376 } 377 return retVal; 378 } 379 380 private int lastIdColumnIn(int query) 381 { 382 int retVal = 0; 383 for (int i = 1; i <= _maxCol; i++) { 384 if (0 < _matrix[query][i]) { 385 retVal = i; 386 } 387 } 388 Debug.assertTrue(0 != retVal,"0 != retVal"); 389 return retVal; 390 } 391 392 private int lastPresenceOf(int query, int id) 393 { 394 int retVal = query; 395 for (int i = query; i <=_maxRow; i++) { 396 if (0 < _matrix[i][id]) { 397 retVal = i; 398 } 399 else { 400 break; 401 } 402 } 403 Debug.assertTrue(0 != retVal,"0 != retVal"); 404 return retVal; 405 } 406 407 408 String printMatrix() 409 { 410 StringBuffer retVal = new StringBuffer (); 411 412 String newLine = System.getProperty("line.separator"); 413 for (int row = 0; row <= _maxRow; row++) { 414 for (int col = 0; col <= _maxCol +1 ; col++) { 415 retVal.append(_matrix[row][col]); 416 retVal.append("\t"); 417 } 418 retVal.append(newLine); 419 } 420 return retVal.toString(); 421 } 422 } 423 | Popular Tags |