Search in sources :

Example 41 with GremlinPipeline

use of com.tinkerpop.gremlin.java.GremlinPipeline in project gremlin by tinkerpop.

the class MapStepTest method test_g_v1_map.

public void test_g_v1_map() {
    super.test_g_v1_map(new GremlinPipeline(g.getVertex(1)).map());
    super.test_g_v1_map(new GremlinPipeline(g.getVertex(1)).optimize(false).map());
}
Also used : GremlinPipeline(com.tinkerpop.gremlin.java.GremlinPipeline)

Example 42 with GremlinPipeline

use of com.tinkerpop.gremlin.java.GremlinPipeline in project gremlin by tinkerpop.

the class MapStepTest method test_g_v1_outXknowsX_map.

public void test_g_v1_outXknowsX_map() {
    super.test_g_v1_outXknowsX_map(new GremlinPipeline(g.getVertex(1)).out("knows").map());
    super.test_g_v1_outXknowsX_map(new GremlinPipeline(g.getVertex(1)).optimize(false).out("knows").map());
}
Also used : GremlinPipeline(com.tinkerpop.gremlin.java.GremlinPipeline)

Example 43 with GremlinPipeline

use of com.tinkerpop.gremlin.java.GremlinPipeline in project orientdb by orientechnologies.

the class OGremlinHelper method execute.

public static Object execute(final OrientBaseGraph graph, final String iText, final Map<Object, Object> iConfiguredParameters, Map<Object, Object> iCurrentParameters, final List<Object> iResult, final OGremlinCallback iBeforeExecution, final OGremlinCallback iAfterExecution) {
    try {
        final ScriptEngine engine = getGremlinEngine(graph);
        try {
            final String output = OGremlinHelper.bindParameters(engine, iConfiguredParameters, iCurrentParameters);
            if (iBeforeExecution != null)
                if (!iBeforeExecution.call(engine, graph))
                    return null;
            if (iText == null) {
                return null;
            }
            final Object scriptResult = engine.eval(iText);
            if (iAfterExecution != null)
                if (!iAfterExecution.call(engine, graph))
                    return null;
            // - Map -> ODocument
            if (output != null) {
                if (scriptResult instanceof GremlinPipeline) {
                    Iterator<?> it = ((GremlinPipeline<?, ?>) scriptResult).iterator();
                    while (it.hasNext()) // ignore iCurrentRecord but traverse still required
                    it.next();
                }
                final Map<String, Object> map = (Map<String, Object>) engine.get(output);
                ODocument oDocument = new ODocument(map);
                iResult.add(oDocument);
                return oDocument;
            }
            // returned for this call in the last pipe
            if (scriptResult instanceof GremlinPipeline) {
                final Iterator<?> it = ((GremlinPipeline<?, ?>) scriptResult).iterator();
                Object finalResult = null;
                List<Object> resultCollection = null;
                while (it.hasNext()) {
                    Object current = it.next();
                    if (finalResult != null) {
                        if (resultCollection == null) {
                            // CONVERT IT INTO A COLLECTION
                            resultCollection = new ArrayList<Object>();
                            resultCollection.add(finalResult);
                        }
                        resultCollection.add(current);
                    } else
                        finalResult = current;
                }
                if (resultCollection != null) {
                    iResult.addAll(resultCollection);
                    return resultCollection;
                } else {
                    if (finalResult != null)
                        iResult.add(finalResult);
                    return finalResult;
                }
            } else if (scriptResult != null)
                iResult.add(scriptResult);
            return scriptResult;
        } catch (Exception e) {
            throw OException.wrapException(new OCommandExecutionException("Error on execution of the GREMLIN script"), e);
        } finally {
            OGremlinHelper.global().releaseEngine(engine);
        }
    } finally {
        OGremlinHelper.global().releaseGraph(graph);
    }
}
Also used : GremlinPipeline(com.tinkerpop.gremlin.java.GremlinPipeline) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) HashMap(java.util.HashMap) Map(java.util.Map) GremlinGroovyScriptEngine(com.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine) ScriptEngine(javax.script.ScriptEngine) OException(com.orientechnologies.common.exception.OException) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 44 with GremlinPipeline

use of com.tinkerpop.gremlin.java.GremlinPipeline in project orientdb by orientechnologies.

the class TestDirtyTrackingTreeRidBagRemote method test.

@Test
public void test() {
    final int max = OGlobalConfiguration.RID_BAG_EMBEDDED_TO_SBTREEBONSAI_THRESHOLD.getValueAsInteger() * 2;
    OrientGraph graph = new OrientGraph("remote:localhost:3064/" + TestDirtyTrackingTreeRidBagRemote.class.getSimpleName(), "root", "root");
    try {
        graph.getRawGraph().declareIntent(new OIntentMassiveInsert());
        graph.createEdgeType("Edge");
        OIdentifiable oneVertex = null;
        Map<Object, Vertex> vertices = new HashMap<Object, Vertex>();
        for (int i = 0; i < max; i++) {
            Vertex v = graph.addVertex("class:V");
            v.setProperty("key", "foo" + i);
            graph.commit();
            vertices.put(v.getProperty("key"), v);
            if (i == max / 2 + 1)
                oneVertex = ((OrientVertex) v).getIdentity();
        }
        graph.commit();
        // Add the edges
        for (int i = 0; i < max; i++) {
            String codeUCD1 = "foo" + i;
            // Take the first vertex
            Vertex med1 = (Vertex) vertices.get(codeUCD1);
            // For the 2nd term
            for (int j = 0; j < max; j++) {
                String key = "foo" + j;
                // Take the second vertex
                Vertex med2 = (Vertex) vertices.get(key);
                // ((OrientVertex)med2).getRecord().reload();
                OrientEdge eInteraction = graph.addEdge(null, med1, med2, "Edge");
                assertNotNull(graph.getRawGraph().getTransaction().getRecordEntry(((OrientVertex) med2).getIdentity()));
            }
            // COMMIT
            graph.commit();
        }
        graph.getRawGraph().getLocalCache().clear();
        OrientVertex vertex = graph.getVertex(oneVertex);
        assertEquals(new GremlinPipeline<Vertex, Long>().start(vertex).in("Edge").count(), max);
    } finally {
        graph.shutdown();
    }
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) GremlinPipeline(com.tinkerpop.gremlin.java.GremlinPipeline) HashMap(java.util.HashMap) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) OIntentMassiveInsert(com.orientechnologies.orient.core.intent.OIntentMassiveInsert) Test(org.junit.Test)

Example 45 with GremlinPipeline

use of com.tinkerpop.gremlin.java.GremlinPipeline in project incubator-atlas by apache.

the class AtlasEntityQueryTest method testExecute_Collection_update.

@Test
public void testExecute_Collection_update() throws Exception {
    AtlasGraph graph = createStrictMock(AtlasGraph.class);
    QueryExpression expression = createStrictMock(QueryExpression.class);
    ResourceDefinition resourceDefinition = createStrictMock(ResourceDefinition.class);
    Request request = createStrictMock(Request.class);
    GremlinPipeline initialPipeline = createStrictMock(GremlinPipeline.class);
    Pipe queryPipe = createStrictMock(Pipe.class);
    Pipe expressionPipe = createStrictMock(Pipe.class);
    Pipe notDeletedPipe = createStrictMock(Pipe.class);
    GremlinPipeline rootPipeline = createStrictMock(GremlinPipeline.class);
    GremlinPipeline queryPipeline = createStrictMock(GremlinPipeline.class);
    GremlinPipeline expressionPipeline = createStrictMock(GremlinPipeline.class);
    GremlinPipeline notDeletedPipeline = createStrictMock(GremlinPipeline.class);
    Vertex vertex1 = createStrictMock(Vertex.class);
    VertexWrapper vertex1Wrapper = createStrictMock(VertexWrapper.class);
    Capture<Long> modifiedTimestampCapture = newCapture();
    List<Vertex> results = new ArrayList<>();
    results.add(vertex1);
    Map<String, Object> vertex1PropertyMap = new HashMap<>();
    vertex1PropertyMap.put("prop1", "prop1.value1");
    vertex1PropertyMap.put("prop2", "prop2.value1");
    Map<String, Object> filteredVertex1PropertyMap = new HashMap<>();
    filteredVertex1PropertyMap.put("prop1", "prop1.value1");
    Map<String, Object> updateProperties = new HashMap<>();
    updateProperties.put("prop3", "newValue");
    // mock expectations
    expect(initialPipeline.add(queryPipe)).andReturn(queryPipeline);
    expect(initialPipeline.add(notDeletedPipe)).andReturn(notDeletedPipeline);
    expect(initialPipeline.as("root")).andReturn(rootPipeline);
    expect(expression.asPipe()).andReturn(expressionPipe);
    expect(rootPipeline.add(expressionPipe)).andReturn(expressionPipeline);
    expect(expressionPipeline.back("root")).andReturn(rootPipeline);
    expect(rootPipeline.toList()).andReturn(results);
    graph.commit();
    vertex1Wrapper.setProperty("prop3", "newValue");
    vertex1Wrapper.setProperty(eq(Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY), capture(modifiedTimestampCapture));
    expect(vertex1Wrapper.getPropertyMap()).andReturn(vertex1PropertyMap);
    expect(resourceDefinition.filterProperties(request, vertex1PropertyMap)).andReturn(filteredVertex1PropertyMap);
    expect(resourceDefinition.resolveHref(filteredVertex1PropertyMap)).andReturn("/foo/bar");
    expect(request.getCardinality()).andReturn(Request.Cardinality.COLLECTION);
    replay(graph, expression, resourceDefinition, request, initialPipeline, queryPipe, expressionPipe, notDeletedPipe, rootPipeline, queryPipeline, expressionPipeline, notDeletedPipeline, vertex1, vertex1Wrapper);
    // end mock expectations
    AtlasEntityQuery query = new TestAtlasEntityQuery(expression, resourceDefinition, request, initialPipeline, queryPipe, notDeletedPipe, graph, vertex1Wrapper);
    long startTime = System.currentTimeMillis();
    // invoke method being tested
    Collection<Map<String, Object>> queryResults = query.execute(updateProperties);
    long endTime = System.currentTimeMillis();
    assertEquals(queryResults.size(), 1);
    Map<String, Object> queryResultMap = queryResults.iterator().next();
    assertEquals(queryResultMap.size(), 2);
    assertEquals(queryResultMap.get("prop1"), "prop1.value1");
    assertEquals(queryResultMap.get("href"), "/foo/bar");
    long modifiedTimestamp = modifiedTimestampCapture.getValue();
    assertTrue(modifiedTimestamp >= startTime && modifiedTimestamp <= endTime);
    verify(graph, expression, resourceDefinition, request, initialPipeline, queryPipe, expressionPipe, notDeletedPipe, rootPipeline, queryPipeline, expressionPipeline, notDeletedPipeline, vertex1, vertex1Wrapper);
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) GremlinPipeline(com.tinkerpop.gremlin.java.GremlinPipeline) ResourceDefinition(org.apache.atlas.catalog.definition.ResourceDefinition) Request(org.apache.atlas.catalog.Request) Pipe(com.tinkerpop.pipes.Pipe) VertexWrapper(org.apache.atlas.catalog.VertexWrapper) AtlasGraph(org.apache.atlas.repository.graphdb.AtlasGraph) Test(org.testng.annotations.Test)

Aggregations

GremlinPipeline (com.tinkerpop.gremlin.java.GremlinPipeline)93 Vertex (com.tinkerpop.blueprints.Vertex)28 PipeFunction (com.tinkerpop.pipes.PipeFunction)13 Pipe (com.tinkerpop.pipes.Pipe)6 HashMap (java.util.HashMap)4 Request (org.apache.atlas.catalog.Request)3 VertexWrapper (org.apache.atlas.catalog.VertexWrapper)3 ResourceDefinition (org.apache.atlas.catalog.definition.ResourceDefinition)3 AtlasGraph (org.apache.atlas.repository.graphdb.AtlasGraph)3 Test (org.testng.annotations.Test)3 LoopPipe (com.tinkerpop.pipes.branch.LoopPipe)2 PropertyFilterPipe (com.tinkerpop.pipes.filter.PropertyFilterPipe)2 Table (com.tinkerpop.pipes.util.structures.Table)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Test (org.junit.Test)2 OException (com.orientechnologies.common.exception.OException)1 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1