Search in sources :

Example 1 with Tuple3

use of uk.gov.gchq.koryphe.tuple.n.Tuple3 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 2 with Tuple3

use of uk.gov.gchq.koryphe.tuple.n.Tuple3 in project Gaffer by gchq.

the class SampleDataForSplitPoints method validate.

@Override
public ValidationResult validate() {
    final ValidationResult result = Operation.super.validate();
    result.add(FieldUtil.validateRequiredFields(new Tuple3<>("proportionToSample must be greater than 0", proportionToSample, new IsMoreThan(0f))));
    return result;
}
Also used : Tuple3(uk.gov.gchq.koryphe.tuple.n.Tuple3) ValidationResult(uk.gov.gchq.koryphe.ValidationResult) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)

Example 3 with Tuple3

use of uk.gov.gchq.koryphe.tuple.n.Tuple3 in project Gaffer by gchq.

the class QueryGenerator method getPathsAndFiltersForGetElements.

private ParquetQuery getPathsAndFiltersForGetElements(final GetElements getElements) throws SerialisationException, OperationException {
    final Iterable<? extends ElementId> seeds = getElements.getInput();
    if (null == seeds || !seeds.iterator().hasNext()) {
        return new ParquetQuery();
    }
    // Stage 1: Use the view to identify all groups that might contain data
    final Set<String> allRelevantGroups = getRelevantGroups(getElements.getView());
    // Stage 2: For each of the above groups, create a Parquet predicate from the view and directedType
    final Map<String, Pair<FilterPredicate, Boolean>> groupToPredicate = new HashMap<>();
    for (final String group : allRelevantGroups) {
        Pair<FilterPredicate, Boolean> filter = getPredicateFromView(getElements.getView(), group, schemaUtils.getEntityGroups().contains(group));
        if (schemaUtils.getEdgeGroups().contains(group)) {
            final FilterPredicate directedTypeFilter = getPredicateFromDirectedType(getElements.getDirectedType());
            filter.setFirst(FilterPredicateUtils.and(filter.getFirst(), directedTypeFilter));
        }
        groupToPredicate.put(group, filter);
    }
    // Stage 3: Convert seeds to ParquetElementSeeds and create Stream of <group, ParquetElementSeed> pairs where
    // each seed appears once for each of the relevant groups
    final Stream<Pair<String, ParquetElementSeed>> groupAndSeeds = StreamSupport.stream(seeds.spliterator(), false).flatMap(seed -> {
        try {
            return seedToParquetObject(seed, allRelevantGroups).stream();
        } catch (final SerialisationException e) {
            throw new RuntimeException("SerialisationException converting seed into a Parquet object", e);
        }
    });
    // Stage 4: Convert stream of <group, ParquetElementSeed> pars to stream of tuples
    // <group, ParquetElementSeed, List<PathInfo>>
    final Stream<Tuple3<String, ParquetElementSeed, Set<PathInfo>>> groupSeedsAndPaths = groupAndSeeds.map(pair -> getRelevantFiles(pair.getFirst(), pair.getSecond()));
    // Stage 5: Create map from path to list of <group, reversed edge flag, Parquet seeds>
    // TODO: Currently this consumes the entire stream - need to do this in batches
    final List<Tuple3<String, ParquetElementSeed, Set<PathInfo>>> groupSeedsAndPathsList = groupSeedsAndPaths.collect(Collectors.toList());
    final Map<PathInfo, List<Tuple3<String, Boolean, ParquetElementSeed>>> pathToSeeds = new HashMap<>();
    for (final Tuple3<String, ParquetElementSeed, Set<PathInfo>> tuple : groupSeedsAndPathsList) {
        Set<PathInfo> paths = tuple.get2();
        for (final PathInfo pathInfo : paths) {
            if (!pathToSeeds.containsKey(pathInfo)) {
                pathToSeeds.put(pathInfo, new ArrayList<>());
            }
            pathToSeeds.get(pathInfo).add(new Tuple3<>(tuple.get0(), pathInfo.isReversed(), tuple.get1()));
        }
    }
    // Stage 6: Create ParquetQuery
    final SeededGraphFilters.IncludeIncomingOutgoingType includeIncomingOutgoingType = getElements.getIncludeIncomingOutGoing();
    final SeedMatching.SeedMatchingType seedMatchingType = getElements.getSeedMatching();
    final ParquetQuery parquetQuery = new ParquetQuery();
    for (final PathInfo pathInfo : pathToSeeds.keySet()) {
        List<Tuple3<String, Boolean, ParquetElementSeed>> seedList = pathToSeeds.get(pathInfo);
        FilterPredicate filterPredicate = seedsToPredicate(seedList, includeIncomingOutgoingType, seedMatchingType);
        if (null != filterPredicate) {
            final String group = pathInfo.getGroup();
            final Pair<FilterPredicate, Boolean> viewFilterPredicate = groupToPredicate.get(group);
            if (null != viewFilterPredicate) {
                // Put view predicate first as filter for checking whether it matches one of many seeds could be complex
                filterPredicate = FilterPredicateUtils.and(viewFilterPredicate.getFirst(), filterPredicate);
            }
            final ParquetFileQuery fileQuery = new ParquetFileQuery(pathInfo.getPath(), filterPredicate, viewFilterPredicate.getSecond());
            parquetQuery.add(group, fileQuery);
        }
    }
    LOGGER.info("Created ParquetQuery of {}", parquetQuery);
    return parquetQuery;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) Pair(uk.gov.gchq.gaffer.commonutil.pair.Pair) SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) Tuple3(uk.gov.gchq.koryphe.tuple.n.Tuple3) FilterPredicate(org.apache.parquet.filter2.predicate.FilterPredicate) SeededGraphFilters(uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters) SeedMatching(uk.gov.gchq.gaffer.operation.SeedMatching)

Aggregations

Tuple3 (uk.gov.gchq.koryphe.tuple.n.Tuple3)3 List (java.util.List)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 BinaryOperator (java.util.function.BinaryOperator)1 FilterPredicate (org.apache.parquet.filter2.predicate.FilterPredicate)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)1 Assertions.assertThatNoException (org.assertj.core.api.Assertions.assertThatNoException)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Assertions.assertSame (org.junit.jupiter.api.Assertions.assertSame)1 Assertions.assertTrue (org.junit.jupiter.api.Assertions.assertTrue)1 Test (org.junit.jupiter.api.Test)1 BDDMockito.given (org.mockito.BDDMockito.given)1 Mockito.mock (org.mockito.Mockito.mock)1 Pair (uk.gov.gchq.gaffer.commonutil.pair.Pair)1 Edge (uk.gov.gchq.gaffer.data.element.Edge)1 Element (uk.gov.gchq.gaffer.data.element.Element)1