Search in sources :

Example 1 with FilterFunction

use of uk.gov.gchq.gaffer.function.FilterFunction in project Gaffer by gchq.

the class ElementFilterTest method shouldWrapElementInElementTupleAndCallSuper.

@Test
public void shouldWrapElementInElementTupleAndCallSuper() {
    // Given
    final String reference = "reference1";
    final String value = "value";
    final ElementFilter filter = new ElementFilter();
    final ConsumerFunctionContext<String, FilterFunction> functionContext1 = mock(ConsumerFunctionContext.class);
    final FilterFunction function = mock(FilterFunction.class);
    given(functionContext1.getFunction()).willReturn(function);
    filter.addFunction(functionContext1);
    final Element element = mock(Element.class);
    given(element.getProperty(reference)).willReturn(value);
    final ArgumentCaptor<ElementTuple> elementTupleCaptor = ArgumentCaptor.forClass(ElementTuple.class);
    given(functionContext1.select(elementTupleCaptor.capture())).willReturn(new Object[] { value });
    // When
    filter.filter(element);
    // Then
    assertSame(element, elementTupleCaptor.getValue().getElement());
    verify(functionContext1).getFunction();
    final ArgumentCaptor<Object[]> argumentCaptor = ArgumentCaptor.forClass(Object[].class);
    verify(function).isValid(argumentCaptor.capture());
    assertEquals(value, argumentCaptor.getValue()[0]);
}
Also used : FilterFunction(uk.gov.gchq.gaffer.function.FilterFunction) Element(uk.gov.gchq.gaffer.data.element.Element) ElementTuple(uk.gov.gchq.gaffer.data.element.ElementTuple) Test(org.junit.Test)

Example 2 with FilterFunction

use of uk.gov.gchq.gaffer.function.FilterFunction in project Gaffer by gchq.

the class Filter method filter.

/**
     * Test an input {@link uk.gov.gchq.gaffer.function.Tuple} against {@link uk.gov.gchq.gaffer.function.FilterFunction}s, performing a
     * logical AND.
     *
     * @param tuple {@link uk.gov.gchq.gaffer.function.Tuple} to be filtered.
     * @return Logical AND of filter function results.
     */
public boolean filter(final Tuple<R> tuple) {
    if (functions == null) {
        return true;
    }
    for (final ConsumerFunctionContext<R, FilterFunction> functionContext : functions) {
        FilterFunction function = functionContext.getFunction();
        Object[] selection = functionContext.select(tuple);
        boolean result = function.isValid(selection);
        if (!result) {
            LOGGER.debug(function.getClass().getName() + " filtered out " + Arrays.toString(selection) + " from input: " + tuple);
            return false;
        }
    }
    return true;
}
Also used : FilterFunction(uk.gov.gchq.gaffer.function.FilterFunction)

Example 3 with FilterFunction

use of uk.gov.gchq.gaffer.function.FilterFunction in project Gaffer by gchq.

the class AndTest method shouldRejectWhenOneFunctionRejects.

@Test
public void shouldRejectWhenOneFunctionRejects() {
    // Given
    final String test = "test";
    final String test1a = "test1a";
    final String test1b = "test1b";
    final String test2a = "test2a";
    final ConsumerFunctionContext<Integer, FilterFunction> funcContext1 = mock(ConsumerFunctionContext.class);
    final ConsumerFunctionContext<Integer, FilterFunction> funcContext2 = mock(ConsumerFunctionContext.class);
    final ConsumerFunctionContext<Integer, FilterFunction> funcContext3 = mock(ConsumerFunctionContext.class);
    final FilterFunction func1 = mock(FilterFunction.class);
    final FilterFunction func2 = mock(FilterFunction.class);
    final FilterFunction func3 = mock(FilterFunction.class);
    final And and = new And(Arrays.asList(funcContext1, funcContext2, funcContext3));
    given(funcContext1.getFunction()).willReturn(func1);
    given(funcContext2.getFunction()).willReturn(func2);
    given(funcContext3.getFunction()).willReturn(func3);
    given(funcContext1.select(Mockito.any(ArrayTuple.class))).willReturn(new Object[] { test, test1a, test1b });
    given(funcContext2.select(Mockito.any(ArrayTuple.class))).willReturn(new Object[] { test, test2a });
    given(funcContext3.select(Mockito.any(ArrayTuple.class))).willReturn(new Object[] { test });
    given(func1.isValid(new String[] { test, test1a, test1b })).willReturn(true);
    given(func2.isValid(new String[] { test, test2a })).willReturn(false);
    given(func3.isValid(new String[] { test })).willReturn(true);
    // When
    boolean accepted = and.isValid(new String[] { test, test1a, test2a, test1b });
    // Then
    assertFalse(accepted);
    verify(func1).isValid(new String[] { test, test1a, test1b });
    verify(func2).isValid(new String[] { test, test2a });
    verify(func3, never()).isValid(new String[] { test, test2a });
}
Also used : FilterFunction(uk.gov.gchq.gaffer.function.FilterFunction) ArrayTuple(uk.gov.gchq.gaffer.function.ArrayTuple) FilterFunctionTest(uk.gov.gchq.gaffer.function.FilterFunctionTest) Test(org.junit.Test)

Example 4 with FilterFunction

use of uk.gov.gchq.gaffer.function.FilterFunction in project Gaffer by gchq.

the class FiltersToOperationConverter method applyPropertyFilters.

private AbstractGetRDD<?> applyPropertyFilters(final View derivedView, final AbstractGetRDD<?> operation) {
    final List<Set<String>> groupsRelatedToFilters = new ArrayList<>();
    for (final Filter filter : filters) {
        final Set<String> groupsRelatedToFilter = getGroupsFromFilter(filter);
        if (groupsRelatedToFilter != null && !groupsRelatedToFilter.isEmpty()) {
            groupsRelatedToFilters.add(groupsRelatedToFilter);
        }
        LOGGER.info("Groups {} are related to filter {}", StringUtils.join(groupsRelatedToFilter, ','), filter);
    }
    LOGGER.info("Groups related to filters are: {}", StringUtils.join(groupsRelatedToFilters, ','));
    // Take the intersection of this list of groups - only these groups can be related to the query
    final Set<String> intersection = new HashSet<>(derivedView.getEntityGroups());
    intersection.addAll(derivedView.getEdgeGroups());
    for (final Set<String> groupsRelatedToFilter : groupsRelatedToFilters) {
        intersection.retainAll(groupsRelatedToFilter);
    }
    LOGGER.info("Groups that can be returned are: {}", StringUtils.join(intersection, ','));
    // Update view with filters and add to operation
    final Map<String, List<ConsumerFunctionContext<String, FilterFunction>>> groupToFunctions = new HashMap<>();
    for (final Filter filter : filters) {
        final Map<String, List<ConsumerFunctionContext<String, FilterFunction>>> map = getFunctionsFromFilter(filter);
        for (final Entry<String, List<ConsumerFunctionContext<String, FilterFunction>>> entry : map.entrySet()) {
            if (!groupToFunctions.containsKey(entry.getKey())) {
                groupToFunctions.put(entry.getKey(), new ArrayList<ConsumerFunctionContext<String, FilterFunction>>());
            }
            groupToFunctions.get(entry.getKey()).addAll(entry.getValue());
        }
    }
    LOGGER.info("The following functions will be applied for the given group:");
    for (final Entry<String, List<ConsumerFunctionContext<String, FilterFunction>>> entry : groupToFunctions.entrySet()) {
        LOGGER.info("Group = {}: ", entry.getKey());
        for (final ConsumerFunctionContext<String, FilterFunction> cfc : entry.getValue()) {
            LOGGER.info("\t{} {}", StringUtils.join(cfc.getSelection(), ','), cfc.getFunction());
        }
    }
    boolean updated = false;
    View.Builder builder = new View.Builder();
    for (final String group : derivedView.getEntityGroups()) {
        if (intersection.contains(group)) {
            if (groupToFunctions.get(group) != null) {
                final ViewElementDefinition ved = new ViewElementDefinition.Builder().merge(derivedView.getEntity(group)).postAggregationFilterFunctions(groupToFunctions.get(group)).build();
                LOGGER.info("Adding the following filter functions to the view for group {}:", group);
                for (final ConsumerFunctionContext<String, FilterFunction> cfc : groupToFunctions.get(group)) {
                    LOGGER.info("\t{} {}", StringUtils.join(cfc.getSelection(), ','), cfc.getFunction());
                }
                builder = builder.entity(group, ved);
                updated = true;
            } else {
                LOGGER.info("Not adding any filter functions to the view for group {}", group);
            }
        }
    }
    for (final String group : derivedView.getEdgeGroups()) {
        if (intersection.contains(group)) {
            if (groupToFunctions.get(group) != null) {
                final ViewElementDefinition ved = new ViewElementDefinition.Builder().merge(derivedView.getEdge(group)).postAggregationFilterFunctions(groupToFunctions.get(group)).build();
                LOGGER.info("Adding the following filter functions to the view for group {}:", group);
                for (final ConsumerFunctionContext<String, FilterFunction> cfc : groupToFunctions.get(group)) {
                    LOGGER.info("\t{} {}", StringUtils.join(cfc.getSelection(), ','), cfc.getFunction());
                }
                builder = builder.edge(group, ved);
                updated = true;
            } else {
                LOGGER.info("Not adding any filter functions to the view for group {}", group);
            }
        }
    }
    if (updated) {
        operation.setView(builder.build());
    } else {
        operation.setView(derivedView);
    }
    return operation;
}
Also used : FilterFunction(uk.gov.gchq.gaffer.function.FilterFunction) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) ConsumerFunctionContext(uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext) Filter(org.apache.spark.sql.sources.Filter) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 5 with FilterFunction

use of uk.gov.gchq.gaffer.function.FilterFunction in project Gaffer by gchq.

the class AndExample method isLessThan3AndIsMoreThan0.

public void isLessThan3AndIsMoreThan0() {
    // ---------------------------------------------------------
    final And function = new And(Arrays.asList(new ConsumerFunctionContext.Builder<Integer, FilterFunction>().select(// select first property
    0).execute(new IsLessThan(3)).build(), new ConsumerFunctionContext.Builder<Integer, FilterFunction>().select(// select first property
    0).execute(new IsMoreThan(0)).build()));
    // ---------------------------------------------------------
    runExample(function, new Object[] { 0 }, new Object[] { 1 }, new Object[] { 2 }, new Object[] { 3 }, new Object[] { 1L }, new Object[] { 2L });
}
Also used : FilterFunction(uk.gov.gchq.gaffer.function.FilterFunction) ConsumerFunctionContext(uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext) IsLessThan(uk.gov.gchq.gaffer.function.filter.IsLessThan) And(uk.gov.gchq.gaffer.function.filter.And) IsMoreThan(uk.gov.gchq.gaffer.function.filter.IsMoreThan)

Aggregations

FilterFunction (uk.gov.gchq.gaffer.function.FilterFunction)22 Test (org.junit.Test)14 FilterFunctionTest (uk.gov.gchq.gaffer.function.FilterFunctionTest)11 ConsumerFunctionContext (uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext)6 ArrayTuple (uk.gov.gchq.gaffer.function.ArrayTuple)4 IsLessThan (uk.gov.gchq.gaffer.function.filter.IsLessThan)4 IsMoreThan (uk.gov.gchq.gaffer.function.filter.IsMoreThan)4 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 And (uk.gov.gchq.gaffer.function.filter.And)2 Or (uk.gov.gchq.gaffer.function.filter.Or)2 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 HashMap (java.util.HashMap)1 InputMismatchException (java.util.InputMismatchException)1 List (java.util.List)1 Set (java.util.Set)1 Filter (org.apache.spark.sql.sources.Filter)1 Element (uk.gov.gchq.gaffer.data.element.Element)1 ElementTuple (uk.gov.gchq.gaffer.data.element.ElementTuple)1 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)1