Search in sources :

Example 66 with Path

use of org.apache.tinkerpop.gremlin.process.traversal.Path in project sqlg by pietermartin.

the class Schema method loadEdgeIndices.

/**
 * load indices for (out) edges on all vertices of schema
 *
 * @param traversalSource
 * @param schemaVertex
 */
void loadEdgeIndices(GraphTraversalSource traversalSource, Vertex schemaVertex) {
    List<Path> indices = traversalSource.V(schemaVertex).out(SQLG_SCHEMA_SCHEMA_VERTEX_EDGE).as("vertex").out(SQLG_SCHEMA_OUT_EDGES_EDGE).as("outEdgeVertex").out(SQLG_SCHEMA_EDGE_INDEX_EDGE).as("index").outE(SQLG_SCHEMA_INDEX_PROPERTY_EDGE).order().by(SQLG_SCHEMA_INDEX_PROPERTY_EDGE_SEQUENCE).inV().as("property").path().toList();
    for (Path vertexIndices : indices) {
        Vertex vertexVertex = null;
        Vertex vertexEdge = null;
        Vertex vertexIndex = null;
        Vertex propertyIndex = null;
        List<Set<String>> labelsList = vertexIndices.labels();
        for (Set<String> labels : labelsList) {
            for (String label : labels) {
                switch(label) {
                    case "vertex":
                        vertexVertex = vertexIndices.get("vertex");
                        break;
                    case "outEdgeVertex":
                        vertexEdge = vertexIndices.get("outEdgeVertex");
                        break;
                    case "index":
                        vertexIndex = vertexIndices.get("index");
                        break;
                    case "property":
                        propertyIndex = vertexIndices.get("property");
                        break;
                    case BaseStrategy.SQLG_PATH_FAKE_LABEL:
                    case BaseStrategy.SQLG_PATH_ORDER_RANGE_LABEL:
                    case MARKER:
                        break;
                    default:
                        throw new IllegalStateException(String.format("BUG: Only \"vertex\",\"outEdgeVertex\",\"index\" and \"property\" is expected as a label. Found %s", label));
                }
            }
        }
        Preconditions.checkState(vertexVertex != null, "BUG: Topology vertex not found.");
        String schemaName = schemaVertex.value(SQLG_SCHEMA_SCHEMA_NAME);
        String tableName = vertexVertex.value(SQLG_SCHEMA_VERTEX_LABEL_NAME);
        VertexLabel vertexLabel = this.vertexLabels.get(schemaName + "." + VERTEX_PREFIX + tableName);
        if (vertexLabel == null) {
            vertexLabel = new VertexLabel(this, tableName);
            this.vertexLabels.put(schemaName + "." + VERTEX_PREFIX + tableName, vertexLabel);
        }
        if (vertexEdge != null) {
            String edgeName = vertexEdge.value(SQLG_SCHEMA_EDGE_LABEL_NAME);
            Optional<EdgeLabel> oel = vertexLabel.getOutEdgeLabel(edgeName);
            if (oel.isPresent()) {
                EdgeLabel edgeLabel = oel.get();
                if (vertexIndex != null) {
                    String indexName = vertexIndex.value(SQLG_SCHEMA_INDEX_NAME);
                    Optional<Index> oidx = edgeLabel.getIndex(indexName);
                    Index idx;
                    if (oidx.isPresent()) {
                        idx = oidx.get();
                    } else {
                        idx = new Index(indexName, IndexType.fromString(vertexIndex.value(SQLG_SCHEMA_INDEX_INDEX_TYPE)), edgeLabel);
                        edgeLabel.addIndex(idx);
                    }
                    if (propertyIndex != null) {
                        String propertyName = propertyIndex.value(SQLG_SCHEMA_PROPERTY_NAME);
                        edgeLabel.getProperty(propertyName).ifPresent((PropertyColumn pc) -> idx.addProperty(pc));
                    }
                }
            }
        }
    }
}
Also used : Path(org.apache.tinkerpop.gremlin.process.traversal.Path) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex)

Example 67 with Path

use of org.apache.tinkerpop.gremlin.process.traversal.Path in project sqlg by pietermartin.

the class TestTraversals method g_V_out_out_path_byXnameX_byXageX.

@Test
public void g_V_out_out_path_byXnameX_byXageX() {
    loadModern();
    List<Vertex> vertices = gt.V().out().out().toList();
    List<Path> paths = gt.V().out().out().path().toList();
    final Traversal<Vertex, Path> traversal = gt.V().out().out().path().by("name").by("age");
    printTraversalForm(traversal);
    int counter = 0;
    while (traversal.hasNext()) {
        counter++;
        final Path path = traversal.next();
        Assert.assertEquals(3, path.size());
        Assert.assertEquals("marko", path.<String>get(0));
        Assert.assertEquals(Integer.valueOf(32), path.<Integer>get(1));
        Assert.assertTrue(path.get(2).equals("lop") || path.get(2).equals("ripple"));
    }
    Assert.assertEquals(2, counter);
}
Also used : Path(org.apache.tinkerpop.gremlin.process.traversal.Path) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 68 with Path

use of org.apache.tinkerpop.gremlin.process.traversal.Path in project graph by krlawrence.

the class JanusCassandra method main.

public static void main(String[] args) {
    System.out.println(" --------------------------");
    System.out.println(" Connecting to Graph       ");
    System.out.println(" --------------------------");
    // JanusGraph jg = JanusGraphFactory.open("janusgraph-cassandra.properties") ;
    JanusGraph jg = JanusGraphFactory.open("janusgraph-cql.properties");
    GraphTraversalSource g = jg.traversal();
    System.out.println(" --------------------------");
    System.out.println(" Sending queries           ");
    System.out.println(" --------------------------");
    Map<String, ?> aus = g.V().has("code", "AUS").valueMap().next();
    System.out.println(aus);
    List city = (List) (aus.get("city"));
    System.out.println("The AUS airport is in " + city.get(0));
    aus.forEach((k, v) -> System.out.println(k + ": " + v));
    Long n = g.V().has("code", "DFW").out().count().next();
    System.out.println("There are " + n + " routes from Dallas");
    List fromAus = (g.V().has("code", "AUS").out().values("code").toList());
    System.out.println(fromAus);
    List<Path> lhrToUsa = g.V().has("code", "LHR").outE().inV().has("country", "US").limit(5).path().by("code").by("dist").toList();
    lhrToUsa.forEach((k) -> System.out.println(k));
    ArrayList<Path> routes = new ArrayList<>();
    g.V().has("code", "SAT").out().path().by("icao").fill(routes);
    System.out.println(routes);
    System.out.println("*******************");
    Vertex v = g.V().has("code", "FRA").next();
    System.out.println(v.keys());
    Set<String> s = v.keys();
    for (String k : s) {
        System.out.println(k + "\t: " + v.property(k).value());
    }
    System.out.println("*******************");
    List eng = g.V().has("code", "AUS").repeat(__.out()).times(2).has("region", "GB-ENG").dedup().values("code").toList();
    System.out.println(eng);
    System.out.println(" --------------------------");
    System.out.println(" Shutting down             ");
    System.out.println(" --------------------------");
    jg.tx().commit();
    jg.close();
    System.exit(0);
}
Also used : GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) Path(org.apache.tinkerpop.gremlin.process.traversal.Path) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 69 with Path

use of org.apache.tinkerpop.gremlin.process.traversal.Path in project graph by krlawrence.

the class RouteSearch method main.

// Run some tests
public static void main(String[] args) {
    // If you want to check your Gremlin version, uncomment the next line
    // System.out.println("Gremlin version is: " + Gremlin.version());
    RouteSearch rs = new RouteSearch();
    boolean loaded = rs.loadGraph("air-routes.graphml");
    if (loaded) {
        boolean done = false;
        while (!done) {
            System.out.println("\nEnter from and to airport codes, eg DFW");
            Console console = System.console();
            System.out.print("From : ");
            String from = console.readLine().toUpperCase();
            System.out.print("To   : ");
            String to = console.readLine().toUpperCase();
            System.out.print("Maximum number of routes to look for : ");
            int max = Integer.parseInt(console.readLine());
            System.out.print("Maximum number of stops : ");
            int stops = Integer.parseInt(console.readLine());
            List<Path> list;
            list = rs.findRoutes(from, to, max, stops);
            if (list == null) {
                System.out.println("\nPlease enter valid airport codes");
            } else if (list.isEmpty()) {
                System.out.println("\nSorry, no routes were found, try more stops");
            } else {
                list.forEach((v) -> System.out.println(v));
            }
            System.out.print("\nAnother search (Y/N)? : ");
            String again = console.readLine().trim().toUpperCase();
            if (again.equals("N")) {
                done = true;
            }
        }
    }
}
Also used : Path(org.apache.tinkerpop.gremlin.process.traversal.Path) org.apache.tinkerpop.gremlin.process.traversal(org.apache.tinkerpop.gremlin.process.traversal) Set(java.util.Set) org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__) IOException(java.io.IOException) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) T(org.apache.tinkerpop.gremlin.structure.T) IoCore(org.apache.tinkerpop.gremlin.structure.io.IoCore) Gremlin(org.apache.tinkerpop.gremlin.util.Gremlin) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) org.apache.tinkerpop.gremlin.tinkergraph.structure(org.apache.tinkerpop.gremlin.tinkergraph.structure) Console(java.io.Console) Path(org.apache.tinkerpop.gremlin.process.traversal.Path) GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) Edge(org.apache.tinkerpop.gremlin.structure.Edge) Console(java.io.Console)

Example 70 with Path

use of org.apache.tinkerpop.gremlin.process.traversal.Path in project graph by krlawrence.

the class CreateGraph method main.

public static void main(String[] args) {
    // If you want to check your Gremlin version, uncomment the next line
    // System.out.println("Gremlin version is: " + Gremlin.version());
    // Create a new (empty) TinkerGrap
    TinkerGraph tg = TinkerGraph.open();
    // Create a Traversal source object
    GraphTraversalSource g = tg.traversal();
    // Add some nodes and vertices - Note the use of "iterate".
    g.addV("airport").property("code", "AUS").as("aus").addV("airport").property("code", "DFW").as("dfw").addV("airport").property("code", "LAX").as("lax").addV("airport").property("code", "JFK").as("jfk").addV("airport").property("code", "ATL").as("atl").addE("route").from("aus").to("dfw").addE("route").from("aus").to("atl").addE("route").from("atl").to("dfw").addE("route").from("atl").to("jfk").addE("route").from("dfw").to("jfk").addE("route").from("dfw").to("lax").addE("route").from("lax").to("jfk").addE("route").from("lax").to("aus").addE("route").from("lax").to("dfw").iterate();
    // System.out.println(g);
    // System.out.println(g.V().valueMap(true).toList());
    // Simple example of how to work with the results we get back from a query
    List<Map<Object, Object>> vm = new ArrayList<Map<Object, Object>>();
    vm = g.V().valueMap(true).toList();
    // Dislpay the code property as well as the label and id.
    for (Map m : vm) {
        System.out.println(((List) (m.get("code"))).get(0) + " " + m.get(T.id) + " " + m.get(T.label));
    }
    System.out.println();
    // Display the routes in the graph we just created.
    // Each path will include the vertex code values and the edge.
    List<Path> paths = new ArrayList<Path>();
    paths = g.V().outE().inV().path().by("code").by().toList();
    for (Path p : paths) {
        System.out.println(p.toString());
    }
    // Count how many vertices and edges we just created.
    // Using groupCount is overkill when we only have one label
    // but typically you will have more so this is a useful technique
    // to be aware of.
    System.out.println("\nWe just created");
    List verts = g.V().groupCount().by(T.label).toList();
    System.out.println(((Map) verts.get(0)).get("airport") + " airports");
    List edges = g.E().groupCount().by(T.label).toList();
    System.out.println(((Map) edges.get(0)).get("route") + " routes");
    // Note that we could also use the following code for a simple
    // case where we are only interested in specific labels.
    Long nv = g.V().hasLabel("airport").count().next();
    Long ne = g.E().hasLabel("route").count().next();
    System.out.println("The graph has " + nv + " airports and " + ne + " routes");
    // Save the graph we just created as GraphML (XML) or GraphSON (JSON)
    try {
        // If you want to save the graph as GraphML uncomment the next line
        tg.io(IoCore.graphml()).writeGraph("mygraph.graphml");
    // If you want to save the graph as JSON uncomment the next line
    // tg.io(IoCore.graphson()).writeGraph("mygraph.json");
    } catch (IOException ioe) {
        System.out.println("Graph failed to save");
    }
}
Also used : GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) Path(org.apache.tinkerpop.gremlin.process.traversal.Path) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Path (org.apache.tinkerpop.gremlin.process.traversal.Path)137 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)131 Test (org.junit.Test)125 BaseTest (org.umlg.sqlg.test.BaseTest)125 DefaultGraphTraversal (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal)81 org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__ (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__)74 T (org.apache.tinkerpop.gremlin.structure.T)74 Predicate (java.util.function.Predicate)73 Assert (org.junit.Assert)73 Graph (org.apache.tinkerpop.gremlin.structure.Graph)50 GraphTraversalSource (org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource)47 IOException (java.io.IOException)46 java.util (java.util)39 SqlgVertexStep (org.umlg.sqlg.step.SqlgVertexStep)39 List (java.util.List)38 MapHelper (org.apache.tinkerpop.gremlin.process.traversal.step.util.MapHelper)38 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)32 SqlgGraphStep (org.umlg.sqlg.step.SqlgGraphStep)26 StopWatch (org.apache.commons.lang3.time.StopWatch)22 Map (java.util.Map)19