jadex.web.examples.puzzle.agent.PuzzleService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jadex-applications-web Show documentation
Show all versions of jadex-applications-web Show documentation
Jadex examples that can be deployed as web application.
package jadex.web.examples.puzzle.agent;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;
import jadex.bdiv3.runtime.IGoal;
import jadex.bdiv3x.features.IBDIXAgentFeature;
import jadex.bridge.IComponentStep;
import jadex.bridge.IExternalAccess;
import jadex.bridge.IInternalAccess;
import jadex.bridge.component.IExecutionFeature;
import jadex.bridge.service.RequiredServiceInfo;
import jadex.bridge.service.annotation.Service;
import jadex.bridge.service.annotation.ServiceComponent;
import jadex.bridge.service.annotation.ServiceShutdown;
import jadex.bridge.service.annotation.ServiceStart;
import jadex.bridge.service.component.IRequiredServicesFeature;
import jadex.bridge.service.search.SServiceProvider;
import jadex.bridge.service.types.clock.IClockService;
import jadex.bridge.service.types.settings.ISettingsService;
import jadex.commons.IPropertiesProvider;
import jadex.commons.Properties;
import jadex.commons.Property;
import jadex.commons.future.DelegationResultListener;
import jadex.commons.future.ExceptionDelegationResultListener;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;
import jadex.commons.future.IResultListener;
import jadex.web.examples.puzzle.Board;
import jadex.web.examples.puzzle.HighscoreEntry;
import jadex.web.examples.puzzle.IPuzzleService;
import jadex.web.examples.puzzle.Move;
import jadex.xml.bean.JavaReader;
import jadex.xml.bean.JavaWriter;
/**
* Implementation of the puzzle service.
*/
@Service(IPuzzleService.class)
public class PuzzleService implements IPuzzleService, IPropertiesProvider
{
//-------- constants --------
/** The number of entries per board size in the high score list. */
protected static final int MAX_ENTRIES = 10;
//-------- attributes --------
/** The agent to which the service belongs. */
@ServiceComponent
protected IInternalAccess agent;
/** The external access for decoupling settings service calls. */
// Hack!!! Remove.
protected IExternalAccess exta;
/** The highscore entries (boardsize->sorted set). */
protected Map> entries;
//-------- constructors --------
/**
* Init method called on service startup.
*/
@ServiceStart
public IFuture startService()
{
final Future ret = new Future();
exta = agent.getExternalAccess();
entries = new HashMap>();
agent.getComponentFeature(IRequiredServicesFeature.class).searchService(ISettingsService.class, RequiredServiceInfo.SCOPE_PLATFORM)
.addResultListener(new IResultListener()
{
public void resultAvailable(ISettingsService settings)
{
settings.registerPropertiesProvider("puzzle", PuzzleService.this)
.addResultListener(new DelegationResultListener(ret)
{
@Override
public void customResultAvailable(Void result)
{
super.customResultAvailable(result);
}
@Override
public void exceptionOccurred(Exception exception)
{
super.exceptionOccurred(exception);
}
});
}
public void exceptionOccurred(Exception exception)
{
ret.setResult(null);
}
});
return ret;
}
/**
* Termination method called on service shutdown.
*/
@ServiceShutdown
public IFuture shutdownService()
{
final Future ret = new Future();
// Does not work, because capability service container no longer available after component cleanup.
// agent.getServiceContainer().searchService(ISettingsService.class, RequiredServiceInfo.SCOPE_PLATFORM)
SServiceProvider.getService(exta, ISettingsService.class, RequiredServiceInfo.SCOPE_PLATFORM)
.addResultListener(new IResultListener()
{
public void resultAvailable(ISettingsService settings)
{
settings.deregisterPropertiesProvider("puzzle")
.addResultListener(new DelegationResultListener(ret));
}
public void exceptionOccurred(Exception exception)
{
ret.setResult(null);
}
});
return ret;
}
//-------- IPuzzleService interface --------
/**
* Solve the game and give a hint on the next move.
* @param board The current board state.
* @param timeout A timeout to stop, when no solution is found in time (-1 for no timeout).
* @return The tile to move next.
* @throws Exception in future, when puzzle can not be solved in time.
*/
public IFuture hint(final Board board, final long timeout)
{
final Future ret = new Future();
final int depth = board.getMoves().size();
final IGoal goal = agent.getComponentFeature(IBDIXAgentFeature.class).getGoalbase().createGoal("makemove");
goal.getParameter("board").setValue(board); // It is safe to use the board object, as it is passed as a copy to the service automatically.
long time = SServiceProvider.getLocalService(agent, IClockService.class, RequiredServiceInfo.SCOPE_PLATFORM).getTime();
goal.getParameter("deadline").setValue(timeout!=-1 ? time+timeout : -1);
agent.getComponentFeature(IBDIXAgentFeature.class).getGoalbase().dispatchTopLevelGoal(goal)
.addResultListener(new ExceptionDelegationResultListener