Search in sources :

Example 1 with ElementTransformer

use of uk.gov.gchq.gaffer.data.element.function.ElementTransformer in project Gaffer by gchq.

the class ViewIT method shouldDeserialiseJsonView.

@Test
public void shouldDeserialiseJsonView() throws IOException {
    // Given
    // When
    View view = loadView();
    // Then
    final ViewElementDefinition edge = view.getEdge(TestGroups.EDGE);
    final ElementTransformer transformer = edge.getTransformer();
    assertNotNull(transformer);
    final List<ConsumerProducerFunctionContext<String, TransformFunction>> contexts = transformer.getFunctions();
    assertEquals(1, contexts.size());
    final List<String> selection = contexts.get(0).getSelection();
    assertEquals(2, selection.size());
    assertEquals(TestPropertyNames.PROP_1, selection.get(0));
    assertEquals(IdentifierType.SOURCE.name(), selection.get(1));
    final List<String> projection = contexts.get(0).getProjection();
    assertEquals(1, projection.size());
    assertEquals(TestPropertyNames.TRANSIENT_1, projection.get(0));
    assertTrue(contexts.get(0).getFunction() instanceof ExampleTransformFunction);
    final ElementFilter postFilter = edge.getPostTransformFilter();
    assertNotNull(postFilter);
    final List<ConsumerFunctionContext<String, FilterFunction>> filterContexts = postFilter.getFunctions();
    assertEquals(1, contexts.size());
    final List<String> postFilterSelection = filterContexts.get(0).getSelection();
    assertEquals(1, postFilterSelection.size());
    assertEquals(TestPropertyNames.TRANSIENT_1, postFilterSelection.get(0));
    assertTrue(filterContexts.get(0).getFunction() instanceof ExampleFilterFunction);
}
Also used : ConsumerProducerFunctionContext(uk.gov.gchq.gaffer.function.context.ConsumerProducerFunctionContext) ConsumerFunctionContext(uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext) ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) ExampleFilterFunction(uk.gov.gchq.gaffer.function.ExampleFilterFunction) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) ExampleTransformFunction(uk.gov.gchq.gaffer.function.ExampleTransformFunction) Test(org.junit.Test)

Example 2 with ElementTransformer

use of uk.gov.gchq.gaffer.data.element.function.ElementTransformer in project Gaffer by gchq.

the class LoadAndQuery4 method run.

public CloseableIterable<Edge> run() throws OperationException {
    // [user] Create a user
    // ---------------------------------------------------------
    final User user = new User("user01");
    // ---------------------------------------------------------
    // [generate] Create some edges from the data file using our data generator class
    // ---------------------------------------------------------
    final List<Element> elements = new ArrayList<>();
    final DataGenerator4 dataGenerator = new DataGenerator4();
    for (final String s : DataUtils.loadData(getData())) {
        elements.add(dataGenerator.getElement(s));
    }
    // ---------------------------------------------------------
    log("Elements generated from the data file.");
    for (final Element element : elements) {
        log("GENERATED_EDGES", element.toString());
    }
    log("");
    // [graph] create a graph using our schema and store properties
    // ---------------------------------------------------------
    final Graph graph = new Graph.Builder().addSchemas(getSchemas()).storeProperties(getStoreProperties()).build();
    // ---------------------------------------------------------
    // [add] add the edges to the graph
    final AddElements addElements = new AddElements.Builder().elements(elements).build();
    graph.execute(addElements, user);
    // ---------------------------------------------------------
    log("The elements have been added.\n");
    // [get simple] get all the edges that contain the vertex "1"
    // ---------------------------------------------------------
    final GetEdges<EntitySeed> getRelatedEdges = new GetEdges.Builder<EntitySeed>().addSeed(new EntitySeed("1")).build();
    final CloseableIterable<Edge> results = graph.execute(getRelatedEdges, user);
    // ---------------------------------------------------------
    log("\nAll edges containing the vertex 1. The counts and 'things' have been aggregated\n");
    for (final Element e : results) {
        log("GET_RELATED_EDGES_RESULT", e.toString());
    }
    // rerun previous query but calculate a mean
    // [transform] Create a mean transient property using an element transformer
    // ---------------------------------------------------------
    final ElementTransformer mean = new ElementTransformer.Builder().select("thing", "count").project("mean").execute(new MeanTransform()).build();
    // ---------------------------------------------------------
    // [get] Add the element transformer to the view and run the query
    // ---------------------------------------------------------
    final View view = new View.Builder().edge("data", new ViewElementDefinition.Builder().transientProperty("mean", Float.class).transformer(mean).build()).build();
    final GetEdges<EntitySeed> getRelatedEdgesWithMean = new GetEdges.Builder<EntitySeed>().addSeed(new EntitySeed("1")).view(view).build();
    final CloseableIterable<Edge> transientResults = graph.execute(getRelatedEdgesWithMean, user);
    // ---------------------------------------------------------
    log("\nWe can add a new property to the edges that is calculated from the aggregated values of other properties\n");
    for (final Element e : transientResults) {
        log("GET_RELATED_ELEMENTS_WITH_MEAN_RESULT", e.toString());
    }
    return transientResults;
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) MeanTransform(uk.gov.gchq.gaffer.example.gettingstarted.function.transform.MeanTransform) User(uk.gov.gchq.gaffer.user.User) ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) Element(uk.gov.gchq.gaffer.data.element.Element) ArrayList(java.util.ArrayList) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) DataGenerator4(uk.gov.gchq.gaffer.example.gettingstarted.generator.DataGenerator4) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Graph(uk.gov.gchq.gaffer.graph.Graph) GetEdges(uk.gov.gchq.gaffer.operation.impl.get.GetEdges) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Edge(uk.gov.gchq.gaffer.data.element.Edge)

Example 3 with ElementTransformer

use of uk.gov.gchq.gaffer.data.element.function.ElementTransformer in project Gaffer by gchq.

the class ViewElementDefinitionTest method shouldFailToBuildElementDefinitionWhenPreAggregationFilterSpecifiedTwice.

@Test(expected = IllegalArgumentException.class)
public void shouldFailToBuildElementDefinitionWhenPreAggregationFilterSpecifiedTwice() {
    // Given
    final ElementTransformer transformer = mock(ElementTransformer.class);
    final ElementFilter filter = mock(ElementFilter.class);
    // When
    final ViewElementDefinition elementDef = new ViewElementDefinition.Builder().transientProperty(TestPropertyNames.PROP_1, String.class).transientProperty(TestPropertyNames.PROP_2, String.class).transformer(transformer).preAggregationFilter(filter).preAggregationFilter(filter).build();
}
Also used : ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) Test(org.junit.Test)

Example 4 with ElementTransformer

use of uk.gov.gchq.gaffer.data.element.function.ElementTransformer in project Gaffer by gchq.

the class ViewElementDefinitionTest method shouldFailToBuildElementDefinitionWhenPostAggregationFilterSpecifiedTwice.

@Test(expected = IllegalArgumentException.class)
public void shouldFailToBuildElementDefinitionWhenPostAggregationFilterSpecifiedTwice() {
    // Given
    final ElementTransformer transformer = mock(ElementTransformer.class);
    final ElementFilter filter = mock(ElementFilter.class);
    // When
    final ViewElementDefinition elementDef = new ViewElementDefinition.Builder().transientProperty(TestPropertyNames.PROP_1, String.class).transientProperty(TestPropertyNames.PROP_2, String.class).transformer(transformer).postAggregationFilter(filter).postAggregationFilter(filter).build();
}
Also used : ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) Test(org.junit.Test)

Example 5 with ElementTransformer

use of uk.gov.gchq.gaffer.data.element.function.ElementTransformer in project Gaffer by gchq.

the class ViewElementDefinitionTest method shouldBuildElementDefinition.

@Test
public void shouldBuildElementDefinition() {
    // Given
    final ElementTransformer transformer = mock(ElementTransformer.class);
    final ElementFilter filter = mock(ElementFilter.class);
    final ElementFilter postFilter = mock(ElementFilter.class);
    // When
    final ViewElementDefinition elementDef = new ViewElementDefinition.Builder().transientProperty(TestPropertyNames.PROP_1, String.class).transientProperty(TestPropertyNames.PROP_2, String.class).transformer(transformer).preAggregationFilter(filter).postTransformFilter(postFilter).build();
    // Then
    assertEquals(2, elementDef.getTransientProperties().size());
    assertTrue(elementDef.containsTransientProperty(TestPropertyNames.PROP_1));
    assertTrue(elementDef.containsTransientProperty(TestPropertyNames.PROP_2));
    assertSame(filter, elementDef.getPreAggregationFilter());
    assertSame(postFilter, elementDef.getPostTransformFilter());
    assertSame(transformer, elementDef.getTransformer());
}
Also used : ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) Test(org.junit.Test)

Aggregations

ElementTransformer (uk.gov.gchq.gaffer.data.element.function.ElementTransformer)6 Test (org.junit.Test)5 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)5 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)2 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)2 ArrayList (java.util.ArrayList)1 Edge (uk.gov.gchq.gaffer.data.element.Edge)1 Element (uk.gov.gchq.gaffer.data.element.Element)1 MeanTransform (uk.gov.gchq.gaffer.example.gettingstarted.function.transform.MeanTransform)1 DataGenerator4 (uk.gov.gchq.gaffer.example.gettingstarted.generator.DataGenerator4)1 ExampleFilterFunction (uk.gov.gchq.gaffer.function.ExampleFilterFunction)1 ExampleTransformFunction (uk.gov.gchq.gaffer.function.ExampleTransformFunction)1 ConsumerFunctionContext (uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext)1 ConsumerProducerFunctionContext (uk.gov.gchq.gaffer.function.context.ConsumerProducerFunctionContext)1 Graph (uk.gov.gchq.gaffer.graph.Graph)1 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)1 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)1 GetEdges (uk.gov.gchq.gaffer.operation.impl.get.GetEdges)1 User (uk.gov.gchq.gaffer.user.User)1