jadex.micro.testcases.intermediate.InvokerAgent 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.intermediate;
import java.util.Collection;
import jadex.base.Starter;
import jadex.base.test.TestReport;
import jadex.base.test.Testcase;
import jadex.bridge.ComponentIdentifier;
import jadex.bridge.IComponentIdentifier;
import jadex.bridge.IExternalAccess;
import jadex.bridge.IInternalAccess;
import jadex.bridge.IResourceIdentifier;
import jadex.bridge.ITransportComponentIdentifier;
import jadex.bridge.LocalResourceIdentifier;
import jadex.bridge.ResourceIdentifier;
import jadex.bridge.component.IArgumentsResultsFeature;
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.bridge.service.types.cms.CreationInfo;
import jadex.bridge.service.types.cms.IComponentManagementService;
import jadex.commons.SReflect;
import jadex.commons.SUtil;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IIntermediateFuture;
import jadex.commons.future.IIntermediateResultListener;
import jadex.commons.future.IResultListener;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.AgentBody;
import jadex.micro.annotation.Description;
import jadex.micro.annotation.Result;
import jadex.micro.annotation.Results;
/**
* The invoker agent tests if intermediate results are directly delivered
* back to the invoker in local and remote case.
*/
@Agent
@Results(@Result(name="testresults", clazz=Testcase.class))
@Description("The invoker agent tests if intermediate results are directly " +
"delivered back to the invoker in local and remote case.")
public class InvokerAgent
{
//-------- attributes --------
@Agent
protected IInternalAccess agent;
//-------- methods --------
/**
* The agent body.
*/
@AgentBody
public void body()
{
final Testcase tc = new Testcase();
if(SReflect.isAndroid())
{
tc.setTestCount(1);
}
else
{
tc.setTestCount(2);
}
final Future ret = new Future();
ret.addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new IResultListener()
{
public void resultAvailable(TestReport result)
{
// System.out.println("tests finished");
agent.getComponentFeature(IArgumentsResultsFeature.class).getResults().put("testresults", tc);
agent.killComponent();
}
public void exceptionOccurred(Exception exception)
{
System.out.println(agent.getComponentFeature(IExecutionFeature.class).isComponentThread()+" "+agent.getComponentIdentifier());
agent.getComponentFeature(IArgumentsResultsFeature.class).getResults().put("testresults", tc);
agent.killComponent();
}
}));
// testLocal().addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new DelegationResultListener(ret)
// {
// public void customResultAvailable(Void result)
// {
// System.out.println("tests finished");
// }
// }));
// testRemote().addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new DelegationResultListener(ret)
// {
// public void customResultAvailable(Void result)
// {
// System.out.println("tests finished");
// }
// }));
testLocal(1, 100, 3).addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new DelegationResultListener(ret)
{
public void customResultAvailable(TestReport result)
{
tc.addReport(result);
if(SReflect.isAndroid())
{
ret.setResult(null);
}
else
{
testRemote(2, 100, 3).addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new DelegationResultListener(ret)
{
public void customResultAvailable(TestReport result)
{
tc.addReport(result);
ret.setResult(null);
}
}));
}
}
}));
}
/**
* Test if local intermediate results are correctly delivered
* (not as bunch when finished has been called).
*/
protected IFuture testLocal(int testno, long delay, int max)
{
return performTest(agent.getComponentIdentifier().getRoot(), testno, delay, max);
}
/**
* Test if remote intermediate results are correctly delivered
* (not as bunch when finished has been called).
*/
protected IFuture testRemote(final int testno, final long delay, final int max)
{
final Future ret = new Future();
// Start platform
try
{
String url = SUtil.getOutputDirsExpression("jadex-applications-micro"); // Todo: support RID for all loaded models.
// String url = process.getModel().getResourceIdentifier().getLocalIdentifier().getUrl().toString();
Starter.createPlatform(new String[]{"-libpath", url, "-platformname", agent.getComponentIdentifier().getPlatformPrefix()+"_*",
"-saveonexit", "false", "-welcome", "false", "-autoshutdown", "false", "-awareness", "false",
// "-logging_level", "java.util.logging.Level.INFO",
"-gui", "false", "-simulation", "false", "-printpass", "false"
}).addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(
new ExceptionDelegationResultListener(ret)
{
public void customResultAvailable(final IExternalAccess platform)
{
ComponentIdentifier.getTransportIdentifier(platform).addResultListener(new ExceptionDelegationResultListener(ret)
{
public void customResultAvailable(ITransportComponentIdentifier result)
{
performTest(result, testno, delay, max)
.addResultListener(agent.getComponentFeature(IExecutionFeature.class).createResultListener(new DelegationResultListener(ret)
{
public void customResultAvailable(final TestReport result)
{
platform.killComponent();
// .addResultListener(new ExceptionDelegationResultListener