All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jadex.bdi.tutorial.EnglishGermanTranslateSentencePlanF4 Maven / Gradle / Ivy

Go to download

The Jadex BDI applications package contain several example applications, benchmarks and testcases using BDI agents.

The newest version!
package jadex.bdi.tutorial;

import jadex.bdi.runtime.GoalFailureException;
import jadex.bdi.runtime.IGoal;
import jadex.bdi.runtime.IMessageEvent;
import jadex.bdi.runtime.Plan;
import jadex.bridge.IComponentIdentifier;
import jadex.bridge.fipa.SFipa;
import jadex.bridge.service.search.SServiceProvider;
import jadex.bridge.service.types.df.IDF;
import jadex.bridge.service.types.df.IDFComponentDescription;
import jadex.bridge.service.types.df.IDFServiceDescription;
import jadex.commons.SUtil;

import java.util.StringTokenizer;

/**
 *  Tries to translate a sentence word by word
 *  using a translation service for words.
 */
public class EnglishGermanTranslateSentencePlanF4 extends Plan
{
	//-------- attributes --------

	/** The translation agent. */
	protected IComponentIdentifier ta;

	//-------- constructors --------

	/**
	 *  Create a new plan.
	 */
	public EnglishGermanTranslateSentencePlanF4()
	{
		getLogger().info("Created:"+this);
	}

	//-------- methods --------

	/**
	 *  The plan body.
	 */
	public void body()
	{
		while(true)
		{
			// Read the user request.
			IMessageEvent mevent = waitForMessageEvent("request_translatesentence");

			// Save the words of the sentence.
			String cont = (String)mevent.getParameter(SFipa.CONTENT).getValue();
			StringTokenizer stok = new StringTokenizer(cont, " ");
			stok.nextToken();
			stok.nextToken();
			String[] words = new String[stok.countTokens()];
			String[] twords = new String[words.length];
			for(int i=0; stok.hasMoreTokens(); i++)
			{
				words[i] = stok.nextToken();
			}

			// Search a translation agent.
			while(ta==null)
			{
				IDF	dfservice	= (IDF)SServiceProvider.getServiceUpwards(getServiceContainer(), IDF.class).get(this);
				
				// Create a service description to search for.
				IDFServiceDescription sd = dfservice.createDFServiceDescription(null, "translate english_german", null);
				IDFComponentDescription ad = dfservice.createDFComponentDescription(null, sd);

				// Use a subgoal to search for a translation agent
				IGoal ft = createGoal("df_search");
                ft.getParameter("description").setValue(ad);

				dispatchSubgoalAndWait(ft);
                //Object result = ft.getResult();
                IDFComponentDescription[]	result = (IDFComponentDescription[])ft.getParameterSet("result").getValues();

				if(result.length>0)
				{
					this.ta = result[0].getName();
				}
				else
				{
					//if(result instanceof Exception)
					//	((Exception)result).printStackTrace();
					System.out.println("No translation agent found.");
					waitFor(5000);
				}
			}

			// Translate the words.
			for(int i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy