Search in sources :

Example 6 with ViewElementDefinition

use of uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition in project Gaffer by gchq.

the class ExamplesService method generateViewBuilder.

protected View.Builder generateViewBuilder() {
    final View.Builder viewBuilder = new View.Builder();
    if (hasEntities()) {
        final ViewElementDefinition viewElement;
        if (null == getAnEntityPropertyName()) {
            viewElement = new ViewElementDefinition();
        } else {
            viewElement = new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select(getAnEntityPropertyName()).execute(new ExampleFilterFunction()).build()).build();
        }
        viewBuilder.entity(getAnEntityGroup(), viewElement);
    }
    if (hasEdges()) {
        final ViewElementDefinition viewElement;
        if (null == getAnEdgePropertyName()) {
            viewElement = new ViewElementDefinition();
        } else {
            viewElement = new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select(getAnEdgePropertyName()).execute(new ExampleFilterFunction()).build()).build();
        }
        viewBuilder.edge(getAnEdgeGroup(), viewElement);
    }
    viewBuilder.globalElements(new GlobalViewElementDefinition.Builder().groupBy().build());
    return viewBuilder;
}
Also used : GlobalViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.GlobalViewElementDefinition) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) ExampleFilterFunction(uk.gov.gchq.gaffer.rest.example.ExampleFilterFunction) GlobalViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.GlobalViewElementDefinition) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View)

Example 7 with ViewElementDefinition

use of uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition in project Gaffer by gchq.

the class ElementValidator method validateAgainstViewFilter.

private boolean validateAgainstViewFilter(final Element element, final FilterType filterType) {
    if (null == element) {
        return false;
    }
    if (null != schema) {
        return validateWithSchema(element);
    }
    final ViewElementDefinition elementDef = view.getElement(element.getGroup());
    if (null == elementDef) {
        return false;
    }
    final ElementFilter validator = getElementFilter(elementDef, filterType);
    return null == validator || validator.filter(element);
}
Also used : ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)

Example 8 with ViewElementDefinition

use of uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition in project Gaffer by gchq.

the class ViewValidator method validate.

/**
     * Checks all {@link uk.gov.gchq.gaffer.function.FilterFunction}s and {@link uk.gov.gchq.gaffer.function.TransformFunction}s defined are
     * compatible with the identifiers and properties in the {@link Schema}
     * and transient properties in the {@link View}.
     *
     * @param view           the {@link View} to validate
     * @param schema         the {@link Schema} to validate the view against
     * @param isStoreOrdered true if the store is ordered
     * @return true if the element definition is valid, otherwise false and an error is logged
     */
public boolean validate(final View view, final Schema schema, final boolean isStoreOrdered) {
    boolean isValid = true;
    if (null != view) {
        if (null != view.getEntities()) {
            for (final Entry<String, ViewElementDefinition> entry : view.getEntities().entrySet()) {
                final String group = entry.getKey();
                final SchemaEntityDefinition schemaElDef = schema.getEntity(group);
                final ViewElementDefinition viewElDef = entry.getValue();
                if (null == schemaElDef) {
                    LOGGER.error("Entity group " + group + " does not exist in the schema");
                    isValid = false;
                } else {
                    for (final String transProp : viewElDef.getTransientProperties()) {
                        if (schemaElDef.containsProperty(transProp)) {
                            LOGGER.error("Transient property " + transProp + " for entity group " + group + " is not transient as it has been found in the schema");
                            isValid = false;
                        }
                    }
                    if (!validateFunctionArgumentTypes(viewElDef.getPreAggregationFilter(), viewElDef, schemaElDef)) {
                        isValid = false;
                    }
                    if (!validateFunctionArgumentTypes(viewElDef.getPostAggregationFilter(), viewElDef, schemaElDef)) {
                        isValid = false;
                    }
                    if (!validateFunctionArgumentTypes(viewElDef.getTransformer(), viewElDef, schemaElDef)) {
                        isValid = false;
                    }
                    if (!validateFunctionArgumentTypes(viewElDef.getPostTransformFilter(), viewElDef, schemaElDef)) {
                        isValid = false;
                    }
                    if (!validateGroupBy(isStoreOrdered, group, viewElDef, schemaElDef)) {
                        isValid = false;
                    }
                }
            }
        }
        if (null != view.getEdges()) {
            for (final Entry<String, ViewElementDefinition> entry : view.getEdges().entrySet()) {
                final String group = entry.getKey();
                final SchemaEdgeDefinition schemaElDef = schema.getEdge(group);
                final ViewElementDefinition viewElDef = entry.getValue();
                if (null == schemaElDef) {
                    LOGGER.error("Edge group " + group + " does not exist in the schema");
                    isValid = false;
                } else {
                    for (final String transProp : viewElDef.getTransientProperties()) {
                        if (schemaElDef.containsProperty(transProp)) {
                            LOGGER.error("Transient property " + transProp + " for edge group " + group + " is not transient as it has been found in the schema");
                            isValid = false;
                        }
                    }
                    if (!validateFunctionArgumentTypes(viewElDef.getPreAggregationFilter(), viewElDef, schemaElDef)) {
                        isValid = false;
                    }
                    if (!validateFunctionArgumentTypes(viewElDef.getPostAggregationFilter(), viewElDef, schemaElDef)) {
                        isValid = false;
                    }
                    if (!validateFunctionArgumentTypes(viewElDef.getTransformer(), viewElDef, schemaElDef)) {
                        isValid = false;
                    }
                    if (!validateFunctionArgumentTypes(viewElDef.getPostTransformFilter(), viewElDef, schemaElDef)) {
                        isValid = false;
                    }
                    if (!validateGroupBy(isStoreOrdered, group, viewElDef, schemaElDef)) {
                        isValid = false;
                    }
                }
            }
        }
    }
    return isValid;
}
Also used : ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)

Example 9 with ViewElementDefinition

use of uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition in project Gaffer by gchq.

the class ElementValidatorTest method shouldReturnTrueWhenViewValidateWithValidElement.

@Test
public void shouldReturnTrueWhenViewValidateWithValidElement() {
    // Given
    final View view = mock(View.class);
    final String group = TestGroups.EDGE;
    final Element elm = mock(Element.class);
    final ViewElementDefinition elementDef = mock(ViewElementDefinition.class);
    final ElementFilter filter = mock(ElementFilter.class);
    final ElementValidator validator = new ElementValidator(view);
    given(elm.getGroup()).willReturn(group);
    given(view.getElement(group)).willReturn(elementDef);
    given(elementDef.getPreAggregationFilter()).willReturn(filter);
    given(filter.filter(elm)).willReturn(true);
    // When
    final boolean isValid = validator.validate(elm);
    // Then
    assertTrue(isValid);
}
Also used : Element(uk.gov.gchq.gaffer.data.element.Element) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Test(org.junit.Test)

Aggregations

ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)9 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)6 Test (org.junit.Test)4 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)4 HashSet (java.util.HashSet)2 Element (uk.gov.gchq.gaffer.data.element.Element)2 ConsumerFunctionContext (uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext)2 Schema (uk.gov.gchq.gaffer.store.schema.Schema)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Set (java.util.Set)1 Filter (org.apache.spark.sql.sources.Filter)1 ElementTransformer (uk.gov.gchq.gaffer.data.element.function.ElementTransformer)1 GlobalViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.GlobalViewElementDefinition)1 ExampleFilterFunction (uk.gov.gchq.gaffer.function.ExampleFilterFunction)1 ExampleTransformFunction (uk.gov.gchq.gaffer.function.ExampleTransformFunction)1 FilterFunction (uk.gov.gchq.gaffer.function.FilterFunction)1 ConsumerProducerFunctionContext (uk.gov.gchq.gaffer.function.context.ConsumerProducerFunctionContext)1 ExampleFilterFunction (uk.gov.gchq.gaffer.rest.example.ExampleFilterFunction)1