use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal in project janusgraph by JanusGraph.
the class JanusGraphIoRegistryTest method testGeoshapeAsGryo.
@Test
public void testGeoshapeAsGryo() throws SerializationException {
Graph graph = EmptyGraph.instance();
GraphTraversalSource g = graph.traversal();
GraphTraversal[] traversals = { g.addV().property("loc", Geoshape.box(0.1d, 0.2d, 0.3d, 0.4d)), g.addV().property("loc", Geoshape.box(0.1f, 0.3f, 0.5f, 0.6f)), g.addV().property("loc", Geoshape.circle(0.1d, 0.3d, 0.3d)), g.addV().property("loc", Geoshape.circle(0.2f, 0.4f, 0.5f)), g.addV().property("loc", Geoshape.point(1.0d, 4.0d)), g.addV().property("loc", Geoshape.point(1.0f, 1.0f)) };
serializationTest(traversals);
}
use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal in project janusgraph by JanusGraph.
the class AdjacentVertexHasIdOptimizerStrategyTest method testAll.
@Test
public void testAll() {
makeSampleGraph();
// AdjacentVertexHasIdOptimizer out/in/both
assertOptimization(g.V(sv[0]).outE("knows").has(ImplicitKey.ADJACENT_ID.name(), vs[50]).inV(), g.V(sv[0]).out("knows").hasId(vs[50]), optimizer);
assertOptimization(g.V(sv[0]).inE("knows").has(ImplicitKey.ADJACENT_ID.name(), vs[50]).outV(), g.V(sv[0]).in("knows").hasId(vs[50]), optimizer);
assertOptimization(g.V(sv[0]).bothE("knows").has(ImplicitKey.ADJACENT_ID.name(), vs[50]).otherV(), g.V(sv[0]).both("knows").hasId(vs[50]), optimizer);
// AdjacentVertexHasIdOptimizer outE/inE/bothE
assertOptimization(g.V(sv[0]).outE("knows").has(ImplicitKey.ADJACENT_ID.name(), vs[50]).inV(), g.V(sv[0]).outE("knows").inV().hasId(vs[50]), optimizer);
assertOptimization(g.V(sv[0]).inE("knows").has(ImplicitKey.ADJACENT_ID.name(), vs[50]).outV(), g.V(sv[0]).inE("knows").outV().hasId(vs[50]), optimizer);
assertOptimization(g.V(sv[0]).bothE("knows").has(ImplicitKey.ADJACENT_ID.name(), vs[50]).otherV(), g.V(sv[0]).bothE("knows").otherV().hasId(vs[50]), optimizer);
// Result should stay the same
assertSameResultWithOptimizations(g.V(sv[0]).as("v1").out("knows").hasId(vs[50].id()).as("v2").select("v1", "v2").by("id"), AdjacentVertexHasIdOptimizerStrategy.instance());
// neq should not be optimized
assertOptimization(g.V(sv[0]).in().hasId(P.neq(vs[50])), g.V(sv[0]).in().hasId(P.neq(vs[50])), optimizer);
// repeat starts from vertex with id 0 and goes in to the sv[0] vertex then loops back out to the vertex with the next id
int[] loop1 = { 0 };
int[] loop2 = { 0 };
GraphTraversal t1 = g.V(vs[0], vs[1], vs[2]).repeat(__.inE("knows").has(ImplicitKey.ADJACENT_ID.name(), sv[0].id()).outV().out(// TINKERPOP-2342
"knows").sideEffect(e -> loop1[0] = e.loops()).has("id", loop1[0])).times(numV);
GraphTraversal t2 = g.V(vs[0], vs[1], vs[2]).repeat(__.inE("knows").outV().hasId(sv[0].id()).out(// TINKERPOP-2342
"knows").sideEffect(e -> loop2[0] = e.loops()).has("id", loop2[0])).times(numV);
assertOptimization(t1, t2, optimizer);
}
use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal in project open-kilda by telstra.
the class FermaMirrorGroupRepository method findFirstUnassignedGroupId.
@Override
public Optional<GroupId> findFirstUnassignedGroupId(SwitchId switchId, GroupId lowestGroupId, GroupId highestGroupId) {
String switchIdAsStr = SwitchIdConverter.INSTANCE.toGraphProperty(switchId);
Long lowestGroupIdAsLong = GroupIdConverter.INSTANCE.toGraphProperty(lowestGroupId);
Long highestGroupIdAsLong = GroupIdConverter.INSTANCE.toGraphProperty(highestGroupId);
try (GraphTraversal<?, ?> traversal = framedGraph().traverse(g -> g.V().hasLabel(MirrorGroupFrame.FRAME_LABEL).has(MirrorGroupFrame.SWITCH_ID_PROPERTY, switchIdAsStr).has(MirrorGroupFrame.GROUP_ID_PROPERTY, P.gte(lowestGroupIdAsLong)).has(MirrorGroupFrame.GROUP_ID_PROPERTY, P.lt(highestGroupIdAsLong)).values(MirrorGroupFrame.GROUP_ID_PROPERTY).order().math("_ + 1").as("a").where(__.not(__.V().hasLabel(MirrorGroupFrame.FRAME_LABEL).has(MirrorGroupFrame.SWITCH_ID_PROPERTY, switchIdAsStr).values(MirrorGroupFrame.GROUP_ID_PROPERTY).where(P.eq("a")))).select("a").limit(1)).getRawTraversal()) {
if (traversal.hasNext()) {
return traversal.tryNext().map(l -> ((Double) l).longValue()).map(GroupIdConverter.INSTANCE::toEntityAttribute);
}
} catch (Exception e) {
throw new PersistenceException("Failed to traverse", e);
}
try (GraphTraversal<?, ?> traversal = framedGraph().traverse(g -> g.V().hasLabel(MirrorGroupFrame.FRAME_LABEL).has(MirrorGroupFrame.SWITCH_ID_PROPERTY, switchIdAsStr).has(MirrorGroupFrame.GROUP_ID_PROPERTY, lowestGroupIdAsLong)).getRawTraversal()) {
if (!traversal.hasNext()) {
return Optional.of(lowestGroupId);
}
} catch (Exception e) {
throw new PersistenceException("Failed to traverse", e);
}
return Optional.empty();
}
use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal in project open-kilda by telstra.
the class DatabaseSupportImpl method getPaths.
/**
* Get all possible paths between source and destination switches.
*
* @param src source switch ID
* @param dst destination switch ID
* @return list of PathInfoData objects
*/
@Override
@SuppressWarnings("unchecked")
public List<PathInfoData> getPaths(SwitchId src, SwitchId dst) {
return transactionManager.doInTransaction(() -> {
FramedGraph framedGraph = getGraph();
GraphTraversal<?, ?> rawTraversal = framedGraph.traverse(input -> input.V().hasLabel(SwitchFrame.FRAME_LABEL).has(SwitchFrame.SWITCH_ID_PROPERTY, src.toString()).repeat(__.outE(IslFrame.FRAME_LABEL).has(IslFrame.STATUS_PROPERTY, "active").inV().hasLabel(SwitchFrame.FRAME_LABEL).simplePath()).until(__.has(SwitchFrame.SWITCH_ID_PROPERTY, dst.toString()).or().loops().is(DEFAULT_DEPTH)).has(SwitchFrame.SWITCH_ID_PROPERTY, dst.toString()).path()).getRawTraversal();
List<PathInfoData> deserializedResults = new ArrayList<>();
while (rawTraversal.hasNext()) {
ImmutablePath tpPath = (ImmutablePath) rawTraversal.next();
List<PathNode> resultPath = new ArrayList<>();
int seqId = 0;
for (Object hop : tpPath) {
if (hop instanceof Edge) {
Edge edge = (Edge) hop;
Vertex srcVertex = edge.outVertex();
resultPath.add(new PathNode(new SwitchId((String) srcVertex.property(SwitchFrame.SWITCH_ID_PROPERTY).value()), (Integer) edge.property(IslFrame.SRC_PORT_PROPERTY).value(), seqId++, (Long) edge.property(IslFrame.LATENCY_PROPERTY).value()));
Vertex dstVertex = edge.inVertex();
resultPath.add(new PathNode(new SwitchId((String) dstVertex.property(SwitchFrame.SWITCH_ID_PROPERTY).value()), (Integer) edge.property(IslFrame.DST_PORT_PROPERTY).value(), seqId++, (Long) edge.property(IslFrame.LATENCY_PROPERTY).value()));
}
}
deserializedResults.add(new PathInfoData(0, resultPath));
}
return deserializedResults;
});
}
use of org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal in project sqlg by pietermartin.
the class TestGithub method test272.
@Test
public void test272() {
this.sqlgGraph.addVertex("category", "a", "name", "hello");
this.sqlgGraph.addVertex("category", "b", "name", "ignore");
this.sqlgGraph.addVertex("category", "a", "name", "world");
this.sqlgGraph.tx().commit();
GraphTraversal gt = this.sqlgGraph.traversal().V().group().by("category").unfold().where(__.select(Column.values).count(Scope.local).is(P.gt(1))).select(Column.values).unfold().<String>values("name");
printTraversalForm(gt);
List<String> values = gt.toList();
Assert.assertEquals(2, values.size());
Assert.assertTrue(values.contains("hello"));
Assert.assertTrue(values.contains("world"));
}
Aggregations