Search in sources :

Example 6 with Element

use of com.tinkerpop.blueprints.Element in project orientdb by orientechnologies.

the class OGraphSONUtility method createJSONMap.

private static ObjectNode createJSONMap(final Map map, final List<String> propertyKeys, final boolean showTypes) {
    final ObjectNode jsonMap = jsonNodeFactory.objectNode();
    for (Object key : map.keySet()) {
        Object value = map.get(key);
        if (value != null) {
            if (value instanceof List) {
                value = createJSONList((List) value, propertyKeys, showTypes);
            } else if (value instanceof Map) {
                value = createJSONMap((Map) value, propertyKeys, showTypes);
            } else if (value instanceof Element) {
                value = objectNodeFromElement((Element) value, propertyKeys, showTypes ? GraphSONMode.EXTENDED : GraphSONMode.NORMAL);
            } else if (value.getClass().isArray()) {
                value = createJSONList(convertArrayToList(value), propertyKeys, showTypes);
            }
        }
        putObject(jsonMap, key.toString(), getValue(value, showTypes));
    }
    return jsonMap;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Element(com.tinkerpop.blueprints.Element) JSONObject(org.codehaus.jettison.json.JSONObject)

Example 7 with Element

use of com.tinkerpop.blueprints.Element 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)

Example 8 with Element

use of com.tinkerpop.blueprints.Element in project blueprints by tinkerpop.

the class ElementHelperTest method testRemoveProperties.

public void testRemoveProperties() {
    Graph graph = TinkerGraphFactory.createTinkerGraph();
    Vertex vertex = graph.getVertex(1);
    assertEquals(vertex.getProperty("name"), "marko");
    assertEquals(vertex.getProperty("age"), 29);
    assertEquals(vertex.getPropertyKeys().size(), 2);
    ElementHelper.removeProperties(Arrays.asList((Element) vertex));
    assertNull(vertex.getProperty("name"));
    assertNull(vertex.getProperty("age"));
    assertEquals(vertex.getPropertyKeys().size(), 0);
    ElementHelper.removeProperties(Arrays.asList((Element) vertex));
    assertNull(vertex.getProperty("name"));
    assertNull(vertex.getProperty("age"));
    assertEquals(vertex.getPropertyKeys().size(), 0);
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) Graph(com.tinkerpop.blueprints.Graph) Element(com.tinkerpop.blueprints.Element)

Aggregations

Element (com.tinkerpop.blueprints.Element)8 Vertex (com.tinkerpop.blueprints.Vertex)3 ArrayList (java.util.ArrayList)3 JSONObject (org.codehaus.jettison.json.JSONObject)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 Map (java.util.Map)2 Graph (com.tinkerpop.blueprints.Graph)1 Index (com.tinkerpop.blueprints.Index)1 TransactionalGraph (com.tinkerpop.blueprints.TransactionalGraph)1 TinkerGraph (com.tinkerpop.blueprints.impls.tg.TinkerGraph)1 FramedVertexIterable (com.tinkerpop.frames.structures.FramedVertexIterable)1 FramedVertexMap (com.tinkerpop.frames.structures.FramedVertexMap)1 Pipe (com.tinkerpop.pipes.Pipe)1 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 MethodHandler (javassist.util.proxy.MethodHandler)1 Proxy (javassist.util.proxy.Proxy)1