cz.cuni.pogamut.Client
Class ScriptedAgent

java.lang.Object
  extended by cz.cuni.pogamut.Client.Agent
      extended by cz.cuni.pogamut.Client.ScriptedAgent
All Implemented Interfaces:
RcvMsgListener, Introspectable, java.lang.Runnable, java.util.EventListener
Direct Known Subclasses:
SPoshBot, SPoshBot

public class ScriptedAgent
extends Agent
implements Introspectable

This is class for the agents that are using the ScriptEngine.

You have to specify the file to load and engine to bind before starting the agent.


Field Summary
protected  javax.script.ScriptEngine engine
          ScriptEngine which contains the file with logic.
protected  javax.script.Invocable invocableEngine
          E. g. engine but Invocable, allowing us to call methods.
protected  java.lang.String scriptFileName
          Name of the file which contains the logic.
 
Fields inherited from class cz.cuni.pogamut.Client.Agent
bindedParserType, body, flagLogicTemporalyStopped, gameBotConnection, gameMap, GARBAGE_COLLECTOR_FREQUENCY, instancesAlive, iterationEndListeners, log, logicFrequency, logicFrequencyChanged, memory, oldLogicFrequency, parserLogger, pauseHolders, platformLog, random, rawGBLog
 
Constructor Summary
protected ScriptedAgent()
          Constructor which is needed when you have to initialize the environment of the scripting language.
  ScriptedAgent(java.io.File file)
          Bind new instance of scripted agent with source file.
  ScriptedAgent(java.lang.String pathToScript)
          Bind new instance of scripted agent with source file.
 
Method Summary
 void bindEngineByExtension(java.lang.String extension)
          Bind engine which is needed to process the script.
 void bindEngineByFileName(java.lang.String fileName)
          Bind engine which is needed to process the script.
 void bindEngineByMimeType(java.lang.String mime)
          Bind engine which is needed to process the script.
 void bindEngineByName(java.lang.String name)
          Bind engine which is needed to process the script.
protected  void bindScriptFile(java.io.File file)
          Sets the file with logic.
protected  void bindScriptFile(java.lang.String fileName)
          Sets the file with logic.
private  boolean callFunction(java.lang.String name)
          Calls function without parameters from the ScriptEngine.
protected static javax.script.ScriptEngineManager createScriptEngineManager()
          Creates script engine manager that looks for engines in classpath and also in $POGAMUT_PLATFORM/scriptEngines.
protected  void doLogic()
          This will call function doLogic() from specified scriptFileName.
protected  void engineBinded()
          This method is called when the engine is binded.
protected  void engineIsGoingToBeBinded()
          Reserved for use of the descendants, making room for configuring some system properties / paths / etc.
protected  boolean evalFile(java.lang.String fileName)
          Evaluate the file within the engine.
protected  boolean evalStream(java.io.InputStream is)
          Evaluates the stream of the script.
 IntrospectableProxy getIntrospectableProxy()
          Returns union of properties and children from Agent (obtained by Java Reflection) and from script (obtained by corresponding InstrospectionProxy).
protected  boolean checkPreconditions()
           
protected  void postPrepareAgent()
          This will call function postPrepareAgent() from specified logicFileName.
protected  void prePrepareAgent()
          This will call function prePrepareAgent() from specified logicFileName.
protected  void scriptFileBinded()
          This method is called whenever the script file is binded and evaluated.
protected  void setAgentAttributes()
          Export important agent variables to the ScriptingEngine.
protected  void setAttribute(java.lang.String name, java.lang.Object attribute)
          Sets attribute to the global scope of the engine.
protected  void shutdownAgent()
          This will call function shutdownAgent() from specified logicFileName.
 java.lang.Thread startAgent(java.lang.String botName)
          check whether everything is ready
startAgent
 
Methods inherited from class cz.cuni.pogamut.Client.Agent
addIterationEndListener, bindLocalParser, bindRemoteParser, clearListeners, disconnect, finalize, getBindedParserType, getBody, getCommunicationState, getFlagLogicTemporalyStopped, getLogger, getLogicThread, getMap, getMediator, getMemory, getMilisecToSleep, getName, getPlatformLog, getRawDataLog, initBody, initGameMap, initLogging, initMemory, isRunning, logicReady, pauseLogic, receiveMessage, removeIterationEndListener, resumeLogic, run, sendMessageToGB, setFlagLogicTemporalyStopped, setLogicStartLatch, startAgent, stopAgentSoft
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

scriptFileName

protected java.lang.String scriptFileName
Name of the file which contains the logic.


engine

protected javax.script.ScriptEngine engine
ScriptEngine which contains the file with logic.


invocableEngine

protected javax.script.Invocable invocableEngine
E. g. engine but Invocable, allowing us to call methods.

Constructor Detail

ScriptedAgent

protected ScriptedAgent()
Constructor which is needed when you have to initialize the environment of the scripting language.

Currently used by SPOSHBot.


ScriptedAgent

public ScriptedAgent(java.lang.String pathToScript)
              throws ScriptedAgentException
Bind new instance of scripted agent with source file.

Parameters:
pathToScript - path to script source file. eg. c:\bot.py
Throws:
ScriptedAgentException

ScriptedAgent

public ScriptedAgent(java.io.File file)
              throws ScriptedAgentException
Bind new instance of scripted agent with source file.

Parameters:
file - File of the script source file
Throws:
ScriptedAgentException
Method Detail

createScriptEngineManager

protected static javax.script.ScriptEngineManager createScriptEngineManager()
Creates script engine manager that looks for engines in classpath and also in $POGAMUT_PLATFORM/scriptEngines.


scriptFileBinded

protected void scriptFileBinded()
This method is called whenever the script file is binded and evaluated.


bindScriptFile

protected void bindScriptFile(java.lang.String fileName)
                       throws ScriptedAgentException
Sets the file with logic. You may do this only once.

If engine is not binded - it will be determined by the file extension.

Script file is then evaluated in the engine.

Parameters:
fileName -
Throws:
ScriptedAgentException

bindScriptFile

protected void bindScriptFile(java.io.File file)
                       throws ScriptedAgentException
Sets the file with logic. You may do this only once.

If engine is not binded - it will be determined by the file extension.

Script file is then evaluated in the engine.

Parameters:
file -
Throws:
ScriptedAgentException

engineBinded

protected void engineBinded()
This method is called when the engine is binded.


bindEngineByName

public void bindEngineByName(java.lang.String name)
Bind engine which is needed to process the script.

Engine may be binded only once.

You have to specify the name of the engine, see ScriptEngineFactory from JavaDoc for more info about the names.

Parameters:
name -

bindEngineByExtension

public void bindEngineByExtension(java.lang.String extension)
Bind engine which is needed to process the script.

Engine may be binded only once.

You have to specify the name of the engine, see ScriptEngineFactory from JavaDoc for more info about the names.

Parameters:
extension -

bindEngineByMimeType

public void bindEngineByMimeType(java.lang.String mime)
Bind engine which is needed to process the script.

Engine may be binded only once.

You have to specify the name of the engine, see ScriptEngineFactory from JavaDoc for more info about the names.

Parameters:
mime -

bindEngineByFileName

public void bindEngineByFileName(java.lang.String fileName)
Bind engine which is needed to process the script.

Engine may be binded only once.

You have to specify the fileName of the engine, see ScriptEngineFactory from JavaDoc for more info about the names.

Parameters:
fileName -

engineIsGoingToBeBinded

protected void engineIsGoingToBeBinded()
Reserved for use of the descendants, making room for configuring some system properties / paths / etc.


setAgentAttributes

protected void setAgentAttributes()
Export important agent variables to the ScriptingEngine.


setAttribute

protected void setAttribute(java.lang.String name,
                            java.lang.Object attribute)
Sets attribute to the global scope of the engine.

Parameters:
name -
attribute -

evalStream

protected boolean evalStream(java.io.InputStream is)
                      throws ScriptedAgentException
Evaluates the stream of the script.

Parameters:
is -
Returns:
true if eval succeeded
Throws:
ScriptedAgentException

evalFile

protected boolean evalFile(java.lang.String fileName)
                    throws ScriptedAgentException
Evaluate the file within the engine.

Parameters:
fileName -
Returns:
true if eval succeeded
Throws:
ScriptedAgentException

checkPreconditions

protected boolean checkPreconditions()

startAgent

public java.lang.Thread startAgent(java.lang.String botName)
check whether everything is ready
startAgent

Overrides:
startAgent in class Agent
Parameters:
botName -
Returns:
logic thread

callFunction

private boolean callFunction(java.lang.String name)
                      throws ScriptedAgentException
Calls function without parameters from the ScriptEngine.

Parameters:
name -
Returns:
true if everything is ok
Throws:
ScriptedAgentException

doLogic

protected void doLogic()
                throws PogamutException
This will call function doLogic() from specified scriptFileName.

It should do the logic iteration (assuming you're already connected and prePreparaAgent and postPrepareAgent were already called).

Overrides:
doLogic in class Agent
Throws:
PogamutException

prePrepareAgent

protected void prePrepareAgent()
                        throws PogamutException
This will call function prePrepareAgent() from specified logicFileName.

prepares agent logic to run - like initializing neural networks etc. not for establishing communication!

Overrides:
prePrepareAgent in class Agent
Throws:
PogamutException

postPrepareAgent

protected void postPrepareAgent()
                         throws PogamutException
This will call function postPrepareAgent() from specified logicFileName.

prepare logic according to information gathered from startCommunication like choosing plan/parameters according to game type.

Overrides:
postPrepareAgent in class Agent
Throws:
PogamutException

shutdownAgent

protected void shutdownAgent()
                      throws PogamutException
This will call function shutdownAgent() from specified logicFileName.

Clean - up after the end of simulation of agent

Overrides:
shutdownAgent in class Agent
Throws:
PogamutException

getIntrospectableProxy

public IntrospectableProxy getIntrospectableProxy()
Returns union of properties and children from Agent (obtained by Java Reflection) and from script (obtained by corresponding InstrospectionProxy).

Specified by:
getIntrospectableProxy in interface Introspectable
Overrides:
getIntrospectableProxy in class Agent
Returns:
introspectable representation of this object