/*
 * ExptScript.java
 *
 * Created on June 8, 2005, 11:52 AM
 */

package DDMT.Nodes;
import java.lang.*;
import java.io.*;
import java.lang.*;
import java.util.*;
/**
 *
 * @author  kanishk1
 */
public class ExptScript 
{
    String Folder = "c:\\Data1\\";
    int MAX_ITER = 200;
    public ExptScript() 
    {
        
    }
    
    public void EvalAvgSize(int NumPeers,int NumClusters,int PointsPerPeer)
    {
        double AvgSize = 0.0;
        double MaxSize = 0.0;
        int max = Integer.MIN_VALUE;
        try
        {
            for (int i=0;i<NumClusters;i++)
            {
                for (int j=0;j<NumPeers;j++)
                {
                    String PeerFolder = Folder+"Node"+j+"\\X_ic\\";
                    String file = "";
                    int MAX_iter = 0;
                    for (int k=1;k<MAX_ITER;k++)
                    {
                        file = PeerFolder+"X_ic-"+i+k+".txt";
                        File fp = new File(file);
                        if (!fp.exists())
                        {
                            MAX_iter = k-1;
                            break;
                        }
                        else
                            continue;
                    }
                 
                    file = PeerFolder+"X_ic-"+i+MAX_iter+".txt";
                    File fp = new File(file);
                    BufferedReader in = new BufferedReader(new FileReader(fp));
                    String[] line = in.readLine().split(" ");
                    AvgSize += (double)Integer.parseInt(line[line.length-1]);
//                    System.out.println(Integer.parseInt(line[line.length-1]));
//                    if (Integer.parseInt(line[line.length-2])>max)
//                    {
//                        max = Integer.parseInt(line[line.length-2]);
//                        MaxSize = (double)Integer.parseInt(line[line.length-2]);
//                        String file1 = Folder+"Node"+j+"\\S_itc\\"+"S_itcavg-"+i+"0.txt";
//                        File fp1 = new File(file1);
//                        in = new BufferedReader(new FileReader(fp1));
//                        String[] line1 = in.readLine().split(" ");
//                        MaxSize /= (double)Integer.parseInt(line1[line1.length-1]);
//                    //    System.out.println("Sitc: "+Integer.parseInt(line1[line1.length-1])+" "+i+" "+j);
//                    }
                //    System.out.println(Integer.parseInt(line[line.length-2]));
                }  
            }
            AvgSize/=(NumPeers*PointsPerPeer);
            System.out.println("AvgSize "+AvgSize);   
            System.out.println("MaxSize "+MaxSize);   
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    public void CalculateNoChange(int NumPeers,int NumClusters,int Size_ofDatabase,double NoiseProb)
    {
        TimedOutput tout = new TimedOutput(NumPeers);
        Vector Time = new Vector();
        Time = new Vector ();
        
        for (int i=0;i<NumPeers;i++)
            tout.OutPutInstance[i] = 1;
        tout.PercentNoChange = 100.0;
        
        
        Time.add(0,tout);
        
        try
        {
            int MaxTime = 0;
            for (int i=0;i<NumPeers;i++)
            {
                
                String line = null;
                String FileName = new String("c:\\Data"+"_"+Size_ofDatabase+"_"+NoiseProb+"\\Node"+i+"\\Output.txt");
                
            //    System.out.println("Reading File "+FileName);
                BufferedReader in = new BufferedReader(new FileReader(FileName));
                while (null != (line = in.readLine())) 
                {
                    //System.out.println(line);
                    String[] n = line.split(" ");
                    String tInstance = n[0];
                    
                    if (Integer.valueOf(n[0]).intValue()>MaxTime)
                        MaxTime = Integer.valueOf(n[0]).intValue();
                }
                
            }
            MaxTime++;
            
            System.out.println("MaxTime : "+MaxTime);
            
            for (int i=1;i<MaxTime;i++)
            {
                 tout = new TimedOutput(NumPeers);
                 Time.add(tout);
            }
            
            for (int i=0;i<NumPeers;i++)
            {
                String line = null;
                String FileName = new String("c:\\Data"+"_"+Size_ofDatabase+"_"+NoiseProb+"\\Node"+i+"\\Output.txt");
            //    System.out.println("Reading File "+FileName);
                BufferedReader in = new BufferedReader(new FileReader(FileName));
                while (null != (line = in.readLine())) 
                {
                    //System.out.println(line);
                    String[] n = line.split(" ");
                    String tInstance = n[0];
                    String value = n[1];
                    tout = new TimedOutput(NumPeers);
                    tout.OutPutInstance[i] = Integer.valueOf(value).intValue();
                  //  TimedOutput CurrElem = (TimedOutput)Time.elementAt(Integer.valueOf(tInstance).intValue());
//                    if (Integer.valueOf(tInstance).intValue()>Time.size())
//                    {
//                        System.out.println(Integer.valueOf(tInstance).intValue());
//                        Time.setSize(Integer.valueOf(tInstance).intValue()+1);
//                       
//                        System.out.println(Time.size());
//                    }
                    Time.set(Integer.valueOf(tInstance).intValue(),tout);  
                }
            }

//            System.out.println(Time.size());
//            for (int i=1;i<1000;i++)
//            {
//                TimedOutput CurrElem = (TimedOutput)Time.elementAt(i);
//                if(CurrElem != null)
//                {
//                    System.out.println(i+" ");
//                   // this.AppendFile("c:\\DummyTest.txt","Time : "+i+"\n");
//                    for (int j=0;j<NumPeers;j++)
//                        if (CurrElem.OutPutInstance[j]!=-1)
//                        {
//                            this.AppendFile("c:\\DummyTest.txt",j+" "+CurrElem.OutPutInstance[j]+" ");
//                            this.AppendFile("c:\\DummyTest.txt","\n");
//                        }
//                }
//            }
            for (int i=1;i<Time.size();i++)
            {
                int NumNoChange = 0;
                TimedOutput CurrElem = (TimedOutput)Time.elementAt(i);
                TimedOutput PrevElem = (TimedOutput)Time.elementAt(i-1);

                for (int j=0;j<NumPeers;j++)
                {
                   if (CurrElem.OutPutInstance[j]==-1)
                        CurrElem.OutPutInstance[j]=PrevElem.OutPutInstance[j];
                }
                
                for (int j=0;j<NumPeers;j++)
                {
                    if (CurrElem.OutPutInstance[j]==1)
                    {
                        NumNoChange++;
                    }
                }
                CurrElem.PercentNoChange = ((double)NumNoChange/(double)NumPeers)*100.0;
                Time.remove(i);
                Time.add(i, CurrElem);
            }

                       
            //now calculate the max,min and avg
            TimedOutput CurrElem = (TimedOutput)Time.elementAt(0);
            double max = CurrElem.PercentNoChange;
            double min = CurrElem.PercentNoChange;
            double avg = CurrElem.PercentNoChange;
            for (int i=1;i<Time.size();i++)
            {
                CurrElem = (TimedOutput)Time.elementAt(i);
                avg += CurrElem.PercentNoChange;
                if (CurrElem.PercentNoChange>max)
                {
                    max = CurrElem.PercentNoChange;
                }
                if (CurrElem.PercentNoChange<min)
                {
                    min = CurrElem.PercentNoChange;
                }
            }
            avg = avg/(MaxTime);
            System.out.println("Max : "+max+" Min : "+min+" Avg : "+avg);
        }
        catch(FileNotFoundException e)
        {

        }
        catch(IOException e)
        {

        }
    }
    
    public void CalculateGlobalMsg(int NumClusters)
    {
        
        Vector Time = new Vector();
        Time = new Vector ();
        int CurrTime = 0;
        try
        {
            int MaxTime = 0;
            String line = null;
            String FileName = new String("d:\\NoofMsg.txt");
        //    System.out.println("Reading File "+FileName);
            File file = new File(FileName);
            byte buffer[] = new byte[(int)file.length()];
            BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
            input.read(buffer,0,buffer.length);
            input.close();					 
            String fileData = new String(buffer);
            
            String[] result = fileData.split("\n");
            String[] temp1 = result[result.length-1].split(" ");
            MaxTime = Integer.parseInt(temp1[0]);
            
            int TimeOld = 0;
            int TimeNew = 0;
            
            System.out.println("MaxTime : "+MaxTime);
            Time = new Vector (MaxTime);
            String TimeString = "";
            int DataMsg = 0;
            int CtrlMsg[] = new int[NumClusters];
                      
            
            for (int i=0;i<result.length;i++)
            {
                System.out.println(result[i]);
                String[] lineres = result[i].split(" ");
                TimeNew = Integer.parseInt(lineres[0]);
                
                if (TimeNew!=TimeOld)
                {
                    TimeString =""+TimeNew+" ";
                    TimeString = TimeString.concat(String.valueOf(DataMsg)+" ");
                    for (int j=0;j<NumClusters;j++)
                    {
                        TimeString = TimeString.concat(String.valueOf(CtrlMsg[j])+" ");
                    }
                    TimeString = TimeString.concat("\n");
                    Time.add(TimeString);
                    DataMsg = 0;
                    for (int j=0;j<NumClusters;j++)
                    {
                        CtrlMsg[j]=0;
                    }
                    TimeOld = TimeNew;
                    i--;
                }
                else
                {
                  //  System.out.println("Line : "+result[i]);
                    
                    if (lineres.length==3)          //ctrl msg
                    {
                    //    System.out.println("Line : "+result[i]);
                        //this is control msg
                        int x  = Integer.valueOf(lineres[1]).intValue();
                        int y  = Integer.valueOf(lineres[2]).intValue();
                        System.out.println(x+" "+y);
                        
                     //   int y  = Integer.parseInt(st.nextToken());
                        CtrlMsg[x]+=y;
                    }
                    else
                    {
                        //this is a data msg
                        int x = Integer.valueOf(lineres[1]).intValue();
                        DataMsg+=x;
                     //   System.out.println(st.nextToken());
                    }  
                }
            }
            
            for (int i=0;i<10;i++)
            {
                System.out.println((String)Time.elementAt(i));
            }            
            
            String t = "";
            for (int i=0;i<Time.size();i++)
            {
                String temp = (String)Time.elementAt(i);
                t = t.concat(temp);
            }
            FileOperations.WriteFile("d:\\Communications.txt",t);
            
//            t = "";
//            for (int i=1;i<Time.size();i++)
//            {
//                t ="";
//                String PrevTemp = (String)Time.elementAt(i-1);
//                String CurrTemp = (String)Time.elementAt(i);
//                
//                String[] result1 = PrevTemp.split(" ");
//                String[] result2 = CurrTemp.split(" ");
//             //   System.out.println(PrevTemp+" "+CurrTemp);
//                for (int j=1;j<5;j++)
//                {
//                 //   System.out.println(result2[j]+" "+result1[j]);
//                    int CumMsg = Integer.parseInt(result2[j])+Integer.parseInt(result1[j]);
//                    result2[j] = String.valueOf(CumMsg);
//                //    System.out.println(result2[j]+" "+result1[j]);
//                }
//                
//                for (int j=0;j<result2.length;j++)
//                {
//                    
//                    t = t.concat(result2[j]+" ");
//                }
//                t = t.concat("\n");
//               // System.out.println(t);
//                Time.set(i, t);
//                //CurrTemp
//                
//            }
//            
//            for (int i=0;i<Time.size();i++)
//            {
//                String temp = (String)Time.elementAt(i);
//                t = t.concat(temp);
//            }
//            FileOperations.WriteFile("d:\\CommunicationsCum.txt",t);
        }
        catch(FileNotFoundException e)
        {

        }
        catch(IOException e)
        {

        }
    }
    
    public void CalculateNorm(int NumPeers,int NumClusters,int Size_ofDatabase,double NoiseProb)
    {
        
    }
    
    public void CalculateNumMsg(int NumPeers,int NumClusters,int Size_ofDatabase,double NoiseProb)
    {
        int NumMsg = 0;
        for (int i=0;i<NumPeers;i++)
        {
            try 
            {    
                String FileName = new String("c:\\Data"+"_"+Size_ofDatabase+"_"+NoiseProb+"\\Node"+i+"\\NumMsg.txt");
                
            //    System.out.println("Reading File "+FileName);
                File file = new File(FileName);
                byte buffer[] = new byte[(int)file.length()];
                BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
                input.read(buffer,0,buffer.length);
                input.close();					 
                String fileData = new String(buffer);
                if (fileData!=""||fileData!=null||fileData!=" ")
                {
                    String[] lines = fileData.split("\n");
                //    System.out.println(lines[lines.length-1]);
                    if (lines[lines.length-1]!="")
                        NumMsg += Integer.valueOf(lines[lines.length-1]).intValue();
                }
            }
            catch (FileNotFoundException e) 
            {
                e.printStackTrace();
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
        NumMsg/=NumPeers;
        System.out.println("Avg Messages Per Peer : "+NumMsg);
     }        
    
    public  void AppendFile(String filename,String fileData)
    {
	try 
	{
            BufferedWriter out = new BufferedWriter(new FileWriter(filename,true));
                    
            out.write(fileData);
            out.close();
        }
	catch (IOException e) 
	{
		System.out.println("Writing exception");
        }
    }
    
    public static void main(String[] args) 
    {
        ExptScript e = new ExptScript();
        //e.EvalAvgSize(50, 6,10000);
    //    e.CalculateNoChange(1000, 1,100,0.5);
    //    e.CalculateNumMsg(1000, 1, 100,0.5);
       e.CalculateGlobalMsg(3);
    //    e.CalculateNorm(200, 3, 300, 0.1);
    }
    
}
