forked from krlawrence/graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphRegion.java
More file actions
89 lines (78 loc) · 2.77 KB
/
Copy pathGraphRegion.java
File metadata and controls
89 lines (78 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// GraphRegion.java
//
// Simple example of using TinkerGraph with Java
//
// This example does the following:
// 1. Create an empty TinkerGraph instance
// 2. Load the air routes graph
// 3. Run some tests that show the where...by and multiple V() constructs
// I have highlighted the places where the Gremlin is slightly different from the
// Gremlin we can use in the Gremlin Console.
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
import org.apache.tinkerpop.gremlin.process.traversal.Path;
import org.apache.tinkerpop.gremlin.process.traversal.*;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.structure.io.IoCore;
import org.apache.tinkerpop.gremlin.tinkergraph.structure.*;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.util.Gremlin;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
public class GraphRegion
{
private TinkerGraph tg;
private GraphTraversalSource g;
// Try to create a new graph and load the specified GraphML file
public boolean loadGraph(String name)
{
tg = TinkerGraph.open() ;
try
{
tg.io(IoCore.graphml()).readGraph(name);
}
catch( IOException e )
{
System.out.println("GraphStats - GraphML file not found");
return false;
}
g = tg.traversal();
return true;
}
// Find all airports in the region of the specified airport
//
// Note that when used from Java we have to prefix eq with a "P."
// Also select has to be prefixed by "__."
public void findByRegion(String iata)
{
System.out.println("\nRegion code lookup for " + iata );
List<List<Object>> list =
g.V().has("code",iata).values("region").as("r").
V().hasLabel("airport").as("a").values("region").
where(P.eq("r")).by().
local(__.select("a").values("city","code","region").fold()).toList();
for(List t : list)
{
System.out.println(t);
}
}
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());
GraphRegion gr = new GraphRegion() ;
if (gr.loadGraph("air-routes.graphml"))
{
gr.findByRegion("NCE"); // Nice
gr.findByRegion("DEN"); // Denver
gr.findByRegion("GVA"); // Geneva
gr.findByRegion("NGS"); // Nagasaki
gr.findByRegion("CAN"); // Guangzhou
}
}
}