import java.io.*;
import java.util.*;

public class RunClusterModel implements Runnable {
  ClusterWeightedStatePredictor cwsp;
  String ndim, model;
  int modeldim;

  public static void main(String arg[]){
    // arg0 is the name of the model
    // arg1 is number of dimensions
    (new Thread(new RunClusterModel(arg[0], arg[1]))).start();
  }
  
  RunClusterModel(String cm, String nd)
  { ndim = nd; model = cm;}

  public void run() {
    FeatureVector f;
    StreamTokenizer st;
    ClusterViewer2D cv2d;
    State s1, s2, s3, s4;
    int ct;


    // First load the model and check if the dimensions match
    System.out.println("Loading model "+model);
    try {
      FileInputStream fis = new FileInputStream(model);
      ObjectInputStream ois = new ObjectInputStream(fis);
      cwsp = (ClusterWeightedStatePredictor) ois.readObject();
      fis.close();
    } catch (Exception e){
      System.out.println("Exception "+e);
    }

    System.out.println("Ready for "+ndim+" dimensional data\n");
    modeldim = Integer.parseInt(ndim);
    if(cwsp.dim != modeldim){
      System.out.println("Loaded model did not match specified dimensions: "+
			 cwsp.dim+" != "+modeldim);
    }

    st = new StreamTokenizer(new InputStreamReader(System.in));
    st.parseNumbers();
    st.eolIsSignificant(false);
    try {
      double store[] = new double[modeldim+1];
      int something = 0;
    for(st.nextToken(); st.ttype != StreamTokenizer.TT_EOF; st.nextToken()){
      if(st.ttype == StreamTokenizer.TT_NUMBER){
	store[something] = st.nval;
	something++;
	if(something == (modeldim)){
	  f = new FeatureVector(modeldim);
	  for(ct=0;ct<modeldim;ct++){
	    f.vec[ct] = store[ct];
	  }
	  State preds = cwsp.predict(f);
	  System.out.println("Predict: "+preds);
	  something = 0;
	}
      }
    }
    } catch (Exception e) { System.out.println("Whoa!"+e); }

  }
}


