// Title:       ClientOfAdd
// Author:      Deepak Rautala
// Modified by: Adam Cheyer
// Description: A simple example agent that on startup, asks the
//		agent community to add two numbers by sending the
//		request "add(10,20,Sum)"

import com.sri.oaa2.com.*;
import com.sri.oaa2.lib.*;
import com.sri.oaa2.icl.*;

public class ClientOfAdd
{
	
	public static void main(String[] args)
	{

		/* Create instance of OAA library using TCP as protocol */
		LibOaa myOaa = new LibOaa(new LibCom(new LibComTcpProtocol(),args));


			/* Connect to the facilitator using the host/port defined in file "setup.pl" */
			if (!myOaa.getComLib().comConnect("parent",IclUtils.icl("tcp(A,B)"),(IclList)IclUtils.icl("[]")))
			{
       	 			System.out.println("Couldn't connect to facilitator");
      	 			return;
    	 		}
    	 		 	
    	 		/* Register my agent info along the connection:  
    	 		      This agent's name will be "client" and the capabilities the agent exposes to
    	 		      the agent community (that other agents can call) is "[]" (empty list) because
    	 		      the agent is a client, not a server.
    	 		*/    	 		
			if (!myOaa.oaaRegister("parent", "client",IclUtils.icl("[]"), (IclList)IclUtils.icl("[]")))
			{
				System.out.println("Could not register");
				return;
			}

			/* Let the Facilitator know I'm finished with any initialization and ready to participate
			   as a member of the agent community.
			*/
    			myOaa.oaaReady(true);


			/* Now, perform the main purpose: ask the agent community to add two numbers, returning
			   the answer in the third parameter.
			*/
			System.out.println("Sending request: add(10,20,Sum)");
    			IclList answers = new IclList();
			if (myOaa.oaaSolve(IclUtils.icl("add(10,20,Sum)"), new IclList(), answers))
			{

			   /* Get the first answer from all the answers potentially returned by agent community */
			   IclTerm response = answers.iclNthTerm(1);
			   /* The response will look like "add(10,20,30)", so grab the "30", the third term */
			   String Sum=response.iclNthTerm(3).toString();
			   System.out.println("Sum= "+ Sum);
			}


 		}

}


