private void writeProperties(final Edge edge, final JsonGenerator jsonGenerator) throws IOException { final Iterator<Property<Object>> elementProperties = normalize ? IteratorUtils.list(edge.properties(), Comparators.PROPERTY_COMPARATOR).iterator() : edge.properties(); if (elementProperties.hasNext()) { jsonGenerator.writeFieldName(GraphSONTokens.PROPERTIES); jsonGenerator.writeStartObject(); elementProperties.forEachRemaining(prop -> safeWriteObjectField(jsonGenerator, prop.key(), prop)); jsonGenerator.writeEndObject(); } } }
private void writeProperties(final Edge edge, final JsonGenerator jsonGenerator) throws IOException { final Iterator<Property<Object>> elementProperties = normalize ? IteratorUtils.list(edge.properties(), Comparators.PROPERTY_COMPARATOR).iterator() : edge.properties(); if (elementProperties.hasNext()) { jsonGenerator.writeFieldName(GraphSONTokens.PROPERTIES); jsonGenerator.writeStartObject(); elementProperties.forEachRemaining(prop -> safeWriteObjectField(jsonGenerator, prop.key(), prop)); jsonGenerator.writeEndObject(); } } }
private void writeProperties(final Edge edge, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider, final TypeSerializer typeSerializer) throws IOException { final Iterator<Property<Object>> elementProperties = normalize ? IteratorUtils.list(edge.properties(), Comparators.PROPERTY_COMPARATOR).iterator() : edge.properties(); if (elementProperties.hasNext()) { jsonGenerator.writeObjectFieldStart(GraphSONTokens.PROPERTIES); if (typeSerializer != null) jsonGenerator.writeStringField(GraphSONTokens.CLASS, HashMap.class.getName()); while (elementProperties.hasNext()) { final Property<Object> elementProperty = elementProperties.next(); GraphSONUtil.writeWithType(elementProperty.key(), elementProperty.value(), jsonGenerator, serializerProvider, typeSerializer); } jsonGenerator.writeEndObject(); } }
protected DetachedEdge(final Edge edge, final boolean withProperties) { super(edge); this.outVertex = DetachedFactory.detach(edge.outVertex(), false); this.inVertex = DetachedFactory.detach(edge.inVertex(), false); // only serialize properties if requested, the graph supports it and there are meta properties present. // this prevents unnecessary object creation of a new HashMap of a new HashMap which will just be empty. // it will use Collections.emptyMap() by default if (withProperties) { final Iterator<Property<Object>> propertyIterator = edge.properties(); if (propertyIterator.hasNext()) { this.properties = new HashMap<>(); propertyIterator.forEachRemaining(property -> this.properties.put(property.key(), Collections.singletonList(DetachedFactory.detach(property)))); } } }
@Override public <V> Iterator<Property<V>> properties(final String... propertyKeys) { return IteratorUtils.<Property<V>, Property<V>>map(this.getBaseEdge().properties(propertyKeys), property -> new HadoopProperty<>(property, HadoopEdge.this)); } }
@Override public void edgeAdded(final Edge element) { eventedEdge.set(element); assertEquals("self", element.label()); assertEquals(id, element.inVertex().id()); assertEquals(id, element.outVertex().id()); assertThat(element.properties().hasNext(), is(false)); triggered.set(true); } };
@Override public void edgeAdded(final Edge element) { assertThat(element, instanceOf(ReferenceEdge.class)); assertEquals("self", element.label()); assertEquals(id, element.inVertex().id()); assertEquals(id, element.outVertex().id()); assertThat(element.properties().hasNext(), is(false)); triggered.set(true); } };
/** * Creates a clone of the given edge between the given in- and out-vertices. * * @param edge The edge to be cloned. * @param outVertex The out-vertex in the given graph.. * @param inVertex The in-vertex in the given graph. * @param graph The graph that holds the cloned edge after this method was called. * @param g A standard traversal source for the given graph. * @return The cloned edge. */ public default Edge createEdge(final Edge edge, final Vertex outVertex, final Vertex inVertex, final Graph graph, final GraphTraversalSource g) { final Edge result = outVertex.addEdge(edge.label(), inVertex); edge.properties().forEachRemaining(property -> result.property(property.key(), property.value())); return result; }
private void addEdgeToSubgraph(final Edge edge) { final Iterator<Edge> edgeIterator = subgraph.edges(edge.id()); if (edgeIterator.hasNext()) return; final Iterator<Vertex> vertexIterator = edge.vertices(Direction.BOTH); final Vertex subGraphOutVertex = getOrCreate(vertexIterator.next()); final Vertex subGraphInVertex = getOrCreate(vertexIterator.next()); final Edge subGraphEdge = subGraphOutVertex.addEdge(edge.label(), subGraphInVertex, T.id, edge.id()); edge.properties().forEachRemaining(property -> subGraphEdge.property(property.key(), property.value())); } }
@Test @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) public void shouldReturnEmptyIteratorIfNoProperties() { final Vertex v = graph.addVertex(); final Edge e = v.addEdge("knows", v); assertEquals(0, IteratorUtils.count(e.properties())); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldAttachToGraph() { final Edge e = g.E(convertToEdgeId("josh", "created", "lop")).next(); final Property toDetach = e.properties("weight").next(); final DetachedProperty<?> detachedProperty = DetachedFactory.detach(toDetach); final Property attached = detachedProperty.attach(Attachable.Method.get(graph)); assertEquals(toDetach, attached); assertFalse(attached instanceof DetachedProperty); }
/** * {@inheritDoc} */ @Override public Edge getOrCreateEdge(final Edge edge, final Vertex outVertex, final Vertex inVertex, final Graph graph, final GraphTraversalSource g) { final Edge e; final Traversal<Vertex, Edge> t = g.V(outVertex).outE(edge.label()).filter(__.inV().is(inVertex)); if (t.hasNext()) { e = t.next(); edge.properties().forEachRemaining(property -> { final Property<?> existing = e.property(property.key()); if (!existing.isPresent() || !existing.value().equals(property.value())) { e.property(property.key(), property.value()); } }); } else { e = createEdge(edge, outVertex, inVertex, graph, g); } return e; }
/** * Creates a new {@link StarGraph} from a {@link Vertex}. */ public static StarGraph of(final Vertex vertex) { if (vertex instanceof StarVertex) return (StarGraph) vertex.graph(); // else convert to a star graph final StarGraph starGraph = new StarGraph(); final StarVertex starVertex = (StarVertex) starGraph.addVertex(T.id, vertex.id(), T.label, vertex.label()); final boolean supportsMetaProperties = vertex.graph().features().vertex().supportsMetaProperties(); vertex.properties().forEachRemaining(vp -> { final VertexProperty<?> starVertexProperty = starVertex.property(VertexProperty.Cardinality.list, vp.key(), vp.value(), T.id, vp.id()); if (supportsMetaProperties) vp.properties().forEachRemaining(p -> starVertexProperty.property(p.key(), p.value())); }); vertex.edges(Direction.IN).forEachRemaining(edge -> { final Edge starEdge = starVertex.addInEdge(edge.label(), starGraph.addVertex(T.id, edge.outVertex().id()), T.id, edge.id()); edge.properties().forEachRemaining(p -> starEdge.property(p.key(), p.value())); }); vertex.edges(Direction.OUT).forEachRemaining(edge -> { final Edge starEdge = starVertex.addOutEdge(edge.label(), starGraph.addVertex(T.id, edge.inVertex().id()), T.id, edge.id()); edge.properties().forEachRemaining(p -> starEdge.property(p.key(), p.value())); }); return starGraph; }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void testAttachableGetMethod() { // vertex host g.V().forEachRemaining(vertex -> TestHelper.validateEquality(vertex, DetachedFactory.detach(vertex, true).attach(Attachable.Method.get(vertex)))); g.V().forEachRemaining(vertex -> vertex.properties().forEachRemaining(vertexProperty -> TestHelper.validateEquality(vertexProperty, DetachedFactory.detach(vertexProperty, true).attach(Attachable.Method.get(vertex))))); g.V().forEachRemaining(vertex -> vertex.properties().forEachRemaining(vertexProperty -> vertexProperty.properties().forEachRemaining(property -> TestHelper.validateEquality(property, DetachedFactory.detach(property).attach(Attachable.Method.get(vertex)))))); g.V().forEachRemaining(vertex -> vertex.edges(Direction.OUT).forEachRemaining(edge -> TestHelper.validateEquality(edge, DetachedFactory.detach(edge, true).attach(Attachable.Method.get(vertex))))); g.V().forEachRemaining(vertex -> vertex.edges(Direction.OUT).forEachRemaining(edge -> edge.properties().forEachRemaining(property -> TestHelper.validateEquality(property, DetachedFactory.detach(property).attach(Attachable.Method.get(vertex)))))); // graph host g.V().forEachRemaining(vertex -> TestHelper.validateEquality(vertex, DetachedFactory.detach(vertex, true).attach(Attachable.Method.get(graph)))); g.V().forEachRemaining(vertex -> vertex.properties().forEachRemaining(vertexProperty -> TestHelper.validateEquality(vertexProperty, DetachedFactory.detach(vertexProperty, true).attach(Attachable.Method.get(graph))))); g.V().forEachRemaining(vertex -> vertex.properties().forEachRemaining(vertexProperty -> vertexProperty.properties().forEachRemaining(property -> TestHelper.validateEquality(property, DetachedFactory.detach(property).attach(Attachable.Method.get(graph)))))); g.V().forEachRemaining(vertex -> vertex.edges(Direction.OUT).forEachRemaining(edge -> TestHelper.validateEquality(edge, DetachedFactory.detach(edge, true).attach(Attachable.Method.get(graph))))); g.V().forEachRemaining(vertex -> vertex.edges(Direction.OUT).forEachRemaining(edge -> edge.properties().forEachRemaining(property -> TestHelper.validateEquality(property, DetachedFactory.detach(property).attach(Attachable.Method.get(graph)))))); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void testAttachableGetMethod() { // vertex host g.V().forEachRemaining(vertex -> TestHelper.validateEquality(vertex, ReferenceFactory.detach(vertex).attach(Attachable.Method.get(vertex)))); g.V().forEachRemaining(vertex -> vertex.properties().forEachRemaining(vertexProperty -> TestHelper.validateEquality(vertexProperty, ReferenceFactory.detach(vertexProperty).attach(Attachable.Method.get(vertex))))); g.V().forEachRemaining(vertex -> vertex.properties().forEachRemaining(vertexProperty -> vertexProperty.properties().forEachRemaining(property -> TestHelper.validateEquality(property, ReferenceFactory.detach(property).attach(Attachable.Method.get(vertex)))))); g.V().forEachRemaining(vertex -> vertex.edges(Direction.OUT).forEachRemaining(edge -> TestHelper.validateEquality(edge, ReferenceFactory.detach(edge).attach(Attachable.Method.get(vertex))))); g.V().forEachRemaining(vertex -> vertex.edges(Direction.OUT).forEachRemaining(edge -> edge.properties().forEachRemaining(property -> TestHelper.validateEquality(property, ReferenceFactory.detach(property).attach(Attachable.Method.get(vertex)))))); // graph host g.V().forEachRemaining(vertex -> TestHelper.validateEquality(vertex, ReferenceFactory.detach(vertex).attach(Attachable.Method.get(graph)))); g.V().forEachRemaining(vertex -> vertex.properties().forEachRemaining(vertexProperty -> TestHelper.validateEquality(vertexProperty, ReferenceFactory.detach(vertexProperty).attach(Attachable.Method.get(graph))))); g.V().forEachRemaining(vertex -> vertex.properties().forEachRemaining(vertexProperty -> vertexProperty.properties().forEachRemaining(property -> TestHelper.validateEquality(property, ReferenceFactory.detach(property).attach(Attachable.Method.get(graph)))))); g.V().forEachRemaining(vertex -> vertex.edges(Direction.OUT).forEachRemaining(edge -> TestHelper.validateEquality(edge, ReferenceFactory.detach(edge).attach(Attachable.Method.get(graph))))); g.V().forEachRemaining(vertex -> vertex.edges(Direction.OUT).forEachRemaining(edge -> edge.properties().forEachRemaining(property -> TestHelper.validateEquality(property, ReferenceFactory.detach(property).attach(Attachable.Method.get(graph)))))); } }
public static Edge createEdge(final Attachable<Edge> attachableEdge, final Graph hostGraph) { final Edge baseEdge = attachableEdge.get(); Iterator<Vertex> vertices = hostGraph.vertices(baseEdge.outVertex().id()); final Vertex outV = vertices.hasNext() ? vertices.next() : hostGraph.features().vertex().willAllowId(baseEdge.outVertex().id()) ? hostGraph.addVertex(T.id, baseEdge.outVertex().id()) : hostGraph.addVertex(); vertices = hostGraph.vertices(baseEdge.inVertex().id()); final Vertex inV = vertices.hasNext() ? vertices.next() : hostGraph.features().vertex().willAllowId(baseEdge.inVertex().id()) ? hostGraph.addVertex(T.id, baseEdge.inVertex().id()) : hostGraph.addVertex(); if (ElementHelper.areEqual(outV, inV)) { final Iterator<Edge> itty = outV.edges(Direction.OUT, baseEdge.label()); while (itty.hasNext()) { final Edge e = itty.next(); if (ElementHelper.areEqual(baseEdge, e)) return e; } } final Edge e = hostGraph.features().edge().willAllowId(baseEdge.id()) ? outV.addEdge(baseEdge.label(), inV, T.id, baseEdge.id()) : outV.addEdge(baseEdge.label(), inV); baseEdge.properties().forEachRemaining(p -> e.property(p.key(), p.value())); return e; }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldAttachWithGetMethod() { // vertex host g.V().forEachRemaining(vertex -> TestHelper.validateEquality(vertex, StarGraph.of(vertex).getStarVertex().attach(Attachable.Method.get(vertex)))); g.V().forEachRemaining(vertex -> StarGraph.of(vertex).getStarVertex().properties().forEachRemaining(vertexProperty -> TestHelper.validateEquality(vertexProperty, ((Attachable<VertexProperty>) vertexProperty).attach(Attachable.Method.get(vertex))))); g.V().forEachRemaining(vertex -> StarGraph.of(vertex).getStarVertex().properties().forEachRemaining(vertexProperty -> vertexProperty.properties().forEachRemaining(property -> TestHelper.validateEquality(property, ((Attachable<Property>) property).attach(Attachable.Method.get(vertex)))))); g.V().forEachRemaining(vertex -> StarGraph.of(vertex).getStarVertex().edges(Direction.OUT).forEachRemaining(edge -> TestHelper.validateEquality(edge, ((Attachable<Edge>) edge).attach(Attachable.Method.get(vertex))))); g.V().forEachRemaining(vertex -> StarGraph.of(vertex).getStarVertex().edges(Direction.OUT).forEachRemaining(edge -> edge.properties().forEachRemaining(property -> TestHelper.validateEquality(property, ((Attachable<Property>) property).attach(Attachable.Method.get(vertex)))))); // graph host g.V().forEachRemaining(vertex -> TestHelper.validateEquality(vertex, StarGraph.of(vertex).getStarVertex().attach(Attachable.Method.get(graph)))); g.V().forEachRemaining(vertex -> StarGraph.of(vertex).getStarVertex().properties().forEachRemaining(vertexProperty -> TestHelper.validateEquality(vertexProperty, ((Attachable<VertexProperty>) vertexProperty).attach(Attachable.Method.get(graph))))); g.V().forEachRemaining(vertex -> StarGraph.of(vertex).getStarVertex().properties().forEachRemaining(vertexProperty -> vertexProperty.properties().forEachRemaining(property -> TestHelper.validateEquality(property, ((Attachable<Property>) property).attach(Attachable.Method.get(graph)))))); g.V().forEachRemaining(vertex -> StarGraph.of(vertex).getStarVertex().edges(Direction.OUT).forEachRemaining(edge -> TestHelper.validateEquality(edge, ((Attachable<Edge>) edge).attach(Attachable.Method.get(graph))))); g.V().forEachRemaining(vertex -> StarGraph.of(vertex).getStarVertex().edges(Direction.OUT).forEachRemaining(edge -> edge.properties().forEachRemaining(property -> TestHelper.validateEquality(property, ((Attachable<Property>) property).attach(Attachable.Method.get(graph)))))); }
@Test @LoadGraphWith(MODERN) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) public void g_VXaX_addEXknowsX_toXbX_propertyXweight_0_1X() { final Vertex a = g.V().has("name", "marko").next(); final Vertex b = g.V().has("name", "peter").next(); final Traversal<Vertex, Edge> traversal = get_g_VXaX_addEXknowsX_toXbX_propertyXweight_0_1X(a, b); printTraversalForm(traversal); final Edge edge = traversal.next(); assertEquals(edge.outVertex(), convertToVertex(graph, "marko")); assertEquals(edge.inVertex(), convertToVertex(graph, "peter")); assertEquals("knows", edge.label()); assertEquals(1, IteratorUtils.count(edge.properties())); assertEquals(0.1d, edge.value("weight"), 0.1d); assertEquals(6L, g.V().count().next().longValue()); assertEquals(7L, g.E().count().next().longValue()); }
@Test @LoadGraphWith(MODERN) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) public void g_addEXknowsX_fromXaX_toXbX_propertyXweight_0_1X() { final Vertex a = g.V().has("name", "marko").next(); final Vertex b = g.V().has("name", "peter").next(); final Traversal<Edge, Edge> traversal = get_g_addEXknowsX_fromXaX_toXbX_propertyXweight_0_1X(a, b); printTraversalForm(traversal); final Edge edge = traversal.next(); assertEquals(edge.outVertex(), convertToVertex(graph, "marko")); assertEquals(edge.inVertex(), convertToVertex(graph, "peter")); assertEquals("knows", edge.label()); assertEquals(1, IteratorUtils.count(edge.properties())); assertEquals(0.1d, edge.value("weight"), 0.1d); assertEquals(6L, g.V().count().next().longValue()); assertEquals(7L, g.E().count().next().longValue()); }
@Test @LoadGraphWith(MODERN) @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) public void g_VX1X_asXaX_outXcreatedX_addEXcreatedByX_toXaX() { final Traversal<Vertex, Edge> traversal = get_g_VX1X_asXaX_outXcreatedX_addEXcreatedByX_toXaX(convertToVertexId("marko")); printTraversalForm(traversal); int count = 0; while (traversal.hasNext()) { final Edge edge = traversal.next(); assertEquals("createdBy", edge.label()); assertEquals(0, IteratorUtils.count(edge.properties())); count++; } assertEquals(1, count); assertEquals(7, IteratorUtils.count(g.E())); assertEquals(6, IteratorUtils.count(g.V())); }