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

jadex.micro.tutorial.ChatE3Agent Maven / Gradle / Ivy

Go to download

The Jadex micro applications package contains several example applications, benchmarks and testcases using micro agents.

There is a newer version: 4.0.267
Show newest version
package jadex.micro.tutorial;

import java.util.Map;

import jadex.bridge.IComponentIdentifier;
import jadex.bridge.IComponentStep;
import jadex.bridge.IInternalAccess;
import jadex.bridge.component.IExecutionFeature;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.component.IRequiredServicesFeature;
import jadex.bridge.service.search.SServiceProvider;
import jadex.bridge.service.types.clock.IClockService;
import jadex.commons.future.DefaultResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.AgentArgument;
import jadex.micro.annotation.AgentBody;
import jadex.micro.annotation.AgentCreated;
import jadex.micro.annotation.Argument;
import jadex.micro.annotation.Arguments;
import jadex.micro.annotation.Binding;
import jadex.micro.annotation.Description;
import jadex.micro.annotation.Implementation;
import jadex.micro.annotation.ProvidedService;
import jadex.micro.annotation.ProvidedServices;
import jadex.micro.annotation.RequiredService;
import jadex.micro.annotation.RequiredServices;

/**
 *  Chat micro agent. 
 */
@Description("This agent provides a basic chat service.")
@Agent
@ProvidedServices(@ProvidedService(type=IChatService.class, 
	implementation=@Implementation(value=ChatServiceD5.class)))
@RequiredServices({
	@RequiredService(name="clockservice", type=IClockService.class, 
		binding=@Binding(scope=Binding.SCOPE_PLATFORM)),
	@RequiredService(name="chatservices", type=IChatService.class, multiple=true,
		binding=@Binding(dynamic=true, scope=Binding.SCOPE_GLOBAL)),
	@RequiredService(name="regservice", type=IRegistryServiceE3.class)
})
@Arguments(@Argument(name="nickname", clazz=String.class, defaultvalue="\"Willi\""))
public class ChatE3Agent
{
	/** The agent. */
	@Agent
	protected IInternalAccess agent;
	
	/** The nickname. */
	@AgentArgument
	protected String nickname;
	
	/** The injected service. */
//	@AgentService
	protected IRegistryServiceE3 regservice;
	
	@AgentCreated
	public IFuture init()
	{
		final Future ret = new Future();
		
		IFuture	fut	= agent.getComponentFeature(IRequiredServicesFeature.class).getRequiredService("regservice");
		fut.addResultListener(new ExceptionDelegationResultListener(ret)
		{
			public void customResultAvailable(final IRegistryServiceE3 rs)
			{
				regservice = rs;
				ret.setResult(null);
			}
			
			public void exceptionOccurred(Exception exception)
			{
				System.out.println("exception, could not find appreg service: "+exception);
//				IRegistryServiceE3 reg = SServiceProvider.getLocalService(agent, IRegistryServiceE3.class);
				IFuture	fut	= agent.getComponentFeature(IRequiredServicesFeature.class).getRequiredService("regservice");
				super.exceptionOccurred(exception);
			}
		});
		return ret;
	}
	
	/**
	 *  Execute the functional body of the agent.
	 *  Is only called once.
	 */
	@AgentBody
	public void executeBody()
	{
//		IFuture	regservice	= agent.getComponentFeature(IRequiredServicesFeature.class).getRequiredService("regservice");
//		regservice.addResultListener(new DefaultResultListener()
//		{
//			public void resultAvailable(final IRegistryServiceE3 rs)
//			{
				regservice.register(agent.getComponentIdentifier(), nickname);
				
				agent.getComponentFeature(IExecutionFeature.class).waitForDelay(10000, new IComponentStep()
				{
					public IFuture execute(IInternalAccess ia)
					{
						regservice.getChatters().addResultListener(new DefaultResultListener>()
						{
							public void resultAvailable(Map result)
							{
								System.out.println("The current chatters: "+result);
							}
						});
						return IFuture.DONE;
					}
				});
//			}
//			
//			public void exceptionOccurred(Exception exception)
//			{
//				super.exceptionOccurred(exception);
//			}
//		});
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy