2010-03-11 00:43:21 +01:00
|
|
|
package com.metaweb.gridworks.expr;
|
|
|
|
|
2010-03-11 20:56:43 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
2010-03-11 00:43:21 +01:00
|
|
|
import java.util.Properties;
|
|
|
|
|
2010-03-19 22:45:01 +01:00
|
|
|
import org.python.core.Py;
|
2010-03-11 00:43:21 +01:00
|
|
|
import org.python.core.PyException;
|
2010-03-19 22:45:01 +01:00
|
|
|
import org.python.core.PyFunction;
|
2010-03-11 20:56:43 +01:00
|
|
|
import org.python.core.PyNone;
|
|
|
|
import org.python.core.PyObject;
|
2010-03-11 00:43:21 +01:00
|
|
|
import org.python.core.PyString;
|
|
|
|
import org.python.util.PythonInterpreter;
|
|
|
|
|
|
|
|
public class JythonEvaluable implements Evaluable {
|
2010-03-11 20:56:43 +01:00
|
|
|
private static final String s_functionName = "___temp___";
|
|
|
|
|
2010-03-11 00:43:21 +01:00
|
|
|
private static PythonInterpreter _engine = new PythonInterpreter();
|
|
|
|
|
|
|
|
public JythonEvaluable(String s) {
|
2010-03-22 01:54:56 +01:00
|
|
|
// indent and create a function out of the code
|
|
|
|
String[] lines = s.split("\r\n|\r|\n");
|
|
|
|
|
2010-04-09 02:14:11 +02:00
|
|
|
StringBuffer sb = new StringBuffer(1024);
|
|
|
|
sb.append("def ");
|
|
|
|
sb.append(s_functionName);
|
|
|
|
sb.append("(value, cell, cells, row, rowIndex):");
|
2010-03-22 01:54:56 +01:00
|
|
|
for (int i = 0; i < lines.length; i++) {
|
2010-04-09 02:14:11 +02:00
|
|
|
sb.append("\n ");
|
|
|
|
sb.append(lines[i]);
|
2010-03-22 01:54:56 +01:00
|
|
|
}
|
|
|
|
|
2010-03-11 20:56:43 +01:00
|
|
|
_engine.exec(sb.toString());
|
2010-03-11 00:43:21 +01:00
|
|
|
}
|
|
|
|
|
2010-03-22 01:54:56 +01:00
|
|
|
public Object evaluate(Properties bindings) {
|
|
|
|
try {
|
|
|
|
// call the temporary PyFunction directly
|
|
|
|
Object result = ((PyFunction)_engine.get(s_functionName)).__call__(
|
2010-03-19 22:45:01 +01:00
|
|
|
new PyObject[] {
|
|
|
|
Py.java2py( bindings.get("value") ),
|
|
|
|
new JythonHasFieldsWrapper((HasFields) bindings.get("cell"), bindings),
|
|
|
|
new JythonHasFieldsWrapper((HasFields) bindings.get("cells"), bindings),
|
|
|
|
new JythonHasFieldsWrapper((HasFields) bindings.get("row"), bindings),
|
|
|
|
Py.java2py( bindings.get("rowIndex") )
|
|
|
|
}
|
2010-03-22 01:54:56 +01:00
|
|
|
);
|
2010-03-11 00:43:21 +01:00
|
|
|
|
2010-03-22 01:54:56 +01:00
|
|
|
return unwrap(result);
|
|
|
|
} catch (PyException e) {
|
2010-04-14 23:30:39 +02:00
|
|
|
return new EvalError(e.getMessage());
|
2010-03-22 01:54:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Object unwrap(Object result) {
|
2010-03-11 20:56:43 +01:00
|
|
|
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;
|
2010-03-22 01:54:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected Object unwrap(PyObject po) {
|
|
|
|
if (po instanceof PyNone) {
|
|
|
|
return null;
|
|
|
|
} else if (po.isNumberType()) {
|
2010-03-11 20:56:43 +01:00
|
|
|
return po.asDouble();
|
|
|
|
} else if (po.isSequenceType()) {
|
|
|
|
Iterator<PyObject> i = po.asIterable().iterator();
|
|
|
|
|
|
|
|
List<Object> list = new ArrayList<Object>();
|
|
|
|
while (i.hasNext()) {
|
2010-04-20 20:50:24 +02:00
|
|
|
list.add(unwrap((Object) i.next()));
|
2010-03-11 20:56:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return list.toArray();
|
|
|
|
} else {
|
|
|
|
return po;
|
|
|
|
}
|
2010-03-22 01:54:56 +01:00
|
|
|
}
|
2010-03-11 00:43:21 +01:00
|
|
|
}
|