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:
David Huynh 2010-03-11 19:56:43 +00:00
parent 4bb9e06772
commit c3ebb5a9f4
4 changed files with 67 additions and 29 deletions

View File

@ -19,5 +19,7 @@
<classpathentry kind="lib" path="lib/poi-ooxml-3.6.jar"/> <classpathentry kind="lib" path="lib/poi-ooxml-3.6.jar"/>
<classpathentry kind="lib" path="lib/apache-tools-tar.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="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"/> <classpathentry kind="output" path="build/classes"/>
</classpath> </classpath>

View File

@ -1,56 +1,88 @@
package com.metaweb.gridworks.expr; 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 java.util.Properties;
import org.python.core.PyException; import org.python.core.PyException;
import org.python.core.PyNone;
import org.python.core.PyObject;
import org.python.core.PyString; import org.python.core.PyString;
import org.python.util.PythonInterpreter; import org.python.util.PythonInterpreter;
public class JythonEvaluable implements Evaluable { 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(); private static PythonInterpreter _engine = new PythonInterpreter();
public JythonEvaluable(String s) { public JythonEvaluable(String s) {
// indent and create a function out of the code // indent and create a function out of the code
String[] lines = s.split("\r\n|\r|\n"); 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++) { 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) { public Object evaluate(Properties bindings) {
try { try {
for (Enumeration<Object> e = bindings.keys() ; e.hasMoreElements() ;) { for (Object key : bindings.keySet()) {
Object k = e.nextElement(); String k = (String) key;
Object v = bindings.get(k); 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 + "()"); _engine.set(
k,
// unwrap the underlying Java objects (v instanceof HasFields) ?
if (result instanceof JythonObjectWrapper) { new JythonHasFieldsWrapper((HasFields) v, bindings) :
result = ((JythonObjectWrapper) result)._obj; v
} else if (result instanceof JythonHasFieldsWrapper) { );
result = ((JythonHasFieldsWrapper) result)._obj;
} }
return result; Object result = _engine.eval(s_functionName + "()");
return unwrap(result);
} catch (PyException e) { } catch (PyException e) {
return new EvalError(e.toString()); 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;
}
}
} }

View File

@ -7,6 +7,8 @@ import org.python.core.PyString;
public class JythonHasFieldsWrapper extends PyObject { public class JythonHasFieldsWrapper extends PyObject {
private static final long serialVersionUID = -1275353513262385099L;
public HasFields _obj; public HasFields _obj;
private Properties _bindings; private Properties _bindings;

View File

@ -3,6 +3,8 @@ package com.metaweb.gridworks.expr;
import org.python.core.PyObject; import org.python.core.PyObject;
public class JythonObjectWrapper extends PyObject { public class JythonObjectWrapper extends PyObject {
private static final long serialVersionUID = -6608115027151667441L;
public Object _obj; public Object _obj;
public JythonObjectWrapper(Object obj) { public JythonObjectWrapper(Object obj) {