Java Code Examples for org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils#reduce()

The following examples show how to use org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils#reduce() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConnectedComponentsVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
private static void updateClusterLabel(Vertex vertex, Messenger<String> messenger, Memory memory) {
    String currentMax = vertex.value(CLUSTER_LABEL);
    String max = IteratorUtils.reduce(messenger.receiveMessages(), currentMax,
            (a, b) -> a.compareTo(b) > 0 ? a : b);
    if (max.compareTo(currentMax) > 0) {
        vertex.property(CLUSTER_LABEL, max);
        messenger.sendMessage(messageScopeIn, max);
        messenger.sendMessage(messageScopeOut, max);
        memory.add(VOTE_TO_HALT, false);
    }
}
 
Example 2
Source File: MinMapReduce.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
Number reduceValues(Iterator<Number> values) {
    if (usingLong()) {
        return IteratorUtils.reduce(values, Long.MAX_VALUE, (a, b) -> Math.min(a.longValue(), b.longValue()));
    } else {
        return IteratorUtils.reduce(values, Double.MAX_VALUE, (a, b) -> Math.min(a.doubleValue(), b.doubleValue()));
    }
}
 
Example 3
Source File: KCoreVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
private static void updateClusterLabel(Vertex vertex, Messenger<String> messenger, Memory memory) {
    String currentMax = vertex.value(K_CORE_LABEL);
    String max = IteratorUtils.reduce(messenger.receiveMessages(), currentMax,
            (a, b) -> a.compareTo(b) > 0 ? a : b);
    if (!max.equals(currentMax)) {
        LOGGER.trace("Cluster label of {} changed from {} to {}", vertex, currentMax, max);
        vertex.property(K_CORE_LABEL, max);
        sendMessage(messenger, max);
        memory.add(VOTE_TO_HALT, false);
    } else {
        LOGGER.trace("Cluster label of {} is still {}", vertex, currentMax);
    }
}
 
Example 4
Source File: KCoreVertexProgram.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
private static void relayClusterLabel(Messenger<String> messenger, Memory memory) {
    String firstMessage = messenger.receiveMessages().next();
    String max = IteratorUtils.reduce(messenger.receiveMessages(), firstMessage,
            (a, b) -> a.compareTo(b) > 0 ? a : b);
    sendMessage(messenger, max);
    memory.add(VOTE_TO_HALT, false);
}
 
Example 5
Source File: MeanMapReduce.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
Map<String, Double> reduceValues(Iterator<Map<String, Double>> values) {
    Map<String, Double> emptyTuple = new HashMap<>(2);
    emptyTuple.put(SUM, 0D);
    emptyTuple.put(COUNT, 0D);
    return IteratorUtils.reduce(values, emptyTuple,
            (a, b) -> {
                a.put(COUNT, a.get(COUNT) + b.get(COUNT));
                a.put(SUM, a.get(SUM) + b.get(SUM));
                return a;
            });
}
 
Example 6
Source File: MaxMapReduce.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
Number reduceValues(Iterator<Number> values) {
    if (usingLong()) {
        return IteratorUtils.reduce(values, Long.MIN_VALUE, (a, b) -> Math.max(a.longValue(), b.longValue()));
    } else {
        return IteratorUtils.reduce(values, Double.MIN_VALUE, (a, b) -> Math.max(a.doubleValue(), b.doubleValue()));
    }
}
 
Example 7
Source File: StdMapReduce.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
Map<String, Double> reduceValues(Iterator<Map<String, Double>> values) {
    Map<String, Double> emptyTuple = new HashMap<>(3);
    emptyTuple.put(SUM, 0D);
    emptyTuple.put(SQUARE_SUM, 0D);
    emptyTuple.put(COUNT, 0D);
    return IteratorUtils.reduce(values, emptyTuple,
            (a, b) -> {
                a.put(COUNT, a.get(COUNT) + b.get(COUNT));
                a.put(SUM, a.get(SUM) + b.get(SUM));
                a.put(SQUARE_SUM, a.get(SQUARE_SUM) + b.get(SQUARE_SUM));
                return a;
            });
}
 
Example 8
Source File: SumMapReduce.java    From grakn with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
Number reduceValues(Iterator<Number> values) {
    if (usingLong()) {
        return IteratorUtils.reduce(values, 0L, (a, b) -> a.longValue() + b.longValue());
    } else {
        return IteratorUtils.reduce(values, 0D, (a, b) -> a.doubleValue() + b.doubleValue());
    }
}
 
Example 9
Source File: PageRankVertexProgram.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(final Vertex vertex, Messenger<Double> messenger, final Memory memory) {
    if (memory.isInitialIteration()) {
        messenger.sendMessage(this.countMessageScope, 1.0d);
        memory.add(VERTEX_COUNT, 1.0d);
    } else {
        final double vertexCount = memory.<Double>get(VERTEX_COUNT);
        final double edgeCount;
        double pageRank;
        if (1 == memory.getIteration()) {
            edgeCount = IteratorUtils.reduce(messenger.receiveMessages(), 0.0d, (a, b) -> a + b);
            vertex.property(VertexProperty.Cardinality.single, EDGE_COUNT, edgeCount);
            pageRank = null == this.initialRankTraversal ?
                    0.0d :
                    TraversalUtil.apply(vertex, this.initialRankTraversal.get()).doubleValue();
        } else {
            edgeCount = vertex.value(EDGE_COUNT);
            pageRank = IteratorUtils.reduce(messenger.receiveMessages(), 0.0d, (a, b) -> a + b);
        }
        //////////////////////////
        final double teleporationEnergy = memory.get(TELEPORTATION_ENERGY);
        if (teleporationEnergy > 0.0d) {
            final double localTerminalEnergy = teleporationEnergy / vertexCount;
            pageRank = pageRank + localTerminalEnergy;
            memory.add(TELEPORTATION_ENERGY, -localTerminalEnergy);
        }
        final double previousPageRank = vertex.<Double>property(this.property).orElse(0.0d);
        memory.add(CONVERGENCE_ERROR, Math.abs(pageRank - previousPageRank));
        vertex.property(VertexProperty.Cardinality.single, this.property, pageRank);
        memory.add(TELEPORTATION_ENERGY, (1.0d - this.alpha) * pageRank);
        pageRank = this.alpha * pageRank;
        if (edgeCount > 0.0d)
            messenger.sendMessage(this.incidentMessageScope, pageRank / edgeCount);
        else
            memory.add(TELEPORTATION_ENERGY, pageRank);
    }
}
 
Example 10
Source File: GraphComputerTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(Vertex vertex, Messenger<Long> messenger, Memory memory) {
    switch (memory.getIteration()) {
        case 0:
            if (vertex.value("name").equals("josh")) {
                messenger.sendMessage(this.countMessageScopeIn, 2L);
                messenger.sendMessage(this.countMessageScopeOut, 1L);
            }
            break;
        case 1:
            long edgeCount = IteratorUtils.reduce(messenger.receiveMessages(), 0L, (a, b) -> a + b);
            vertex.property(MEMORY_KEY, edgeCount);
            break;
    }
}
 
Example 11
Source File: GraphComputerTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(Vertex vertex, Messenger<Long> messenger, Memory memory) {
    switch (memory.getIteration()) {
        case 0:
            if (vertex.value("name").equals("josh")) {
                messenger.sendMessage(this.countMessageScopeIn, 2L);
                messenger.sendMessage(this.countMessageScopeOut, 1L);
            }
            break;
        case 1:
            long edgeCount = IteratorUtils.reduce(messenger.receiveMessages(), 0L, (a, b) -> a + b);
            vertex.property(MEMORY_KEY, edgeCount);
            break;
    }
}
 
Example 12
Source File: GraknVertexProgram.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
static long getMessageCount(Messenger<Long> messenger) {
    return IteratorUtils.reduce(messenger.receiveMessages(), 0L, (a, b) -> a + b);
}
 
Example 13
Source File: CountMapReduce.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
Long reduceValues(Iterator<Long> values) {
    return IteratorUtils.reduce(values, 0L, (a, b) -> a + b);
}
 
Example 14
Source File: ClusterSizeMapReduce.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
Long reduceValues(Iterator<Long> values) {
    return IteratorUtils.reduce(values, 0L, (a, b) -> a + b);
}