Search in sources :

Example 11 with BinaryOperator

use of java.util.function.BinaryOperator in project rest.li by linkedin.

the class SchemaToAvroJsonEncoder method produceFieldProperties.

@SuppressWarnings("unchecked")
static /* package private */
Map<String, ?> produceFieldProperties(RecordDataSchema.Field field, DataToAvroSchemaTranslationOptions options) {
    Stream<Map.Entry<String, Object>> toBeFiltered = field.getProperties().entrySet().stream();
    // and merge with record field's properties.
    if (field.getType().getType() == DataSchema.Type.TYPEREF) {
        toBeFiltered = Stream.concat(toBeFiltered, ((TyperefDataSchema) field.getType()).getMergedTyperefProperties().entrySet().stream()).filter(entry -> !options.getTyperefPropertiesExcludeSet().contains(entry.getKey()));
    }
    // Property merge rule:
    // For property content inherited from TypeRef that appears to be have same property name as the record field:
    // if the two property contents are Map type, they will be merged at this level,
    // otherwise Typeref field property content will be overridden by record field property's content.
    BinaryOperator<Object> propertyMergeLogic = (originalPropertyContent, inheritedPropertyContent) -> {
        if (originalPropertyContent instanceof Map && inheritedPropertyContent instanceof Map) {
            Map<String, Object> mergedMap = new DataMap((Map<String, Object>) originalPropertyContent);
            ((Map<String, Object>) inheritedPropertyContent).forEach(mergedMap::putIfAbsent);
            return mergedMap;
        } else {
            return originalPropertyContent;
        }
    };
    return toBeFiltered.filter(entry -> !RESERVED_DATA_PROPERTIES.contains(entry.getKey())).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, propertyMergeLogic));
}
Also used : Arrays(java.util.Arrays) DataSchema(com.linkedin.data.schema.DataSchema) JsonBuilder(com.linkedin.data.schema.JsonBuilder) ArrayList(java.util.ArrayList) Formatter(java.util.Formatter) Data(com.linkedin.data.Data) HashSet(java.util.HashSet) DataMap(com.linkedin.data.DataMap) Named(com.linkedin.data.schema.Named) UnionDataSchema(com.linkedin.data.schema.UnionDataSchema) Map(java.util.Map) AVRO_PREFIX(com.linkedin.data.avro.SchemaTranslator.AVRO_PREFIX) SchemaToJsonEncoder(com.linkedin.data.schema.SchemaToJsonEncoder) Set(java.util.Set) TYPE_KEY(com.linkedin.data.schema.DataSchemaConstants.TYPE_KEY) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) BinaryOperator(java.util.function.BinaryOperator) TyperefDataSchema(com.linkedin.data.schema.TyperefDataSchema) List(java.util.List) RecordDataSchema(com.linkedin.data.schema.RecordDataSchema) Stream(java.util.stream.Stream) DEFAULT_KEY(com.linkedin.data.schema.DataSchemaConstants.DEFAULT_KEY) DataSchemaConstants(com.linkedin.data.schema.DataSchemaConstants) NamedDataSchema(com.linkedin.data.schema.NamedDataSchema) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) DataMap(com.linkedin.data.DataMap)

Example 12 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 13 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 14 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)

Example 15 with BinaryOperator

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

the class AggregateValidator method validateElementAggregator.

private ValidationResult validateElementAggregator(final Map.Entry<String, ?> entry, final Schema schema) {
    final ValidationResult result = new ValidationResult();
    List<TupleAdaptedBinaryOperator<String, ?>> aggregateFunctions = new ArrayList<>();
    final SchemaElementDefinition schemaElement = schema.getElement(entry.getKey());
    if (null != schemaElement) {
        aggregateFunctions = schemaElement.getOriginalAggregateFunctions();
    }
    List<BinaryOperator<?>> schemaOperators = new ArrayList<>();
    if (null != aggregateFunctions) {
        schemaOperators = Streams.toStream(aggregateFunctions).map(AdaptedBinaryOperator::getBinaryOperator).collect(Collectors.toList());
    }
    if (schemaOperators.contains(null)) {
        result.addError("Schema contains an ElementAggregator with a null function.");
    }
    final AggregatePair pair = (AggregatePair) entry.getValue();
    final ElementAggregator aggregator = pair.getElementAggregator();
    if (null != aggregator && null != aggregator.getComponents()) {
        for (final TupleAdaptedBinaryOperator<String, ?> adaptedFunction : aggregator.getComponents()) {
            if (null == adaptedFunction.getBinaryOperator()) {
                result.addError(aggregator.getClass().getSimpleName() + " contains a null function.");
            }
        }
    }
    return result;
}
Also used : TupleAdaptedBinaryOperator(uk.gov.gchq.koryphe.tuple.binaryoperator.TupleAdaptedBinaryOperator) AdaptedBinaryOperator(uk.gov.gchq.koryphe.binaryoperator.AdaptedBinaryOperator) ArrayList(java.util.ArrayList) TupleAdaptedBinaryOperator(uk.gov.gchq.koryphe.tuple.binaryoperator.TupleAdaptedBinaryOperator) AggregatePair(uk.gov.gchq.gaffer.operation.util.AggregatePair) ValidationResult(uk.gov.gchq.koryphe.ValidationResult) TupleAdaptedBinaryOperator(uk.gov.gchq.koryphe.tuple.binaryoperator.TupleAdaptedBinaryOperator) BinaryOperator(java.util.function.BinaryOperator) AdaptedBinaryOperator(uk.gov.gchq.koryphe.binaryoperator.AdaptedBinaryOperator) SchemaElementDefinition(uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator)

Aggregations

BinaryOperator (java.util.function.BinaryOperator)15 TupleAdaptedBinaryOperator (uk.gov.gchq.koryphe.tuple.binaryoperator.TupleAdaptedBinaryOperator)6 List (java.util.List)5 Test (org.junit.jupiter.api.Test)5 ExampleTuple2BinaryOperator (uk.gov.gchq.gaffer.function.ExampleTuple2BinaryOperator)4 KorypheBinaryOperator (uk.gov.gchq.koryphe.binaryoperator.KorypheBinaryOperator)4 ArrayList (java.util.ArrayList)3 Arrays (java.util.Arrays)3 Map (java.util.Map)3 Supplier (java.util.function.Supplier)3 ActionListener (java.awt.event.ActionListener)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Comparator (java.util.Comparator)2 Set (java.util.Set)2 Consumer (java.util.function.Consumer)2 Stream (java.util.stream.Stream)2 Cache (javax.cache.Cache)2 Ignite (org.apache.ignite.Ignite)2 IgniteCache (org.apache.ignite.IgniteCache)2