1 Execute Appway Script From Java
The following code shows how to execute a given script from java.
(The basic idea is to get an interpreter and just interpret the script.)
1public Object execute(String script){
2 // get the interpreter
3 Interpreter interpreter = Nm.getInstance().getExpressionLanguageService().createInterpreter();
4
5 Object val = null;
6 try {
7 val = interpreter.interpret(script);
8 } catch (Exception e) {
9 // error while parsing or executing the script
10 }
11 return val;
12}
1.1 With Parameter Definitions
Let's assume we have paramters which we would like to pass to our script.
Here's an example script
1JOIN('hello ', $name, '.')
The script outputs 'hello' followed by a 'name'.
Please note that the variable '$name' is not defined in the script!
I - the writer of the script - know that the variable type is a String!
1// get the execution context
2ExecutionContext executionContext=interpreter.getExecutionContext();
3// get the current scope
4Scope scope=executionContext.getScope();
5// define the variable
6scope.define(new VariableDefNode("name","String",DataProperty.COLLECTION_TYPE_NONE));
7// set the value
8scope.setValue("name", DataValueUtils.newInstance("String","James Bond",executionContext));
The complete code looks like this:
1public Object execute(String script){
2 // get the interpreter
3 Interpreter interpreter = Nm.getInstance().getExpressionLanguageService().createInterpreter();
4
5 // get the execution context
6 ExecutionContext executionContext=interpreter.getExecutionContext();
7 // get the current scope
8 Scope scope=executionContext.getScope();
9 // define the variable
10 scope.define(new VariableDefNode("name","String",DataProperty.COLLECTION_TYPE_NONE));
11 // set the value
12 scope.setValue("name", DataValueUtils.newInstance("String","James Bond",executionContext));
13
14
15 Object val = null;
16 try {
17 val = interpreter.interpret(script);
18 } catch (Exception e) {
19 // error while parsing or executing the script
20 }
21 return val;
22}
1.2 Handling the return value
The return value of a script is either the value of the last line beeing executed (implicit return value) or the value that has been returned by using the "Return" keyword (explicit return value).
When evaluating a script the type of the default interpret function is Object. This might be a DataEntity, a java Object or a Pointer. If the return type of a script is known in advance one might use one of the more specific interpret functions in the Interpreter interface.
1// returns a java object
2// if the script result is-a datavalue the wrapped java object will be returned
3interpreter.interpretObject(script);
4
5
6// returns a string
7interpreter.interpretString(script);
8
9// return an integer
10interpreter.interpretInteger(script);
11
12// return a DataEntity (do not unwrap DataValue)
13interpreter.interpretDataEntity(scipt);
14...
A returned value may be converted into a desired type using the DefaultConverter like this:
1Object obj=interpreter.interpret(script);
2Double mydouble=executionContext.getConverter().convert(obj,Double.class,executionContext,true);
The last parameter of the convert method defines whether primitive values should be converted to their default value if the script returns NULL.
If conversion is not possible the convert method returns the object "DefaultConverter.UNCONVERTABLE"
1.3 Creating/Retrieve an Interpreter / ExecutionContext
Interpreter and ExecutionContext can be created in many different ways. It all depends on the context you are in.
Please note: In 90% of all use cases it is a bad idea to create a new interpreter using the method "createInterpreter()" (the one without paramter!) of the Expression Language Service.
1.3.1 Using the ExpressionLanguageService
1// create an interpreter using one of the methods in the expression language service
2Nm.getInstance().getExpressionLanguageService().createInterpreter(...);
3
4
5// create an execution context
6Nm.getInstance().getExpressionLanguageService().createExecutionContext(...)
1.3.2 Using ExecutionContext
1// given an execution context the interpreter may be retrieved like this
2Interpreter interpreter=executionContext.getInterpreter()
1.3.3 Using PageContext
In a screen component renderer the interpreter / execution context can be retrieved like this:
1Interpreter interpreter=pageContext.createInterpreter();