Search in sources :

Example 1 with FramedVertexIterable

use of com.tinkerpop.frames.structures.FramedVertexIterable in project frames by tinkerpop.

the class GremlinGroovyAnnotationHandler method processVertex.

public Object processVertex(final GremlinGroovy annotation, final Method method, final Object[] arguments, final FramedGraph framedGraph, final Vertex vertex) {
    try {
        final CompiledScript script = this.engine.compile(annotation.value());
        final Bindings bindings = getBindings(method, arguments);
        bindings.put(IT, vertex);
        bindings.put(G, framedGraph);
        final Object result = script.eval(bindings);
        // TODO: Deprecate the use of _() and replace with it
        if (result instanceof Pipe & annotation.value().startsWith(PIPE)) {
            LOGGER.warning("_() is deprecated in favor of using 'it' to represent the framed vertex");
            ((Pipe) result).setStarts(new SingleIterator<Element>(vertex));
        }
        if (annotation.frame()) {
            if (result instanceof Iterable) {
                final FramedVertexIterable r = new FramedVertexIterable(framedGraph, (Iterable) result, ClassUtilities.getGenericClass(method));
                return (ClassUtilities.returnsIterable(method)) ? r : r.iterator().hasNext() ? r.iterator().next() : null;
            } else if (ClassUtilities.returnsMap(method)) {
                return new FramedVertexMap(framedGraph, (Map) result, ClassUtilities.getGenericClass(method));
            } else if (result instanceof Vertex) {
                return framedGraph.frame((Vertex) result, ClassUtilities.getGenericClass(method));
            } else {
                throw new IllegalStateException("The returned object can not be framed: " + result.getClass());
            }
        } else {
            return result;
        }
    } catch (ScriptException e) {
        //Preserve original exception functionality.
        ExceptionUtils.sneakyThrow(e);
        return null;
    }
}
Also used : CompiledScript(javax.script.CompiledScript) Vertex(com.tinkerpop.blueprints.Vertex) FramedVertexIterable(com.tinkerpop.frames.structures.FramedVertexIterable) Element(com.tinkerpop.blueprints.Element) FramedVertexMap(com.tinkerpop.frames.structures.FramedVertexMap) Pipe(com.tinkerpop.pipes.Pipe) Bindings(javax.script.Bindings) ScriptException(javax.script.ScriptException) FramedVertexIterable(com.tinkerpop.frames.structures.FramedVertexIterable) Map(java.util.Map) FramedVertexMap(com.tinkerpop.frames.structures.FramedVertexMap)

Aggregations

Element (com.tinkerpop.blueprints.Element)1 Vertex (com.tinkerpop.blueprints.Vertex)1 FramedVertexIterable (com.tinkerpop.frames.structures.FramedVertexIterable)1 FramedVertexMap (com.tinkerpop.frames.structures.FramedVertexMap)1 Pipe (com.tinkerpop.pipes.Pipe)1 Map (java.util.Map)1 Bindings (javax.script.Bindings)1 CompiledScript (javax.script.CompiledScript)1 ScriptException (javax.script.ScriptException)1