Search in sources :

Example 11 with ConsumerFunctionContext

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

the class SchemaElementDefinitionValidatorTest method shouldValidateFunctionSelectionsAndReturnFalseWhenFunctionTypeDoesNotEqualSelectionType.

@Test
public void shouldValidateFunctionSelectionsAndReturnFalseWhenFunctionTypeDoesNotEqualSelectionType() {
    // Given
    final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class);
    final Processor<String, ConsumerFunctionContext<String, ConsumerFunction>> processor = mock(Processor.class);
    final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator();
    final List<ConsumerFunctionContext<String, ConsumerFunction>> functions = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        final ConsumerFunctionContext<String, ConsumerFunction> context = mock(ConsumerFunctionContext.class);
        final ConsumerFunction function = mock(ConsumerFunction.class);
        given(function.getInputClasses()).willReturn(new Class<?>[] { String.class });
        given(context.getFunction()).willReturn(function);
        given(context.getSelection()).willReturn(Collections.singletonList("key" + i + "a"));
        functions.add(context);
        given(elementDef.getClass(context.getSelection().get(0))).willReturn((Class) Integer.class);
    }
    given(processor.getFunctions()).willReturn(functions);
    // When
    final boolean isValid = validator.validateFunctionArgumentTypes(processor, elementDef);
    // Then
    assertFalse(isValid);
}
Also used : ConsumerFunctionContext(uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext) ArrayList(java.util.ArrayList) ConsumerFunction(uk.gov.gchq.gaffer.function.ConsumerFunction) Test(org.junit.Test)

Example 12 with ConsumerFunctionContext

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

the class SchemaTest method testLoadingSchemaFromJson.

@Test
public void testLoadingSchemaFromJson() {
    // Edge definitions
    SchemaElementDefinition edgeDefinition = schema.getEdge(TestGroups.EDGE);
    assertNotNull(edgeDefinition);
    assertEquals(EDGE_DESCRIPTION, edgeDefinition.getDescription());
    final Map<String, String> propertyMap = edgeDefinition.getPropertyMap();
    assertEquals(3, propertyMap.size());
    assertEquals("prop.string", propertyMap.get(TestPropertyNames.PROP_2));
    assertEquals("prop.date", propertyMap.get(TestPropertyNames.DATE));
    assertEquals("timestamp", propertyMap.get(TestPropertyNames.TIMESTAMP));
    assertEquals(Sets.newLinkedHashSet(Collections.singletonList(TestPropertyNames.DATE)), edgeDefinition.getGroupBy());
    // Check validator
    ElementFilter validator = edgeDefinition.getValidator();
    final List<ConsumerFunctionContext<String, FilterFunction>> valContexts = validator.getFunctions();
    int index = 0;
    ConsumerFunctionContext<String, FilterFunction> valContext = valContexts.get(index++);
    assertTrue(valContext.getFunction() instanceof IsA);
    assertEquals(1, valContext.getSelection().size());
    assertEquals(IdentifierType.SOURCE.name(), valContext.getSelection().get(0));
    valContext = valContexts.get(index++);
    assertTrue(valContext.getFunction() instanceof IsA);
    assertEquals(1, valContext.getSelection().size());
    assertEquals(IdentifierType.DESTINATION.name(), valContext.getSelection().get(0));
    valContext = valContexts.get(index++);
    assertTrue(valContext.getFunction() instanceof IsA);
    assertEquals(1, valContext.getSelection().size());
    assertEquals(IdentifierType.DIRECTED.name(), valContext.getSelection().get(0));
    valContext = valContexts.get(index++);
    assertTrue(valContext.getFunction() instanceof ExampleFilterFunction);
    assertEquals(1, valContext.getSelection().size());
    assertEquals(IdentifierType.DIRECTED.name(), valContext.getSelection().get(0));
    valContext = valContexts.get(index++);
    assertTrue(valContext.getFunction() instanceof IsA);
    assertEquals(1, valContext.getSelection().size());
    assertEquals(TestPropertyNames.PROP_2, valContext.getSelection().get(0));
    valContext = valContexts.get(index++);
    assertTrue(valContext.getFunction() instanceof ExampleFilterFunction);
    assertEquals(1, valContext.getSelection().size());
    assertEquals(TestPropertyNames.PROP_2, valContext.getSelection().get(0));
    valContext = valContexts.get(index++);
    assertTrue(valContext.getFunction() instanceof IsA);
    assertEquals(1, valContext.getSelection().size());
    assertEquals(TestPropertyNames.DATE, valContext.getSelection().get(0));
    valContext = valContexts.get(index++);
    assertTrue(valContext.getFunction() instanceof IsA);
    assertEquals(1, valContext.getSelection().size());
    assertEquals(TestPropertyNames.TIMESTAMP, valContext.getSelection().get(0));
    assertEquals(index, valContexts.size());
    TypeDefinition type = edgeDefinition.getPropertyTypeDef(TestPropertyNames.DATE);
    assertEquals(Date.class, type.getClazz());
    assertEquals(DATE_TYPE_DESCRIPTION, type.getDescription());
    assertNull(type.getSerialiser());
    assertTrue(type.getAggregateFunction() instanceof ExampleAggregateFunction);
    // Entity definitions
    SchemaElementDefinition entityDefinition = schema.getEntity(TestGroups.ENTITY);
    assertNotNull(entityDefinition);
    assertEquals(ENTITY_DESCRIPTION, entityDefinition.getDescription());
    assertTrue(entityDefinition.containsProperty(TestPropertyNames.PROP_1));
    type = entityDefinition.getPropertyTypeDef(TestPropertyNames.PROP_1);
    assertEquals(0, entityDefinition.getGroupBy().size());
    assertEquals(STRING_TYPE_DESCRIPTION, type.getDescription());
    assertEquals(String.class, type.getClazz());
    assertNull(type.getSerialiser());
    assertTrue(type.getAggregateFunction() instanceof ExampleAggregateFunction);
    ElementAggregator aggregator = edgeDefinition.getAggregator();
    List<PassThroughFunctionContext<String, AggregateFunction>> aggContexts = aggregator.getFunctions();
    assertEquals(3, aggContexts.size());
    PassThroughFunctionContext<String, AggregateFunction> aggContext = aggContexts.get(0);
    assertTrue(aggContext.getFunction() instanceof ExampleAggregateFunction);
    assertEquals(1, aggContext.getSelection().size());
    assertEquals(TestPropertyNames.PROP_2, aggContext.getSelection().get(0));
    aggContext = aggContexts.get(1);
    assertTrue(aggContext.getFunction() instanceof ExampleAggregateFunction);
    assertEquals(1, aggContext.getSelection().size());
    assertEquals(TestPropertyNames.DATE, aggContext.getSelection().get(0));
}
Also used : FilterFunction(uk.gov.gchq.gaffer.function.FilterFunction) ExampleFilterFunction(uk.gov.gchq.gaffer.function.ExampleFilterFunction) PassThroughFunctionContext(uk.gov.gchq.gaffer.function.context.PassThroughFunctionContext) ConsumerFunctionContext(uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext) IsA(uk.gov.gchq.gaffer.function.IsA) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ExampleAggregateFunction(uk.gov.gchq.gaffer.function.ExampleAggregateFunction) AggregateFunction(uk.gov.gchq.gaffer.function.AggregateFunction) ExampleAggregateFunction(uk.gov.gchq.gaffer.function.ExampleAggregateFunction) ExampleFilterFunction(uk.gov.gchq.gaffer.function.ExampleFilterFunction) ElementAggregator(uk.gov.gchq.gaffer.data.element.function.ElementAggregator) Test(org.junit.Test)

Example 13 with ConsumerFunctionContext

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

the class Filter method cloneFunctions.

/**
     * Create a deep copy of the {@link uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext}s executed by this
     * <code>Filter</code>.
     *
     * @return Deep copy of {@link uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext}s.
     */
protected List<ConsumerFunctionContext<R, FilterFunction>> cloneFunctions() {
    final List<ConsumerFunctionContext<R, FilterFunction>> functionClones = new ArrayList<>();
    for (final ConsumerFunctionContext<R, FilterFunction> function : functions) {
        final ConsumerFunctionContext<R, FilterFunction> cloneContext = new ConsumerFunctionContext<>();
        cloneContext.setSelection(function.getSelection());
        final FilterFunction af = function.getFunction();
        if (af != null) {
            cloneContext.setFunction(af.statelessClone());
        }
        functionClones.add(cloneContext);
    }
    return functionClones;
}
Also used : FilterFunction(uk.gov.gchq.gaffer.function.FilterFunction) ConsumerFunctionContext(uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext) ArrayList(java.util.ArrayList)

Example 14 with ConsumerFunctionContext

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

the class AndTest method shouldJsonSerialiseAndDeserialise.

@Test
public void shouldJsonSerialiseAndDeserialise() throws SerialisationException {
    // Given
    final And filter = new And(Collections.singletonList(new ConsumerFunctionContext<Integer, FilterFunction>(new And(), Arrays.asList(0, 1, 2))));
    // When
    final String json = new String(new JSONSerialiser().serialise(filter, true));
    // Then
    JsonUtil.assertEquals(String.format("{%n" + "  \"class\" : \"uk.gov.gchq.gaffer.function.filter.And\",%n" + "  \"functions\" : [ {%n" + "    \"function\" : {%n" + "      \"class\" : \"uk.gov.gchq.gaffer.function.filter.And\",%n" + "      \"functions\" : [ ]%n" + "    },%n" + "    \"selection\" : [ 0, 1, 2 ]%n" + "  } ]%n" + "}"), json);
    // When 2
    final And deserialisedFilter = new JSONSerialiser().deserialise(json.getBytes(), And.class);
    // Then 2
    assertNotNull(deserialisedFilter);
}
Also used : ConsumerFunctionContext(uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext) JSONSerialiser(uk.gov.gchq.gaffer.jsonserialisation.JSONSerialiser) FilterFunctionTest(uk.gov.gchq.gaffer.function.FilterFunctionTest) Test(org.junit.Test)

Aggregations

ConsumerFunctionContext (uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext)14 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)10 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)7 FilterFunction (uk.gov.gchq.gaffer.function.FilterFunction)7 IsMoreThan (uk.gov.gchq.gaffer.function.filter.IsMoreThan)6 Filter (org.apache.spark.sql.sources.Filter)5 GreaterThan (org.apache.spark.sql.sources.GreaterThan)5 LessThan (org.apache.spark.sql.sources.LessThan)5 IsLessThan (uk.gov.gchq.gaffer.function.filter.IsLessThan)5 HashSet (java.util.HashSet)4 SQLContext (org.apache.spark.sql.SQLContext)4 Schema (uk.gov.gchq.gaffer.store.schema.Schema)4 EqualTo (org.apache.spark.sql.sources.EqualTo)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Set (java.util.Set)2 And (org.apache.spark.sql.sources.And)2 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)2 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)2