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

jadex.micro.testcases.blocking.ShutdownAgent 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.testcases.blocking;

import java.util.Map;

import jadex.bridge.IComponentIdentifier;
import jadex.bridge.IInternalAccess;
import jadex.bridge.component.IExecutionFeature;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.annotation.Service;
import jadex.bridge.service.component.IRequiredServicesFeature;
import jadex.bridge.service.types.cms.CreationInfo;
import jadex.bridge.service.types.cms.IComponentManagementService;
import jadex.commons.SReflect;
import jadex.commons.future.DefaultTuple2ResultListener;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.AgentBody;
import jadex.micro.annotation.Binding;
import jadex.micro.annotation.ComponentType;
import jadex.micro.annotation.ComponentTypes;
import jadex.micro.annotation.RequiredService;
import jadex.micro.annotation.RequiredServices;

/**
 * 
 */
@Agent
@Service
@ComponentTypes(@ComponentType(name="block", clazz=BlockAgent.class))
@RequiredServices(@RequiredService(name="cms", type=IComponentManagementService.class, binding=@Binding(scope=RequiredServiceInfo.SCOPE_PLATFORM)))
public class ShutdownAgent
{
	//-------- attributes --------
	
	/** The agent. */
	@Agent
	protected IInternalAccess agent;
	
	/**
	 * 
	 */
	@AgentBody
	public IFuture body()
	{
		final Future ret = new Future();
		
		IFuture fut = agent.getComponentFeature(IRequiredServicesFeature.class).getRequiredService("cms");
		fut.addResultListener(new ExceptionDelegationResultListener(ret)
		{
			public void customResultAvailable(final IComponentManagementService cms)
			{
//				cms.createComponent("block", new CreationInfo(agent.getComponentIdentifier()))
				cms.createComponent(BlockAgent.class.getName()+".class", new CreationInfo(agent.getComponentIdentifier()))
					.addResultListener(new DefaultTuple2ResultListener>()
				{
					public void firstResultAvailable(final IComponentIdentifier cid)
					{
						// call several times a blocking method on the agent and then terminate it
						
						agent.getComponentFeature(IRequiredServicesFeature.class).searchService(IBlockService.class, cid)
							.addResultListener(new ExceptionDelegationResultListener(ret)
						{
							public void customResultAvailable(IBlockService bs)
							{
								int numBlocks = 1000;
								if (SReflect.isAndroid()) {
									numBlocks = 100;
								}
								for(int i=0; i(ret)
								{
									public void customResultAvailable(Void result)
									{
										cms.destroyComponent(cid).addResultListener(new ExceptionDelegationResultListener, Void>(ret)
										{
											public void customResultAvailable(Map result)
											{
												System.out.println("fini1: "+result);
											}
										});
									}
								});
							}
						});
					}
					
					public void secondResultAvailable(Map result)
					{
						System.out.println("fini2: "+result);
//						String model = ShutdownAgent.class.getName()+".class";
//						cms.createComponent(model, new CreationInfo(agent.getModel().getResourceIdentifier()))
//							.addResultListener(new DefaultTuple2ResultListener>()
//						{
//							public void firstResultAvailable(IComponentIdentifier result)
//							{
								agent.killComponent();
//							}
//							
//							public void secondResultAvailable(Map result)
//							{
//							}
//							
//							public void exceptionOccurred(Exception exception)
//							{
//								System.out.println("except: "+exception);
//							}
//						});
					}
					
					public void exceptionOccurred(Exception exception)
					{
						ret.setException(exception);
					}
				});
			}
		});
	
		return ret;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy