Java Code Examples for org.python.core.PyFunction#__call__

The following examples show how to use org.python.core.PyFunction#__call__ . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: VertexNeighbourUtilities.java    From constellation with Apache License 2.0 6 votes vote down vote up
public List<Object> for_nodes_at_distance(final PyFunction condition, final PyFunction computation, final int distance) throws ScriptException {
    context.require(GraphElementType.VERTEX);
    final List<Integer> elementsAtDistance = new LinkedList<>();
    final List<Object> results = new ArrayList<>();
    getAtDistance(distance, context.current(), elementsAtDistance, new BitSet(context.graph.getVertexCount()));
    for (int elementId : elementsAtDistance) {
        context.enter(elementId);
        if (condition == null || isTrueValue(condition.__call__())) {
            final Object result = computation.__call__();
            if (!nullCheck(result)) {
                results.add(result);
            }
        }
        context.exit();
    }
    return results;
}
 
Example 2
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 6 votes vote down vote up
private PyList forTransactions(final PyFunction condition, PyFunction nodeCondition, PyFunction computation, final int direction) {
    context.require(GraphElementType.VERTEX);
    final Map<Integer, Integer> transactions = getTransactions(context.current(), direction);
    final List<Object> results = new ArrayList<>();
    for (final Map.Entry<Integer, Integer> entry : transactions.entrySet()) {
        context.enter(entry.getKey(), GraphElementType.TRANSACTION);
        if (condition == null || isTrueValue(condition.__call__())) {
            context.enter(entry.getValue(), GraphElementType.VERTEX);
            boolean otherNodeMatches = nodeCondition == null || isTrueValue(nodeCondition.__call__());
            context.exit();
            if (otherNodeMatches) {
                Object result = computation.__call__();
                if (!nullCheck(result)) {
                    results.add(result);
                }
            }
        }
        context.exit();
    }
    return new PyList(results);
}
 
Example 3
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 6 votes vote down vote up
public PyList for_parallel_transactions(final PyFunction condition, final PyFunction computation) throws ScriptException {
    context.require(GraphElementType.TRANSACTION);
    final Set<Integer> transactions = getParallelTransactions(context.current());
    final List<Object> results = new ArrayList<>();
    for (int transId : transactions) {
        context.enter(transId, GraphElementType.TRANSACTION);
        if (condition == null || isTrueValue(condition.__call__())) {
            final Object result = computation.__call__();
            if (!nullCheck(result)) {
                results.add(result);
            }
        }
        context.exit();
    }
    return new PyList(results);
}
 
Example 4
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 6 votes vote down vote up
public PyList for_edges(final PyFunction condition, PyFunction nodeCondition, PyFunction computation) {
    context.require(GraphElementType.VERTEX);
    final Map<Integer, Integer> edges = getEdges(context.current());
    final List<Object> results = new ArrayList<>();
    for (final Map.Entry<Integer, Integer> entry : edges.entrySet()) {
        context.enter(entry.getKey(), GraphElementType.EDGE);
        if (condition == null || isTrueAndContainsNonNulls(condition.__call__())) {
            context.enter(entry.getValue(), GraphElementType.VERTEX);
            boolean otherNodeMatches = nodeCondition == null || isTrueValue(nodeCondition.__call__());
            context.exit();
            if (otherNodeMatches) {
                final Object result = computation.__call__();
                if (!nullCheck(result)) {
                    results.add(result);
                }
            }
        }
        context.exit();
    }
    return new PyList(results);
}
 
Example 5
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 6 votes vote down vote up
public PyList for_links(final PyFunction condition, PyFunction nodeCondition, PyFunction computation) {
    context.require(GraphElementType.VERTEX);
    final Map<Integer, Integer> links = getLinks(context.current());
    final List<Object> results = new ArrayList<>();
    for (final Map.Entry<Integer, Integer> entry : links.entrySet()) {
        context.enter(entry.getKey(), GraphElementType.LINK);
        if (condition == null || isTrueAndContainsNonNulls(condition.__call__())) {
            context.enter(entry.getValue(), GraphElementType.VERTEX);
            boolean otherNodeMatches = nodeCondition == null || isTrueValue(nodeCondition.__call__());
            context.exit();
            if (otherNodeMatches) {
                final Object result = computation.__call__();
                if (!nullCheck(result)) {
                    results.add(result);
                }
            }
        }
        context.exit();
    }
    return new PyList(results);
}
 
Example 6
Source File: XunfengInner.java    From TrackRay with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void process() {

    try {
        PythonInterpreter interpreter = python.interpreter();
        interpreter.execfile(filename);
        PyFunction check = interpreter.get("check", PyFunction.class);

        PyObject check_call = check.__call__(new PyString(ip), new PyInteger(port), new PyInteger(timeout));

        String result = check_call.toString();
        if (result!=null &&
                !StringUtils.contains(result,"None")
                && !StringUtils.contains(result,"False")) {

            //PyObject get_plugin_info = interpreter.get("get_plugin_info").__call__();
            //Map map = (Map) get_plugin_info.getDict().__tojava__(Map.class);
            this.result = result;
            return;
        }
    }catch (Exception e){
        log.error(e.toString());
    }

    result="";
}
 
Example 7
Source File: GraphAnalysisUtilities.java    From constellation with Apache License 2.0 5 votes vote down vote up
public List<Object> graph_for_transactions(final PyFunction condition, final PyFunction computation) throws ScriptException {
    final List<Object> results = new ArrayList<>();
    for (int i = 0; i < context.graph.getTransactionCount(); i++) {
        final int elementId = context.graph.getTransaction(i);
        context.enter(elementId, GraphElementType.TRANSACTION);
        if (condition == null || isTrueValue(condition.__call__())) {
            final Object result = computation.__call__();
            if (!nullCheck(result)) {
                results.add(result);
            }
        }
        context.exit();
    }
    return results;
}
 
Example 8
Source File: GraphAnalysisUtilities.java    From constellation with Apache License 2.0 5 votes vote down vote up
public List<Object> graph_for_nodes(final PyFunction condition, final PyFunction computation) throws ScriptException {
    final List<Object> results = new ArrayList<>();
    for (int i = 0; i < context.graph.getVertexCount(); i++) {
        final int elementId = context.graph.getVertex(i);
        context.enter(elementId, GraphElementType.VERTEX);
        if (condition == null || isTrueValue(condition.__call__())) {
            final Object result = computation.__call__();
            if (!nullCheck(result)) {
                results.add(result);
            }
        }
        context.exit();
    }
    return results;
}
 
Example 9
Source File: AndroidDriver.java    From AndroidRobot with Apache License 2.0 5 votes vote down vote up
private PyObject executePython(String filePath, String function, PyObject param0, PyObject param1){
	PythonInterpreter interpreter = new PythonInterpreter();
	Vector<AndroidDriver> drivers = new Vector();
	drivers.add(this);
	interpreter.set("device", drivers);
	interpreter.execfile(filePath);
	PyFunction pyfunction = interpreter.get(function, PyFunction.class);
	PyObject pyobj = pyfunction.__call__(param0, param1);
	return pyobj;
}
 
Example 10
Source File: PythonCode.java    From pycode-minecraft with MIT License 5 votes vote down vote up
public void invoke(String method) {
    PyObject obj = (PyObject) this.bindings.get(method);
    if (obj == null) {
        failz0r(world, pos, "Unknown function '%s'", method);
        return;
    }
    PyFunction func = (PyFunction)obj;
    try {
        func.__call__();
    } catch (RuntimeException e) {
        failz0r(world, pos, "Error running code: %s", e.toString());
    }
}
 
Example 11
Source File: AndroidDriver.java    From AndroidRobot with Apache License 2.0 5 votes vote down vote up
private PyObject executePython(String filePath, String function){
	PythonInterpreter interpreter = new PythonInterpreter();
	Vector<AndroidDriver> drivers = new Vector();
	drivers.add(this);
	interpreter.set("device", drivers);
	interpreter.execfile(filePath);
	PyFunction pyfunction = interpreter.get(function, PyFunction.class);
	PyObject pyobj = pyfunction.__call__();
	return pyobj;
}
 
Example 12
Source File: AndroidDriver.java    From AndroidRobot with Apache License 2.0 5 votes vote down vote up
private PyObject executePython(String filePath, String function, PyObject params){
	PythonInterpreter interpreter = new PythonInterpreter();
	Vector<AndroidDriver> drivers = new Vector();
	drivers.add(this);
	interpreter.set("device", drivers);
	interpreter.execfile(filePath);
	PyFunction pyfunction = interpreter.get(function, PyFunction.class);
	PyObject pyobj = pyfunction.__call__(params.__getitem__(0));
	return pyobj;
}
 
Example 13
Source File: PyRoutineWrapper.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * A wrapper static method for Python user-defined function
 *
 * @param args if the registered stored procedure has n parameters, the args[0] to args[n-1] are these parameters.
 * The args[n] must be a String which contains the Python script.
 */
public static Object pyFunctionWrapper(Object... args)
        throws Exception
{
    PyInterpreterPool pool = null;
    PythonInterpreter interpreter = null;
    String setFacFuncName = "setFactory";
    String funcName = "execute";
    PyObject pyResult = null;
    Object javaResult = null;

    try {
        byte[] compiledCode;
        int nargs = args.length;
        int pyScriptIdx = args.length - 1;

        // set pyScript
        compiledCode = (byte[]) args[pyScriptIdx];

        // set the Object[] to pass in
        Object[] pyArgs;
        if(nargs - 1 ==0){
            pyArgs = null;
        }
        else{
            pyArgs = new Object[nargs - 1];
            System.arraycopy(args, 0, pyArgs, 0, nargs - 1);
        }

        pool = PyInterpreterPool.getInstance();
        interpreter = pool.acquire();
        PyCodeUtil.exec(compiledCode, interpreter);
        // add global variable factory, so that the user can use it to construct JDBC ResultSet
        Object[] factoryArg = {new PyStoredProcedureResultSetFactory()};
        PyFunction addFacFunc = interpreter.get(setFacFuncName, PyFunction.class);
        addFacFunc._jcall(factoryArg);

        // execute the user defined function, the user needs to fill the ResultSet himself,
        // just like the original Java Stored Procedure
        PyFunction userFunc = interpreter.get(funcName, PyFunction.class);
        if(pyArgs == null){
            pyResult = userFunc.__call__();
        }else{
            pyResult = userFunc._jcall(pyArgs);
        }
        javaResult = pyResult.__tojava__(Object.class);
        if(pyResult instanceof PyLong){
            // When the result has type PyLong, the result's corresponding
            // sql type should be BigInt.
            javaResult = ((BigInteger) javaResult).longValue();
        }
        return javaResult;
    }
    catch (Exception e){
        throw StandardException.plainWrapException(e);
    }
    finally{
        if(pool != null && interpreter != null){
            pool.release(interpreter);
        }
    }
}
 
Example 14
Source File: PyRoutineWrapper.java    From spliceengine with GNU Affero General Public License v3.0 4 votes vote down vote up
/**
 * A wrapper static method for Python Stored Procedure
 *
 * @param args if the registered stored procedure has n parameters, the args[0] to args[n-1] are these parameters.
 * The args[n] must be a String which contains the Python script. The rest of elements in the args are ResultSet[].
 * In the current test version only allows one ResultSet[]
 */
public static void pyProcedureWrapper(Object... args)
        throws Exception
{
    PyInterpreterPool pool = null;
    PythonInterpreter interpreter = null;
    String setFacFuncName = "setFactory";
    String funcName = "execute";

    try {
        int pyScriptIdx;
        byte[] compiledCode;
        int nargs = args.length;
        Integer rsDelim = null;


        for(int i = 0; i < nargs; ++i){
            if(args[i] instanceof ResultSet[]){
                rsDelim = i;
                break;
            }
        }

        // set pyScript
        if(rsDelim == null){
            pyScriptIdx = nargs - 1;
        }
        else{
            pyScriptIdx = rsDelim - 1;
        }
        compiledCode = (byte[]) args[pyScriptIdx];

        // set the Object[] to pass into Jython code
        int j = 0;
        Object[] pyArgs;
        if(nargs - 1 ==0){
            pyArgs = null;
        }
        else{
            pyArgs = new Object[nargs - 1];
        }
        for(int i = 0; i < nargs; ++i){
            if(i != pyScriptIdx){
                pyArgs[j] = args[i];
                j++;
            }
        }

        pool = PyInterpreterPool.getInstance();
        interpreter = pool.acquire();
        PyCodeUtil.exec(compiledCode, interpreter);
        // add global variable factory, so that the user can use it to construct JDBC ResultSet
        Object[] factoryArg = {new PyStoredProcedureResultSetFactory()};
        PyFunction addFacFunc = interpreter.get(setFacFuncName, PyFunction.class);
        addFacFunc._jcall(factoryArg);

        // execute the user defined function, the user needs to fill the ResultSet himself,
        // just like the original Java Stored Procedure
        PyFunction userFunc = interpreter.get(funcName, PyFunction.class);
        if(pyArgs == null){
            userFunc.__call__();
        }else{
            userFunc._jcall(pyArgs);
        }
    }
    catch (Exception e){
        throw StandardException.plainWrapException(e);
    }
    finally{
        if(pool != null && interpreter != null){
            pool.release(interpreter);
        }
    }
}
 
Example 15
Source File: CoreData.java    From ytk-learn with MIT License 4 votes vote down vote up
private static PyList transform(String line, PyFunction pyTransformFunc) throws UnsupportedEncodingException {
    return (PyList) pyTransformFunc.__call__(new PyByteArray(line.getBytes("utf-8")));
}
 
Example 16
Source File: GBDTOnlinePredictor.java    From ytk-learn with MIT License 4 votes vote down vote up
private static PyList transform(String line, PyFunction pyTransformFunc) throws UnsupportedEncodingException {
    return (PyList) pyTransformFunc.__call__(new PyByteArray(line.getBytes("utf-8")));
}
 
Example 17
Source File: ContinuousOnlinePredictor.java    From ytk-learn with MIT License 4 votes vote down vote up
private static PyList transform(String line, PyFunction pyTransformFunc) throws UnsupportedEncodingException {
    return (PyList) pyTransformFunc.__call__(new PyByteArray(line.getBytes("utf-8")));
}