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

jadex.micro.examples.mandelbrot.DisplayService 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.examples.mandelbrot;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import jadex.bridge.SFuture;
import jadex.bridge.service.annotation.Service;
import jadex.bridge.service.annotation.ServiceComponent;
import jadex.commons.future.IFuture;
import jadex.commons.future.ISubscriptionIntermediateFuture;
import jadex.commons.future.SubscriptionIntermediateFuture;

/**
 *  The service allows displaying results in the frame
 *  managed by the service providing agent.
 */
@Service
public class DisplayService implements IDisplayService
{
	//-------- attributes --------
	
	/** The agent. */
	@ServiceComponent
	protected DisplayAgent agent;
	
	/** The display subscribers. */
	protected Map> subscribers = new HashMap>();

	//-------- IDisplayService interface --------

	/**
	 *  Display the result of a calculation.
	 */
	public IFuture displayResult(AreaData result)
	{
//		System.out.println("displayRes: "+agent.getComponentIdentifier());
//		agent.getPanel().setResults(result);
		String id = result.getDisplayId();
		if(id!=null)
		{
			SubscriptionIntermediateFuture sub = subscribers.get(id);
			sub.addIntermediateResult(result);
		}
		else
		{
			// todo: use default display
			for(Iterator> it=subscribers.values().iterator(); it.hasNext(); )
			{
				SubscriptionIntermediateFuture sub = it.next();
				sub.addIntermediateResult(result);
			}
		}
		return IFuture.DONE;
	}


	/**
	 *  Display intermediate calculation results.
	 */
	public IFuture displayIntermediateResult(ProgressData progress)
	{
//		System.out.println("displayInRes");
//		agent.getPanel().addProgress(progress);
		String id = progress.getDisplayId();
		if(id!=null)
		{
			SubscriptionIntermediateFuture sub = subscribers.get(id);
			sub.addIntermediateResult(progress);
		}
		else
		{
			// todo: use default display
			for(Iterator> it=subscribers.values().iterator(); it.hasNext(); )
			{
				SubscriptionIntermediateFuture sub = it.next();
				sub.addIntermediateResult(progress);
			}
		}
		
		return IFuture.DONE;
	}
	
	/**
	 *  Subscribe to display events.
	 */
	public ISubscriptionIntermediateFuture subscribeToDisplayUpdates(String displayid)
	{
//		SubscriptionIntermediateFuture ret = new SubscriptionIntermediateFuture();
		final SubscriptionIntermediateFuture	ret	= (SubscriptionIntermediateFuture)SFuture.getNoTimeoutFuture(SubscriptionIntermediateFuture.class, agent.agent);

		subscribers.put(displayid, ret);
		return ret;
	}
}