- implement shared script variables
- move docs
- implement run methods
This commit is contained in:
dlukyanov 2017-03-26 00:06:51 +02:00
parent dd388b3607
commit 41a17026ed
4 changed files with 306 additions and 102 deletions

View file

@ -0,0 +1,97 @@
## Groovy Interpreter
### Samples
```groovy
%groovy
//get a parameter defined as z.angularBind('ngSearchParam', value, 'paragraph_id')
//g is a context object for groovy to avoid mix with z object
def param = g.angular('ngSearchParam')
//send request https://www.googleapis.com/customsearch/v1?q=ngSearchParam_value
def r = HTTP.get(
//assume you defined the groovy interpreter property
// `search_baseurl`='https://www.googleapis.com/customsearch/v1'
//in groovy object o.getProperty('A') == o.'A' == o.A == o['A']
url : g.search_baseurl,
query: [ q: param ],
headers: [
'Accept':'application/json',
//'Authorization:' : g.getProperty('search_auth'),
]
)
//check response code
if( r.response.code==200 ) {
g.html().with{
//g.html() renders %angular to output and returns groovy.xml.MarkupBuilder
h2("the response ${r.response.code}")
span( r.response.body )
h2("headers")
pre( r.response.headers.join('\n') )
}
} else {
//just to show that it's possible to use println with multiline groovy string to render output
println("""%angular
<script> alert ("code=${r.response.code} \n msg=${r.response.message}") </script>
""")
}
```
```groovy
%groovy
//renders a table with headers a, b, c and two rows
g.table(
[
['a','b','c'],
['a1','b1','c1'],
['a2','b2','c2'],
]
)
```
### the `g` object
* `g.angular(String name)`
Returns angular object by name. Look up notebook scope first and then global scope.
* `g.angularBind(String name, Object value)`
Assign a new `value` into angular object `name`
* `java.util.Properties g.getProperties()`
returns all properties defined for this interpreter
* `String g.getProperty('PROPERTY_NAME')`
```groovy
g.PROPERTY_NAME
g.'PROPERTY_NAME'
g['PROPERTY_NAME']
g.getProperties().getProperty('PROPERTY_NAME')
```
All above the accessor to named property defined in groovy interpreter.
In this case with name `PROPERTY_NAME`
* `groovy.xml.MarkupBuilder g.html()`
Starts or continues rendering of `%angular` to output and returns [groovy.xml.MarkupBuilder](https://www.google.com/search?q=groovy.xml.MarkupBuilder)
MarkupBuilder is usefull to generate html (xml)
* `void g.table(obj)`
starts or continues rendering table rows.
obj: List(rows) of List(columns) where first line is a header

View file

@ -1,97 +1,3 @@
## Groovy Interpreter
### Samples
```groovy
%groovy
//get a parameter defined as z.angularBind('ngSearchParam', value, 'paragraph_id')
//g is a context object for groovy to avoid mix with z object
def param = g.angular('ngSearchParam')
//send request https://www.googleapis.com/customsearch/v1?q=ngSearchParam_value
def r = HTTP.get(
//assume you defined the groovy interpreter property
// `search_baseurl`='https://www.googleapis.com/customsearch/v1'
//in groovy object o.getProperty('A') == o.'A' == o.A == o['A']
url : g.search_baseurl,
query: [ q: param ],
headers: [
'Accept':'application/json',
//'Authorization:' : g.getProperty('search_auth'),
]
)
//check response code
if( r.response.code==200 ) {
g.html().with{
//g.html() renders %angular to output and returns groovy.xml.MarkupBuilder
h2("the response ${r.response.code}")
span( r.response.body )
h2("headers")
pre( r.response.headers.join('\n') )
}
} else {
//just to show that it's possible to use println with multiline groovy string to render output
println("""%angular
<script> alert ("code=${r.response.code} \n msg=${r.response.message}") </script>
""")
}
```
```groovy
%groovy
//renders a table with headers a, b, c and two rows
g.table(
[
['a','b','c'],
['a1','b1','c1'],
['a2','b2','c2'],
]
)
```
### the `g` object
* `g.angular(String name)`
Returns angular object by name. Look up notebook scope first and then global scope.
* `g.angularBind(String name, Object value)`
Assign a new `value` into angular object `name`
* `java.util.Properties g.getProperties()`
returns all properties defined for this interpreter
* `String g.getProperty('PROPERTY_NAME')`
```groovy
g.PROPERTY_NAME
g.'PROPERTY_NAME'
g['PROPERTY_NAME']
g.getProperties().getProperty('PROPERTY_NAME')
```
All above the accessor to named property defined in groovy interpreter.
In this case with name `PROPERTY_NAME`
* `groovy.xml.MarkupBuilder g.html()`
Starts or continues rendering of `%angular` to output and returns [groovy.xml.MarkupBuilder](https://www.google.com/search?q=groovy.xml.MarkupBuilder)
MarkupBuilder is usefull to generate html (xml)
* `void g.table(obj)`
starts or continues rendering table rows.
obj: List(rows) of List(columns) where first line is a header
[see groovy documentation](../docs/interpreter/groovy.md)

View file

@ -21,14 +21,23 @@ import java.io.StringWriter;
import org.slf4j.Logger;
import java.util.Properties;
import java.util.Collection;
import java.util.Map;
import java.util.List;
import java.util.LinkedList;
import groovy.xml.MarkupBuilder;
import groovy.lang.Closure;
import org.apache.zeppelin.interpreter.InterpreterContext;
import org.apache.zeppelin.interpreter.InterpreterContextRunner;
import org.apache.zeppelin.display.AngularObjectRegistry;
import org.apache.zeppelin.display.AngularObject;
import org.apache.zeppelin.display.GUI;
import org.apache.zeppelin.display.Input.ParamOption;
import org.apache.zeppelin.annotation.ZeppelinApi;
import org.apache.zeppelin.interpreter.RemoteWorksController;
import org.apache.zeppelin.interpreter.InterpreterException;
/**
* Groovy interpreter for Zeppelin.
@ -38,12 +47,15 @@ public class GObject extends groovy.lang.GroovyObjectSupport {
StringWriter out;
Properties props;
InterpreterContext interpreterContext;
Map<String,Object> bindings;
public GObject(Logger log, StringWriter out, Properties p, InterpreterContext ctx){
public GObject(Logger log, StringWriter out, Properties p, InterpreterContext ctx, Map<String,Object> bindings){
this.log=log;
this.out=out;
this.interpreterContext=ctx;
this.props=p;
this.bindings=bindings;
}
public Object getProperty(String key){
@ -68,6 +80,82 @@ public class GObject extends groovy.lang.GroovyObjectSupport {
out.append('\n');
}
}
/**
* returns gui object
*/
public GUI getGui(){
return interpreterContext.getGui();
}
@ZeppelinApi
public Object input(String name) {
return input(name, "");
}
@ZeppelinApi
public Object input(String name, Object defaultValue) {
return getGui().input(name, defaultValue);
}
private ParamOption[] toParamOptions(Map<Object, String> options){
ParamOption[] paramOptions = new ParamOption[options.size()];
int i = 0;
for(Map.Entry<Object,String> e : options.entrySet()){
paramOptions[i++] = new ParamOption(e.getKey(), e.getValue());
}
return paramOptions;
}
@ZeppelinApi
public Object select(String name, Map<Object, String> options) {
return select( name, "", options );
}
@ZeppelinApi
public Object select(String name, Object defaultValue, Map<Object, String> options) {
return getGui().select( name, defaultValue, toParamOptions(options) );
}
@ZeppelinApi
public Collection<Object> checkbox(String name, Map<Object, String> options) {
return checkbox(name, options.keySet(), options);
}
@ZeppelinApi
public Collection<Object> checkbox(String name, Collection<Object> defaultChecked, Map<Object, String> options) {
return getGui().checkbox(name, defaultChecked, toParamOptions(options));
}
/**
* Returns shared variable if it was previously set.
* The same as getting groovy script variables but this method will return null if script variable not assigned.
* To understand groovy script variables see groovy.transform.Field annotation for more information.
* @see #put
*/
public Object get(String varName){
return bindings.get(varName);
}
/**
* Returns script (shared) variable value but if value was not set returns default value.
* The same as getting groovy script variables but this method will return default value if script variable not assigned.
* To understand groovy script variables see groovy.transform.Field annotation for more information.
* @see #put
*/
public Object get(String varName, Object defValue){
return bindings.containsKey(varName) ? bindings.get(varName) : defValue;
}
/**
* Sets a ne value to interpreter's shared variables.
* Could be set by <code>put('varName', newValue )</code>
* or by just assigning <code>varName = value</code> without declaring a variable.
*/
public Object put(String varName, Object newValue){
return bindings.put(varName, newValue);
}
/**
* starts or continues rendering html/angular and returns MarkupBuilder to build html.
@ -166,4 +254,95 @@ public class GObject extends groovy.lang.GroovyObjectSupport {
angularBind(name, o, interpreterContext.getNoteId());
}
/*------------------------------------------RUN----------------------------------------*/
@ZeppelinApi
public List<InterpreterContextRunner> getInterpreterContextRunner(String noteId, String paragraphId, InterpreterContext interpreterContext) {
RemoteWorksController remoteWorksController = interpreterContext.getRemoteWorksController();
if (remoteWorksController != null) {
return remoteWorksController.getRemoteContextRunner(noteId, paragraphId);
}
return new LinkedList<InterpreterContextRunner>();
}
@ZeppelinApi
public List<InterpreterContextRunner> getInterpreterContextRunner(String noteId, InterpreterContext interpreterContext) {
RemoteWorksController remoteWorksController = interpreterContext.getRemoteWorksController();
if (remoteWorksController != null) {
return remoteWorksController.getRemoteContextRunner(noteId);
}
return new LinkedList<InterpreterContextRunner>();
}
/**
* Run paragraph by id
* @param noteId
* @param paragraphId
*/
@ZeppelinApi
public void run(String noteId, String paragraphId) {
run(noteId, paragraphId, interpreterContext);
}
/**
* Run paragraph by id
* @param paragraphId
*/
@ZeppelinApi
public void run(String paragraphId) {
String noteId = interpreterContext.getNoteId();
run(noteId, paragraphId, interpreterContext);
}
/**
* Run paragraph by id
* @param noteId
* @param context
*/
@ZeppelinApi
public void run(String noteId, String paragraphId, InterpreterContext context) {
if (paragraphId.equals(context.getParagraphId())) {
throw new InterpreterException("Can not run current Paragraph");
}
List<InterpreterContextRunner> runners = getInterpreterContextRunner(noteId, paragraphId, context);
if (runners.size() <= 0) {
throw new InterpreterException("Paragraph " + paragraphId + " not found " + runners.size());
}
for (InterpreterContextRunner r : runners) {
r.run();
}
}
public void runNote(String noteId) {
runNote(noteId, interpreterContext);
}
public void runNote(String noteId, InterpreterContext context) {
String runningNoteId = context.getNoteId();
String runningParagraphId = context.getParagraphId();
List<InterpreterContextRunner> runners = getInterpreterContextRunner(noteId, context);
if (runners.size() <= 0) {
throw new InterpreterException("Note " + noteId + " not found " + runners.size());
}
for (InterpreterContextRunner r : runners) {
if (r.getNoteId().equals(runningNoteId) && r.getParagraphId().equals(runningParagraphId)) {
continue;
}
r.run();
}
}
/**
* Run all paragraphs. except this.
*/
@ZeppelinApi
public void runAll() {
runAll(interpreterContext);
}
/**
* Run all paragraphs. except this.
*/
@ZeppelinApi
public void runAll(InterpreterContext context) {
runNote(context.getNoteId());
}
}

View file

@ -51,12 +51,19 @@ import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.runtime.ResourceGroovyMethods;
import org.codehaus.groovy.runtime.StackTraceUtils;
import java.util.concurrent.ConcurrentHashMap;
/**
* Groovy interpreter for Zeppelin.
*/
public class GroovyInterpreter extends Interpreter {
Logger log = LoggerFactory.getLogger(GroovyInterpreter.class);
GroovyShell shell = null; //new GroovyShell();
//here we will store shared variables. concurrent just in case.
Map<String,Object> sharedBindings = new ConcurrentHashMap<String,Object>();
//cache for groovy compiled scripts
Map<String,Class<Script>> scriptCache = Collections.synchronizedMap( new WeakHashMap<String,Class<Script>>(1000) );
public GroovyInterpreter(Properties property) {
super(property);
@ -89,7 +96,8 @@ public class GroovyInterpreter extends Interpreter {
@Override
public FormType getFormType() {
return FormType.NONE;
return FormType.NATIVE;
//return FormType.NONE;
}
@Override
@ -118,9 +126,6 @@ public class GroovyInterpreter extends Interpreter {
return null;
}
//cache for groovy compiled scripts
Map<String,Class<Script>> scriptCache = Collections.synchronizedMap( new WeakHashMap<String,Class<Script>>(1000) );
@SuppressWarnings("unchecked")
Script getGroovyScript(String id, String scriptText) /*throws SQLException*/ {
if(shell==null){
@ -141,6 +146,11 @@ public class GroovyInterpreter extends Interpreter {
}
}
private static Set<String> predefinedBindings = new HashSet<String>();
static{
predefinedBindings.add("g");
predefinedBindings.add("out");
}
@Override
@SuppressWarnings("unchecked")
@ -152,10 +162,21 @@ public class GroovyInterpreter extends Interpreter {
Map<String,Object> bindings = script.getBinding().getVariables();
bindings.clear();
StringWriter out = new StringWriter( (int) (cmd.length()*1.75) );
bindings.put("g", new GObject(log, out, this.getProperty(), contextInterpreter) );
//put shared bindings evaluated in this interpreter
bindings.putAll(sharedBindings);
//put predefined bindings
bindings.put("g", new GObject(log, out, this.getProperty(), contextInterpreter, bindings) );
bindings.put("out", new PrintWriter(out, true));
script.run();
//let's get shared variables defined in current script and store them in shared map
for(Map.Entry<String,Object> e : bindings.entrySet()){
if( !predefinedBindings.contains(e.getKey()) ){
if(log.isTraceEnabled())log.trace("groovy script variable "+e); //let's see what we have...
sharedBindings.put( e.getKey(), e.getValue() );
}
}
bindings.clear();
InterpreterResult result = new InterpreterResult(Code.SUCCESS, out.toString());
//log.info("RESULT: "+result);
@ -187,5 +208,6 @@ public class GroovyInterpreter extends Interpreter {
}
}
}
}