/** Read the edges from the temp file and load them to the graph. */ private void readFromTempEdges(final Input input, final Graph graphToWriteTo) { while (!input.eof()) { // in this case the outId is the id assigned by the graph Object next = kryo.readClassAndObject(input); while (!next.equals(EdgeTerminator.INSTANCE)) { final List<Object> edgeArgs = new ArrayList<>(); final DetachedEdge detachedEdge = (DetachedEdge) next; final Vertex vOut = graphToWriteTo.v(detachedEdge.iterators().vertexIterator(Direction.OUT).next().id()); final Vertex inV = graphToWriteTo.v(detachedEdge.iterators().vertexIterator(Direction.IN).next().id()); detachedEdge .iterators() .propertyIterator() .forEachRemaining(p -> edgeArgs.addAll(Arrays.asList(p.key(), p.value()))); // detachedEdge.iterators().hiddenPropertyIterator().forEachRemaining(p -> // edgeArgs.addAll(Arrays.asList(Graph.Key.hide(p.key()), p.value()))); appendToArgList(edgeArgs, T.id, detachedEdge.id()); vOut.addEdge(detachedEdge.label(), inV, edgeArgs.toArray()); next = kryo.readClassAndObject(input); } // vertex terminator kryo.readClassAndObject(input); } }
@Test public void shouldAppendPartitionToVertex() { final Vertex v = g.addVertex("any", "thing"); assertNotNull(v); assertEquals("thing", v.property("any").value()); assertEquals("A", v.property(partition).value()); }
@Before public void setup() { final Vertex v = mock(Vertex.class); when(v.id()).thenReturn("1"); when(v.label()).thenReturn("l"); this.mv = DetachedVertex.detach(v); }
private void removeEdge(Vertex v) { Set<Edge> edges = UMLG.get().getEdgesBetween(this.vertex, v, this.getLabel()); for (Edge edge : edges) { Vertex associationClassVertex = UMLG.get().v(edge.value(UmlgCollection.ASSOCIATION_CLASS_VERTEX_ID)); // The remove code will delete all in and out edges associationClassVertex.remove(); } }
@Test public void shouldNotEvaluateToEqualDifferentId() { final Vertex v = mock(Vertex.class); when(v.id()).thenReturn("2"); when(v.label()).thenReturn("l"); final DetachedVertex mv1 = DetachedVertex.detach(v); assertFalse(mv1.equals(this.mv)); }
private Vertex getDeletionVertex(Graph g) { Vertex root = g.v(0L); if (root != null && root.outE(UmlgGraph.DELETED_VERTEX_EDGE).hasNext()) { return root.outE(UmlgGraph.DELETED_VERTEX_EDGE).next().inV().next(); } else { throw new IllegalStateException( "The root node or deletion vertex is not present. It must be created at graph initialization!"); } }
@Override public void serialize( final GraphSONVertex directionalVertex, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) throws IOException, JsonGenerationException { final Vertex vertex = directionalVertex.getVertexToSerialize(); jsonGenerator.writeStartObject(); jsonGenerator.writeObjectField(GraphSONTokens.ID, vertex.getId()); jsonGenerator.writeStringField(GraphSONTokens.LABEL, vertex.getLabel()); jsonGenerator.writeStringField(GraphSONTokens.TYPE, GraphSONTokens.VERTEX); if (normalize) { jsonGenerator.writeObjectFieldStart(GraphSONTokens.PROPERTIES); vertex .getProperties() .values() .stream() .sorted(Comparators.PROPERTY_COMPARATOR) .forEachOrdered( FunctionUtils.wrapConsumer( e -> jsonGenerator.writeObjectField(e.getKey(), e.get()))); jsonGenerator.writeEndObject(); } else { jsonGenerator.writeObjectFieldStart(GraphSONTokens.PROPERTIES); vertex .getProperties() .values() .forEach( FunctionUtils.wrapConsumer( e -> jsonGenerator.writeObjectField(e.getKey(), e.get()))); jsonGenerator.writeEndObject(); } if (directionalVertex.getDirection() == Direction.BOTH || directionalVertex.getDirection() == Direction.OUT) { jsonGenerator.writeArrayFieldStart(GraphSONTokens.OUT); if (normalize) vertex .outE() .order(Comparators.HELD_EDGE_COMPARATOR) .forEach(FunctionUtils.wrapConsumer(jsonGenerator::writeObject)); else vertex.outE().forEach(FunctionUtils.wrapConsumer(jsonGenerator::writeObject)); jsonGenerator.writeEndArray(); } if (directionalVertex.getDirection() == Direction.BOTH || directionalVertex.getDirection() == Direction.IN) { jsonGenerator.writeArrayFieldStart(GraphSONTokens.IN); if (normalize) vertex .inE() .order(Comparators.HELD_EDGE_COMPARATOR) .forEach(FunctionUtils.wrapConsumer(jsonGenerator::writeObject)); else vertex.inE().forEach(FunctionUtils.wrapConsumer(jsonGenerator::writeObject)); jsonGenerator.writeEndArray(); } jsonGenerator.writeEndObject(); }
@Test public void shouldWrapProperties() { final Vertex v = g.addVertex("any", "a"); final Edge e = v.addEdge("to", v, "all", "a"); assertTrue(v.property("any") instanceof StrategyWrappedProperty); assertTrue(v.properties().get("any") instanceof StrategyWrappedProperty); assertTrue(e.property("all") instanceof StrategyWrappedProperty); assertTrue(e.properties().get("all") instanceof StrategyWrappedProperty); assertTrue(g.V().property("any").next() instanceof StrategyWrappedProperty); assertTrue(g.E().property("any").next() instanceof StrategyWrappedProperty); }
@Test public void shouldThrowExceptionOnvInDifferentPartition() { final Vertex vA = g.addVertex("any", "a"); assertEquals(vA.id(), g.v(vA.id()).id()); final PartitionGraphStrategy strategy = (PartitionGraphStrategy) ((StrategyWrappedGraph) g).strategy().getGraphStrategy().get(); strategy.clearReadPartitions(); try { g.v(vA.id()); } catch (Exception ex) { final Exception expected = Graph.Exceptions.elementNotFound(Vertex.class, vA.id()); assertEquals(expected.getClass(), ex.getClass()); assertEquals(expected.getMessage(), ex.getMessage()); } }
@Override public UnaryOperator<Supplier<Void>> getRemoveElementStrategy( final Strategy.Context<? extends StrategyWrappedElement> ctx) { if (ctx.getCurrent() instanceof StrategyWrappedVertex) { return (t) -> () -> { Vertex v = ((StrategyWrappedVertex) ctx.getCurrent()).getBaseVertex(); v.bothE().forEach(e -> e.remove()); getDeletionVertex(ctx.getBaseGraph()).addEdge(UmlgGraph.DELETION_VERTEX, v); v.properties().values().forEach(p -> p.remove()); v.property("_deleted", true); return null; }; } else { return UnaryOperator.identity(); } }
public void makeAGraph() { Graph graph = EmptyGraph.instance(); Vertex marko = graph.addVertex(T.label, "person", T.id, 1, "name", "marko", "age", 29); Vertex vadas = graph.addVertex(T.label, "person", T.id, 2, "name", "vadas", "age", 27); Vertex lop = graph.addVertex(T.label, "software", T.id, 3, "name", "lop", "lang", "java"); Vertex josh = graph.addVertex(T.label, "person", T.id, 4, "name", "josh", "age", 32); Vertex ripple = graph.addVertex(T.label, "software", T.id, 5, "name", "ripple", "lang", "java"); Vertex peter = graph.addVertex(T.label, "person", T.id, 6, "name", "peter", "age", 35); marko.addEdge("knows", vadas, T.id, 7, "weight", 0.5f); marko.addEdge("knows", josh, T.id, 8, "weight", 1.0f); marko.addEdge("created", lop, T.id, 9, "weight", 0.4f); josh.addEdge("created", ripple, T.id, 10, "weight", 1.0f); josh.addEdge("created", lop, T.id, 11, "weight", 0.4f); peter.addEdge("created", lop, T.id, 12, "weight", 0.2f); }
@Override public boolean remove(Object o) { maybeLoad(); Vertex v; if (o instanceof UmlgNode) { UmlgNode node = (UmlgNode) o; v = node.getVertex(); removeEdge(v); } else if (o.getClass().isEnum()) { v = removeFromInternalMap(o); removeEdge(v); v.remove(); } else if (isOnePrimitive() || getDataTypeEnum() != null) { throw new IllegalStateException( "one primitive or data type can not have an association class."); } else { v = removeFromInternalMap(o); removeEdge(v); v.remove(); } return super.remove(o); }
private static void createVertexProperty( final Graph graphToWriteTo, final Vertex v, final VertexProperty<Object> p, final boolean hidden) { final List<Object> propertyArgs = new ArrayList<>(); if (graphToWriteTo.features().vertex().properties().supportsUserSuppliedIds()) appendToArgList(propertyArgs, T.id, p.id()); p.iterators() .propertyIterator() .forEachRemaining(it -> appendToArgList(propertyArgs, it.key(), it.value())); // p.iterators().hiddenPropertyIterator().forEachRemaining(it -> appendToArgList(propertyArgs, // Graph.Key.hide(it.key()), it.value())); v.property(hidden ? Graph.Key.hide(p.key()) : p.key(), p.value(), propertyArgs.toArray()); }
@Test public void shouldAppendPartitionToEdge() { final Vertex v1 = g.addVertex("any", "thing"); final Vertex v2 = g.addVertex("some", "thing"); final Edge e = v1.addEdge("connectsTo", v2, "every", "thing"); assertNotNull(v1); assertEquals("thing", v1.property("any").value()); assertEquals("A", v2.property(partition).value()); assertNotNull(v2); assertEquals("thing", v2.property("some").value()); assertEquals("A", v2.property(partition).value()); assertNotNull(e); assertEquals("thing", e.property("every").value()); assertEquals("connectsTo", e.label()); assertEquals("A", e.property(partition).value()); }
@Test public void shouldWriteVerticesToMultiplePartitions() { final Vertex vA = g.addVertex("any", "a"); final PartitionGraphStrategy strategy = (PartitionGraphStrategy) ((StrategyWrappedGraph) g).strategy().getGraphStrategy().get(); strategy.setWritePartition("B"); final Vertex vB = g.addVertex("any", "b"); assertNotNull(vA); assertEquals("a", vA.property("any").value()); assertEquals("A", vA.property(partition).value()); assertNotNull(vB); assertEquals("b", vB.property("any").value()); assertEquals("B", vB.property(partition).value()); /* not applicable to SubgraphStrategy final GraphTraversal t = g.V(); assertTrue(t.strategies().get().stream().anyMatch(o -> o.getClass().equals(PartitionGraphStrategy.PartitionGraphTraversalStrategy.class))); */ g.V() .forEach( v -> { assertTrue(v instanceof StrategyWrappedVertex); assertEquals("a", v.property("any").value()); }); strategy.removeReadPartition("A"); strategy.addReadPartition("B"); g.V() .forEach( v -> { assertTrue(v instanceof StrategyWrappedVertex); assertEquals("b", v.property("any").value()); }); strategy.addReadPartition("A"); assertEquals(new Long(2), g.V().count().next()); }
@Test public void shouldWriteToMultiplePartitions() { final Vertex vA = g.addVertex("any", "a"); final Vertex vAA = g.addVertex("any", "aa"); final Edge eAtoAA = vA.addEdge("a->a", vAA); final PartitionGraphStrategy strategy = (PartitionGraphStrategy) ((StrategyWrappedGraph) g).strategy().getGraphStrategy().get(); strategy.setWritePartition("B"); final Vertex vB = g.addVertex("any", "b"); vA.addEdge("a->b", vB); strategy.setWritePartition("C"); final Vertex vC = g.addVertex("any", "c"); final Edge eBtovC = vB.addEdge("b->c", vC); final Edge eAtovC = vA.addEdge("a->c", vC); /* not applicable to SubgraphStrategy final GraphTraversal t = g.V(); assertTrue(t.strategies().get().stream().anyMatch(o -> o.getClass().equals(PartitionGraphStrategy.PartitionGraphTraversalStrategy.class))); */ strategy.clearReadPartitions(); assertEquals(new Long(0), g.V().count().next()); assertEquals(new Long(0), g.E().count().next()); strategy.addReadPartition("A"); assertEquals(new Long(2), g.V().count().next()); assertEquals(new Long(1), g.E().count().next()); assertEquals(new Long(1), g.v(vA.id()).outE().count().next()); assertEquals(eAtoAA.id(), g.v(vA.id()).outE().next().id()); assertEquals(new Long(1), g.v(vA.id()).out().count().next()); assertEquals(vAA.id(), g.v(vA.id()).out().next().id()); strategy.addReadPartition("B"); assertEquals(new Long(3), g.V().count().next()); assertEquals(new Long(2), g.E().count().next()); strategy.addReadPartition("C"); assertEquals(new Long(4), g.V().count().next()); assertEquals(new Long(4), g.E().count().next()); strategy.removeReadPartition("A"); strategy.removeReadPartition("B"); assertEquals(new Long(1), g.V().count().next()); // two edges are in the "C" partition, but one each of their incident vertices are not assertEquals(new Long(0), g.E().count().next()); assertEquals(new Long(0), g.v(vC.id()).inE().count().next()); assertEquals(new Long(0), g.v(vC.id()).in().count().next()); strategy.addReadPartition("B"); // only one edge in, due to excluded vertices; vA is not in {B,C} assertEquals(new Long(1), g.v(vC.id()).inE().count().next()); assertEquals(new Long(1), g.v(vC.id()).in().count().next()); assertEquals(vC.id(), g.e(eBtovC.id()).inV().id().next()); assertEquals(vB.id(), g.e(eBtovC.id()).outV().id().next()); assertEquals(vC.id(), g.e(eAtovC.id()).inV().id().next()); assertFalse(g.e(eAtovC.id()).outV().hasNext()); /* not applicable to SubgraphStrategy strategy.addReadPartition("A"); g.v(vA.id()).out().out().forEach(v -> { assertTrue(v instanceof StrategyWrapped); assertFalse(((StrategyWrappedElement) v).getBaseElement() instanceof StrategyWrapped); }); g.v(vA.id()).outE().inV().outE().forEach(e -> { assertTrue(e instanceof StrategyWrapped); assertFalse(((StrategyWrappedElement) e).getBaseElement() instanceof StrategyWrapped); }); */ }