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); }); }
@Override public void remove() { this.baseProperty.remove(); }
@Override public <U> Property<U> property(String key, U value) { return Property.<U>empty(); }
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 -> { while (vertexIterator.hasNext()) { final Vertex v = vertexIterator.next(); if (idAccessor.apply(v).toString().equals(originalVertex.id().toString())) { tmpVertex = v; break; }); 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()); }); });
@Override public void edgePropertyRemoved(final Edge element, final Property property) { assertThat(element, instanceOf(DetachedEdge.class)); assertEquals(label, element.label()); assertEquals(inId, element.inVertex().id()); assertEquals(outId, element.outVertex().id()); assertEquals("dah", property.value()); assertEquals("to-drop", property.key()); triggered.set(true); } };
public static void dumpGraph(Graph graph) { graph.traversal().V().forEachRemaining(vertex -> { System.out.println(String.format("%s=%s", vertex.id(), vertex.label())); vertex.properties().forEachRemaining(prop -> { System.out.println(String.format("\t%s=%s", prop.key(), prop.value())); }); }); graph.traversal().E().forEachRemaining(edge -> { System.out.println(String.format("%s- %s >%s", edge.inVertex().id(), edge.label(), edge.outVertex().id())); edge.properties().forEachRemaining(prop -> { System.out.println(String.format("\t%s=%s", prop.key(), prop.value())); }); }); }
private Vertex getOrCreate(final Vertex vertex) { final Iterator<Vertex> vertexIterator = subgraph.vertices(vertex.id()); if (vertexIterator.hasNext()) return vertexIterator.next(); final Vertex subgraphVertex = subgraph.addVertex(T.id, vertex.id(), T.label, vertex.label()); vertex.properties().forEachRemaining(vertexProperty -> { final VertexProperty.Cardinality cardinality = parentGraphFeatures.getCardinality(vertexProperty.key()); final VertexProperty<?> subgraphVertexProperty = subgraphVertex.property(cardinality, vertexProperty.key(), vertexProperty.value(), T.id, vertexProperty.id()); // only iterate the VertexProperties if the current graph can have them and if the subgraph can support // them. unfortunately we don't have a way to write a test for this as we dont' have a graph that supports // user supplied ids and doesn't support metaproperties. if (parentGraphFeatures.supportsMetaProperties() && subgraphSupportsMetaProperties) { vertexProperty.properties().forEachRemaining(property -> subgraphVertexProperty.property(property.key(), property.value())); } }); return subgraphVertex; }
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; }
static void moveOutgoingEdges(Vertex vertex, Vertex duplicate, IndexHandler indexHandler) { for (Iterator<Edge> edges = vertex.edges(Direction.OUT); edges.hasNext(); ) { Edge edge = edges.next(); if (edge.label().equals(VERSION_OF)) { continue; } Edge duplicateEdge = duplicate.addEdge(edge.label(), edge.inVertex()); for (Iterator<Property<Object>> properties = edge.properties(); properties.hasNext(); ) { Property<Object> property = properties.next(); duplicateEdge.property(property.key(), property.value()); } if (duplicateEdge.<Boolean>property("isLatest").orElse(false)) { duplicateEdge.<String>property("tim_id") .ifPresent(p -> indexHandler.upsertIntoEdgeIdIndex(UUID.fromString(p), duplicateEdge)); } edge.remove(); } }
@Test public void removingVertexMetaPropertiesWorks() { Graph g = this.getGraph(); Vertex me = g.addVertex("kind", "person", "name", "martin"); me.property("kind").property("job", "researcher"); g.tx().commit(); Vertex me2 = g.traversal().V().has("name", "martin").toSet().iterator().next(); assertNotNull(me2); assertTrue(me2.property("kind").isPresent()); assertTrue(me2.property("kind").property("job").isPresent()); assertEquals("person", me2.value("kind")); assertEquals("researcher", me2.property("kind").value("job")); me2.property("kind").property("job").remove(); g.tx().commit(); Vertex me3 = g.traversal().V().has("name", "martin").toSet().iterator().next(); assertNotNull(me3); assertTrue(me3.property("kind").isPresent()); assertFalse(me3.property("kind").property("job").isPresent()); }
/** * Creates a clone of the given property for the given vertex. * * @param property The property to be cloned. * @param vertex The vertex in the given graph.. * @param graph The graph that holds the given vertex. * @param g A standard traversal source for the given graph. * @return The cloned property. */ public default VertexProperty createVertexProperty(final VertexProperty<?> property, final Vertex vertex, final Graph graph, final GraphTraversalSource g) { final VertexProperty result = vertex.property(property.key(), property.value()); property.properties().forEachRemaining(metaProperty -> result.property(metaProperty.key(), metaProperty.value())); return result; }
/** * Returns the total effort points in all of the {@link ClassificationModel}s associated with the provided {@link FileModel}. */ public int getMigrationEffortPoints(FileModel fileModel) { GraphTraversal<Vertex, Vertex> classificationPipeline = new GraphTraversalSource(getGraphContext().getGraph()).V(fileModel.getElement()); classificationPipeline.in(ClassificationModel.FILE_MODEL); classificationPipeline.has(EffortReportModel.EFFORT, P.gt(0)); classificationPipeline.has(WindupVertexFrame.TYPE_PROP, Text.textContains(ClassificationModel.TYPE)); int classificationEffort = 0; for (Vertex v : classificationPipeline.toList()) { Property<Integer> migrationEffort = v.property(ClassificationModel.EFFORT); if (migrationEffort.isPresent()) { classificationEffort += migrationEffort.value(); } } return classificationEffort; }
@Test public void removingEdgePropertiesWorks() { Graph g = this.getGraph(); Vertex me = g.addVertex("kind", "person", "name", "martin"); Vertex john = g.addVertex("kind", "person", "name", "john"); me.addEdge("friend", john, "since", "forever"); g.tx().commit(); // find the edge Edge friendship = g.traversal().E().has("since", "forever").toSet().iterator().next(); assertNotNull(friendship); // remove the edge property and commit friendship.property("since").remove(); g.tx().commit(); // assert that the property is gone Vertex me2 = g.traversal().V().has("name", "martin").toSet().iterator().next(); Iterator<Edge> edges = me2.edges(Direction.BOTH, "friend"); assertTrue(edges.hasNext()); Edge friendship2 = Iterators.getOnlyElement(edges); assertNotNull(friendship2); assertFalse(friendship2.property("since").isPresent()); }
public static VertexProperty createVertexProperty(final Attachable<VertexProperty> attachableVertexProperty, final Graph hostGraph) { final VertexProperty<Object> baseVertexProperty = attachableVertexProperty.get(); final Iterator<Vertex> vertexIterator = hostGraph.vertices(baseVertexProperty.element().id()); if (vertexIterator.hasNext()) { final VertexProperty vertexProperty = hostGraph.features().vertex().properties().willAllowId(baseVertexProperty.id()) ? vertexIterator.next().property(hostGraph.features().vertex().getCardinality(baseVertexProperty.key()), baseVertexProperty.key(), baseVertexProperty.value(), T.id, baseVertexProperty.id()) : vertexIterator.next().property(hostGraph.features().vertex().getCardinality(baseVertexProperty.key()), baseVertexProperty.key(), baseVertexProperty.value()); baseVertexProperty.properties().forEachRemaining(p -> vertexProperty.property(p.key(), p.value())); return vertexProperty; } throw new IllegalStateException("Could not find vertex to create the attachable vertex property on"); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeProperty() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().version(GraphSONVersion.V3_0).create().createMapper(); final Property p = g.E(convertToEdgeId("marko", "knows", "vadas")).next().property("weight"); final String json = mapper.writeValueAsString(p); final Property detached = mapper.readValue(json, Property.class); assertNotNull(detached); assertEquals(p.key(), detached.key()); assertEquals(p.value(), detached.value()); }
public void shouldDetachCollections() { final Vertex vertex = DetachedFactory.detach(g.V().has("name", "marko").next(), true); assertTrue(vertex instanceof DetachedVertex); final List<Vertex> vertices = DetachedFactory.detach(g.V().hasLabel("software").fold().next(), true); for (final Vertex v : vertices) { assertTrue(v instanceof DetachedVertex); assertEquals("java", v.value("lang")); final List<List<Vertex>> lists = DetachedFactory.detach(g.V().hasLabel("software").fold().fold().next(), true); for (final Vertex v : lists.get(0)) { assertTrue(v instanceof DetachedVertex); assertEquals("java", v.value("lang")); for (final Vertex v : set.iterator().next()) { assertTrue(v instanceof DetachedVertex); assertEquals("java", v.value("lang")); for (final Edge edge : entry.getValue()) { assertTrue(edge instanceof DetachedEdge); assertTrue(edge.property("weight").isPresent()); for (final Edge edge : entry.getValue()) { assertTrue(edge instanceof DetachedEdge); assertTrue(edge.property("weight").isPresent());
@Test @LoadGraphWith(GraphData.MODERN) @FeatureRequirementSet(FeatureRequirementSet.Package.SIMPLE) @FeatureRequirement(featureClass = Graph.Features.EdgePropertyFeatures.class, feature = Graph.Features.EdgePropertyFeatures.FEATURE_DOUBLE_VALUES) public void shouldConstructDetachedEdge() { g.E(convertToEdgeId("marko", "knows", "vadas")).next().property("year", 2002); final DetachedEdge detachedEdge = DetachedFactory.detach(g.E(convertToEdgeId("marko", "knows", "vadas")).next(), true); assertEquals(convertToEdgeId("marko", "knows", "vadas"), detachedEdge.id()); assertEquals("knows", detachedEdge.label()); assertEquals(DetachedVertex.class, detachedEdge.vertices(Direction.OUT).next().getClass()); assertEquals(convertToVertexId("marko"), detachedEdge.vertices(Direction.OUT).next().id()); assertEquals("person", detachedEdge.vertices(Direction.IN).next().label()); assertEquals(DetachedVertex.class, detachedEdge.vertices(Direction.IN).next().getClass()); assertEquals(convertToVertexId("vadas"), detachedEdge.vertices(Direction.IN).next().id()); assertEquals("person", detachedEdge.vertices(Direction.IN).next().label()); assertEquals(2, IteratorUtils.count(detachedEdge.properties())); assertEquals(1, IteratorUtils.count(detachedEdge.properties("year"))); assertEquals(0.5d, detachedEdge.properties("weight").next().value()); }
private void writeEdges(final XMLStreamWriter writer, final Graph graph) throws XMLStreamException { if (normalize) { final List<Edge> edges = IteratorUtils.list(graph.edges()); Collections.sort(edges, Comparators.ELEMENT_COMPARATOR); writer.writeAttribute(GraphMLTokens.ID, edge.id().toString()); writer.writeAttribute(GraphMLTokens.SOURCE, edge.outVertex().id().toString()); writer.writeAttribute(GraphMLTokens.TARGET, edge.inVertex().id().toString()); writer.writeCharacters(edge.property(key).orElse("").toString()); writer.writeEndElement(); final Iterator<Edge> iterator = graph.edges(); while (iterator.hasNext()) { final Edge edge = iterator.next(); writer.writeStartElement(GraphMLTokens.EDGE); writer.writeAttribute(GraphMLTokens.ID, edge.id().toString()); writer.writeAttribute(GraphMLTokens.SOURCE, edge.outVertex().id().toString()); writer.writeAttribute(GraphMLTokens.TARGET, edge.inVertex().id().toString()); writer.writeCharacters(edge.property(key).orElse("").toString()); writer.writeEndElement();
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())); } }