From c3ebb5a9f4e854cdb829ed5afc106ceb3f503d90 Mon Sep 17 00:00:00 2001 From: David Huynh Date: Thu, 11 Mar 2010 19:56:43 +0000 Subject: [PATCH] Got Vishal's jython integration to work. git-svn-id: http://google-refine.googlecode.com/svn/trunk@277 7d457c2a-affb-35e4-300a-418c747d4874 --- .classpath | 2 + .../gridworks/expr/JythonEvaluable.java | 86 +++++++++++++------ .../expr/JythonHasFieldsWrapper.java | 4 +- .../gridworks/expr/JythonObjectWrapper.java | 4 +- 4 files changed, 67 insertions(+), 29 deletions(-) diff --git a/.classpath b/.classpath index 283c64846..10ae32917 100644 --- a/.classpath +++ b/.classpath @@ -19,5 +19,7 @@ + + diff --git a/src/main/java/com/metaweb/gridworks/expr/JythonEvaluable.java b/src/main/java/com/metaweb/gridworks/expr/JythonEvaluable.java index 94f697e07..13084c290 100644 --- a/src/main/java/com/metaweb/gridworks/expr/JythonEvaluable.java +++ b/src/main/java/com/metaweb/gridworks/expr/JythonEvaluable.java @@ -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 String _code; - private static final String _functionName = "temp"; + private static final String s_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 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.set( + k, + (v instanceof HasFields) ? + new JythonHasFieldsWrapper((HasFields) v, bindings) : + v + ); } - _engine.exec(_code); - Object result = _engine.eval(_functionName + "()"); + Object result = _engine.eval(s_functionName + "()"); - // unwrap the underlying Java objects - if (result instanceof JythonObjectWrapper) { - result = ((JythonObjectWrapper) result)._obj; - } else if (result instanceof JythonHasFieldsWrapper) { - result = ((JythonHasFieldsWrapper) result)._obj; - } - - return result; + 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 i = po.asIterable().iterator(); + + List list = new ArrayList(); + while (i.hasNext()) { + list.add(unwrap(i.next())); + } + + return list.toArray(); + } else { + return po; + } + } } diff --git a/src/main/java/com/metaweb/gridworks/expr/JythonHasFieldsWrapper.java b/src/main/java/com/metaweb/gridworks/expr/JythonHasFieldsWrapper.java index e613330ed..b15ad39ce 100644 --- a/src/main/java/com/metaweb/gridworks/expr/JythonHasFieldsWrapper.java +++ b/src/main/java/com/metaweb/gridworks/expr/JythonHasFieldsWrapper.java @@ -7,7 +7,9 @@ import org.python.core.PyString; public class JythonHasFieldsWrapper extends PyObject { - public HasFields _obj; + private static final long serialVersionUID = -1275353513262385099L; + + public HasFields _obj; private Properties _bindings; public JythonHasFieldsWrapper(HasFields obj, Properties bindings) { diff --git a/src/main/java/com/metaweb/gridworks/expr/JythonObjectWrapper.java b/src/main/java/com/metaweb/gridworks/expr/JythonObjectWrapper.java index 813c74a64..3fc3ac438 100644 --- a/src/main/java/com/metaweb/gridworks/expr/JythonObjectWrapper.java +++ b/src/main/java/com/metaweb/gridworks/expr/JythonObjectWrapper.java @@ -3,7 +3,9 @@ package com.metaweb.gridworks.expr; import org.python.core.PyObject; public class JythonObjectWrapper extends PyObject { - public Object _obj; + private static final long serialVersionUID = -6608115027151667441L; + + public Object _obj; public JythonObjectWrapper(Object obj) { _obj = obj;