Search in sources :

Example 6 with Map

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

the class JoinIT method shouldLeftSideOuterJoinUsingKeyFunctionMatch.

@Test
public void shouldLeftSideOuterJoinUsingKeyFunctionMatch() throws OperationException {
    // Given
    final Map map = new Map.Builder<>().input(Lists.newArrayList(4L, 1L, 2L)).first(new Identity()).build();
    final ArrayList<Entity> input = Lists.newArrayList(getJoinEntity(TestGroups.ENTITY, 4), getJoinEntity(TestGroups.ENTITY_2, 4), getJoinEntity(TestGroups.ENTITY_3, 2), getJoinEntity(TestGroups.ENTITY_4, 5), getJoinEntity(TestGroups.ENTITY_4, 6), getJoinEntity(TestGroups.ENTITY_5, 5));
    final Join<Object> leftJoin = new Join.Builder<>().flatten(true).matchKey(MatchKey.LEFT).joinType(JoinType.OUTER).operation(map).matchMethod(new KeyFunctionMatch(new ExtractProperty("count"), new ToLong())).input(input).build();
    // When
    final Iterable<? extends MapTuple> results = graph.execute(leftJoin, user);
    final List<java.util.Map> loadedResults = new ArrayList<>();
    results.forEach(e -> loadedResults.add(e.getValues()));
    // Then
    assertThat(loadedResults).hasSize(3);
    assertThat(loadedResults.get(0)).containsEntry(MatchKey.LEFT.name(), getJoinEntity(TestGroups.ENTITY_4, 5)).containsEntry(MatchKey.RIGHT.name(), null);
    assertThat(loadedResults.get(1)).containsEntry(MatchKey.LEFT.name(), getJoinEntity(TestGroups.ENTITY_4, 6)).containsEntry(MatchKey.RIGHT.name(), null);
    assertThat(loadedResults.get(2)).containsEntry(MatchKey.LEFT.name(), getJoinEntity(TestGroups.ENTITY_5, 5)).containsEntry(MatchKey.RIGHT.name(), null);
}
Also used : ToLong(uk.gov.gchq.koryphe.impl.function.ToLong) Entity(uk.gov.gchq.gaffer.data.element.Entity) ArrayList(java.util.ArrayList) Join(uk.gov.gchq.gaffer.operation.impl.join.Join) KeyFunctionMatch(uk.gov.gchq.gaffer.store.operation.handler.join.match.KeyFunctionMatch) ExtractProperty(uk.gov.gchq.gaffer.data.element.function.ExtractProperty) Identity(uk.gov.gchq.koryphe.impl.function.Identity) Map(uk.gov.gchq.gaffer.operation.impl.Map) Test(org.junit.Test)

Example 7 with Map

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

the class WhileScoreResolverTest method shouldGetScoreWithOperationChainAsOperation.

@Test
public void shouldGetScoreWithOperationChainAsOperation() {
    // Given
    final Object input = new EntitySeed(3);
    final int repeats = 3;
    final GetElements getElements = mock(GetElements.class);
    final Map map = mock(Map.class);
    final ToSet toSet = mock(ToSet.class);
    final OperationChain transformChain = mock(OperationChain.class);
    final List<Operation> transformOps = new LinkedList<>();
    transformOps.add(map);
    transformOps.add(toSet);
    given(transformChain.getOperations()).willReturn(transformOps);
    final Conditional conditional = mock(Conditional.class);
    given(conditional.getTransform()).willReturn(transformChain);
    final While operation = new While.Builder<>().input(input).maxRepeats(repeats).conditional(conditional).operation(getElements).build();
    final LinkedHashMap<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
    opScores.put(Operation.class, 1);
    opScores.put(Map.class, 3);
    opScores.put(ToSet.class, 1);
    opScores.put(GetElements.class, 2);
    final WhileScoreResolver resolver = new WhileScoreResolver();
    final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(opScores);
    // When
    final int score = resolver.getScore(operation, defaultResolver);
    // Then
    assertEquals(18, score);
}
Also used : GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) Operation(uk.gov.gchq.gaffer.operation.Operation) While(uk.gov.gchq.gaffer.operation.impl.While) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) ToSet(uk.gov.gchq.gaffer.operation.impl.output.ToSet) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) LinkedHashMap(java.util.LinkedHashMap) Map(uk.gov.gchq.gaffer.operation.impl.Map) Test(org.junit.jupiter.api.Test)

Example 8 with Map

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

the class MapHandlerTest method shouldProcessWalksWithEdgeExtraction.

@Test
public void shouldProcessWalksWithEdgeExtraction() throws OperationException {
    // Given
    final Iterable<Walk> walks = Arrays.asList(walk, walk1);
    final Map<Iterable<Walk>, Iterable<Edge>> map = new Map.Builder<Iterable<Walk>>().input(walks).first(new IterableFunction.Builder<Walk>().first(new ExtractWalkEdgesFromHop(1)).then(new FirstItem<>()).build()).build();
    final ToVertices toVertices = new ToVertices.Builder().edgeVertices(ToVertices.EdgeVertices.SOURCE).build();
    final ToSet<Object> toSet = new ToSet<>();
    final OperationChain<Set<?>> opChain = new OperationChain.Builder().first(map).then(toVertices).then(toSet).build();
    final OperationChainValidator opChainValidator = mock(OperationChainValidator.class);
    final List<OperationChainOptimiser> opChainOptimisers = Collections.emptyList();
    given(opChainValidator.validate(any(), any(), any())).willReturn(new ValidationResult());
    final OperationChainHandler<Set<?>> opChainHandler = new OperationChainHandler<>(opChainValidator, opChainOptimisers);
    given(store.handleOperation(map, context)).willReturn(Arrays.asList(EDGE_BC, EDGE_BD));
    given(store.handleOperation(toVertices, context)).willReturn(Arrays.asList("B", "B"));
    given(store.handleOperation(toSet, context)).willReturn(Sets.newHashSet("B", "B"));
    // When
    final Iterable<?> results = opChainHandler.doOperation(opChain, context, store);
    // Then
    assertThat((Iterable<String>) results).contains("B");
}
Also used : Walk(uk.gov.gchq.gaffer.data.graph.Walk) ToSet(uk.gov.gchq.gaffer.operation.impl.output.ToSet) Set(java.util.Set) ToVertices(uk.gov.gchq.gaffer.operation.impl.output.ToVertices) OperationChainValidator(uk.gov.gchq.gaffer.store.operation.OperationChainValidator) ValidationResult(uk.gov.gchq.koryphe.ValidationResult) OperationChainOptimiser(uk.gov.gchq.gaffer.store.optimiser.OperationChainOptimiser) ExtractWalkEdgesFromHop(uk.gov.gchq.gaffer.data.graph.function.walk.ExtractWalkEdgesFromHop) ToSet(uk.gov.gchq.gaffer.operation.impl.output.ToSet) IterableFunction(uk.gov.gchq.koryphe.impl.function.IterableFunction) FirstItem(uk.gov.gchq.koryphe.impl.function.FirstItem) Map(uk.gov.gchq.gaffer.operation.impl.Map) Test(org.junit.jupiter.api.Test)

Example 9 with Map

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

the class MapHandlerTest method shouldProcessWalksInOperationChain.

@Test
public void shouldProcessWalksInOperationChain() throws OperationException {
    // Given
    final Iterable<Iterable<Set<Edge>>> walks = Arrays.asList(walk, walk1);
    final Map<Iterable<Iterable<Set<Edge>>>, Iterable<Edge>> map = new Map.Builder<Iterable<Iterable<Set<Edge>>>>().input(walks).first(new IterableFunction.Builder<Iterable<Set<Edge>>>().first(new FirstItem<>()).then(new FirstItem<>()).build()).build();
    final ToVertices toVertices = new ToVertices.Builder().edgeVertices(ToVertices.EdgeVertices.SOURCE).build();
    final ToSet<Object> toSet = new ToSet<>();
    final OperationChain<Set<?>> opChain = new OperationChain.Builder().first(map).then(toVertices).then(toSet).build();
    final OperationChainValidator opChainValidator = mock(OperationChainValidator.class);
    final List<OperationChainOptimiser> opChainOptimisers = Collections.emptyList();
    given(opChainValidator.validate(any(), any(), any())).willReturn(new ValidationResult());
    final OperationChainHandler<Set<?>> opChainHandler = new OperationChainHandler<>(opChainValidator, opChainOptimisers);
    given(store.handleOperation(map, context)).willReturn(Arrays.asList(EDGE_AB, EDGE_CB));
    given(store.handleOperation(toVertices, context)).willReturn(Arrays.asList("A", "C"));
    given(store.handleOperation(toSet, context)).willReturn(Sets.newHashSet("A", "C"));
    // When
    final Iterable<?> results = opChainHandler.doOperation(opChain, context, store);
    // Then
    assertThat((Iterable<String>) results).containsOnly("A", "C");
}
Also used : ToSet(uk.gov.gchq.gaffer.operation.impl.output.ToSet) Set(java.util.Set) ToVertices(uk.gov.gchq.gaffer.operation.impl.output.ToVertices) OperationChainValidator(uk.gov.gchq.gaffer.store.operation.OperationChainValidator) ValidationResult(uk.gov.gchq.koryphe.ValidationResult) OperationChainOptimiser(uk.gov.gchq.gaffer.store.optimiser.OperationChainOptimiser) ToSet(uk.gov.gchq.gaffer.operation.impl.output.ToSet) IterableFunction(uk.gov.gchq.koryphe.impl.function.IterableFunction) FirstItem(uk.gov.gchq.koryphe.impl.function.FirstItem) Edge(uk.gov.gchq.gaffer.data.element.Edge) Map(uk.gov.gchq.gaffer.operation.impl.Map) Test(org.junit.jupiter.api.Test)

Example 10 with Map

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

the class MapHandlerTest method shouldMapSingleObject.

@Test
public void shouldMapSingleObject() throws OperationException {
    // Given
    final MapHandler<Integer, String> handler = new MapHandler<>();
    final Map<Integer, String> operation = new Map.Builder<Integer>().input(7).first(Object::toString).build();
    // When
    final String result = handler.doOperation(operation, context, store);
    // Then
    assertNotNull(result);
    assertEquals("7", result);
}
Also used : ToString(uk.gov.gchq.koryphe.impl.function.ToString) Map(uk.gov.gchq.gaffer.operation.impl.Map) Test(org.junit.jupiter.api.Test)

Aggregations

Map (uk.gov.gchq.gaffer.operation.impl.Map)12 Test (org.junit.jupiter.api.Test)9 ArrayList (java.util.ArrayList)5 Entity (uk.gov.gchq.gaffer.data.element.Entity)4 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)4 LinkedHashMap (java.util.LinkedHashMap)3 Test (org.junit.Test)3 ExtractProperty (uk.gov.gchq.gaffer.data.element.function.ExtractProperty)3 Operation (uk.gov.gchq.gaffer.operation.Operation)3 Join (uk.gov.gchq.gaffer.operation.impl.join.Join)3 ToSet (uk.gov.gchq.gaffer.operation.impl.output.ToSet)3 Conditional (uk.gov.gchq.gaffer.operation.util.Conditional)3 ToString (uk.gov.gchq.koryphe.impl.function.ToString)3 LinkedList (java.util.LinkedList)2 Set (java.util.Set)2 Edge (uk.gov.gchq.gaffer.data.element.Edge)2 If (uk.gov.gchq.gaffer.operation.impl.If)2 GetAdjacentIds (uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds)2 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)2 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)2