jadex.micro.testcases.servicevalue.NewsProviderAgent 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.servicevalue;
import java.util.ArrayList;
import java.util.List;
import jadex.bridge.IInternalAccess;
import jadex.bridge.SFuture;
import jadex.bridge.component.IExecutionFeature;
import jadex.commons.future.ISubscriptionIntermediateFuture;
import jadex.commons.future.ITerminationCommand;
import jadex.commons.future.SubscriptionIntermediateFuture;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.AgentBody;
/**
* Agent that provides a news service.
*/
@Agent(autoprovide=true)
public class NewsProviderAgent implements INewsService
{
/** The agent. */
@Agent
protected IInternalAccess agent;
/** The subscriptions. */
protected List> subscriptions = new ArrayList>();
/**
* The agent body.
*/
@AgentBody
public void body()
{
for(int i=0; i<100; i++)
{
agent.getComponentFeature(IExecutionFeature.class).waitForDelay(2000).get();
for(SubscriptionIntermediateFuture sub: subscriptions)
{
if(!sub.addIntermediateResultIfUndone("News "+i))
{
subscriptions.remove(sub);
}
}
}
}
/**
* Subscribe to the newsprovider.
*/
public ISubscriptionIntermediateFuture subscribeToNews()
{
final SubscriptionIntermediateFuture ret = (SubscriptionIntermediateFuture)SFuture.getNoTimeoutFuture(SubscriptionIntermediateFuture.class, agent.getExternalAccess());
ret.setTerminationCommand(new ITerminationCommand()
{
public void terminated(Exception reason)
{
subscriptions.remove(ret);
}
public boolean checkTermination(Exception reason)
{
return true;
}
});
subscriptions.add(ret);
return ret;
}
}