public abstract class StreamScriptLogic
extends cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004BotLogicController
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:
Modifier and Type | Field and Description |
---|---|
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 |
Constructor and Description |
---|
StreamScriptLogic() |
Modifier and Type | Method and Description |
---|---|
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.
|
cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot |
getBot() |
protected abstract Reader |
getScriptStream()
Return stream for script that this class should execute.
|
void |
initializeController(cz.cuni.amis.pogamut.ut2004.bot.impl.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.
|
beforeFirstLogic, getLogicInitializeTime, getLogicShutdownTime, logic, logicInitialize, logicShutdown
botFirstSpawn, botInitialized, botKilled, botShutdown, finishControllerInitialization, getAct, getInitializeCommand, getLog, getPassword, getWorldView, prepareBot
protected ScriptEngineManager scriptEngineManager
protected ScriptEngine engine
protected Invocable invocableEngine
engine
, but recasted as Invocable, allowing us to call methods.public void initializeController(cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot bot)
Currently used by SPOSHBot.
initializeController
in interface cz.cuni.amis.pogamut.ut2004.bot.IUT2004BotController
initializeController
in class cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004BotLogicController
protected abstract ScriptEngine createScriptEngine(ScriptEngineManager manager)
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.
manager
- ScriptEngineManager
that is used to manage engines.ScriptEngine
for this agent.protected abstract Reader getScriptStream() throws IOException
IOException
protected abstract void scriptBinded()
protected abstract void engineBinded()
protected final void setAttribute(String name, Object attribute)
name
- attribute
- protected final boolean evalStream(Reader reader) throws ScriptedAgentException
is
- ScriptedAgentException
protected final boolean callFunction(String name) throws ScriptedAgentException
name
- ScriptedAgentException
public cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004Bot getBot()
getBot
in interface cz.cuni.amis.pogamut.ut2004.bot.IUT2004BotController
getBot
in class cz.cuni.amis.pogamut.ut2004.bot.impl.UT2004BotController
Copyright © 2012 AMIS research group, Faculty of Mathematics and Physics, Charles University in Prague, Czech Republic. All Rights Reserved.