Embedded scripting in Java/Android
TASK 1
Here comes a task, the requirements are:
- Make a ChatBot program.
- We can talk to him, and he would respond as pre-defined scene.
- If not pre-defined, respond “I don’t know what you say”
- Input & Output are both pure string text.
A simple scene
Here might be your code v1.0 looks like:
Try it again, but it does not work as expected…
Something wrong?
Refactor code, you got v1.1
It works
A few days later, requirements changed:
A new scene added
You should refactor code again, then you got something like:
Things become more and more complicate, then you may want to extract the mutable logic:
Scene can be describe as json easily:
The simplest SceneManager implementation:
But mutable logic still in application.
If you want to define more complex scenes, SceneManger logic should be modified again and again.
Such as:
A better way to describe rule is like:
Write rule as DSL format would got more flexibility.
This is how rule parser works:
Then you got a clear version of SceneManager:
EMBEDDED SCRIPT
Now we need an embedded script to implement our Rule Parser.
But what is embedded scripting language?
- An embedded scripting language would be a scripting language interpreter that can be embedded into applications.
- The interpreter allowing scripts to control all or parts of the application.
With embedded scripting language, Application developers only have to provide the interface which can interact with the language, they don’t need to implement the actual language.
To make an application “scriptable” can add great flexibility to an application or framework without recompiling.
There are many embedded scripting language for Java, such as Jython, JRuby, Groovy.
This is how embedded script work on Java:
TASK 2
Not we try to port ChatBot program to Android, but it failed.
Jython/JRuby/Groovy do not support compile to .dex
Lua is a powerful, efficient, lightweight, embeddable scripting language which written by C programming language. It is fast, portable, embeddable, powerful and small, works well with C.
We can make a Lua wrapper in Android native c++ and make a proxy in Java layer.
Let’s see how it works: