Search in sources :

Example 1 with TupleAdaptedFunction

use of uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunction in project Gaffer by gchq.

the class ElementTransformerTest method shouldBuildTransformer.

@Test
public void shouldBuildTransformer() {
    // Given
    final String property1 = "property 1";
    final String property2a = "property 2a";
    final String property2b = "property 2b";
    final IdentifierType identifier3 = IdentifierType.SOURCE;
    final String property1Proj = "property 1 proj";
    final String property2aProj = "property 2a proj";
    final String property2bProj = "property 2b proj";
    final IdentifierType identifier3Proj = IdentifierType.DESTINATION;
    final Function func1 = mock(Function.class);
    final Function func2 = mock(Function.class);
    final Function func3 = mock(Function.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(property2a, property2b).execute(func2).project(property2aProj, property2bProj).select(identifier3.name()).execute(func3).project(identifier3Proj.name()).build();
    // Then
    int i = 0;
    TupleAdaptedFunction<String, ?, ?> context = transformer.getComponents().get(i++);
    assertThat(context.getSelection()).hasSize(1);
    assertEquals(property1, context.getSelection()[0]);
    assertSame(func1, context.getFunction());
    assertThat(context.getProjection()).hasSize(1);
    assertEquals(property1Proj, context.getProjection()[0]);
    context = transformer.getComponents().get(i++);
    assertThat(context.getSelection()).hasSize(2);
    assertEquals(property2a, context.getSelection()[0]);
    assertEquals(property2b, context.getSelection()[1]);
    assertSame(func2, context.getFunction());
    assertThat(context.getProjection()).hasSize(2);
    assertEquals(property2aProj, context.getProjection()[0]);
    assertEquals(property2bProj, context.getProjection()[1]);
    context = transformer.getComponents().get(i++);
    assertSame(func3, context.getFunction());
    assertThat(context.getSelection()).hasSize(1);
    assertEquals(identifier3.name(), context.getSelection()[0]);
    assertThat(context.getProjection()).hasSize(1);
    assertEquals(identifier3Proj.name(), context.getProjection()[0]);
    assertEquals(i, transformer.getComponents().size());
}
Also used : Function(java.util.function.Function) TupleAdaptedFunction(uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunction) IdentifierType(uk.gov.gchq.gaffer.data.element.IdentifierType) FunctionTest(uk.gov.gchq.koryphe.function.FunctionTest) Test(org.junit.jupiter.api.Test)

Example 2 with TupleAdaptedFunction

use of uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunction in project Gaffer by gchq.

the class FunctionAuthoriserTest method shouldNotAllowGetElementsOperationWithUnauthorisedFunctionsInTheView.

@Test
public void shouldNotAllowGetElementsOperationWithUnauthorisedFunctionsInTheView() {
    final OperationChain<CloseableIterable<? extends Element>> viewOperation = new OperationChain.Builder().first(new GetElements.Builder().view(new View.Builder().globalElements(new GlobalViewElementDefinition.Builder().transformFunctions(Lists.newArrayList(new TupleAdaptedFunction(new String[] { "input" }, new DivideBy(6), new String[] { "output" }))).build()).build()).build()).build();
    FunctionAuthoriser functionAuthoriser = new FunctionAuthoriser();
    // When
    functionAuthoriser.setUnauthorisedFunctions(Lists.newArrayList(DivideBy.class));
    // Then
    assertThatExceptionOfType(UnauthorisedException.class).isThrownBy(() -> functionAuthoriser.preExecute(viewOperation, new Context())).withMessage("Operation chain contained an unauthorised function: uk.gov.gchq.koryphe.impl.function.DivideBy");
}
Also used : DivideBy(uk.gov.gchq.koryphe.impl.function.DivideBy) Context(uk.gov.gchq.gaffer.store.Context) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) Element(uk.gov.gchq.gaffer.data.element.Element) TupleAdaptedFunction(uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunction) ToString(uk.gov.gchq.koryphe.impl.function.ToString) Test(org.junit.jupiter.api.Test)

Example 3 with TupleAdaptedFunction

use of uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunction in project Gaffer by gchq.

the class ViewElementDefinitionTest method shouldJsonSerialiseAndDeserialise.

@Test
public void shouldJsonSerialiseAndDeserialise() throws SerialisationException {
    // Given
    final ViewElementDefinition elementDef = new ViewElementDefinition.Builder().transientProperty(TestPropertyNames.PROP_1, String.class).transientProperty(TestPropertyNames.PROP_2, String.class).properties(TestPropertyNames.COUNT, TestPropertyNames.DATE).preAggregationFilter(new ElementFilter.Builder().select(TestPropertyNames.COUNT).execute(new IsMoreThan(5)).build()).aggregator(new ElementAggregator.Builder().select(TestPropertyNames.COUNT).execute(new Max()).build()).postAggregationFilter(new ElementFilter.Builder().select(TestPropertyNames.COUNT).execute(new IsLessThan(10)).build()).transformer(new ElementTransformer.Builder().select(TestPropertyNames.COUNT).execute(new TestTransform()).project(TestPropertyNames.PROP_1).build()).postTransformFilter(new ElementFilter.Builder().select(TestPropertyNames.PROP_1).execute(new IsEqual("9")).build()).build();
    // When
    final byte[] json = JSONSerialiser.serialise(elementDef, true);
    final ViewElementDefinition deserialisedElementDef = JSONSerialiser.deserialise(json, ViewElementDefinition.class);
    assertEquals(Sets.newHashSet(TestPropertyNames.COUNT, TestPropertyNames.DATE), deserialisedElementDef.getProperties());
    assertNull(deserialisedElementDef.getExcludeProperties());
    final List<TupleAdaptedPredicate<String, ?>> preFilterComponents = deserialisedElementDef.getPreAggregationFilter().getComponents();
    assertThat(preFilterComponents).hasSize(1);
    assertArrayEquals(new String[] { TestPropertyNames.COUNT }, preFilterComponents.get(0).getSelection());
    assertEquals(new IsMoreThan(5), preFilterComponents.get(0).getPredicate());
    final List<TupleAdaptedBinaryOperator<String, ?>> aggComponents = deserialisedElementDef.getAggregator().getComponents();
    assertThat(aggComponents).hasSize(1);
    assertArrayEquals(new String[] { TestPropertyNames.COUNT }, aggComponents.get(0).getSelection());
    assertEquals(new Max(), aggComponents.get(0).getBinaryOperator());
    final List<TupleAdaptedPredicate<String, ?>> postFilterComponents = deserialisedElementDef.getPostAggregationFilter().getComponents();
    assertThat(postFilterComponents).hasSize(1);
    assertArrayEquals(new String[] { TestPropertyNames.COUNT }, postFilterComponents.get(0).getSelection());
    assertEquals(new IsLessThan(10), postFilterComponents.get(0).getPredicate());
    final List<TupleAdaptedFunction<String, ?, ?>> transformComponents = deserialisedElementDef.getTransformer().getComponents();
    assertThat(transformComponents).hasSize(1);
    assertArrayEquals(new String[] { TestPropertyNames.COUNT }, transformComponents.get(0).getSelection());
    assertEquals(new TestTransform(), transformComponents.get(0).getFunction());
    assertArrayEquals(new String[] { TestPropertyNames.PROP_1 }, transformComponents.get(0).getProjection());
    final List<TupleAdaptedPredicate<String, ?>> postTransformFilterComponents = deserialisedElementDef.getPostTransformFilter().getComponents();
    assertThat(postTransformFilterComponents).hasSize(1);
    assertArrayEquals(new String[] { TestPropertyNames.PROP_1 }, postTransformFilterComponents.get(0).getSelection());
    assertEquals(new IsEqual("9"), postTransformFilterComponents.get(0).getPredicate());
}
Also used : TupleAdaptedPredicate(uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate) Max(uk.gov.gchq.koryphe.impl.binaryoperator.Max) ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) TupleAdaptedFunction(uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunction) IsEqual(uk.gov.gchq.koryphe.impl.predicate.IsEqual) IsLessThan(uk.gov.gchq.koryphe.impl.predicate.IsLessThan) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) TupleAdaptedBinaryOperator(uk.gov.gchq.koryphe.tuple.binaryoperator.TupleAdaptedBinaryOperator) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Test(org.junit.jupiter.api.Test)

Example 4 with TupleAdaptedFunction

use of uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunction in project Gaffer by gchq.

the class TransformValidator method validateTransformPropertyClasses.

/**
 * Validates that the functions to be executed are assignable to the corresponding properties
 *
 * @param elementDef  The SchemaElementDefinition to validate against.
 * @param transformer The ElementFilter to be validated against
 * @return ValidationResult of the validation
 */
private ValidationResult validateTransformPropertyClasses(final SchemaElementDefinition elementDef, final ElementTransformer transformer) {
    final ValidationResult result = new ValidationResult();
    if (null != elementDef) {
        final List<TupleAdaptedFunction<String, ?, ?>> components = transformer.getComponents();
        for (final TupleAdaptedFunction<String, ?, ?> component : components) {
            final Map<String, String> properties = elementDef.getPropertyMap();
            if (!properties.isEmpty()) {
                if (null == component.getFunction()) {
                    result.addError(transformer.getClass().getSimpleName());
                } else {
                    final Class[] selectionClasses = getTypeClasses(component.getSelection(), elementDef);
                    if (!ArrayUtils.contains(selectionClasses, null)) {
                        final Signature inputSig = Signature.getInputSignature(component.getFunction());
                        result.add(inputSig.assignable(selectionClasses));
                    }
                    final Class[] projectionClasses = getTypeClasses(component.getProjection(), elementDef);
                    if (!ArrayUtils.contains(projectionClasses, null)) {
                        final Signature outputSig = Signature.getOutputSignature(component.getFunction());
                        result.add(outputSig.assignable(projectionClasses));
                    }
                }
            }
        }
    }
    return result;
}
Also used : Signature(uk.gov.gchq.koryphe.signature.Signature) TupleAdaptedFunction(uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunction) ValidationResult(uk.gov.gchq.koryphe.ValidationResult)

Example 5 with TupleAdaptedFunction

use of uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunction in project Gaffer by gchq.

the class GetAllElementsIT method shouldAllowBiFunctionInView.

@Test
@TraitRequirement({ StoreTrait.TRANSFORMATION })
public void shouldAllowBiFunctionInView() throws OperationException {
    final Map<String, Class<?>> transientProperties = new HashMap<>();
    transientProperties.put("propLong", Long.class);
    transientProperties.put("combined", Long.class);
    final List<TupleAdaptedFunction<String, ?, ?>> transformFunctions = new ArrayList<>();
    final TupleAdaptedFunction<String, Integer, Long> convertToLong = new TupleAdaptedFunction<>();
    convertToLong.setSelection(new String[] { TestPropertyNames.INT });
    convertToLong.setFunction((Function) new ToLong());
    convertToLong.setProjection(new String[] { "propLong" });
    final TupleAdaptedFunction<String, Integer, Long> sum = new TupleAdaptedFunction<>();
    sum.setSelection(new String[] { "propLong", TestPropertyNames.COUNT });
    sum.setFunction(new ApplyBiFunction(new Sum()));
    sum.setProjection(new String[] { "combined" });
    transformFunctions.add(convertToLong);
    transformFunctions.add(sum);
    final GetAllElements get = new GetAllElements.Builder().view(new View.Builder().edge(TestGroups.EDGE, new ViewElementDefinition.Builder().transientProperties(transientProperties).addTransformFunctions(transformFunctions).build()).build()).build();
    final CloseableIterable<? extends Element> results = graph.execute(get, user);
    for (final Element result : results) {
        final Long expectedResult = (Long) result.getProperty("propLong") + (Long) result.getProperty(TestPropertyNames.COUNT);
        final Long combined = (Long) result.getProperty("combined");
        assertThat(combined).isEqualTo(expectedResult);
    }
}
Also used : ToLong(uk.gov.gchq.koryphe.impl.function.ToLong) HashMap(java.util.HashMap) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) ApplyBiFunction(uk.gov.gchq.koryphe.impl.function.ApplyBiFunction) TupleAdaptedFunction(uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunction) Sum(uk.gov.gchq.koryphe.impl.binaryoperator.Sum) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) ToLong(uk.gov.gchq.koryphe.impl.function.ToLong) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) Test(org.junit.Test) TraitRequirement(uk.gov.gchq.gaffer.integration.TraitRequirement)

Aggregations

TupleAdaptedFunction (uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunction)5 Test (org.junit.jupiter.api.Test)3 Element (uk.gov.gchq.gaffer.data.element.Element)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Function (java.util.function.Function)1 Test (org.junit.Test)1 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)1 IdentifierType (uk.gov.gchq.gaffer.data.element.IdentifierType)1 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)1 ElementTransformer (uk.gov.gchq.gaffer.data.element.function.ElementTransformer)1 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)1 TraitRequirement (uk.gov.gchq.gaffer.integration.TraitRequirement)1 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)1 Context (uk.gov.gchq.gaffer.store.Context)1 ValidationResult (uk.gov.gchq.koryphe.ValidationResult)1 FunctionTest (uk.gov.gchq.koryphe.function.FunctionTest)1 Max (uk.gov.gchq.koryphe.impl.binaryoperator.Max)1 Sum (uk.gov.gchq.koryphe.impl.binaryoperator.Sum)1 ApplyBiFunction (uk.gov.gchq.koryphe.impl.function.ApplyBiFunction)1