⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 carrier.java

📁 一个用来控制poineer2型机器人的智能体软件
💻 JAVA
字号:

/**
 * Carrier.java
 *
 * This is the main application class for the CarrierUnit package.
 *
 */

package OaaSaphira.CarrierUnit;

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

/**
* Title:        	The Carrier agent.<p>
* Description:  	Allocates and manages the resources.<p>
* OAA Solvables:	"bringto(Object, Transferplace)",
					"goto(Transferplace)",
					"checkfree(Object,Transferplace)",
					"taskfinished(Task)".<p>
* Company:      	University of Applied Sciences, Hamburg.<p>
* <p>
* If you find this package useful or you want to make comments on it,
* please drop me a line (osherenko@gmx.de).
* @author Alexander M. Osherenko (osherenko@gmx.de)
* @version 1.0
*/

public class Carrier
{
/**
The interface to the Saphira environment.
<p>
Communicates with Saphira starting Colbert scripts, starting tasks etc.
*/
        protected SaphiraManager saphiraManager_ = new SaphiraManager();
/**
* The interface to the OAA facilitator.
* <p>
* Implements the communication feature in the agent.
*/
        protected LibOaa oaaManager_;
        
/**
Defines if the carrier is free and ready for performing tasks.
*/
        protected boolean isFree_ = true;

/**
* Class constructor. Starts the OAA event loop and the Saphira environment.
* <p>
* @param args The command line arguments. Specifies the address and the port number
* where to find the OAA facilitator.
*/
	public Carrier(String[] args)
	{

    //!!! wrong number (1) of arguments in the developer's guide
    //!!! without the args parameter
    oaaManager_ = new LibOaa( new LibCom(new LibComTcpProtocol(), args ) );

 		// First, connects to the facilitator
		if (oaaManager_.getComLib().comConnected("parent")) {
			System.out.println("Already connected");
			return;
		}

    //start the saphira console
    saphiraManager_.start();

    if (!oaaManager_.getComLib().comConnect("parent",
        IclUtils.icl("tcp(A,B)"),
        (IclList)IclUtils.icl("[]"))) {
      		System.out.println("Couldn't connect to the facilitator");
      		return;
    }

		String solvables = 
			  "[" + "bringto(Object, Transferplace)," +
				"goto(Transferplace)," +
				"checkfree(Object,Transferplace)" + 
				"taskfinished(Task)"	+ "]";

		// Once the connection is established, performs handshaking with the facilitator
		if (!oaaManager_.oaaRegister("parent", "carrier", 
			IclUtils.icl(solvables), (IclList) IclUtils.icl("[]")))
		{
			System.out.println("Could not register the solvables");
			return;
		}

		//register the callbacks
		System.out.println ("Registering callbacks");
		oaaManager_.oaaRegisterCallback("oaa_AppDoEvent",
		   new OAAEventListener() {
				public boolean doOAAEvent(IclTerm goal, IclList params, IclList answers)
				{
					return oaaEventCallback(goal, params, answers);
				}
			});


		// Connection succeeded!
		oaaManager_.oaaReady(true);
	}

/**
Determines if the carrier is free and ready for performing tasks.
<p>
@return true if the carrier is free and false otherwise.
*/
	public boolean isFree() {
System.out.println("The carrier is free: " + isFree_);
		return isFree_;
	};

/**
Sets carrier status true/false (free/busy).
@param val The carrier status to set.
*/
	protected void setFree(boolean val) {
System.out.println("Setting the carrier freedom status to : " + val);
		isFree_ = val;	
	}
		
/**
* The OAA event callback. Is called by the OAA library and shouldn't be called directly from user code.
* Entry point for event handling.
<p>
* @param goal Event goal, for example "bringto(cup, transferplace)".
* @param params Event parameters for example "[block(true)]".
* @param answers A list with answers for example "answer1(true), answer2(false)".
* @return true if an answer was found and false otherwise.
*/
	public boolean oaaEventCallback(IclTerm goal, IclList params, IclList answers)
	{
//TODO: analyze synchronization 
		System.out.println( "The strategy function for the carrier");

		if (goal.iclStr().toString().equals("checkfree")) {
			return proceedCheckFree(goal, params, answers);
		};
	
		if (goal.iclStr().toString().equals("bringto")) {
			return proceedBringTo(goal, params, answers);
		};
	
		if (goal.iclStr().toString().equals("goto")) {
			return proceedGoTo(goal, params, answers);
		};

		if (goal.iclStr().toString().equals("taskfinished")) {
			return proceedTaskFinished(goal, params, answers);
		};
	
		return false;

	};


/**
* Implements the bringto activity to move the object specified by obj
* to the location specified by target.
* <p>
* @param obj The object, for example "cup".
* @param target The target, for example "transferplace".
*/
	protected void doBringTo(String obj, String target) {
		//TODO: should be replaced by a concrete function
		//here only for testing
		System.out.println("Bringto before performing");
		//TODO: should be replaced by a concrete function
		//here only for testing
		System.out.println("Calling");
		
		if (obj.equals("cup") && target.equals("transferplace")) {
			 saphiraManager_.sfLoadEvalFile("dobringtoab.act");
			 saphiraManager_.sfStartTask("dobringtoab", "dobringtoab", 0, 0, 0, 0);
		}
			
		if (obj.equals("cup") && target.equals("finish")) {
			 saphiraManager_.sfLoadEvalFile("dobringtoac.act");
			 saphiraManager_.sfStartTask("dobringtoac", "dobringtoac", 0, 0, 0, 0);
		}
	};
	
/**
* Manages the bringto operation.
* <p>
* @param goal Event goal, for example "bringto(cup, transferplace)".
* @param params Event parameters, for example "[block(true)]".
* @param answers The address of this agent, for example "addr(tcp('141.22.10.3'), 3378)".
* @return true if an answer was found and false otherwise.
*/
	public boolean proceedBringTo(IclTerm goal, IclList params, IclList answers)
	{
		
		//check the arguments
	
		//can I carry the specified object?
		
		if (!goal.iclNthTerm(1).toString().equals("cup") )
		{			
				System.out.println("Can't carry the '" + goal.iclNthTerm(1) + "' object");
				return false;
		};
	
		//the carrier becomes busy
		if (isFree()) {
			System.out.println("The carrier is bringing the object to the transfer place");
			setFree(false);
	
			doBringTo(goal.iclNthTerm(1).toString(), goal.iclNthTerm(2).toString());
	
			answers.iclAddToList(oaaManager_.oaaPrimaryAddress(), true);
	
			setFree(true);
			System.out.println("Bringto done");
	
			return true;
	
		} else {
			System.out.println( "ERROR: The carrier is busy and can't perform the bringto operation");
			return false;
		};
	}

/**
* Implements the goto activity. The carrier goes to the location specified by target.
* <p>
* @param target Currently only a placeholder.
*/
	protected void doGoTo(String target) {

		//TODO: should be replaced by a concrete function
		//here only for testing
		System.out.println("Goto before performing");
		
		saphiraManager_.sfLoadEvalFile("dogoto.act");

	};

/**
* Manages the goto operation.
* <p>
* @param goal Event goal, for example "goto(transferplace)".
* @param params Event parameters, for example "[block(true)]".
* @param answers The address of this agent, for example "addr(tcp('141.22.10.3', 3378), 200)".
* @return true if an answer was found and false otherwise.
*/
	protected boolean proceedGoTo(IclTerm goal, IclList params, IclList answers)
	{
		//check the arguments
	
		//can I carry the specified object?
		
		if (!goal.iclNthTerm(1).toString().equals("transferplace") )
		{			
				System.out.println("ERROR: Can't go to the '" + goal.iclNthTerm(1) + "' place");
				return false;
		};
	
		//the carrier becomes busy
		if (isFree()) {
			System.out.println("The carrier is going to the transfer place");
			setFree(false);
	
	//TODO: check if the call is correct
			doGoTo(goal.iclNthTerm(1).toString());
	
			answers.iclAddToList(oaaManager_.oaaPrimaryAddress(), true);
	
			setFree(true);
			System.out.println("Goto done");
	
			return true;
			
		} else {
			System.out.println( "ERROR: The carrier is busy and can't perform the goto operation");
			return false;
		};
	}

/**
* Manages the checkfree operation. Checks if the carrier can move the specified object
* to the specified location.
* <p>
* @param goal Event goal, for example "checkfree(cup,transferplace)".
* @param params Event parameters, for example "[block(true)]".
* @param answers The address of this agent, for example "addr(tcp('141.22.10.3', 3378), 200)".
* @return true if an answer was found and false otherwise.
*/
	protected boolean proceedCheckFree(IclTerm goal, IclList params, IclList answers)
	{
		
		if (!goal.iclNthTerm(1).toString().equals("cup") )
		{			
				System.out.println("ERROR: Can't carry the '" + goal.iclNthTerm(1) + "' object");
				return false;
		};
		
		if (isFree()) {
			answers.iclAddToList( oaaManager_.oaaPrimaryAddress(), true );
			return true;
		}
		return false;
	}
	

/**
* Implements the taskfinished activity. Checks if the task specified by task
* is finished in Saphira.
* <p>
* @param task A Saphira task name.
*/
	protected int doProceedTaskFinished(String task) {

		saphiraManager_.sfMessage("Waiting for the end of the function " + task);
		
		return saphiraManager_.sfTaskFinished(task);

	};

/**
* Manages the taskfinished operation. Checks if the specified task is finished.
* <p>
* @param goal Event goal, for example "taskfinished(dobringtoac)".
* @param params Event parameters, for example "[block(true)]".
* @param answers The address of this agent, for example "addr(tcp('141.22.10.3', 3378), 200)".
* @return true if an answer was found and false otherwise.
*/
	protected boolean proceedTaskFinished(IclTerm goal, IclList params, IclList answers)
	{
		//check the arguments
	
		if (goal.iclNthTerm(1).toString().equals("") )
		{			
				System.out.println("ERROR: Specify the argument for the taskfinished service");
				return false;
		};
	
		System.out.println("The carrier checks if the task is finished");

		//call the sfTaskFinished function
		if (doProceedTaskFinished(goal.iclNthTerm(1).toString())==1)
		{
				answers.iclAddToList(oaaManager_.oaaPrimaryAddress(), true);

				System.out.println("Proceed Task Finished done");
		
				return true;
		}
		
		return false;
			
	}
	
/**
* Tests the Saphira interface.
*/
	public void testSaphira()
	{
		saphiraManager_.sfMessage("Testing sfMessage Function");
		saphiraManager_.sfLoadEvalFile("sample.act");
		System.out.println(
			saphiraManager_.sfTaskFinished("patrol")
		);

	}

/**
* Runs the Carrier agent.<p>
* @param args Command line arguments.
*/
	public static void main(String[] args)
	{
		//instantiate the App
		new Carrier(args);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -