package natlab.tame.valueanalysis.components.shape;

import java.util.*;

import natlab.tame.valueanalysis.aggrvalue.AggrValue;
import natlab.tame.valueanalysis.basicmatrix.BasicMatrixValue;
import natlab.tame.valueanalysis.components.constant.Constant;
import natlab.tame.valueanalysis.value.*;

/**
 * allows construction of shapes
 */
public class ShapeFactory<V extends Value<V>> {
	ValueFactory<V> factory;
	
	public ShapeFactory(){
	}
	
	public ShapeFactory(ValueFactory<V> factory){
		this.factory = factory;
	}
	
	
    /**
     * returns a shape with the given dimensions.
     * The given constants should be scalar.
     * 
     */
    public Shape<V> newShapeFromConstants(List<Constant> dims){
    	ArrayList<V> list = new ArrayList<V>(dims.size());
    	for (Constant dim : dims){
    		list.add(factory.newMatrixValue(dim));
    	}
    	return null;
    	//return new Shape<V>(factory, list);
        //throw new UnsupportedOperationException();//return new Shape<D>(factory); XU modified
    }

    
    /**
     * returns a shape using the given values as dimensions
     */
    public Shape<V> newShapeFromValues(List<V> dims){
    	return null;
    	//return new Shape<V>(factory,dims);
    }
    
    /**
     * return a shape using the given input string, like 3x3...
     */
    public Shape<V> newShapeFromInputString(String s){
    	String[] array = s.split("[\\*]");
    	ArrayList<Integer> list = new ArrayList<Integer>();
    	for(String a : array){
    		Integer i = Integer.parseInt(a);
    		list.add(i);
    	}
    	return new Shape<V>(factory,list);
    }
    
    /**
     * return a shape using the given integer list as dimensions
     */
    public Shape<V> newShapeFromIntegers(List<Integer> dims){
    	/*ArrayList<V> list = new ArrayList<V>(dims.size());
    	for (V dim : dims){
    		list.add(factory.newMatrixValue(dim)); //this line lead to infinite loop!!!
    	}*/
    	return new Shape<V>(factory,dims);
    }
    
    /**
     * returns a 0x0 shape
     */
    @SuppressWarnings("unchecked")
	public Shape<V> getEmptyShape(){
    	return null;
/*        return new Shape<V>(factory,Arrays.asList(
        		factory.newMatrixValue(0),
        		factory.newMatrixValue(0)        		
        		));*/
    }
    
    
    /**
     * returns 1x1 shape 
     */
    @SuppressWarnings("unchecked")
	public Shape<V> getScalarShape(){
    	return null;
/*        return new Shape<V>(factory,Arrays.asList(
        		factory.newMatrixValue(1),
        		factory.newMatrixValue(1)
        		));*/
    }
    
    public static void main(String[] args){
    	ShapeFactory sf = new ShapeFactory();
    	Shape<AggrValue<BasicMatrixValue>> testShape = sf.newShapeFromInputString("3*3");
    	System.out.println(testShape);
    }
	
}

@@ -1469,16 +1469,93 @@
 array)%7B%0A
+    %09%09if(a.matches(%22%5B%5C%5C?%5D%22))%7B%0A    %09%09%09list.add(null);%0A    %09%09%7D%0A    %09%09else%7B%0A    
     %09%09In
@@ -1585,16 +1585,20 @@
 Int(a);%0A
+    
     %09%09li
@@ -1608,16 +1608,24 @@
 add(i);%0A
+    %09%09%7D%0A
     %09%7D%0A 
@@ -2792,17 +2792,19 @@
 ring(%223*
-3
+?*2
 %22);%0A    

make it to support ?