/**
 * The Driver class acts as the central driver of the workbench, the class creates the VPPController
 * object.
 *
 * @author Richard Payne
 * @version 25/04/2008
 */
 
import java.io.*;
import java.util.LinkedList; 
import java.util.Scanner;
import jp.co.csk.vdm.toolbox.api.*;
import jp.co.csk.vdm.toolbox.api.corba.ToolboxAPI.*;
import jp.co.csk.vdm.toolbox.api.corba.VDM.*;

public class Driver
{
	//variables for control of the workbench
	public VDMController control = new VDMController();
	
	/**
	 * Main Method for the workbench.
	 *
	 * @param args the command line arguments
	 */
	public static void main(String args[]) 
	{			
		Driver test = new Driver();
		test.run();
	}
	
	/**
	 * The run method for Driver class
	 */
	public void run()
	{
		try
		{			
			//list for the files to be used in 
			LinkedList files = new LinkedList();
			
			//add *absolute* file locations of VDM++ files.
			files.add(new String("/Models/model1.vpp"));
			files.add(new String("/Models/model2.vpp"));
			
			// Add VDM++ files to project and initialise files.
			// This involves the syntax and type checking of the files. 
			control.init(files);			
						
			control.evaluateCommand("create t := new Test()");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
			
	/**
	 * Method to demonstrate usage of simple commands
	 */
	public void sampleCommands()
	{
		try
		{	
			//some sample operations - ensure use object name as denoted in line above - e.g. 't'
			control.evaluateExpression("t.add(t.map, {t.id1 |-> t.record1})"); 
			control.evaluateExpression("t.remove(t.map, t.id1)");
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * Method to retreive all strings from records in domain of a map
	 *
	 * @return	a list of information the agent knows
	 */
	public LinkedList sampleExpression()
	{	
		LinkedList list = new LinkedList();
		
		try 
		{
			String expr = ("t.getMapRange(t.map)");
			VDMGeneric result = control.evaluateExpression(expr);
			
			//we know result is a set, so can narrow it to a VDMSet object
			VDMSet s = VDMSetHelper.narrow(result);
			
			//declare local variables
			VDMGeneric element;
			String newRng;
			
			while (!(s.IsEmpty()))
			{
				//retreive and remove the first element in the set
				element = s.GetElem();
				s.RemElem(element);
				
				//we know result is a record, so can narrow it to a VDMRecord object
				VDMRecord vdmInfo = VDMRecordHelper.narrow(element);
				
				//retrieve the fields from the record
				String itemName = (vdmInfo.GetField(1)).ToAscii();
				
				Scanner scan = new Scanner(itemName).useDelimiter("\"");
				//get rid of mk_Token part
				scan.next();
				
				//initialise the Information object and add it to the list
				newRng = new String(scan.next());
				list.add(newRng);
			}
		}
		catch(VDMError ex)
		{
			System.out.println("Error occured whilst retreiving onmicient agent knowledge \n" +
							   "This is likely due to a malformed entry in the result gained from the VDM++ toolbox \n"+
							   "VDMError status code: " + ex.err + ", see API guide for further details");
			ex.printStackTrace();
		}
		
		return list;
	}
}

