Search in sources :

Example 21 with BinaryOperator

use of java.util.function.BinaryOperator in project waltz by khartec.

the class BatchProcessingCollector_combinerTest method canGetCombinerForNullConsumerAndZeroBatchSize.

@Test
public void canGetCombinerForNullConsumerAndZeroBatchSize() {
    BatchProcessingCollector batch = new BatchProcessingCollector(0, null);
    BinaryOperator bin = batch.combiner();
    assertNotNull(bin);
}
Also used : BinaryOperator(java.util.function.BinaryOperator) Test(org.junit.jupiter.api.Test)

Example 22 with BinaryOperator

use of java.util.function.BinaryOperator in project ignite by apache.

the class CacheUtils method sparseFold.

private static <K, V, A> A sparseFold(String cacheName, IgniteBiFunction<Cache.Entry<K, V>, A, A> folder, IgnitePredicate<K> keyFilter, BinaryOperator<A> accumulator, A zeroVal, V defVal, K defKey, long defValCnt, boolean isNilpotent) {
    A defRes = zeroVal;
    if (!isNilpotent)
        for (int i = 0; i < defValCnt; i++) defRes = folder.apply(new CacheEntryImpl<>(defKey, defVal), defRes);
    Collection<A> totalRes = bcast(cacheName, () -> {
        Ignite ignite = Ignition.localIgnite();
        IgniteCache<K, V> cache = ignite.getOrCreateCache(cacheName);
        int partsCnt = ignite.affinity(cacheName).partitions();
        // Use affinity in filter for ScanQuery. Otherwise we accept consumer in each node which is wrong.
        Affinity affinity = ignite.affinity(cacheName);
        ClusterNode localNode = ignite.cluster().localNode();
        A a = zeroVal;
        // Iterate over all partitions. Some of them will be stored on that local node.
        for (int part = 0; part < partsCnt; part++) {
            int p = part;
            // Query returns an empty cursor if this partition is not stored on this node.
            for (Cache.Entry<K, V> entry : cache.query(new ScanQuery<K, V>(part, (k, v) -> affinity.mapPartitionToNode(p) == localNode && (keyFilter == null || keyFilter.apply(k))))) a = folder.apply(entry, a);
        }
        return a;
    });
    totalRes.add(defRes);
    return totalRes.stream().reduce(zeroVal, accumulator);
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteConsumer(org.apache.ignite.ml.math.functions.IgniteConsumer) IgniteFunction(org.apache.ignite.ml.math.functions.IgniteFunction) Affinity(org.apache.ignite.cache.affinity.Affinity) SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) SparseDistributedMatrixStorage(org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage) IgniteCallable(org.apache.ignite.lang.IgniteCallable) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ValueMapper(org.apache.ignite.ml.math.ValueMapper) Map(java.util.Map) Cache(javax.cache.Cache) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) KeyMapper(org.apache.ignite.ml.math.KeyMapper) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) Collection(java.util.Collection) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) Ignite(org.apache.ignite.Ignite) BinaryOperator(java.util.function.BinaryOperator) IgniteCache(org.apache.ignite.IgniteCache) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Ignition(org.apache.ignite.Ignition) IgniteBiFunction(org.apache.ignite.ml.math.functions.IgniteBiFunction) Collections(java.util.Collections) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteUuid(org.apache.ignite.lang.IgniteUuid) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) Affinity(org.apache.ignite.cache.affinity.Affinity) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 23 with BinaryOperator

use of java.util.function.BinaryOperator in project Gaffer by gchq.

the class SchemaElementDefinitionValidatorTest method shouldValidateAndReturnTrueWhenAggregatorIsValid.

@Test
public void shouldValidateAndReturnTrueWhenAggregatorIsValid() {
    // Given
    final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator();
    final BinaryOperator<Integer> function1 = mock(BinaryOperator.class);
    final BinaryOperator function2 = mock(BinaryOperator.class);
    final Schema schema = new Schema.Builder().entity(TestGroups.ENTITY, new SchemaEntityDefinition.Builder().vertex("int1").property(TestPropertyNames.PROP_1, "int1").property(TestPropertyNames.PROP_2, "int2").build()).type("int1", new TypeDefinition.Builder().clazz(Integer.class).aggregateFunction(function1).build()).type("int2", new TypeDefinition.Builder().clazz(Integer.class).aggregateFunction(function2).build()).build();
    // When
    final ValidationResult result = validator.validate(schema.getEntity(TestGroups.ENTITY));
    // Then
    assertTrue(result.isValid());
}
Also used : BinaryOperator(java.util.function.BinaryOperator) ValidationResult(uk.gov.gchq.koryphe.ValidationResult) Test(org.junit.jupiter.api.Test)

Example 24 with BinaryOperator

use of java.util.function.BinaryOperator in project Gaffer by gchq.

the class ElementAggregatorTest method shouldAggregatePropertiesWithMultipleSelection.

@Test
public void shouldAggregatePropertiesWithMultipleSelection() {
    // Given
    final BinaryOperator<Tuple3<Integer, Integer, Integer>> maxMinRange = (t1, t2) -> new Tuple3<>(Math.max(t1.get0(), t2.get0()), Math.min(t1.get1(), t2.get1()), Math.max(t1.get0(), t2.get0()) - Math.min(t1.get1(), t2.get1()));
    final ElementAggregator aggregator = new ElementAggregator.Builder().select("max", "min", "range").execute(maxMinRange).build();
    final Properties properties1 = new Properties();
    properties1.put("max", 10);
    properties1.put("min", 10);
    final Properties properties2 = new Properties();
    properties2.put("max", 100);
    properties2.put("min", 100);
    final Properties properties3 = new Properties();
    properties3.put("max", 1000);
    properties3.put("min", 1000);
    // When
    Properties state = aggregator.apply(properties1, properties2);
    state = aggregator.apply(state, properties3);
    // Then
    assertThat(state).hasFieldOrPropertyWithValue("max", 1000).hasFieldOrPropertyWithValue("min", 10).hasFieldOrPropertyWithValue("range", 990);
}
Also used : TupleAdaptedBinaryOperator(uk.gov.gchq.koryphe.tuple.binaryoperator.TupleAdaptedBinaryOperator) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Properties(uk.gov.gchq.gaffer.data.element.Properties) Element(uk.gov.gchq.gaffer.data.element.Element) KorypheBinaryOperator(uk.gov.gchq.koryphe.binaryoperator.KorypheBinaryOperator) BinaryOperator(java.util.function.BinaryOperator) Assertions.assertSame(org.junit.jupiter.api.Assertions.assertSame) Test(org.junit.jupiter.api.Test) ExampleTuple2BinaryOperator(uk.gov.gchq.gaffer.function.ExampleTuple2BinaryOperator) List(java.util.List) Tuple3(uk.gov.gchq.koryphe.tuple.n.Tuple3) Assertions.assertThatNoException(org.assertj.core.api.Assertions.assertThatNoException) BDDMockito.given(org.mockito.BDDMockito.given) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Edge(uk.gov.gchq.gaffer.data.element.Edge) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Tuple3(uk.gov.gchq.koryphe.tuple.n.Tuple3) Properties(uk.gov.gchq.gaffer.data.element.Properties) Test(org.junit.jupiter.api.Test)

Example 25 with BinaryOperator

use of java.util.function.BinaryOperator in project Gaffer by gchq.

the class ElementAggregatorTest method shouldBuildAggregator.

@Test
public void shouldBuildAggregator() {
    // Given
    final String property1 = "property 1";
    final String property2a = "property 2a";
    final String property2b = "property 2b";
    final String property3 = "property 3";
    final BinaryOperator func1 = mock(BinaryOperator.class);
    final BinaryOperator func2 = mock(BinaryOperator.class);
    final BinaryOperator func3 = mock(BinaryOperator.class);
    // When - check you can build the selection/function in any order,
    // although normally it will be done - select then execute.
    final ElementAggregator aggregator = new ElementAggregator.Builder().select(property1).execute(func1).select(property2a, property2b).execute(func2).select(property3).execute(func3).build();
    // Then
    int i = 0;
    TupleAdaptedBinaryOperator<String, ?> adaptedFunction = aggregator.getComponents().get(i++);
    assertEquals(1, adaptedFunction.getSelection().length);
    assertEquals(property1, adaptedFunction.getSelection()[0]);
    assertSame(func1, adaptedFunction.getBinaryOperator());
    adaptedFunction = aggregator.getComponents().get(i++);
    assertThat(adaptedFunction.getSelection()).hasSize(2);
    assertEquals(property2a, adaptedFunction.getSelection()[0]);
    assertEquals(property2b, adaptedFunction.getSelection()[1]);
    assertSame(func2, adaptedFunction.getBinaryOperator());
    adaptedFunction = aggregator.getComponents().get(i++);
    assertSame(func3, adaptedFunction.getBinaryOperator());
    assertThat(adaptedFunction.getSelection()).hasSize(1);
    assertEquals(property3, adaptedFunction.getSelection()[0]);
    assertEquals(i, aggregator.getComponents().size());
}
Also used : TupleAdaptedBinaryOperator(uk.gov.gchq.koryphe.tuple.binaryoperator.TupleAdaptedBinaryOperator) KorypheBinaryOperator(uk.gov.gchq.koryphe.binaryoperator.KorypheBinaryOperator) BinaryOperator(java.util.function.BinaryOperator) ExampleTuple2BinaryOperator(uk.gov.gchq.gaffer.function.ExampleTuple2BinaryOperator) Test(org.junit.jupiter.api.Test)

Aggregations

BinaryOperator (java.util.function.BinaryOperator)38 List (java.util.List)20 ArrayList (java.util.ArrayList)12 Arrays (java.util.Arrays)12 Function (java.util.function.Function)10 Test (org.junit.jupiter.api.Test)10 Collections (java.util.Collections)8 Optional (java.util.Optional)8 Collectors (java.util.stream.Collectors)8 Stream (java.util.stream.Stream)8 BiFunction (java.util.function.BiFunction)7 Map (java.util.Map)6 Collector (java.util.stream.Collector)6 CommonUtils.checkArgument (com.amazon.randomcutforest.CommonUtils.checkArgument)4 CommonUtils.checkNotNull (com.amazon.randomcutforest.CommonUtils.checkNotNull)4 CommonUtils.toDoubleArray (com.amazon.randomcutforest.CommonUtils.toDoubleArray)4 CommonUtils.toFloatArray (com.amazon.randomcutforest.CommonUtils.toFloatArray)4 AnomalyAttributionVisitor (com.amazon.randomcutforest.anomalydetection.AnomalyAttributionVisitor)4 AnomalyScoreVisitor (com.amazon.randomcutforest.anomalydetection.AnomalyScoreVisitor)4 DynamicAttributionVisitor (com.amazon.randomcutforest.anomalydetection.DynamicAttributionVisitor)4