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

jadex.micro.examples.lottery.PlayerAgent 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.lottery;

import java.util.Collection;

import jadex.bridge.IInternalAccess;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.search.SServiceProvider;
import jadex.commons.IResultCommand;
import jadex.commons.future.IFuture;
import jadex.commons.future.IIntermediateResultListener;
import jadex.commons.future.ITerminableIntermediateFuture;
import jadex.micro.annotation.Agent;
import jadex.micro.annotation.AgentBody;
import jadex.micro.annotation.Binding;
import jadex.micro.annotation.RequiredService;
import jadex.micro.annotation.RequiredServices;

@Agent
@RequiredServices(@RequiredService(name="ls", type=ILotteryService.class, binding=@Binding(scope=RequiredServiceInfo.SCOPE_GLOBAL)))
public class PlayerAgent
{
	@Agent
	protected IInternalAccess agent;
	
	@AgentBody
	public void body()
	{
//		final ILotteryService ls = SServiceProvider.getService(agent, ILotteryService.class, RequiredServiceInfo.SCOPE_GLOBAL).get();
		final ILotteryService ls = (ILotteryService)SServiceProvider.waitForService(agent, "ls", 3, 3000).get();
		
//		ILotteryService ls = SServiceProvider.waitForService(agent, new IResultCommand, Void>()
//		{
//			public IFuture execute(Void args)
//			{
//				return SServiceProvider.getService(agent, ILotteryService.class, RequiredServiceInfo.SCOPE_GLOBAL);
//			}
//		}, 10, 3000).get();
		
		ITerminableIntermediateFuture sub = ls.subscribeToLottery();
		
		sub.addIntermediateResultListener(new IIntermediateResultListener()
		{
			public void exceptionOccurred(Exception exception)
			{
				System.out.println("Exception: "+exception);
			}
			
			public void resultAvailable(Collection result)
			{
				System.out.println("Lottery ended.");
			}
			
			public void intermediateResultAvailable(String item)
			{
//				System.out.println("Item offered: "+item);
				
				System.out.println(agent.getComponentIdentifier()+": "+(ls.claimItem(item).get()? "I won item: ": "I did not win item: ")+item);
			}
			
			public void finished()
			{
				System.out.println("Lottery ended.");
			}
		});
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy