1 package org.objectweb.jac.samples.solitaire.optimization; 2 3 import org.objectweb.jac.core.*; 4 import org.objectweb.jac.samples.solitaire.*; 5 import org.objectweb.jac.lib.java.util.Vector; 6 import java.util.*; 7 8 public class OptimizingWrapper extends Wrapper { 9 10 public boolean optimized = false; 11 12 public boolean isOptimized() { 13 System.out.println( "-GO-" ); 14 return optimized; 15 } 16 17 public void setOptimized( boolean b ) { 18 System.out.println( "-SO-" ); 19 optimized = b; 20 } 21 22 public Object setCaller() { 23 attrdef( "caller", wrappee() ); 24 return proceed(); 25 } 26 27 public Object cleanEquivalentConfigs() { 28 29 System.out.println( " cleaning equivalent configs... " ); 30 if (arg(0) instanceof Configuration) { 31 32 if (isThere((Vector) wrappee(),(Configuration)arg(0))) { 33 return new Boolean (false); 34 } 35 else return proceed(); 36 37 } 38 Date d1 = new Date(); 39 if (arg(0) instanceof Vector) { 40 Object [] tab = ((Vector)arg(0)).toArray(); 41 for (int i=0; i<tab.length; i++) { 42 if ( ! isThere((Vector) wrappee(),(Configuration)tab[i])) { 43 ((Vector) wrappee()).add( tab[i] ); 44 } 45 } 46 ((Vector)arg(0)).clear(); 47 } 48 Date d2 = new Date(); 49 return proceed(); 51 } 52 53 54 55 public boolean isThere(Vector configs, Configuration config) { 56 byte[][] element = config.game; 57 58 59 byte[][] copy1 = new byte[element.length][]; 60 for(int k = 0 ; k < element.length ; k++){ 61 copy1[10 - k] = (byte[]) element[k].clone();} 62 63 byte[][] copy2 = new byte[element.length][element.length]; 64 for(int k = 0 ; k < element.length ; k++) 65 for(int l = 0 ; l < element.length ; l++){ 66 copy2[k][l] = element[l][k];} 67 68 byte[][] copy3 = new byte[element.length][]; 69 for(int k = 0 ; k < element.length ; k++){ 70 copy3[10 - k] = (byte[]) copy2[k].clone();} 71 72 byte[][] copy4 = new byte[element.length][element.length]; 73 for(int k = 0 ; k < element.length ; k++) 74 for(int l = 0 ; l < element.length ; l++){ 75 copy4[k][l] = copy1[l][k];} 76 77 byte[][] copy5 = new byte[element.length][]; 78 for(int k = 0 ; k < element.length ; k++){ 79 copy5[10 - k] = (byte[]) copy4[k].clone();} 80 81 byte[][] copy6 = new byte[element.length][element.length]; 82 for(int k = 0 ; k < element.length ; k++) 83 for(int l = 0 ; l < element.length ; l++){ 84 copy6[k][l] = copy5[l][k];} 85 86 byte[][] copy7 = new byte[element.length][]; 87 for(int k = 0 ; k < element.length ; k++){ 88 copy7[10 - k] = (byte[]) copy6[k].clone();} 89 90 91 if (containsGame(element) || 92 containsGame(copy1) || 93 containsGame(copy2) || 94 containsGame(copy3) || 95 containsGame(copy4) || 96 containsGame(copy5) || 97 containsGame(copy6) || 98 containsGame(copy7) ) { 99 100 return true; 101 } 102 return false; 103 } 104 105 106 107 public boolean containsGame(byte[][] toTest) { 108 Object [] configs = ((Vector)wrappee()).toArray(); 109 110 for ( int i = 0; i < configs.length; i++ ) { 111 if ( confHasGame( (Configuration) configs[i], toTest ) ) return true; 112 } 113 return false; 114 } 115 116 public boolean confHasGame( Configuration c, byte[][] game ) { 117 118 if ( c == null ) return false; 119 if ( c.game == null || game == null ) return false; 120 121 for(int k = 0 ; k < 11; k++) { 122 for(int l = 0 ; l < 11; l++) { 123 if( c.game[k][l] != game[k][l] ) 124 return false; 125 } 126 } 127 return true; 128 } 129 130 } 131 132 133 134 | Popular Tags |