Search in sources :

Example 1 with Map

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

the class IfScoreResolverTest method shouldGetScoreForNestedOperations.

@Test
public void shouldGetScoreForNestedOperations() {
    // Given
    final Map map = mock(Map.class);
    final Conditional conditional = mock(Conditional.class);
    given(conditional.getTransform()).willReturn(map);
    final GetWalks getWalks = mock(GetWalks.class);
    given(getWalks.getOperations()).willReturn(Collections.singletonList(new OperationChain<>(new GetAdjacentIds(), new GetAdjacentIds())));
    final GetAllElements getAllElements = new GetAllElements();
    final If operation = new If.Builder<>().conditional(conditional).then(getWalks).otherwise(getAllElements).build();
    final LinkedHashMap<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
    opScores.put(Operation.class, 1);
    opScores.put(Map.class, 3);
    opScores.put(GetAdjacentIds.class, 2);
    opScores.put(GetAllElements.class, 3);
    final IfScoreResolver resolver = new IfScoreResolver();
    final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(opScores);
    // When
    final int score = resolver.getScore(operation, defaultResolver);
    // Then
    assertEquals(7, score);
}
Also used : GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) GetWalks(uk.gov.gchq.gaffer.operation.impl.GetWalks) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) Operation(uk.gov.gchq.gaffer.operation.Operation) LinkedHashMap(java.util.LinkedHashMap) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) ToMap(uk.gov.gchq.gaffer.operation.impl.output.ToMap) LinkedHashMap(java.util.LinkedHashMap) Map(uk.gov.gchq.gaffer.operation.impl.Map) If(uk.gov.gchq.gaffer.operation.impl.If) Test(org.junit.jupiter.api.Test)

Example 2 with Map

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

the class IfScoreResolverTest method shouldGetScoreWithOperationChainAsAnOperation.

@Test
public void shouldGetScoreWithOperationChainAsAnOperation() {
    // Given
    final GetElements getElements = mock(GetElements.class);
    final ToMap toMap = mock(ToMap.class);
    final Map map = mock(Map.class);
    final OperationChain conditionalChain = mock(OperationChain.class);
    final List<Operation> conditionalOps = new LinkedList<>();
    conditionalOps.add(getElements);
    conditionalOps.add(toMap);
    conditionalOps.add(map);
    given(conditionalChain.getOperations()).willReturn(conditionalOps);
    final Conditional conditional = mock(Conditional.class);
    given(conditional.getTransform()).willReturn(conditionalChain);
    final GetAdjacentIds getAdjacentIds = mock(GetAdjacentIds.class);
    final GetAllElements getAllElements = mock(GetAllElements.class);
    final If operation = new If.Builder<>().conditional(conditional).then(getAdjacentIds).otherwise(getAllElements).build();
    final LinkedHashMap<Class<? extends Operation>, Integer> opScores = new LinkedHashMap<>();
    opScores.put(Operation.class, 1);
    opScores.put(GetElements.class, 2);
    opScores.put(ToMap.class, 2);
    opScores.put(Map.class, 3);
    opScores.put(GetAdjacentIds.class, 3);
    opScores.put(GetAllElements.class, 4);
    final IfScoreResolver resolver = new IfScoreResolver();
    final DefaultScoreResolver defaultResolver = new DefaultScoreResolver(opScores);
    // When
    final int score = resolver.getScore(operation, defaultResolver);
    // Then
    assertEquals(11, score);
}
Also used : GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) Operation(uk.gov.gchq.gaffer.operation.Operation) LinkedList(java.util.LinkedList) LinkedHashMap(java.util.LinkedHashMap) ToMap(uk.gov.gchq.gaffer.operation.impl.output.ToMap) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) GetAllElements(uk.gov.gchq.gaffer.operation.impl.get.GetAllElements) ToMap(uk.gov.gchq.gaffer.operation.impl.output.ToMap) LinkedHashMap(java.util.LinkedHashMap) Map(uk.gov.gchq.gaffer.operation.impl.Map) If(uk.gov.gchq.gaffer.operation.impl.If) Test(org.junit.jupiter.api.Test)

Example 3 with Map

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

the class RowToElementGeneratorTest method getElements.

private List<Element> getElements() {
    final List<String> names = Lists.newArrayList("Alice", "Bob", "Charlie", "David");
    final List<Element> elements = new ArrayList<>();
    final List<Entity> entities = names.stream().map(n -> {
        return new Entity.Builder().vertex(n.substring(0, 1).toLowerCase()).group(TestGroups.ENTITY).property("fullname", n).build();
    }).collect(Collectors.toList());
    final Edge edge1 = new Edge.Builder().source("a").dest("b").directed(true).group(TestGroups.EDGE).property("type", "friend").build();
    final Edge edge2 = new Edge.Builder().source("b").dest("c").directed(false).group(TestGroups.EDGE).property("type", "follow").build();
    final Edge edge3 = new Edge.Builder().source("a").dest("c").directed(true).group(TestGroups.EDGE).property("type", "follow").build();
    final Edge edge4 = new Edge.Builder().source("c").dest("a").directed(true).group(TestGroups.EDGE).property("type", "follow").build();
    final Edge edge5 = new Edge.Builder().source("d").dest("c").directed(true).group(TestGroups.EDGE).property("type", "follow").build();
    final List<Edge> edges = Lists.newArrayList(edge1, edge2, edge3, edge4, edge5);
    elements.addAll(entities);
    elements.addAll(edges);
    return elements;
}
Also used : ElementUtil(uk.gov.gchq.gaffer.data.util.ElementUtil) BeforeEach(org.junit.jupiter.api.BeforeEach) User(uk.gov.gchq.gaffer.user.User) GenerateElements(uk.gov.gchq.gaffer.operation.impl.generate.GenerateElements) GraphConfig(uk.gov.gchq.gaffer.graph.GraphConfig) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) SparkSessionProvider(uk.gov.gchq.gaffer.spark.SparkSessionProvider) Lists(com.google.common.collect.Lists) Graph(uk.gov.gchq.gaffer.graph.Graph) GetGraphFrameOfElements(uk.gov.gchq.gaffer.spark.operation.graphframe.GetGraphFrameOfElements) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) Edge(uk.gov.gchq.gaffer.data.element.Edge) RowToElementGenerator(uk.gov.gchq.gaffer.spark.data.generator.RowToElementGenerator) TestGroups(uk.gov.gchq.gaffer.commonutil.TestGroups) Map(uk.gov.gchq.gaffer.operation.impl.Map) StreamUtil(uk.gov.gchq.gaffer.commonutil.StreamUtil) AccumuloProperties(uk.gov.gchq.gaffer.accumulostore.AccumuloProperties) Row(org.apache.spark.sql.Row) Entity(uk.gov.gchq.gaffer.data.element.Entity) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) GraphFrameToIterableRow(uk.gov.gchq.gaffer.spark.function.GraphFrameToIterableRow) AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) OperationException(uk.gov.gchq.gaffer.operation.OperationException) Entity(uk.gov.gchq.gaffer.data.element.Entity) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) Edge(uk.gov.gchq.gaffer.data.element.Edge)

Example 4 with Map

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

the class MapHandlerTest method shouldBuildWithInvalidArgumentsAndFailExecution.

@Test
public void shouldBuildWithInvalidArgumentsAndFailExecution() throws OperationException {
    final List<Function> functions = new ArrayList<>();
    final Function<Long, Double> func = Double::longBitsToDouble;
    final Function<String, Integer> func1 = Integer::valueOf;
    functions.add(new ToString());
    functions.add(func);
    functions.add(func1);
    final Map map = new Map();
    map.setInput(3);
    map.setFunctions(functions);
    final MapHandler handler = new MapHandler();
    // When / Then
    assertThatExceptionOfType(OperationException.class).isThrownBy(() -> handler.doOperation(map, context, store)).withMessage("The input/output types of the functions were incompatible");
}
Also used : Function(java.util.function.Function) IterableFunction(uk.gov.gchq.koryphe.impl.function.IterableFunction) ArrayList(java.util.ArrayList) ToString(uk.gov.gchq.koryphe.impl.function.ToString) ToString(uk.gov.gchq.koryphe.impl.function.ToString) Map(uk.gov.gchq.gaffer.operation.impl.Map) Test(org.junit.jupiter.api.Test)

Example 5 with Map

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

the class MapHandlerTest method shouldMapMultipleObjectsAtOnce.

@Test
public void shouldMapMultipleObjectsAtOnce() throws OperationException {
    // Given
    final MapHandler<Iterable<Integer>, String> handler = new MapHandler<>();
    final Map<Iterable<Integer>, String> operation = new Map.Builder<Iterable<Integer>>().input(Arrays.asList(1, 2)).first(Object::toString).build();
    // When
    final String result = handler.doOperation(operation, context, store);
    // Then
    assertNotNull(result);
    assertEquals("[1, 2]", 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