package version10_2_2_1.agnosticMemory;

import java.util.ArrayList;

public class HypothesisMemory {

	public ArrayList<Hypothesis> memory;
	
	public HypothesisMemory(){
		memory=new ArrayList<Hypothesis>();
	}
	
	public void addHypothesis(Byte[] p, ArrayList<Short[]> o){
		
		int i=0;

		// create the new hypothesis
		boolean identical=true;
		i=0;
		while (identical && i<p.length){
			for (int j=0;j<o.size();j++){
				for (int k=j;k<o.size();k++){
					if (o.get(j)[i]!=o.get(k)[i]) identical=false;
				}
			}
			i++;
		}
		if (i==p.length) i++;

		for (int j=0;j<o.size();j++){
			for (int k=j+1;k<o.size();k++){
				if (o.get(j)[i-1]==o.get(k)[i-1]){
					o.remove(k);
					k--;
				}
			}
		}
		
		boolean found=false;
		int mem=0;
		while (!found && mem<memory.size()){
			if (memory.get(mem).isEqual(p, o)) found=true;
			mem++;
		}
		
		if (!found){

			if (o.size()>1){
				Byte[] path=new Byte[i-1];
				Short[][] origin=new Short[o.size()][i];
				for (int j=0;j<origin.length;j++){
					origin[j][0]=o.get(j)[0];
				}
				
				for (int l=1;l<i;l++){
					path[l-1]=p[l-1];
					for (int j=0;j<origin.length;j++){
						origin[j][l]=o.get(j)[l];
					}
				}
				
				/*System.out.println("+++ group ");
				for (int k=0;k<o.size();k++){
					for (int l=origin[k].length-1;l>=0;l--) System.out.print(origin[k][l]+", ");
					System.out.println();
				}
				System.out.println("level "+i);*/
				
				memory.add(new Hypothesis(path,origin));
			}
		}
	}
	
	public void print(){
		for (int i=0;i<memory.size();i++){
			System.out.print("[");
			for (int l=memory.get(i).path.length-1;l>=0;l--) System.out.print(memory.get(i).path[l]+", ");
			System.out.println("]");
			for (int k=0;k<memory.get(i).origin.length;k++){
				System.out.print("--- ");
				for (int l=memory.get(i).origin[k].length-1;l>=0;l--) System.out.print(memory.get(i).origin[k][l]+", ");
				System.out.println();
			}
		}
	}
	
	
	private class Hypothesis{
		public Byte[] path;
		public Short[][] origin;
		public byte status;
		
		public Hypothesis(Byte[] p, Short[][] o){
			path=p;
			origin=o;
			status=0;
		}
		
		public void setStatus(boolean s){
			if (s) status=1;
			else status=-1;
		}
		
		public boolean isEqual(Byte[] p, ArrayList<Short[]> o){
			
			/*System.out.println("+++ test group ");
			for (int k=0;k<o.size();k++){
				for (int l=o.get(k).length-1;l>=0;l--) System.out.print(o.get(k)[l]+", ");
				System.out.println();
			}
			System.out.println("with hypothesis :");
			for (int k=0;k<origin.length;k++){
				for (int l=origin[k].length-1;l>=0;l--) System.out.print(origin[k][l]+", ");
				System.out.println();
			}
			System.out.println("-----");*/
			
			if (o.size()!=origin.length) return false; // not the same number of proto-objects
			//System.out.println("test1");
			if (p.length>=path.length){
				//System.out.println("test2");	
				for (int k=0;k<origin.length;k++){
					if (origin[k][0]!=o.get(k)[0]) return false;
				}
				//System.out.println("test3");
				for (int l=0;l<path.length;l++){
					if (p[l]!=path[l]) return false;		// not the same proto-objects
					
					for (int k=0;k<origin.length;k++){
						if (origin[k][l+1]!=o.get(k)[l+1]) return false;
					}
				}
				//System.out.println("test4");
			}
			return true;
		}
	}
	
}
