cz.cuni.amis.pogamut.ut2004.bot.sposh
Class StreamScriptLogic

Package class diagram package StreamScriptLogic
java.lang.Object
  extended by cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004BotController<BOT>
      extended by cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004BotLogicController
          extended by cz.cuni.amis.pogamut.ut2004.bot.sposh.StreamScriptLogic
All Implemented Interfaces:
cz.cuni.amis.pogamut.base.agent.module.IAgentLogic, IUT2004BotController, IUT2004BotLogicController
Direct Known Subclasses:
FileScriptLogic

public abstract class StreamScriptLogic
extends UT2004BotLogicController

This is class for the agents that are using the ScriptEngine. ScriptEngine is a part of Java API that allows execution of scripting file using integrated engine.

This basically allows us to create a agent with logic implemented in some scripting language (like JavaScript or Python). Beware, Java has currently implemented only one engine and that is JavaScript, for others it is necessary to provide custom binding library for the language (like jython).

The script that will be executed shoudl contain all the normal functions that are expected, i.e.:

You have to specify the file to load and engine to bind before starting the agent. Subclasses has to implement StreamScriptedAgent.getScriptFile() to specify the script. This is done mostly because of google Guice. Also, if subclass is utilizing some script that is not available as default, it should override StreamScriptedAgent.engineIsGoingToBeBinded() to set it up. TODO: this class is rather ugly, it deserves some clean up.

It should go approx. like this * Create a new instance * Create ScriptEngineManager * call engineIsGoingToBeBinded() * get engine from manager based on extension or something How to get data: get file get stream from file What can be done:

  1. Override getScriptFile, in such case default get stream from file will be used. Engine will be created based on extension of file.
  2. Override getScriptStream, in that case getScriptFile won't be used, but user has to specify the type of engine.and
  3. Author:
    Honza

    Field Summary
    protected  ScriptEngine engine
              ScriptEngine which contains the file with logic.
    protected  Invocable invocableEngine
              Same engine as in engine, but recasted as Invocable, allowing us to call methods.
    protected  ScriptEngineManager scriptEngineManager
               
     
    Fields inherited from class cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004BotLogicController
    logicModule
     
    Fields inherited from class cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004BotController
    bot, log, USER_LOG_CATEGORY_ID
     
    Constructor Summary
    StreamScriptLogic()
               
     
    Method Summary
    protected  boolean callFunction(String name)
              Calls function without parameters from the ScriptEngine.
    protected abstract  ScriptEngine createScriptEngine(ScriptEngineManager manager)
              Return correct ScriptEngine for script this bot is going to run.
    protected abstract  void engineBinded()
              This method is called when the engine is binded.
    protected  boolean evalStream(Reader reader)
              Evaluates the stream of the script.
     UT2004Bot getBot()
               
    protected abstract  Reader getScriptStream()
              Return stream for script that this class should execute.
     void initializeController(UT2004Bot bot)
              Constructor which is needed when you have to initialize the environment of the scripting language.
    protected abstract  void scriptBinded()
              This method is called after the script is evaluated.
    protected  void setAttribute(String name, Object attribute)
              Sets attribute to the global scope of the engine.
     
    Methods inherited from class cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004BotLogicController
    beforeFirstLogic, getLogicInitializeTime, getLogicShutdownTime, initializeLogic, logic, logicInitialize, logicShutdown
     
    Methods inherited from class cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004BotController
    botFirstSpawn, botInitialized, botKilled, botShutdown, finishControllerInitialization, getAct, getInitializeCommand, getLog, getName, getPassword, getWorldView, prepareBot
     
    Methods inherited from class java.lang.Object
    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
     
    Methods inherited from interface cz.cuni.amis.pogamut.ut2004.bot.IUT2004BotController
    botFirstSpawn, botInitialized, botKilled, botShutdown, finishControllerInitialization, getAct, getInitializeCommand, getLog, getPassword, getWorldView, prepareBot
     

    Field Detail

    scriptEngineManager

    protected ScriptEngineManager scriptEngineManager

    engine

    protected ScriptEngine engine
    ScriptEngine which contains the file with logic.


    invocableEngine

    protected Invocable invocableEngine
    Same engine as in engine, but recasted as Invocable, allowing us to call methods.

    Constructor Detail

    StreamScriptLogic

    public StreamScriptLogic()
    Method Detail

    initializeController

    public void initializeController(UT2004Bot bot)
    Constructor which is needed when you have to initialize the environment of the scripting language.

    Currently used by SPOSHBot.

    Specified by:
    initializeController in interface IUT2004BotController
    Overrides:
    initializeController in class UT2004BotLogicController

    createScriptEngine

    protected abstract ScriptEngine createScriptEngine(ScriptEngineManager manager)
    Return correct ScriptEngine for script this bot is going to run.

    Because this class is using stream instead of file we don't know what is scripting language of processed script. That is what this function is for.

    Parameters:
    manager - ScriptEngineManager that is used to manage engines.
    Returns:
    correct ScriptEngine for this agent.

    getScriptStream

    protected abstract Reader getScriptStream()
                                       throws IOException
    Return stream for script that this class should execute.

    Returns:
    input stream containing script. Not null.
    Throws:
    IOException

    scriptBinded

    protected abstract void scriptBinded()
    This method is called after the script is evaluated. To be used by developer, for some adjustments or such.


    engineBinded

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


    setAttribute

    protected final void setAttribute(String name,
                                      Object attribute)
    Sets attribute to the global scope of the engine. TODO: same as engine.put(name, attribute) ? Learn more about scopes.

    Parameters:
    name -
    attribute -

    evalStream

    protected final boolean evalStream(Reader reader)
                                throws ScriptedAgentException
    Evaluates the stream of the script.

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

    callFunction

    protected final boolean callFunction(String name)
                                  throws ScriptedAgentException
    Calls function without parameters from the ScriptEngine.

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

    getBot

    public UT2004Bot getBot()
    Specified by:
    getBot in interface IUT2004BotController
    Overrides:
    getBot in class UT2004BotController


    Copyright © 2014 AMIS research group, Faculty of Mathematics and Physics, Charles University in Prague, Czech Republic. All Rights Reserved.