Got Vishal's jython integration to work.
git-svn-id: http://google-refine.googlecode.com/svn/trunk@277 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
parent
4bb9e06772
commit
c3ebb5a9f4
@ -19,5 +19,7 @@
|
||||
<classpathentry kind="lib" path="lib/poi-ooxml-3.6.jar"/>
|
||||
<classpathentry kind="lib" path="lib/apache-tools-tar.jar"/>
|
||||
<classpathentry kind="lib" path="tests/lib/junit-4.8.1.jar" sourcepath="tests/lib-src/junit-4.8.1-sources.jar"/>
|
||||
<classpathentry kind="lib" path="lib/jython-2.5.1.jar"/>
|
||||
<classpathentry kind="lib" path="lib/clojure-1.1.0.jar"/>
|
||||
<classpathentry kind="output" path="build/classes"/>
|
||||
</classpath>
|
||||
|
@ -1,56 +1,88 @@
|
||||
package com.metaweb.gridworks.expr;
|
||||
|
||||
import java.util.Enumeration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.python.core.PyException;
|
||||
import org.python.core.PyNone;
|
||||
import org.python.core.PyObject;
|
||||
import org.python.core.PyString;
|
||||
import org.python.util.PythonInterpreter;
|
||||
|
||||
public class JythonEvaluable implements Evaluable {
|
||||
private static final String s_functionName = "___temp___";
|
||||
|
||||
private String _code;
|
||||
private static final String _functionName = "temp";
|
||||
private static PythonInterpreter _engine = new PythonInterpreter();
|
||||
|
||||
|
||||
public JythonEvaluable(String s) {
|
||||
// indent and create a function out of the code
|
||||
String[] lines = s.split("\r\n|\r|\n");
|
||||
StringBuilder function = new StringBuilder();
|
||||
function.append("def " + _functionName + "():");
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append("def " + s_functionName + "():");
|
||||
for (int i = 0; i < lines.length; i++) {
|
||||
function.append("\n " + lines[i]);
|
||||
sb.append("\n " + lines[i]);
|
||||
}
|
||||
_code = function.toString();
|
||||
|
||||
_engine.exec(sb.toString());
|
||||
}
|
||||
|
||||
public Object evaluate(Properties bindings) {
|
||||
try {
|
||||
for (Enumeration<Object> e = bindings.keys() ; e.hasMoreElements() ;) {
|
||||
Object k = e.nextElement();
|
||||
for (Object key : bindings.keySet()) {
|
||||
String k = (String) key;
|
||||
Object v = bindings.get(k);
|
||||
if (v instanceof HasFields) {
|
||||
_engine.set((String)k, new JythonHasFieldsWrapper((HasFields) v, bindings));
|
||||
} else {
|
||||
_engine.set((String) k, v);
|
||||
}
|
||||
}
|
||||
_engine.exec(_code);
|
||||
|
||||
Object result = _engine.eval(_functionName + "()");
|
||||
|
||||
// unwrap the underlying Java objects
|
||||
if (result instanceof JythonObjectWrapper) {
|
||||
result = ((JythonObjectWrapper) result)._obj;
|
||||
} else if (result instanceof JythonHasFieldsWrapper) {
|
||||
result = ((JythonHasFieldsWrapper) result)._obj;
|
||||
_engine.set(
|
||||
k,
|
||||
(v instanceof HasFields) ?
|
||||
new JythonHasFieldsWrapper((HasFields) v, bindings) :
|
||||
v
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
Object result = _engine.eval(s_functionName + "()");
|
||||
|
||||
return unwrap(result);
|
||||
} catch (PyException e) {
|
||||
return new EvalError(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
protected Object unwrap(Object result) {
|
||||
if (result != null) {
|
||||
if (result instanceof JythonObjectWrapper) {
|
||||
return ((JythonObjectWrapper) result)._obj;
|
||||
} else if (result instanceof JythonHasFieldsWrapper) {
|
||||
return ((JythonHasFieldsWrapper) result)._obj;
|
||||
} else if (result instanceof PyString) {
|
||||
return ((PyString) result).asString();
|
||||
} else if (result instanceof PyObject) {
|
||||
return unwrap((PyObject) result);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
protected Object unwrap(PyObject po) {
|
||||
if (po instanceof PyNone) {
|
||||
return null;
|
||||
} else if (po.isNumberType()) {
|
||||
return po.asDouble();
|
||||
} else if (po.isSequenceType()) {
|
||||
Iterator<PyObject> i = po.asIterable().iterator();
|
||||
|
||||
List<Object> list = new ArrayList<Object>();
|
||||
while (i.hasNext()) {
|
||||
list.add(unwrap(i.next()));
|
||||
}
|
||||
|
||||
return list.toArray();
|
||||
} else {
|
||||
return po;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,8 @@ import org.python.core.PyString;
|
||||
|
||||
|
||||
public class JythonHasFieldsWrapper extends PyObject {
|
||||
private static final long serialVersionUID = -1275353513262385099L;
|
||||
|
||||
public HasFields _obj;
|
||||
private Properties _bindings;
|
||||
|
||||
|
@ -3,6 +3,8 @@ package com.metaweb.gridworks.expr;
|
||||
import org.python.core.PyObject;
|
||||
|
||||
public class JythonObjectWrapper extends PyObject {
|
||||
private static final long serialVersionUID = -6608115027151667441L;
|
||||
|
||||
public Object _obj;
|
||||
|
||||
public JythonObjectWrapper(Object obj) {
|
||||
|
Loading…
Reference in New Issue
Block a user