jadex.micro.testcases.CAgent Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-applications-micro Show documentation
Show all versions of jadex-applications-micro Show documentation
The Jadex micro applications package contains several example applications, benchmarks and testcases using micro agents.
package jadex.micro.testcases;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import jadex.base.test.TestReport;
import jadex.base.test.Testcase;
import jadex.bridge.IInternalAccess;
import jadex.bridge.component.IArgumentsResultsFeature;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.annotation.Service;
import jadex.bridge.service.component.IRequiredServicesFeature;
import jadex.bridge.service.search.SServiceProvider;
import jadex.commons.future.DefaultResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IIntermediateFuture;
import jadex.commons.future.IntermediateFuture;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.AgentBody;
import jadex.micro.annotation.Binding;
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;
import jadex.micro.annotation.Result;
import jadex.micro.annotation.Results;
/**
* Simple test agent with one service for testing parameter and result copying.
*/
@ProvidedServices(@ProvidedService(type=ICService.class, implementation=@Implementation(expression="$pojoagent")))
@RequiredServices(@RequiredService(name="cservice", type=ICService.class, binding=@Binding(scope=RequiredServiceInfo.SCOPE_LOCAL)))
@Results(@Result(name="testresults", clazz=Testcase.class))
@Service(ICService.class)
@Agent
public class CAgent implements ICService
{
@Agent
protected IInternalAccess agent;
/**
* Test if copy parameters work.
*/
@AgentBody
public IFuture executeBody()
{
final Future ret = new Future();
final List testcases = new ArrayList();
// Test with required service proxy.
IFuture fut = agent.getComponentFeature(IRequiredServicesFeature.class).getRequiredService("cservice");
fut.addResultListener(new DefaultResultListener()
{
public void resultAvailable(ICService result)
{
testService(testcases, result)
.addResultListener(new DefaultResultListener()
{
public void resultAvailable(Void result)
{
// Test with provided service proxy.
SServiceProvider.getService(agent, ICService.class)
.addResultListener(new DefaultResultListener()
{
public void resultAvailable(ICService result)
{
testService(testcases, result)
.addResultListener(new DefaultResultListener()
{
public void resultAvailable(Void result)
{
agent.getComponentFeature(IArgumentsResultsFeature.class).getResults().put("testresults", new Testcase(testcases.size(),
(TestReport[])testcases.toArray(new TestReport[testcases.size()])));
// killAgent();
ret.setResult(null);
}
});
}
});
}
});
}
});
return ret;
}
/**
* Test if no copy works.
*/
public IFuture testArgumentReference(Object arg, int hash)
{
// System.out.println("called service");
return arg.hashCode()==hash ? IFuture.TRUE: IFuture.FALSE;
}
/**
* Test if no copy works.
*/
public IFuture testArgumentCopy(Object arg, int hash)
{
return arg.hashCode()!=hash ? IFuture.TRUE: IFuture.FALSE;
}
/**
* Test if result value can be passed by reference.
*/
public IFuture