/*
 * Delivered to the U.S. Government in Accordance with Contract FA8650-10-C-7080.
 *
 * EXPORT CONTROLLED
 * ECCN#   EAR99
 * Warning:  This document contains technical data which is controlled under the
 * Bureau of Industry & Security's Export Administration Regulations and may not be
 * exported or re-exported without authorization by the Bureau of Industry & Security.
 * Diversion contrary to US law is prohibited.
 *
 * (c) 2011 United Technologies Corp.
 * Developed with the sponsorship of the Defense Advanced Research Projects Agency (DARPA).
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this data,
 * including any software or models in source or binary form, as well as any drawings, 
 * specifications, and documentation (collectively "the Data"), to deal in the Data without 
 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Data, and to permit persons to whom the Data
 * is furnished to do so, subject to the following conditions:
 *  
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Data.
 *  
 * THE DATA IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, SPONSORS, DEVELOPERS, CONTRIBUTORS, OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE DATA OR THE
 * USE OR OTHER DEALINGS IN THE DATA.
 */

package meta.ampl.runner;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

import meta.util.FileUtils;
/**
 * Encapsulate the methods used to obtain the solution status 
 * for the solver
 *
 */
public class AMPLsolution {
private static final String displaySolveResultFormat = "display solve_result > '%s';";
private static final String displaySolveResultNumFormat = "display solve_result_num >> '%s';";
private static final String closeFileFormat = "close '%s';";
private File file;
private String filename;
  public AMPLsolution() 
  {
	  file = null;
	  filename = null;
  }
  public List<String> getCmds() throws AMPLException 
  {
		try {
			 file = File.createTempFile("sol",".txt");
		} catch (IOException e) {
			throw new AMPLException(e.getMessage());
		}
		List<String> cmds = new ArrayList<String>();
		filename = file.getPath();
		String displayCmd = String.format(displaySolveResultFormat, filename); 
		cmds.add(displayCmd);
		String displayCmd2 = String.format(displaySolveResultNumFormat,filename);
		cmds.add(displayCmd2);
		String closeCmd = String.format(closeFileFormat, filename);
		cmds.add(closeCmd);
	 return cmds;  
  }
  public SolutionStatus getSolutionStatus() throws AMPLException
  {
	  BlockingQueue<Integer> q = new ArrayBlockingQueue<Integer>(1);
	  String text = "Unknown";
	  int code = -1;
      AMPLwait wait = new AMPLwait(file,49,q);
      wait.start();
      try {
		Integer j = q.take();
		if ( j.intValue() > 0 ) throw new AMPLException("TimeOut waiting for " + filename);
		List<String> lines = FileUtils.read(filename);
        for (String line : lines) {
            String [] s = line.split("\\s+");
            if ( s[0].equals("solve_result") ) {
            	text = s[2];
            }
            if ( s[0].equals("solve_result_num") )  {
            	code = Integer.parseInt(s[2]);
            }
 	     }
	} catch (InterruptedException e) {
		throw new AMPLException(e.getMessage());
	}
	return new SolutionStatus(text,code);
  }
}
