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

jadex.micro.testcases.intermediate.IntermediateResultProviderAgent 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.intermediate;

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.IntermediateFuture;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.Description;
import jadex.micro.annotation.Implementation;
import jadex.micro.annotation.ProvidedService;
import jadex.micro.annotation.ProvidedServices;

/**
 *  Agent that provides a service with intermediate results.
 */
@Agent
@Service
@ProvidedServices(@ProvidedService(type=IIntermediateResultService.class, implementation=@Implementation(expression="$pojoagent")))
@Description("Agent that provides a service with intermediate results")
public class IntermediateResultProviderAgent implements IIntermediateResultService
{
	//-------- attributes ---------
	
	/** The agent. */
	@Agent
	protected IInternalAccess agent;
	
	//-------- constructors ---------

	/**
	 *  Get the results.
	 *  @param delay The delay that is waited between intermediate results.
	 *  @param max The number of intermediate results that will be returned.
	 *  @return The results.
	 */
	public IIntermediateFuture getResults(final long delay, final int max)
	{
		final IntermediateFuture ret = new IntermediateFuture();

		final int[] cnt = new int[1];
//		final int max = 5;
//		final long delay = 200;
		
//		System.out.println("start: "+System.currentTimeMillis());
		agent.getComponentFeature(IExecutionFeature.class).waitForDelay(delay, new IComponentStep()
		{
			public IFuture execute(IInternalAccess ia)
			{
//				System.out.println("setting intermediate result: "+cnt[0]);//+" - "+System.currentTimeMillis());
				ret.addIntermediateResult("step("+(cnt[0]++)+"/"+max+")");
				if(cnt[0]==max)
				{
					ret.setFinished();
				}
				else
				{
					agent.getComponentFeature(IExecutionFeature.class).waitForDelay(delay, this);
				}
				return null;
			}
		});
		
		return ret;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy