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);
}
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;
}
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();
}
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();
}
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());
}
Aggregations