/*
 * SlaveNode.java
 *
 * Created on February 14, 2005, 4:14 PM
 */

package DDMT.Nodes;

import java.util.Vector;
import java.io.*;
import jade.lang.acl.*;
import java.util.regex.*;
import DDMT.Agents.NodeAgent.AgentInterface;
/**
 *
 * @author  jk3
 */
public class SlaveNode extends NodeCode{
    
    //Define patterns for integers, strings, doubles, first string, and last string
    private Pattern integerP = Pattern.compile("\\d+");
    private Pattern doubleP = Pattern.compile("\\d+\\p{Punct}\\d+");
    private Pattern stringP = Pattern.compile("\\S+\\s");
    private Pattern alphaStringP = Pattern.compile("[a-zA-Z]\\S*");
    private Pattern neighborP = Pattern.compile("\\s\\d+\\s\\d+\\p{Punct}\\d+");
    private Pattern fileP = Pattern.compile("\\S+\\s\\S+");
    private Pattern firstP = Pattern.compile("\\A\\S+");
    private Pattern lastP = Pattern.compile("\\S+\\Z");
    private Pattern middleP = Pattern.compile("\\s\\S+\\s");
    private Pattern relationP = Pattern.compile("\\A@[Rr][Ee][Ll][Aa][Tt][Ii][Oo][Nn]");
    private Pattern attributeTypeP = Pattern.compile("\\A@[Aa][Tt][Tt][Rr][Ii][Bb][Uu][Tt][Ee]\\s+\\S+\\s+\\S+");
    private Pattern attributeP = Pattern.compile("\\[S^,]*");
    private Pattern heteroDistP = Pattern.compile("\\s(\\S+)");
    private Matcher heteroDistM;
    private Matcher attributeM;
    private Matcher attributeTypeM;
    private Matcher relationM;
    private Matcher fileM;
    private Matcher firstM;
    private Matcher lastM;
    private Matcher middleM;
    private Matcher integerM;
    private Matcher doubleM;
    private Matcher stringM;
    private Matcher alphaStringM;
    private Matcher neighborM;
    
    //State of the node
    int state;
    
    //Array of data
    Vector data;
    
    public SlaveNode()
    {
        state = 0;
    }
    
    public void run(ACLMessage message)
    {
    }
    
    public void start()
    {
        //System.out.println(started");
        this.openFile();
        
        iface.printMessage("started");
        int total = 0;
        for( int i = 0; i< data.size(); i++)
        {
            total += ((Integer)data.elementAt(i)).intValue();
        }
        
        //System.out.println( ((Vector)this.getNeighbors()).size());
        //Find the neighbor
        int numNeighbor = ((Neighbor)((Vector)this.getNeighbors()).elementAt(0)).getNumber();
        
        iface.sendMessage(new String(""+total), numNeighbor);
        //System.out.println("From: "+this.getNumber()+" To: "+numNeighbor+" Content: "+total);
    }
    
    /**
     *Method to open a data file
     */
    private void openFile()
    {
        data = new Vector();
        //Delete the old file
        //System.out.println(this.getDataLocation());
        File file = new File(this.getDataLocation());
        
        //Update display
        Vector attributeNames = new Vector();
        int numTuples = 0;
        try{
            
            //Pattern that will be defined for a tuple
            Pattern tupleP = null;
            Matcher tupleM;

            //Read the data types
            FileReader reader = new FileReader(file);
            BufferedReader bReader = new BufferedReader(reader);
            String line = bReader.readLine();
            boolean allAttributes = false; //Boolean that shows whether or not all the attributes have been read
            while(line != null)
            {
                //Find the attribute names and put them in the list
                attributeTypeM = attributeTypeP.matcher(line);
                if( attributeTypeM.matches() )
                {
                    middleM = middleP.matcher(line);
                    middleM.find();
                    Pattern midP = Pattern.compile("\\s+(.+)\\s+");
                    Matcher midM = midP.matcher(middleM.group());
                    midM.find();
                    attributeNames.add(midM.group(1));
                        
                }
                
                //If the line isn't an attribute but there are attributes in the system, then assume that we have all the attributes and generate the pattern.
                else if( attributeNames.size() > 0 && allAttributes == false )
                {
                    allAttributes = true;
                    
                    //Define the tuple
                    String output = new String("(\\s+)?\\S+");
                    for( int i = 1; i< attributeNames.size(); i++)
                    {
                        output = output + "\\s+\\S+";
                    }
                    tupleP = Pattern.compile(output);
                }
                
                //If all the attributes have been read in and the line is a tuple, then...
                else if( tupleP != null )
                {
                    tupleM = tupleP.matcher(line);
                    if( tupleM.find() )
                    {
                        if( !line.equals("@DATA") )
                        {
                            data.addElement(new Integer(Integer.parseInt(line)));
                        }
                    }
                }
                
                line = bReader.readLine();
            }
            reader.close();
            bReader.close();
                
        }catch(IOException ex)
        {
            System.out.println("Error reading local data file!");
            ex.printStackTrace();
        }
    }
    
    public void run(String message) {
    }
    
}
