Search in sources :

Example 1 with TransformFunction

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

the class ElementTransformerTest method shouldBuildTransformer.

@Test
public void shouldBuildTransformer() {
    // Given
    final String property1 = "property 1";
    final String property2 = "property 2";
    final String property3a = "property 3a";
    final String property3b = "property 3b";
    final IdentifierType identifier5 = IdentifierType.SOURCE;
    final String property1Proj = "property 1 proj";
    final String property2Proj = "property 2 proj";
    final String property3aProj = "property 3a proj";
    final String property3bProj = "property 3b proj";
    final IdentifierType identifier5Proj = IdentifierType.DESTINATION;
    final TransformFunction func1 = mock(TransformFunction.class);
    final TransformFunction func3 = mock(TransformFunction.class);
    final TransformFunction func4 = mock(TransformFunction.class);
    final TransformFunction func5 = mock(TransformFunction.class);
    // When - check you can build the selection/function/projections in any order,
    // although normally it will be done - select, execute then project.
    final ElementTransformer transformer = new ElementTransformer.Builder().select(property1).execute(func1).project(property1Proj).select(property2).project(property2Proj).project(property3aProj, property3bProj).select(property3a, property3b).execute(func3).execute(func4).execute(func5).project(identifier5Proj.name()).select(identifier5.name()).build();
    // Then
    int i = 0;
    ConsumerProducerFunctionContext<String, TransformFunction> context = transformer.getFunctions().get(i++);
    assertEquals(1, context.getSelection().size());
    assertEquals(property1, context.getSelection().get(0));
    assertSame(func1, context.getFunction());
    assertEquals(1, context.getProjection().size());
    assertEquals(property1Proj, context.getProjection().get(0));
    context = transformer.getFunctions().get(i++);
    assertEquals(1, context.getSelection().size());
    assertEquals(property2, context.getSelection().get(0));
    assertEquals(1, context.getProjection().size());
    assertEquals(property2Proj, context.getProjection().get(0));
    context = transformer.getFunctions().get(i++);
    assertEquals(2, context.getSelection().size());
    assertEquals(property3a, context.getSelection().get(0));
    assertEquals(property3b, context.getSelection().get(1));
    assertSame(func3, context.getFunction());
    assertEquals(2, context.getProjection().size());
    assertEquals(property3aProj, context.getProjection().get(0));
    assertEquals(property3bProj, context.getProjection().get(1));
    context = transformer.getFunctions().get(i++);
    assertSame(func4, context.getFunction());
    context = transformer.getFunctions().get(i++);
    assertSame(func5, context.getFunction());
    assertEquals(1, context.getSelection().size());
    assertEquals(identifier5.name(), context.getSelection().get(0));
    assertEquals(1, context.getProjection().size());
    assertEquals(identifier5Proj.name(), context.getProjection().get(0));
    assertEquals(i, transformer.getFunctions().size());
}
Also used : TransformFunction(uk.gov.gchq.gaffer.function.TransformFunction) IdentifierType(uk.gov.gchq.gaffer.data.element.IdentifierType) Test(org.junit.Test)

Example 2 with TransformFunction

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

the class Transformer method cloneFunctions.

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

Example 3 with TransformFunction

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

the class ElementTransformerTest method shouldWrapElementInElementTupleAndCallSuper.

@Test
public void shouldWrapElementInElementTupleAndCallSuper() {
    // Given
    final String reference = "reference1";
    final String value = "value";
    final ElementTransformer transformer = new ElementTransformer();
    final ConsumerProducerFunctionContext<String, TransformFunction> functionContext1 = mock(ConsumerProducerFunctionContext.class);
    final TransformFunction function = mock(TransformFunction.class);
    given(functionContext1.getFunction()).willReturn(function);
    transformer.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
    transformer.transform(element);
    // Then
    assertSame(element, elementTupleCaptor.getValue().getElement());
    verify(functionContext1).getFunction();
    final ArgumentCaptor<Object[]> argumentCaptor = ArgumentCaptor.forClass(Object[].class);
    verify(function).transform(argumentCaptor.capture());
    assertEquals(value, argumentCaptor.getValue()[0]);
}
Also used : TransformFunction(uk.gov.gchq.gaffer.function.TransformFunction) Element(uk.gov.gchq.gaffer.data.element.Element) ElementTuple(uk.gov.gchq.gaffer.data.element.ElementTuple) Test(org.junit.Test)

Example 4 with TransformFunction

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

the class ElementTransformerTest method shouldCloneTransformer.

@Test
public void shouldCloneTransformer() {
    // Given
    final String reference1 = "reference1";
    final String reference2 = "reference2";
    final ElementTransformer transformer = new ElementTransformer();
    final ConsumerProducerFunctionContext<String, TransformFunction> functionContext1 = mock(ConsumerProducerFunctionContext.class);
    final TransformFunction function = mock(TransformFunction.class);
    final TransformFunction clonedFunction = mock(TransformFunction.class);
    given(functionContext1.getFunction()).willReturn(function);
    given(functionContext1.getSelection()).willReturn(Collections.singletonList(reference1));
    given(functionContext1.getProjection()).willReturn(Collections.singletonList(reference2));
    given(function.statelessClone()).willReturn(clonedFunction);
    transformer.addFunction(functionContext1);
    // When
    final ElementTransformer clone = transformer.clone();
    // Then
    assertNotSame(transformer, clone);
    assertEquals(1, clone.getFunctions().size());
    final ConsumerProducerFunctionContext<String, TransformFunction> resultClonedFunction = clone.getFunctions().get(0);
    assertEquals(1, resultClonedFunction.getSelection().size());
    assertEquals(reference1, resultClonedFunction.getSelection().get(0));
    assertEquals(1, resultClonedFunction.getProjection().size());
    assertEquals(reference2, resultClonedFunction.getProjection().get(0));
    assertNotSame(functionContext1, resultClonedFunction);
    assertNotSame(function, resultClonedFunction.getFunction());
    assertSame(clonedFunction, resultClonedFunction.getFunction());
}
Also used : TransformFunction(uk.gov.gchq.gaffer.function.TransformFunction) Test(org.junit.Test)

Example 5 with TransformFunction

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

the class Transformer method transform.

/**
     * Transform an input {@link uk.gov.gchq.gaffer.function.Tuple} using {@link uk.gov.gchq.gaffer.function.TransformFunction}s.
     *
     * @param tuple {@link uk.gov.gchq.gaffer.function.Tuple} to be transformed.
     */
public void transform(final Tuple<R> tuple) {
    if (functions == null) {
        return;
    }
    for (final ConsumerProducerFunctionContext<R, TransformFunction> functionContext : functions) {
        TransformFunction function = functionContext.getFunction();
        Object[] selection = functionContext.select(tuple);
        Object[] result = function.transform(selection);
        functionContext.project(tuple, result);
    }
}
Also used : TransformFunction(uk.gov.gchq.gaffer.function.TransformFunction)

Aggregations

TransformFunction (uk.gov.gchq.gaffer.function.TransformFunction)7 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)1 Element (uk.gov.gchq.gaffer.data.element.Element)1 ElementTuple (uk.gov.gchq.gaffer.data.element.ElementTuple)1 IdentifierType (uk.gov.gchq.gaffer.data.element.IdentifierType)1 ConsumerProducerFunctionContext (uk.gov.gchq.gaffer.function.context.ConsumerProducerFunctionContext)1