adding extremely crude jython and clojure expression evaluation

git-svn-id: http://google-refine.googlecode.com/svn/trunk@276 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Vishal Talwar 2010-03-10 23:43:21 +00:00
parent c81548529b
commit 4bb9e06772
6 changed files with 121 additions and 11 deletions

BIN
lib/clojure-1.1.0.jar Normal file

Binary file not shown.

BIN
lib/jython-2.5.1.jar Normal file

Binary file not shown.

View File

@ -0,0 +1,56 @@
package com.metaweb.gridworks.expr;
import java.util.Enumeration;
import java.util.Properties;
import org.python.core.PyException;
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 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 + "():");
for (int i = 0; i < lines.length; i++) {
function.append("\n " + lines[i]);
}
_code = function.toString();
}
public Object evaluate(Properties bindings) {
try {
for (Enumeration<Object> e = bindings.keys() ; e.hasMoreElements() ;) {
Object k = e.nextElement();
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;
}
return result;
} catch (PyException e) {
return new EvalError(e.toString());
}
}
}

View File

@ -0,0 +1,36 @@
package com.metaweb.gridworks.expr;
import java.util.Properties;
import org.python.core.PyObject;
import org.python.core.PyString;
public class JythonHasFieldsWrapper extends PyObject {
public HasFields _obj;
private Properties _bindings;
public JythonHasFieldsWrapper(HasFields obj, Properties bindings) {
_obj = obj;
_bindings = bindings;
}
public PyObject __finditem__(PyObject key) {
String k = (String) key.__tojava__(String.class);
Object v = _obj.getField(k, _bindings);
if (v != null) {
if (v instanceof HasFields) {
return new JythonHasFieldsWrapper((HasFields) v, _bindings);
} else if (v instanceof String) {
return new PyString((String) v);
} else if (v instanceof PyObject) {
return (PyObject) v;
} else {
return new JythonObjectWrapper(v);
}
} else {
return null;
}
}
}

View File

@ -0,0 +1,15 @@
package com.metaweb.gridworks.expr;
import org.python.core.PyObject;
public class JythonObjectWrapper extends PyObject {
public Object _obj;
public JythonObjectWrapper(Object obj) {
_obj = obj;
}
public String toString() {
return _obj.getClass().getSimpleName();
}
}

View File

@ -1,5 +1,10 @@
package com.metaweb.gridworks.expr; package com.metaweb.gridworks.expr;
import java.io.StringReader;
import java.util.Properties;
import clojure.lang.IFn;
import com.metaweb.gridworks.gel.Parser; import com.metaweb.gridworks.gel.Parser;
abstract public class MetaParser { abstract public class MetaParser {
@ -29,19 +34,17 @@ abstract public class MetaParser {
} }
static protected Evaluable parseJython(String s) throws ParsingException { static protected Evaluable parseJython(String s) throws ParsingException {
return null; return new JythonEvaluable(s);
} }
static protected Evaluable parseClojure(String s) throws ParsingException { static protected Evaluable parseClojure(String s) throws ParsingException {
/*
try { try {
IFn fn = (IFn) clojure.lang.Compiler.load(new StringReader( IFn fn = (IFn) clojure.lang.Compiler.load(new StringReader(
"(fn [value row cells] " + s + ")" "(fn [value cell cells row rowIndex] " + s + ")"
)); ));
return new Evaluable() { return new Evaluable() {
final private IFn _fn; private IFn _fn;
public Evaluable init(IFn fn) { public Evaluable init(IFn fn) {
_fn = fn; _fn = fn;
@ -51,9 +54,11 @@ abstract public class MetaParser {
public Object evaluate(Properties bindings) { public Object evaluate(Properties bindings) {
try { try {
return _fn.invoke( return _fn.invoke(
bindings.get("value"), bindings.get("value"),
bindings.get("row"), bindings.get("cell"),
bindings.get("cells") bindings.get("cells"),
bindings.get("row"),
bindings.get("rowIndex")
); );
} catch (Exception e) { } catch (Exception e) {
return new EvalError(e.getMessage()); return new EvalError(e.getMessage());
@ -61,9 +66,7 @@ abstract public class MetaParser {
} }
}.init(fn); }.init(fn);
} catch (Exception e) { } catch (Exception e) {
return new ParsingException(e); throw new ParsingException(e.getMessage());
} }
*/
return null;
} }
} }