private void updateVertexWithProperties(String propertyKey, Object propertyValue, Map<Object, Object> map) { if (graph.traversal().V().has(propertyKey, propertyValue).hasNext()) { final Vertex v = graph.traversal().V().has(propertyKey, propertyValue).next(); map.forEach((key, value) -> v.property((String) key, value)); graph.tx().commit(); } }
/** * Get a list of all Configurations, excluding the template configuration; if none exist, * return an empty list * * @return List<Map<String, Object>> */ public List<Map<String, Object>> getConfigurations() { final List<Map<String, Object>> graphConfigurations = graph.traversal().V().has(PROPERTY_TEMPLATE, false).valueMap().toList(); return graphConfigurations.stream().map(this::deserializeVertexProperties).collect(Collectors.toList()); }
/** * Get template configuration if exists, else return null. * * @return Map<String, Object> */ public Map<String, Object> getTemplateConfiguration() { final List<Map<String, Object>> templateConfigurations = graph.traversal().V().has(PROPERTY_TEMPLATE, true).valueMap().toList(); if (templateConfigurations.size() == 0) return null; if (templateConfigurations.size() > 1) { log.warn("Your configuration management graph is an a bad state. Please " + "ensure you have just one template configuration. The behavior " + "of the class' APIs are henceforth unpredictable until this is fixed."); } templateConfigurations.get(0).remove(PROPERTY_TEMPLATE); return deserializeVertexProperties(templateConfigurations.get(0)); }
assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices())); assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices())); assertEquals(v1, getOnlyElement(graph.query().has(property, value2).vertices())); assertEquals(v1, getOnlyElement(graph.query().has(property, value1).vertices())); g.V().drop().iterate(); v1 = g.addV().property(property, value1).property(property, value2).next(); g.addV().property(property, value1).property(property, value2).next(); assertEquals(2, g.V().has(property, value1).toList().size()); g.V().properties().drop().iterate(); assertFalse(g.V().has(property, value1).hasNext()); assertFalse(g.V().has(property, value2).hasNext());
@Test @LoadGraphWith(MODERN) public void shouldSupportGraphFilter() throws Exception { fail("Should throw an unsupported operation exception"); } catch (final UnsupportedOperationException e) { assertEquals(GraphComputer.Exceptions.graphFilterNotSupported().getMessage(), e.getMessage()); graphProvider.getGraphComputer(graph).edges(__.bothE("knows")).program(new VertexProgramM(VertexProgramM.KNOWS_ONLY)).submit().get(); graphProvider.getGraphComputer(graph).vertices(__.hasLabel("person")).edges(__.bothE("knows")).program(new VertexProgramM(VertexProgramM.PEOPLE_KNOWS_ONLY)).submit().get(); graphProvider.getGraphComputer(graph).vertices(__.hasLabel("person")).edges(__.<Vertex>bothE("knows").has("weight", P.gt(0.5f))).program(new VertexProgramM(VertexProgramM.PEOPLE_KNOWS_WELL_ONLY)).submit().get(); graphProvider.getGraphComputer(graph).edges(__.<Vertex>bothE().limit(0)).program(new VertexProgramM(VertexProgramM.VERTICES_ONLY)).submit().get(); graphProvider.getGraphComputer(graph).edges(__.<Vertex>outE().limit(1)).program(new VertexProgramM(VertexProgramM.ONE_OUT_EDGE_ONLY)).submit().get(); graphProvider.getGraphComputer(graph).edges(__.bothE("knows")).program(new VertexProgramM(VertexProgramM.KNOWS_ONLY)).mapReduce(new MapReduceJ(VertexProgramM.KNOWS_ONLY)).submit().get(); graphProvider.getGraphComputer(graph).vertices(__.hasLabel("person")).edges(__.bothE("knows")).program(new VertexProgramM(VertexProgramM.PEOPLE_KNOWS_ONLY)).mapReduce(new MapReduceJ(VertexProgramM.PEOPLE_KNOWS_ONLY)).submit().get(); graphProvider.getGraphComputer(graph).vertices(__.hasLabel("person")).edges(__.<Vertex>bothE("knows").has("weight", P.gt(0.5f))).program(new VertexProgramM(VertexProgramM.PEOPLE_KNOWS_WELL_ONLY)).mapReduce(new MapReduceJ(VertexProgramM.PEOPLE_KNOWS_WELL_ONLY)).submit().get(); graphProvider.getGraphComputer(graph).edges(__.<Vertex>bothE().limit(0)).program(new VertexProgramM(VertexProgramM.VERTICES_ONLY)).mapReduce(new MapReduceJ(VertexProgramM.VERTICES_ONLY)).submit().get(); graphProvider.getGraphComputer(graph).edges(__.<Vertex>outE().limit(1)).program(new VertexProgramM(VertexProgramM.ONE_OUT_EDGE_ONLY)).mapReduce(new MapReduceJ(VertexProgramM.ONE_OUT_EDGE_ONLY)).submit().get(); graphProvider.getGraphComputer(graph).edges(__.bothE("knows")).mapReduce(new MapReduceJ(VertexProgramM.KNOWS_ONLY)).submit().get(); graphProvider.getGraphComputer(graph).vertices(__.hasLabel("person")).edges(__.bothE("knows")).mapReduce(new MapReduceJ(VertexProgramM.PEOPLE_KNOWS_ONLY)).submit().get(); graphProvider.getGraphComputer(graph).vertices(__.hasLabel("person")).edges(__.<Vertex>bothE("knows").has("weight", P.gt(0.5f))).mapReduce(new MapReduceJ(VertexProgramM.PEOPLE_KNOWS_WELL_ONLY)).submit().get(); graphProvider.getGraphComputer(graph).edges(__.<Vertex>bothE().limit(0)).mapReduce(new MapReduceJ(VertexProgramM.VERTICES_ONLY)).submit().get(); graphProvider.getGraphComputer(graph).edges(__.<Vertex>outE().limit(1)).mapReduce(new MapReduceJ(VertexProgramM.ONE_OUT_EDGE_ONLY)).submit().get();
assertNumStep(numV / 5, 1, gts.V(sv[0]).outE("knows").has("weight", 1), TitanVertexStep.class); assertNumStep(numV, 1, gts.V(sv[0]).outE("knows"), TitanVertexStep.class); assertNumStep(numV, 1, gts.V(sv[0]).out("knows"), TitanVertexStep.class); assertNumStep(numV / 5, 2, gts.V(sv[0]).outE("knows").has("weight", 1).order().by("weight", incr), TitanVertexStep.class, OrderGlobalStep.class); assertNumStep(10, 1, gts.V(sv[0]).local(__.outE("knows").has("weight", 1).order().by("weight", incr).limit(10)), TitanVertexStep.class); assertNumStep(5, 1, gts.V(sv[0]).local(__.outE("knows").has("weight", 1).has("weight", 1).order().by("weight", incr).range(10, 15)), LocalStep.class); assertNumStep(numV / 5, 1, gts.V(sv[0]).properties("names").has("weight", 1), TitanPropertiesStep.class); assertNumStep(1, 1, gts.V().has("id", numV / 5), TitanGraphStep.class); assertNumStep(1, 1, gts.V().has("id", numV / 5).has("weight", (numV / 5) % 5), TitanGraphStep.class); assertNumStep(numV / 5, 1, gts.V().has("weight", 1), TitanGraphStep.class); assertNumStep(10, 1, gts.V().has("weight", 1).range(0, 10), TitanGraphStep.class); assertNumStep(superV, 1, gts.V().has("id", sid), TitanGraphStep.class); assertNumStep(1, 1, gts.V().has("id", numV / 5).as("x"), TitanGraphStep.class); assertNumStep(1, 1, gts.V().has("id", numV / 5).has("weight", (numV / 5) % 5).as("x"), TitanGraphStep.class); assertNumStep(superV * (numV / 5), 2, gts.V().has("id", sid).outE("knows").has("weight", 1), TitanGraphStep.class, TitanVertexStep.class); assertNumStep(superV * (numV / 5 * 2), 2, gts.V().has("id", sid).outE("knows").has("weight", P.gte(1)).has("weight", P.lt(3)), TitanGraphStep.class, TitanVertexStep.class); assertNumStep(superV * (numV / 5 * 2), 2, gts.V().has("id", sid).outE("knows").has("weight", P.between(1, 3)), TitanGraphStep.class, TitanVertexStep.class); assertNumStep(superV * 10, 2, gts.V().has("id", sid).local(__.outE("knows").has("weight", P.gte(1)).has("weight", P.lt(3)).limit(10)), TitanGraphStep.class, TitanVertexStep.class); assertNumStep(superV * 10, 2, gts.V().has("id", sid).local(__.outE("knows").has("weight", P.between(1, 3)).order().by("weight", decr).limit(10)), TitanGraphStep.class, TitanVertexStep.class); assertNumStep(superV * (numV / 5), 2, gts.V().has("id", sid).outE("knows").has("weight", 1), TitanGraphStep.class, TitanVertexStep.class); assertNumStep(superV * (numV / 5 * 2), 2, gts.V().has("id", sid).outE("knows").has("weight", P.between(1, 3)), TitanGraphStep.class, TitanVertexStep.class); assertNumStep(superV * 10, 2, gts.V().has("id", sid).local(__.outE("knows").has("weight", P.gte(1)).has("weight", P.lt(3)).limit(10)), TitanGraphStep.class, TitanVertexStep.class);
private void assertGeoshape(Function<Geoshape,Geoshape> makeGeoshape) { graph.traversal().E().has("place").toList().forEach(e-> { assertTrue(e.property("shape").isPresent()); Geoshape place = (Geoshape) e.property("place").value(); Geoshape expected = makeGeoshape.apply(place); Geoshape actual = (Geoshape) e.property("shape").value(); assertEquals(expected, actual); }); }
private static void assertGraphEquality(final Graph source, final Graph target, final Function<Vertex, Object> idAccessor) { final GraphTraversalSource tg = target.traversal(); assertEquals(IteratorUtils.count(source.vertices()), IteratorUtils.count(target.vertices())); assertEquals(IteratorUtils.count(target.edges()), IteratorUtils.count(target.edges())); source.vertices().forEachRemaining(originalVertex -> { Vertex tmpVertex = null; assertEquals(IteratorUtils.count(originalVertex.edges(Direction.IN)), IteratorUtils.count(clonedVertex.edges(Direction.IN))); assertEquals(IteratorUtils.count(originalVertex.edges(Direction.OUT)), IteratorUtils.count(clonedVertex.edges(Direction.OUT))); assertEquals(originalVertex.label(), clonedVertex.label()); }); originalVertex.edges(Direction.OUT).forEachRemaining(originalEdge -> { GraphTraversal t = tg.V(clonedVertex).outE(originalEdge.label()); originalEdge.properties().forEachRemaining(p -> t.has(p.key(), p.value())); assertTrue(t.hasNext()); });
private void testOr(final Graph aGraph) { final GraphTraversalSource g = aGraph.traversal(); final Vertex hiro = g.addV().property("name", "Hiro").property("age", 2).property("length", 90).next(); final Vertex totoro = g.addV().property("name", "Totoro").property("age", 1).next(); final Vertex john = g.addV().property("name", "John").property("age", 3).property("length", 110).next(); final Vertex mike = g.addV().property("name", "Mike").property("age", 4).property("length", 130).next(); aGraph.tx().commit(); assertCount(1, g.V().has("name", "Totoro")); assertCount(1, g.V().has("age", 2)); assertCount(1, g.V().or(__.has("name", "Hiro"),__.has("age", 2))); assertCount(2, g.V().or(__.has("name", "Totoro"),__.has("age", 2))); assertCount(2, g.V().or(__.has("name", "Totoro").has("age", 1),__.has("age", 2))); assertCount(2, g.V().or(__.and(__.has("name", "Totoro"), __.has("age", 1)),__.has("age", 2))); assertTraversal(g.V().has("length", P.lte(100)).or(__.has("name", "Totoro"),__.has("age", P.gte(2))), hiro); assertTraversal(g.V().or(__.has("name", "Totoro"),__.has("age", P.gte(2))).has("length", P.lte(100)), hiro); assertTraversal(g.V().or(__.has("name", "Totoro"),__.has("age", 2)).order().by(ORDER_AGE_DECR), hiro, totoro); assertTraversal(g.V().or(__.has("name", "Totoro"), __.has("length", P.lte(130)).order().by(ORDER_LENGTH_INCR).range(1, 3)).limit(2), totoro, john); assertTraversal(g.V().has("length", P.gte(130).or(P.lt(100))).order().by(ORDER_AGE_INCR), hiro, mike); assertTraversal(g.V().has("length", P.gte(80).and(P.gte(130).or(P.lt(100)))).order().by(ORDER_AGE_INCR), hiro, mike); if (indexFeatures.supportNotQueryNormalForm()) { assertTraversal(g.V().has("length", P.gte(80).and(P.gte(130)).or(P.gte(80).and(P.lt(100)))).order().by(ORDER_AGE_INCR), hiro, mike);
/** * {@inheritDoc} */ @Override public Vertex getOrCreateVertex(final Vertex vertex, final Graph graph, final GraphTraversalSource g) { final Iterator<Vertex> iterator = useUserSuppliedIds() ? g.V().hasId(vertex.id()) : g.V().has(vertex.label(), bulkLoaderVertexId, vertex.id().toString()); return iterator.hasNext() ? iterator.next() : useUserSuppliedIds() ? g.addV(vertex.label()).property(T.id, vertex.id()).next() : g.addV(vertex.label()).property(bulkLoaderVertexId, vertex.id().toString()).next(); }
public static void assertNoEdgeGraph(final Graph g1, final boolean assertDouble, final boolean lossyForId) { assertEquals(2, IteratorUtils.count(g1.vertices())); assertEquals(1, IteratorUtils.count(g1.edges())); final Vertex v1 = g1.traversal().V().has("name", "marko").next(); assertEquals(29, v1.<Integer>value("age").intValue()); assertEquals(2, v1.keys().size()); assertEquals(Vertex.DEFAULT_LABEL, v1.label()); assertId(g1, lossyForId, v1, 1); final List<Edge> v1Edges = IteratorUtils.list(v1.edges(Direction.BOTH)); assertEquals(1, v1Edges.size()); v1Edges.forEach(e -> { if (e.inVertex().value("name").equals("vadas")) { assertEquals(Edge.DEFAULT_LABEL, e.label()); if (assertDouble) assertWeightLoosely(0.5d, e); else assertWeightLoosely(0.5f, e); assertEquals(1, e.keys().size()); assertId(g1, lossyForId, e, 7); } else { fail("Edge not expected"); } }); }
/** * Get Configuration according to supplied graphName mapped to a specific * {@link Graph}; if does not exist, return null. * * @return Map<String, Object> */ public Map<String, Object> getConfiguration(final String configName) { final List<Map<String, Object>> graphConfiguration = graph.traversal().V().has(PROPERTY_GRAPH_NAME, configName).valueMap().toList(); if (graphConfiguration.isEmpty()) return null; else if (graphConfiguration.size() > 1) { // this case shouldn't happen because our index has a unique constraint log.warn("Your configuration management graph is an a bad state. Please " + "ensure you have just one configuration per graph. The behavior " + "of the class' APIs are henceforth unpredictable until this is fixed."); } return deserializeVertexProperties(graphConfiguration.get(0)); }
assertNumStep(numV / 5, 1, gts.V(sv[0]).outE("knows").has("weight", 1), JanusGraphVertexStep.class); assertNumStep(numV, 1, gts.V(sv[0]).outE("knows"), JanusGraphVertexStep.class); assertNumStep(numV, 1, gts.V(sv[0]).out("knows"), JanusGraphVertexStep.class); assertNumStep(numV / 5, 2, gts.V(sv[0]).outE("knows").has("weight", 1).order().by("weight", incr), JanusGraphVertexStep.class, OrderGlobalStep.class); assertNumStep(10, 0, gts.V(sv[0]).local(__.outE("knows").has("weight", 1).order().by("weight", incr).limit(10)), LocalStep.class); assertNumStep(5, 1, gts.V(sv[0]).local(__.outE("knows").has("weight", 1).has("weight", 1).order().by("weight", incr).range(10, 15)), LocalStep.class); assertNumStep(numV / 5, 1, gts.V(sv[0]).properties("names").has("weight", 1), JanusGraphPropertiesStep.class); assertNumStep(1, 1, gts.V().has("id", numV / 5), JanusGraphStep.class); assertNumStep(1, 1, gts.V().has("id", numV / 5).has("weight", (numV / 5) % 5), JanusGraphStep.class); assertNumStep(numV / 5, 1, gts.V().has("weight", 1), JanusGraphStep.class); assertNumStep(10, 1, gts.V().has("weight", 1).range(0, 10), JanusGraphStep.class); assertNumStep(superV, 1, gts.V().has("id", sid), JanusGraphStep.class); assertNumStep(1, 1, gts.V().has("id", numV / 5).as("x"), JanusGraphStep.class); assertNumStep(1, 1, gts.V().has("id", numV / 5).has("weight", (numV / 5) % 5).as("x"), JanusGraphStep.class); assertNumStep(superV * (numV / 5), 2, gts.V().has("id", sid).outE("knows").has("weight", 1), JanusGraphStep.class, JanusGraphVertexStep.class); assertNumStep(superV * (numV / 5 * 2), 2, gts.V().has("id", sid).outE("knows").has("weight", P.gte(1)).has("weight", P.lt(3)), JanusGraphStep.class, JanusGraphVertexStep.class); assertNumStep(superV * (numV / 5 * 2), 2, gts.V().has("id", sid).outE("knows").has("weight", P.between(1, 3)), JanusGraphStep.class, JanusGraphVertexStep.class); assertNumStep(superV * 10, 2, gts.V().has("id", sid).local(__.outE("knows").has("weight", P.gte(1)).has("weight", P.lt(3)).limit(10)), JanusGraphStep.class, JanusGraphVertexStep.class); assertNumStep(superV * 10, 1, gts.V().has("id", sid).local(__.outE("knows").has("weight", P.between(1, 3)).order().by("weight", decr).limit(10)), JanusGraphStep.class); assertNumStep(superV * 10, 0, gts.V().has("id", sid).local(__.outE("knows").has("weight", P.between(1, 3)).order().by("weight", decr).limit(10)), LocalStep.class); clopen(option(USE_MULTIQUERY), true); gts = graph.traversal();
@Override public Traversal<Vertex, Map<Object, List<Object>>> get_g_V_hasLabelXsoftwareX_hasXname_rippleX_pageRankX1X_byXinEXcreatedXX_timesX1X_byXpriorsX_inXcreatedX_unionXboth__identityX_valueMapXname_priorsX() { return g.V().hasLabel("software").has("name", "ripple").pageRank(1.0).by(__.inE("created")).times(1).by("priors").in("created").union(__.both(), __.identity()).valueMap("name", "priors"); }
@Test public void testQueryByPrimaryValuesAndProps() { HugeGraph graph = graph(); init10Vertices(); // Query vertex by primary-values List<Vertex> vertexes = graph.traversal().V() .hasLabel("author").has("id", 1) .has("name", "James Gosling").toList(); Assert.assertEquals(1, vertexes.size()); assertContains(vertexes, T.label, "author", "id", 1, "name", "James Gosling", "age", 62, "lived", "Canadian"); vertexes = graph.traversal().V().hasLabel("author") .has("id", 1).has("name", "fake-name").toList(); Assert.assertEquals(0, vertexes.size()); }