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

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

import java.util.Map;

import jadex.base.test.TestReport;
import jadex.base.test.Testcase;
import jadex.bridge.IComponentIdentifier;
import jadex.bridge.IInternalAccess;
import jadex.bridge.component.IArgumentsResultsFeature;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.component.IRequiredServicesFeature;
import jadex.bridge.service.types.cms.CreationInfo;
import jadex.bridge.service.types.cms.IComponentManagementService;
import jadex.commons.future.DefaultTuple2ResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.ITuple2Future;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.AgentBody;
import jadex.micro.annotation.Binding;
import jadex.micro.annotation.RequiredService;
import jadex.micro.annotation.RequiredServices;
import jadex.micro.annotation.Result;
import jadex.micro.annotation.Results;

/**
 *  Test if result back transfer from injected fields works.
 *  Starts a subagent that uses the result injection and
 *  checks if its result values are correct.
 */
@Agent
@RequiredServices(@RequiredService(name="cms", type=IComponentManagementService.class, 
	binding=@Binding(scope=RequiredServiceInfo.SCOPE_PLATFORM)))
@Results(@Result(name="testresults", clazz=Testcase.class))
public class TestInjectedResultsAgent
{
	/** The agent. */
	@Agent
	protected IInternalAccess agent;
	
	/**
	 *  The agent body.
	 */
	@AgentBody
	public IFuture executeBody()
	{
		final Future ret = new Future();

		final TestReport tr	= new TestReport("#1", "Test if injected results work.");
		
		IFuture fut = agent.getComponentFeature(IRequiredServicesFeature.class).getRequiredService("cms");
		fut.addResultListener(new ExceptionDelegationResultListener(ret)
		{
			public void customResultAvailable(IComponentManagementService cms)
			{
				ITuple2Future> fut = cms.createComponent(InjectedResultsAgent.class.getName()+".class", 
					new CreationInfo(agent.getComponentIdentifier()));
				fut.addResultListener(new DefaultTuple2ResultListener>()
				{
					public void firstResultAvailable(IComponentIdentifier result)
					{
					}
					
					public void secondResultAvailable(Map result)
					{
						Object myres = result.get("myres");
						Object myint = result.get("myint");
						
						if("def_val".equals(myres) && Integer.valueOf(99).equals(myint))
						{
							tr.setSucceeded(true);
						}
						else
						{
							tr.setFailed("Wrong result values: myres="+myres+", myint="+myint);
						}
						
						agent.getComponentFeature(IArgumentsResultsFeature.class).getResults().put("testresults", new Testcase(1, new TestReport[]{tr}));
						ret.setResult(null);
					}
					
					public void exceptionOccurred(Exception exception)
					{
						tr.setFailed("Exception occurred: "+exception);
						agent.getComponentFeature(IArgumentsResultsFeature.class).getResults().put("testresults", new Testcase(1, new TestReport[]{tr}));
						ret.setResult(null);
					}
				});
				
//				, agent.getComponentFeature(IExecutionFeature.class).createResultListener(new IResultListener>>()
//				{
//					public void resultAvailable(Collection> results)
//					{
//						Object myres = Argument.getResult(results, "myres");
//						Object myint = Argument.getResult(results, "myint");
//						
//						if("def_val".equals(myres) && Integer.valueOf(99).equals(myint))
//						{
//							tr.setSucceeded(true);
//						}
//						else
//						{
//							tr.setFailed("Wrong result values: myres="+myres+", myint="+myint);
//						}
//						
//						agent.getComponentFeature(IArgumentsFeature.class).getResults().put("testresults", new Testcase(1, new TestReport[]{tr}));
//						ret.setResult(null);
//					}
//					
//					public void exceptionOccurred(Exception exception)
//					{
//						tr.setFailed("Exception occurred: "+exception);
//						agent.getComponentFeature(IArgumentsFeature.class).getResults().put("testresults", new Testcase(1, new TestReport[]{tr}));
//						ret.setResult(null);
//					}
//				}));
			}
		});
		
		return ret;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy