Search in sources :

Example 1 with Transform

use of uk.gov.gchq.gaffer.operation.impl.function.Transform in project gaffer-doc by gchq.

the class TransformExample method transformACountPropertyIntoACountStringPropertyOnlyForEdgesOfTypeEdge.

public void transformACountPropertyIntoACountStringPropertyOnlyForEdgesOfTypeEdge() {
    // ---------------------------------------------------------
    final Transform transform = new Transform.Builder().edge("edge", new ElementTransformer.Builder().select("count").execute(new ToString()).project("countString").build()).build();
    // ---------------------------------------------------------
    showExample(transform, null);
}
Also used : Transform(uk.gov.gchq.gaffer.operation.impl.function.Transform) ToString(uk.gov.gchq.koryphe.impl.function.ToString)

Example 2 with Transform

use of uk.gov.gchq.gaffer.operation.impl.function.Transform in project Gaffer by gchq.

the class ViewMigration method createMigrationOps.

public static List<Operation> createMigrationOps(final boolean aggregateAfter, final Iterable<ViewMigration> views) {
    final ViewMigration viewMig = new ViewMigration(aggregateAfter);
    for (final ViewMigration view : views) {
        viewMig.edgesPostAggregationFilter.putAll(view.edgesPostAggregationFilter);
        viewMig.entitiesPostAggregationFilter.putAll(view.entitiesPostAggregationFilter);
        viewMig.edgesPostTransformFilter.putAll(view.edgesPostTransformFilter);
        viewMig.entitiesPostTransformFilter.putAll(view.entitiesPostTransformFilter);
        viewMig.edgesTransformer.putAll(view.edgesTransformer);
        viewMig.entitiesTransformer.putAll(view.entitiesTransformer);
    }
    final List<Operation> updatedOps = new ArrayList<>();
    if (aggregateAfter) {
        final Aggregate aggregate = new Aggregate();
        updatedOps.add(aggregate);
        if (!viewMig.entitiesPostAggregationFilter.isEmpty() || !viewMig.edgesPostAggregationFilter.isEmpty()) {
            final Filter postAggregationFilter = new Filter.Builder().entities(viewMig.entitiesPostAggregationFilter).edges(viewMig.edgesPostAggregationFilter).build();
            updatedOps.add(postAggregationFilter);
        }
        if (!viewMig.entitiesTransformer.isEmpty() || !viewMig.edgesTransformer.isEmpty()) {
            final Transform transformFunction = new Transform.Builder().entities(viewMig.entitiesTransformer).edges(viewMig.edgesTransformer).build();
            updatedOps.add(transformFunction);
        }
        if (!viewMig.edgesPostTransformFilter.isEmpty() || !viewMig.entitiesPostTransformFilter.isEmpty()) {
            final Filter postTransformFilter = new Filter.Builder().entities(viewMig.entitiesPostTransformFilter).edges(viewMig.edgesPostTransformFilter).build();
            updatedOps.add(postTransformFilter);
        }
    }
    return updatedOps;
}
Also used : Filter(uk.gov.gchq.gaffer.operation.impl.function.Filter) TransformAndFilter(uk.gov.gchq.gaffer.graph.hook.migrate.predicate.TransformAndFilter) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ArrayList(java.util.ArrayList) Operation(uk.gov.gchq.gaffer.operation.Operation) Aggregate(uk.gov.gchq.gaffer.operation.impl.function.Aggregate) Transform(uk.gov.gchq.gaffer.operation.impl.function.Transform)

Example 3 with Transform

use of uk.gov.gchq.gaffer.operation.impl.function.Transform in project Gaffer by gchq.

the class ScoreOperationChainHandlerTest method shouldCorrectlyResolveScoreForNestedOperationWithNullOperationList.

@Test
public void shouldCorrectlyResolveScoreForNestedOperationWithNullOperationList() throws OperationException {
    // Given
    final ScoreOperationChainHandler handler = new ScoreOperationChainHandler();
    final Map<Class<? extends Operation>, ScoreResolver> resolvers = new HashMap<>();
    final ScoreResolver scoreResolver = mock(NamedOperationScoreResolver.class);
    final Context context = mock(Context.class);
    final Store store = mock(Store.class);
    final User user = mock(User.class);
    final ScoreOperationChain scoreOperationChain = mock(ScoreOperationChain.class);
    final StoreProperties properties = mock(StoreProperties.class);
    final GetAllElements op1 = mock(GetAllElements.class);
    final Transform op2 = mock(Transform.class);
    final Map<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
    opScores.put(GetAllElements.class, 2);
    opScores.put(Transform.class, 1);
    handler.setOpScores(opScores);
    final String opName = "namedOp";
    final NamedOperation<Iterable<? extends Element>, Iterable<? extends Element>> namedOp = mock(NamedOperation.class);
    namedOp.setOperationName(opName);
    resolvers.put(namedOp.getClass(), scoreResolver);
    handler.setScoreResolvers(resolvers);
    given(scoreResolver.getScore(eq(namedOp), any())).willReturn(3);
    final List<? extends Operation> opList = null;
    final OperationChain nestedOpChain = new OperationChain(opList);
    final OperationChain opChain = new OperationChain(Arrays.asList(op1, op2, nestedOpChain, namedOp));
    given(scoreOperationChain.getOperationChain()).willReturn(opChain);
    given(context.getUser()).willReturn(user);
    Set<String> opAuths = new HashSet<>();
    opAuths.add("TEST_USER");
    given(user.getOpAuths()).willReturn(opAuths);
    given(store.getProperties()).willReturn(properties);
    // When
    final Object result = handler.doOperation(new ScoreOperationChain.Builder().operationChain(opChain).build(), context, store);
    // Then
    assertEquals(6, result);
}
Also used : User(uk.gov.gchq.gaffer.user.User) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Element(uk.gov.gchq.gaffer.data.element.Element) Store(uk.gov.gchq.gaffer.store.Store) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) Operation(uk.gov.gchq.gaffer.operation.Operation) ScoreOperationChain(uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain) LinkedHashMap(java.util.LinkedHashMap) NamedOperationScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.named.NamedOperationScoreResolver) DefaultScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.DefaultScoreResolver) ScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.ScoreResolver) IfScoreResolver(uk.gov.gchq.gaffer.store.operation.resolver.IfScoreResolver) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) HashSet(java.util.HashSet) Context(uk.gov.gchq.gaffer.store.Context) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) ScoreOperationChain(uk.gov.gchq.gaffer.operation.impl.ScoreOperationChain) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) Transform(uk.gov.gchq.gaffer.operation.impl.function.Transform) Test(org.junit.jupiter.api.Test)

Example 4 with Transform

use of uk.gov.gchq.gaffer.operation.impl.function.Transform in project Gaffer by gchq.

the class TransformHandlerTest method shouldTransformElementsUsingIdentityFunction.

@Test
public void shouldTransformElementsUsingIdentityFunction() throws OperationException {
    // Given
    given(store.getSchema()).willReturn(schema);
    final Entity entity = new Entity.Builder().group(TestGroups.ENTITY).property(TestPropertyNames.PROP_1, TestPropertyNames.INT).property(TestPropertyNames.PROP_2, TestPropertyNames.STRING).build();
    final Entity entity1 = new Entity.Builder().group(TestGroups.ENTITY).property(TestPropertyNames.PROP_1, TestPropertyNames.INT).build();
    final ElementTransformer transformer = new ElementTransformer.Builder().select(TestPropertyNames.PROP_1).execute(new Identity()).project(TestPropertyNames.PROP_3).build();
    final Entity expectedEntity = new Entity.Builder().group(TestGroups.ENTITY).property(TestPropertyNames.PROP_3, TestPropertyNames.INT).property(TestPropertyNames.PROP_2, TestPropertyNames.STRING).build();
    final Entity expectedEntity1 = new Entity.Builder().group(TestGroups.ENTITY).property(TestPropertyNames.PROP_3, TestPropertyNames.INT).build();
    final Transform transform = new Transform.Builder().input(input).entity(TestGroups.ENTITY, transformer).build();
    input.add(entity);
    input.add(entity1);
    expected.add(expectedEntity);
    expected.add(expectedEntity1);
    // When
    final Iterable<? extends Element> results = handler.doOperation(transform, context, store);
    final List<Element> resultsList = Lists.newArrayList(results);
    // Then
    boolean isSame = false;
    for (int i = 0; i < expected.size(); i++) {
        isSame = expected.get(i).getProperty(TestPropertyNames.PROP_3).equals(resultsList.get(i).getProperty(TestPropertyNames.PROP_3));
    }
    assertTrue(isSame);
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) Element(uk.gov.gchq.gaffer.data.element.Element) Identity(uk.gov.gchq.koryphe.impl.function.Identity) Transform(uk.gov.gchq.gaffer.operation.impl.function.Transform) Test(org.junit.jupiter.api.Test)

Example 5 with Transform

use of uk.gov.gchq.gaffer.operation.impl.function.Transform in project Gaffer by gchq.

the class TransformHandlerTest method shouldTransformElementsUsingInlineFunction.

@Test
public void shouldTransformElementsUsingInlineFunction() throws OperationException {
    // Given
    final Function<String, Integer> function = String::length;
    final Map<String, ElementTransformer> entities = new HashMap<>();
    given(store.getSchema()).willReturn(schema);
    final Entity entity = new Entity.Builder().group(TestGroups.ENTITY).property(TestPropertyNames.PROP_1, "value").property(TestPropertyNames.PROP_2, 1).build();
    final Entity entity1 = new Entity.Builder().group(TestGroups.ENTITY_2).property(TestPropertyNames.PROP_1, "otherValue").property(TestPropertyNames.PROP_2, 3).build();
    final ElementTransformer transformer = new ElementTransformer.Builder().select(TestPropertyNames.PROP_1).execute(function).project(TestPropertyNames.PROP_3).build();
    entities.put(TestGroups.ENTITY, transformer);
    entities.put(TestGroups.ENTITY_2, transformer);
    final Entity expectedEntity = new Entity.Builder().group(TestGroups.ENTITY).property(TestPropertyNames.PROP_3, 5).property(TestPropertyNames.PROP_2, 1).build();
    final Entity expectedEntity1 = new Entity.Builder().group(TestGroups.ENTITY_2).property(TestPropertyNames.PROP_3, 10).property(TestPropertyNames.PROP_2, 3).build();
    final Transform transform = new Transform.Builder().input(input).entities(entities).build();
    input.add(entity);
    input.add(entity1);
    expected.add(expectedEntity);
    expected.add(expectedEntity1);
    // When
    final Iterable<? extends Element> results = handler.doOperation(transform, context, store);
    final List<Element> resultsList = Lists.newArrayList(results);
    // Then
    boolean isSame = false;
    for (int i = 0; i < resultsList.size(); i++) {
        isSame = expected.get(i).getProperty(TestPropertyNames.PROP_3).equals(resultsList.get(i).getProperty(TestPropertyNames.PROP_3));
    }
    assertTrue(isSame);
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) HashMap(java.util.HashMap) ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) Element(uk.gov.gchq.gaffer.data.element.Element) Transform(uk.gov.gchq.gaffer.operation.impl.function.Transform) Test(org.junit.jupiter.api.Test)

Aggregations

Transform (uk.gov.gchq.gaffer.operation.impl.function.Transform)13 Test (org.junit.jupiter.api.Test)11 ElementTransformer (uk.gov.gchq.gaffer.data.element.function.ElementTransformer)7 Entity (uk.gov.gchq.gaffer.data.element.Entity)6 Element (uk.gov.gchq.gaffer.data.element.Element)5 Edge (uk.gov.gchq.gaffer.data.element.Edge)4 Identity (uk.gov.gchq.koryphe.impl.function.Identity)4 HashMap (java.util.HashMap)3 Schema (uk.gov.gchq.gaffer.store.schema.Schema)3 Operation (uk.gov.gchq.gaffer.operation.Operation)2 Context (uk.gov.gchq.gaffer.store.Context)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)1 FederatedStore (uk.gov.gchq.gaffer.federatedstore.FederatedStore)1 FederatedTransformHandler (uk.gov.gchq.gaffer.federatedstore.operation.handler.FederatedTransformHandler)1 TransformAndFilter (uk.gov.gchq.gaffer.graph.hook.migrate.predicate.TransformAndFilter)1 NamedOperation (uk.gov.gchq.gaffer.named.operation.NamedOperation)1 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)1