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

jadex.micro.testcases.servicevalue.NewsProviderAgent 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.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;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy