@Test public void testGraphTraverseRandom6() throws Exception { GraphWalker<VocabWord> walker = new RandomWalker.Builder<>(graphDirected).setWalkLength(20) .setWalkDirection(WalkDirection.FORWARD_UNIQUE) .setNoEdgeHandling(NoEdgeHandling.CUTOFF_ON_DISCONNECTED).build(); Sequence<VocabWord> sequence = walker.next(); assertEquals("0", sequence.getElements().get(0).getLabel()); assertEquals("3", sequence.getElements().get(1).getLabel()); assertEquals("6", sequence.getElements().get(2).getLabel()); assertEquals("9", sequence.getElements().get(3).getLabel()); assertEquals(4, sequence.getElements().size()); }
@Override public Sequence<T> next() { int[] visitedHops = new int[walkLength]; Arrays.fill(visitedHops, -1); Sequence<T> sequence = new Sequence<>(); int startPosition = position.getAndIncrement(); int lastId = -1; int startPoint = order[startPosition]; startPosition = startPoint; for (int i = 0; i < walkLength; i++) { Vertex<T> vertex = sourceGraph.getVertex(startPosition); int currentPosition = startPosition; sequence.addElement(vertex.getValue()); visitedHops[i] = vertex.vertexID(); if (alpha > 0 && lastId != startPoint && lastId != -1 && alpha > rng.nextDouble()) { startPosition = startPoint; continue; } switch (walkDirection) { case RANDOM: { int[] nextHops = sourceGraph.getConnectedVertexIndices(currentPosition); startPosition = nextHops[rng.nextInt(nextHops.length)]; } break; case FORWARD_ONLY: { int[] nextHops = ArrayUtils.removeElements(sourceGraph.getConnectedVertexIndices(currentPosition), lastId); if (nextHops.length > 0) { startPosition = nextHops[rng.nextInt(nextHops.length)]; } else { switch (noEdgeHandling) { case CUTOFF_ON_DISCONNECTED: { i += walkLength; } break; case EXCEPTION_ON_DISCONNECTED: { throw new NoEdgesException("No more edges at vertex [" + currentPosition + "]"); } case SELF_LOOP_ON_DISCONNECTED: { startPosition = currentPosition; } break; case PADDING_ON_DISCONNECTED: { throw new UnsupportedOperationException("PADDING not implemented yet"); } case RESTART_ON_DISCONNECTED: { startPosition = startPoint; } break; default: throw new UnsupportedOperationException( "NoEdgeHandling mode [" + noEdgeHandling + "] not implemented yet."); } } } break; case FORWARD_UNIQUE: { int[] nextHops = ArrayUtils.removeElements(sourceGraph.getConnectedVertexIndices(currentPosition), visitedHops); if (nextHops.length > 0) { startPosition = nextHops[rng.nextInt(nextHops.length)]; } else { switch (noEdgeHandling) { case CUTOFF_ON_DISCONNECTED: { i += walkLength; } break; case EXCEPTION_ON_DISCONNECTED: { throw new NoEdgesException("No more edges at vertex [" + currentPosition + "]"); } case SELF_LOOP_ON_DISCONNECTED: { startPosition = currentPosition; } break; case PADDING_ON_DISCONNECTED: { throw new UnsupportedOperationException("PADDING not implemented yet"); } case RESTART_ON_DISCONNECTED: { startPosition = startPoint; } break; default: throw new UnsupportedOperationException( "NoEdgeHandling mode [" + noEdgeHandling + "] not implemented yet."); } } } break; case FORWARD_PREFERRED: { int[] nextHops = ArrayUtils.removeElements(sourceGraph.getConnectedVertexIndices(currentPosition), visitedHops); if (nextHops.length == 0) { nextHops = ArrayUtils.removeElements(sourceGraph.getConnectedVertexIndices(currentPosition), lastId); if (nextHops.length == 0) { switch (noEdgeHandling) { case CUTOFF_ON_DISCONNECTED: { i += walkLength; } break; case EXCEPTION_ON_DISCONNECTED: { throw new NoEdgesException("No more edges at vertex [" + currentPosition + "]"); } case SELF_LOOP_ON_DISCONNECTED: { startPosition = currentPosition; } break; case PADDING_ON_DISCONNECTED: { throw new UnsupportedOperationException("PADDING not implemented yet"); } case RESTART_ON_DISCONNECTED: { startPosition = startPoint; } break; default: throw new UnsupportedOperationException("NoEdgeHandling mode [" + noEdgeHandling + "] not implemented yet."); } } else startPosition = nextHops[rng.nextInt(nextHops.length)]; } } break; default: throw new UnsupportedOperationException("Unknown WalkDirection [" + walkDirection + "]"); } lastId = vertex.vertexID(); } return sequence; }
RandomWalker implements GraphWalker<T> { @Override public Sequence<T> next() { int[] visitedHops = new int[walkLength]; Arrays.fill(visitedHops, -1); Sequence<T> sequence = new Sequence<>(); int startPosition = position.getAndIncrement(); int lastId = -1; int startPoint = order[startPosition]; startPosition = startPoint; for (int i = 0; i < walkLength; i++) { Vertex<T> vertex = sourceGraph.getVertex(startPosition); int currentPosition = startPosition; sequence.addElement(vertex.getValue()); visitedHops[i] = vertex.vertexID(); if (alpha > 0 && lastId != startPoint && lastId != -1 && alpha > rng.nextDouble()) { startPosition = startPoint; continue; } switch (walkDirection) { case RANDOM: { int[] nextHops = sourceGraph.getConnectedVertexIndices(currentPosition); startPosition = nextHops[rng.nextInt(nextHops.length)]; } break; case FORWARD_ONLY: { int[] nextHops = ArrayUtils.removeElements(sourceGraph.getConnectedVertexIndices(currentPosition), lastId); if (nextHops.length > 0) { startPosition = nextHops[rng.nextInt(nextHops.length)]; } else { switch (noEdgeHandling) { case CUTOFF_ON_DISCONNECTED: { i += walkLength; } break; case EXCEPTION_ON_DISCONNECTED: { throw new NoEdgesException("No more edges at vertex [" + currentPosition + "]"); } case SELF_LOOP_ON_DISCONNECTED: { startPosition = currentPosition; } break; case PADDING_ON_DISCONNECTED: { throw new UnsupportedOperationException("PADDING not implemented yet"); } case RESTART_ON_DISCONNECTED: { startPosition = startPoint; } break; default: throw new UnsupportedOperationException( "NoEdgeHandling mode [" + noEdgeHandling + "] not implemented yet."); } } } break; case FORWARD_UNIQUE: { int[] nextHops = ArrayUtils.removeElements(sourceGraph.getConnectedVertexIndices(currentPosition), visitedHops); if (nextHops.length > 0) { startPosition = nextHops[rng.nextInt(nextHops.length)]; } else { switch (noEdgeHandling) { case CUTOFF_ON_DISCONNECTED: { i += walkLength; } break; case EXCEPTION_ON_DISCONNECTED: { throw new NoEdgesException("No more edges at vertex [" + currentPosition + "]"); } case SELF_LOOP_ON_DISCONNECTED: { startPosition = currentPosition; } break; case PADDING_ON_DISCONNECTED: { throw new UnsupportedOperationException("PADDING not implemented yet"); } case RESTART_ON_DISCONNECTED: { startPosition = startPoint; } break; default: throw new UnsupportedOperationException( "NoEdgeHandling mode [" + noEdgeHandling + "] not implemented yet."); } } } break; case FORWARD_PREFERRED: { int[] nextHops = ArrayUtils.removeElements(sourceGraph.getConnectedVertexIndices(currentPosition), visitedHops); if (nextHops.length == 0) { nextHops = ArrayUtils.removeElements(sourceGraph.getConnectedVertexIndices(currentPosition), lastId); if (nextHops.length == 0) { switch (noEdgeHandling) { case CUTOFF_ON_DISCONNECTED: { i += walkLength; } break; case EXCEPTION_ON_DISCONNECTED: { throw new NoEdgesException("No more edges at vertex [" + currentPosition + "]"); } case SELF_LOOP_ON_DISCONNECTED: { startPosition = currentPosition; } break; case PADDING_ON_DISCONNECTED: { throw new UnsupportedOperationException("PADDING not implemented yet"); } case RESTART_ON_DISCONNECTED: { startPosition = startPoint; } break; default: throw new UnsupportedOperationException("NoEdgeHandling mode [" + noEdgeHandling + "] not implemented yet."); } } else startPosition = nextHops[rng.nextInt(nextHops.length)]; } } break; default: throw new UnsupportedOperationException("Unknown WalkDirection [" + walkDirection + "]"); } lastId = vertex.vertexID(); } return sequence; } }
RandomWalker implements GraphWalker<T> { @Override public Sequence<T> next() { int[] visitedHops = new int[walkLength]; Arrays.fill(visitedHops, -1); Sequence<T> sequence = new Sequence<>(); int startPosition = position.getAndIncrement(); int lastId = -1; int startPoint = order[startPosition]; startPosition = startPoint; for (int i = 0; i < walkLength; i++) { Vertex<T> vertex = sourceGraph.getVertex(startPosition); int currentPosition = startPosition; sequence.addElement(vertex.getValue()); visitedHops[i] = vertex.vertexID(); if (alpha > 0 && lastId != startPoint && lastId != -1 && alpha > rng.nextDouble()) { startPosition = startPoint; continue; } switch (walkDirection) { case RANDOM: { int[] nextHops = sourceGraph.getConnectedVertexIndices(currentPosition); startPosition = nextHops[rng.nextInt(nextHops.length)]; } break; case FORWARD_ONLY: { int[] nextHops = ArrayUtils.removeElements(sourceGraph.getConnectedVertexIndices(currentPosition), lastId); if (nextHops.length > 0) { startPosition = nextHops[rng.nextInt(nextHops.length)]; } else { switch (noEdgeHandling) { case CUTOFF_ON_DISCONNECTED: { i += walkLength; } break; case EXCEPTION_ON_DISCONNECTED: { throw new NoEdgesException("No more edges at vertex [" + currentPosition + "]"); } case SELF_LOOP_ON_DISCONNECTED: { startPosition = currentPosition; } break; case PADDING_ON_DISCONNECTED: { throw new UnsupportedOperationException("PADDING not implemented yet"); } case RESTART_ON_DISCONNECTED: { startPosition = startPoint; } break; default: throw new UnsupportedOperationException( "NoEdgeHandling mode [" + noEdgeHandling + "] not implemented yet."); } } } break; case FORWARD_UNIQUE: { int[] nextHops = ArrayUtils.removeElements(sourceGraph.getConnectedVertexIndices(currentPosition), visitedHops); if (nextHops.length > 0) { startPosition = nextHops[rng.nextInt(nextHops.length)]; } else { switch (noEdgeHandling) { case CUTOFF_ON_DISCONNECTED: { i += walkLength; } break; case EXCEPTION_ON_DISCONNECTED: { throw new NoEdgesException("No more edges at vertex [" + currentPosition + "]"); } case SELF_LOOP_ON_DISCONNECTED: { startPosition = currentPosition; } break; case PADDING_ON_DISCONNECTED: { throw new UnsupportedOperationException("PADDING not implemented yet"); } case RESTART_ON_DISCONNECTED: { startPosition = startPoint; } break; default: throw new UnsupportedOperationException( "NoEdgeHandling mode [" + noEdgeHandling + "] not implemented yet."); } } } break; case FORWARD_PREFERRED: { int[] nextHops = ArrayUtils.removeElements(sourceGraph.getConnectedVertexIndices(currentPosition), visitedHops); if (nextHops.length == 0) { nextHops = ArrayUtils.removeElements(sourceGraph.getConnectedVertexIndices(currentPosition), lastId); if (nextHops.length == 0) { switch (noEdgeHandling) { case CUTOFF_ON_DISCONNECTED: { i += walkLength; } break; case EXCEPTION_ON_DISCONNECTED: { throw new NoEdgesException("No more edges at vertex [" + currentPosition + "]"); } case SELF_LOOP_ON_DISCONNECTED: { startPosition = currentPosition; } break; case PADDING_ON_DISCONNECTED: { throw new UnsupportedOperationException("PADDING not implemented yet"); } case RESTART_ON_DISCONNECTED: { startPosition = startPoint; } break; default: throw new UnsupportedOperationException("NoEdgeHandling mode [" + noEdgeHandling + "] not implemented yet."); } } else startPosition = nextHops[rng.nextInt(nextHops.length)]; } } break; default: throw new UnsupportedOperationException("Unknown WalkDirection [" + walkDirection + "]"); } lastId = vertex.vertexID(); } return sequence; } protected  RandomWalker(); }
RandomWalker implements GraphWalker<T> { @Override public Sequence<T> next() { int[] visitedHops = new int[walkLength]; Arrays.fill(visitedHops, -1); Sequence<T> sequence = new Sequence<>(); int startPosition = position.getAndIncrement(); int lastId = -1; int startPoint = order[startPosition]; startPosition = startPoint; for (int i = 0; i < walkLength; i++) { Vertex<T> vertex = sourceGraph.getVertex(startPosition); int currentPosition = startPosition; sequence.addElement(vertex.getValue()); visitedHops[i] = vertex.vertexID(); if (alpha > 0 && lastId != startPoint && lastId != -1 && alpha > rng.nextDouble()) { startPosition = startPoint; continue; } switch (walkDirection) { case RANDOM: { int[] nextHops = sourceGraph.getConnectedVertexIndices(currentPosition); startPosition = nextHops[rng.nextInt(nextHops.length)]; } break; case FORWARD_ONLY: { int[] nextHops = ArrayUtils.removeElements(sourceGraph.getConnectedVertexIndices(currentPosition), lastId); if (nextHops.length > 0) { startPosition = nextHops[rng.nextInt(nextHops.length)]; } else { switch (noEdgeHandling) { case CUTOFF_ON_DISCONNECTED: { i += walkLength; } break; case EXCEPTION_ON_DISCONNECTED: { throw new NoEdgesException("No more edges at vertex [" + currentPosition + "]"); } case SELF_LOOP_ON_DISCONNECTED: { startPosition = currentPosition; } break; case PADDING_ON_DISCONNECTED: { throw new UnsupportedOperationException("PADDING not implemented yet"); } case RESTART_ON_DISCONNECTED: { startPosition = startPoint; } break; default: throw new UnsupportedOperationException( "NoEdgeHandling mode [" + noEdgeHandling + "] not implemented yet."); } } } break; case FORWARD_UNIQUE: { int[] nextHops = ArrayUtils.removeElements(sourceGraph.getConnectedVertexIndices(currentPosition), visitedHops); if (nextHops.length > 0) { startPosition = nextHops[rng.nextInt(nextHops.length)]; } else { switch (noEdgeHandling) { case CUTOFF_ON_DISCONNECTED: { i += walkLength; } break; case EXCEPTION_ON_DISCONNECTED: { throw new NoEdgesException("No more edges at vertex [" + currentPosition + "]"); } case SELF_LOOP_ON_DISCONNECTED: { startPosition = currentPosition; } break; case PADDING_ON_DISCONNECTED: { throw new UnsupportedOperationException("PADDING not implemented yet"); } case RESTART_ON_DISCONNECTED: { startPosition = startPoint; } break; default: throw new UnsupportedOperationException( "NoEdgeHandling mode [" + noEdgeHandling + "] not implemented yet."); } } } break; case FORWARD_PREFERRED: { int[] nextHops = ArrayUtils.removeElements(sourceGraph.getConnectedVertexIndices(currentPosition), visitedHops); if (nextHops.length == 0) { nextHops = ArrayUtils.removeElements(sourceGraph.getConnectedVertexIndices(currentPosition), lastId); if (nextHops.length == 0) { switch (noEdgeHandling) { case CUTOFF_ON_DISCONNECTED: { i += walkLength; } break; case EXCEPTION_ON_DISCONNECTED: { throw new NoEdgesException("No more edges at vertex [" + currentPosition + "]"); } case SELF_LOOP_ON_DISCONNECTED: { startPosition = currentPosition; } break; case PADDING_ON_DISCONNECTED: { throw new UnsupportedOperationException("PADDING not implemented yet"); } case RESTART_ON_DISCONNECTED: { startPosition = startPoint; } break; default: throw new UnsupportedOperationException("NoEdgeHandling mode [" + noEdgeHandling + "] not implemented yet."); } } else startPosition = nextHops[rng.nextInt(nextHops.length)]; } } break; default: throw new UnsupportedOperationException("Unknown WalkDirection [" + walkDirection + "]"); } lastId = vertex.vertexID(); } return sequence; } protected  RandomWalker(); @Override boolean hasNext(); @Override boolean isLabelEnabled(); @Override Sequence<T> next(); @Override void reset(boolean shuffle); }
RandomWalker implements GraphWalker<T> { @Override public Sequence<T> next() { int[] visitedHops = new int[walkLength]; Arrays.fill(visitedHops, -1); Sequence<T> sequence = new Sequence<>(); int startPosition = position.getAndIncrement(); int lastId = -1; int startPoint = order[startPosition]; startPosition = startPoint; for (int i = 0; i < walkLength; i++) { Vertex<T> vertex = sourceGraph.getVertex(startPosition); int currentPosition = startPosition; sequence.addElement(vertex.getValue()); visitedHops[i] = vertex.vertexID(); if (alpha > 0 && lastId != startPoint && lastId != -1 && alpha > rng.nextDouble()) { startPosition = startPoint; continue; } switch (walkDirection) { case RANDOM: { int[] nextHops = sourceGraph.getConnectedVertexIndices(currentPosition); startPosition = nextHops[rng.nextInt(nextHops.length)]; } break; case FORWARD_ONLY: { int[] nextHops = ArrayUtils.removeElements(sourceGraph.getConnectedVertexIndices(currentPosition), lastId); if (nextHops.length > 0) { startPosition = nextHops[rng.nextInt(nextHops.length)]; } else { switch (noEdgeHandling) { case CUTOFF_ON_DISCONNECTED: { i += walkLength; } break; case EXCEPTION_ON_DISCONNECTED: { throw new NoEdgesException("No more edges at vertex [" + currentPosition + "]"); } case SELF_LOOP_ON_DISCONNECTED: { startPosition = currentPosition; } break; case PADDING_ON_DISCONNECTED: { throw new UnsupportedOperationException("PADDING not implemented yet"); } case RESTART_ON_DISCONNECTED: { startPosition = startPoint; } break; default: throw new UnsupportedOperationException( "NoEdgeHandling mode [" + noEdgeHandling + "] not implemented yet."); } } } break; case FORWARD_UNIQUE: { int[] nextHops = ArrayUtils.removeElements(sourceGraph.getConnectedVertexIndices(currentPosition), visitedHops); if (nextHops.length > 0) { startPosition = nextHops[rng.nextInt(nextHops.length)]; } else { switch (noEdgeHandling) { case CUTOFF_ON_DISCONNECTED: { i += walkLength; } break; case EXCEPTION_ON_DISCONNECTED: { throw new NoEdgesException("No more edges at vertex [" + currentPosition + "]"); } case SELF_LOOP_ON_DISCONNECTED: { startPosition = currentPosition; } break; case PADDING_ON_DISCONNECTED: { throw new UnsupportedOperationException("PADDING not implemented yet"); } case RESTART_ON_DISCONNECTED: { startPosition = startPoint; } break; default: throw new UnsupportedOperationException( "NoEdgeHandling mode [" + noEdgeHandling + "] not implemented yet."); } } } break; case FORWARD_PREFERRED: { int[] nextHops = ArrayUtils.removeElements(sourceGraph.getConnectedVertexIndices(currentPosition), visitedHops); if (nextHops.length == 0) { nextHops = ArrayUtils.removeElements(sourceGraph.getConnectedVertexIndices(currentPosition), lastId); if (nextHops.length == 0) { switch (noEdgeHandling) { case CUTOFF_ON_DISCONNECTED: { i += walkLength; } break; case EXCEPTION_ON_DISCONNECTED: { throw new NoEdgesException("No more edges at vertex [" + currentPosition + "]"); } case SELF_LOOP_ON_DISCONNECTED: { startPosition = currentPosition; } break; case PADDING_ON_DISCONNECTED: { throw new UnsupportedOperationException("PADDING not implemented yet"); } case RESTART_ON_DISCONNECTED: { startPosition = startPoint; } break; default: throw new UnsupportedOperationException("NoEdgeHandling mode [" + noEdgeHandling + "] not implemented yet."); } } else startPosition = nextHops[rng.nextInt(nextHops.length)]; } } break; default: throw new UnsupportedOperationException("Unknown WalkDirection [" + walkDirection + "]"); } lastId = vertex.vertexID(); } return sequence; } protected  RandomWalker(); @Override boolean hasNext(); @Override boolean isLabelEnabled(); @Override Sequence<T> next(); @Override void reset(boolean shuffle);  }