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

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

import jadex.bridge.IComponentStep;
import jadex.bridge.IInternalAccess;
import jadex.bridge.component.IExecutionFeature;
import jadex.bridge.service.annotation.Service;
import jadex.commons.future.IFuture;
import jadex.commons.future.IIntermediateFuture;
import jadex.commons.future.ISubscriptionIntermediateFuture;
import jadex.commons.future.IntermediateFuture;
import jadex.commons.future.SubscriptionIntermediateFuture;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.AgentService;
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;

/**
 *  An agent that provides the stepped service.
 */
@Agent
@Service
@ProvidedServices(@ProvidedService(type=IStepService.class,
	implementation=@Implementation(expression="$pojoagent")))
@RequiredServices(@RequiredService(name="block", type=IBlockService.class))
public class StepAgent	implements	IStepService
{
	//-------- attributes --------
	
	/** The agent. */
	@Agent
	protected IInternalAccess	agent;
	
	/** The blocking service. */
	@AgentService
	protected IBlockService	block;
	
	//-------- IIntermediateBlockingService interface --------
	
	/**
	 *  Perform some steps and block some milliseconds in between.
	 */
	public IIntermediateFuture	performSteps(final int steps, final long millis)
	{
		final IntermediateFuture	ret	= new IntermediateFuture();
		
		agent.getComponentFeature(IExecutionFeature.class).waitForDelay(0, new IComponentStep()
		{
			public IFuture execute(IInternalAccess ia)
			{
				for(int i=1; i<=steps; i++)
				{
					ia.getComponentFeature(IExecutionFeature.class).waitForDelay(millis).get();
					ret.addIntermediateResult(Integer.valueOf(i));
				}
				ret.setFinished();
				return IFuture.DONE;
			}
		});
		
		return ret;
	}
	
	/**
	 *  Perform periodical steps and block some milliseconds in between.
	 */
	public ISubscriptionIntermediateFuture	subscribeToSteps(final long millis)
	{
		final SubscriptionIntermediateFuture	ret	= new SubscriptionIntermediateFuture();
		
		agent.getComponentFeature(IExecutionFeature.class).waitForDelay(0, new IComponentStep()
		{
			public IFuture execute(IInternalAccess ia)
			{
				for(int i=1; !ret.isDone(); i++)
				{
					ia.getComponentFeature(IExecutionFeature.class).waitForDelay(millis).get();
					ret.addIntermediateResult(Integer.valueOf(i));
				}
				return IFuture.DONE;
			}
		});
		
		return ret;		
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy