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

jadex.micro.testcases.killcomponent.KillerAgent 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.killcomponent;

import java.util.Collection;
import java.util.Map;

import jadex.base.test.TestReport;
import jadex.base.test.Testcase;
import jadex.bridge.IComponentIdentifier;
import jadex.bridge.IExternalAccess;
import jadex.bridge.component.IExecutionFeature;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.component.IRequiredServicesFeature;
import jadex.bridge.service.types.cms.IComponentManagementService;
import jadex.commons.Tuple2;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IResultListener;
import jadex.micro.annotation.Agent;
import jadex.micro.testcases.TestAgent;

/**
 * 
 */
@Agent
public class KillerAgent extends TestAgent
{
	/**
	 *  Perform the tests.
	 */
	protected IFuture performTests(final Testcase tc)
	{
		tc.setTestCount(2);
		final Future ret = new Future();

		testLocal(1).addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new ExceptionDelegationResultListener(ret)
		{
			public void customResultAvailable(TestReport result)
			{
				tc.addReport(result);
//				ret.setResult(null);
				testRemote(2).addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new ExceptionDelegationResultListener(ret)
				{
					public void customResultAvailable(TestReport result)
					{
						tc.addReport(result);
						ret.setResult(null);
					}
				}));
			}
		}));
		
		return ret;
	}

	@Override
	protected int getTestCount() {
		return 2;
	}

	/**
	 *  Test local.
	 */
	protected IFuture testLocal(final int testno)
	{
		final Future ret = new Future();
		
		performTest(agent.getComponentIdentifier().getRoot(), testno, true)
			.addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new DelegationResultListener(ret)
		{
			public void customResultAvailable(final TestReport result)
			{
				ret.setResult(result);
			}
		}));
		
		return ret;
	}
	
	/**
	 *  Test remote.
	 */
	protected IFuture testRemote(final int testno)
	{
		final Future ret = new Future();
		
		setupRemotePlatform(false).addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(
			new ExceptionDelegationResultListener(ret)
		{
			public void customResultAvailable(final IExternalAccess platform)
			{
				performTest(platform.getComponentIdentifier(), testno, false)
					.addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new DelegationResultListener(ret)));
			}
		}));
		
		return ret;
	}
	
	/**
	 *  Perform the test. Consists of the following steps:
	 *  Create provider agent
	 *  kill it
	 */
	protected IFuture performTest(final IComponentIdentifier root, final int testno, final boolean hassectrans)
	{
		final Future ret = new Future();

		final Future>> resfut = new Future>>();
		IResultListener>> reslis = new DelegationResultListener>>(resfut);
		
		createComponent(ProviderAgent.class.getName()+".class", root, reslis)
			.addResultListener(new ExceptionDelegationResultListener(ret)
		{
			public void customResultAvailable(final IComponentIdentifier cid) 
			{
				IComponentManagementService cms = getCms().get();
				IExternalAccess exta = cms.getExternalAccess(cid).get();
				final TestReport tr = new TestReport("#"+testno, "Test if kill returns result");
				System.out.println("Killing my subcomponent...");
				exta.killComponent().addResultListener(new IResultListener>() {
					@Override
					public void exceptionOccurred(Exception exception) {
						tr.setFailed(exception);
						ret.setResult(tr);
					}

					@Override
					public void resultAvailable(Map result) {
//						System.out.println("got result in killeragent: " +result);
						tr.setSucceeded(result.get("exampleresult").equals("value"));
						ret.setResult(tr);
					}
				});
//				cms.destroyComponent(cid).addResultListener(new IResultListener>() {
//					@Override
//					public void exceptionOccurred(Exception exception) {
//						tr.setFailed(exception);
//						ret.setResult(tr);
//					}
//
//					@Override
//					public void resultAvailable(Map result) {
//						System.out.println("got result in killeragent: " +result);
//						tr.setSucceeded(true);
//						ret.setResult(tr);
//					}
//				});
			}
			
			public void exceptionOccurred(Exception exception)
			{
				exception.printStackTrace();
				super.exceptionOccurred(exception);
			}
		});
		
		return ret;
	}
	
	private Future getCms() {
		Future ret = new Future();
		agent.getComponentFeature(IRequiredServicesFeature.class).searchService(IComponentManagementService.class, RequiredServiceInfo.SCOPE_PLATFORM)
				.addResultListener(new DelegationResultListener(ret));
		return ret;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy