Search in sources :

Example 21 with ViewElementDefinition

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

the class ExamplesService method generateViewBuilder.

public 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 22 with ViewElementDefinition

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

the class GetAdjacentIdsTest method shouldPassValidationOnEntitiesWithoutFilters.

@Test
public void shouldPassValidationOnEntitiesWithoutFilters() throws OperationException {
    // Given
    final Graph graph = GetAllElementsHandlerTest.getGraph();
    final AddElements addElements = new AddElements.Builder().input(GetAllElementsHandlerTest.getElements()).build();
    graph.execute(addElements, new User());
    // When
    final GetAdjacentIds getAdjacentIds = new GetAdjacentIds.Builder().input(new EntitySeed("A"), new EntitySeed("Y2")).view(new View.Builder().edge(GetAllElementsHandlerTest.BASIC_EDGE1, new ViewElementDefinition.Builder().postAggregationFilter(new ElementFilter.Builder().select(GetAllElementsHandlerTest.COUNT).execute(new IsMoreThan(5)).build()).build()).entity(GetAllElementsHandlerTest.BASIC_ENTITY, new ViewElementDefinition()).build()).build();
    final CloseableIterable<? extends EntityId> results = graph.execute(getAdjacentIds, new User());
    // Then
    final Set<EntityId> resultsSet = new HashSet<>();
    Streams.toStream(results).forEach(resultsSet::add);
    final Set<EntityId> expectedResults = new HashSet<>();
    GetAllElementsHandlerTest.getElements().stream().filter(element -> element instanceof Edge).filter(element -> element.getGroup().equals(GetAllElementsHandlerTest.BASIC_EDGE1)).filter(element -> {
        final Edge edge = (Edge) element;
        return edge.getSource().equals("A") || edge.getDestination().equals("A") || edge.getSource().equals("Y2") || edge.getDestination().equals("Y2");
    }).filter(element -> ((Integer) element.getProperty(GetAllElementsHandlerTest.COUNT)) > 5).map(element -> {
        final Edge edge = (Edge) element;
        final Set<EntityId> nodes = new HashSet<>();
        nodes.add(new EntitySeed(edge.getSource()));
        nodes.add(new EntitySeed(edge.getDestination()));
        return nodes;
    }).flatMap(nodes -> nodes.stream()).forEach(expectedResults::add);
    expectedResults.remove(new EntitySeed("A"));
    expectedResults.remove(new EntitySeed("Y2"));
    assertEquals(expectedResults, resultsSet);
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) Assertions.fail(org.junit.jupiter.api.Assertions.fail) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) User(uk.gov.gchq.gaffer.user.User) Set(java.util.Set) IsEqual(uk.gov.gchq.koryphe.impl.predicate.IsEqual) VERTEX_1(uk.gov.gchq.gaffer.mapstore.impl.VisibilityTest.VERTEX_1) Test(org.junit.jupiter.api.Test) HashSet(java.util.HashSet) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Graph(uk.gov.gchq.gaffer.graph.Graph) TestCase.assertTrue(junit.framework.TestCase.assertTrue) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) Edge(uk.gov.gchq.gaffer.data.element.Edge) OperationException(uk.gov.gchq.gaffer.operation.OperationException) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Streams(uk.gov.gchq.gaffer.commonutil.stream.Streams) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Collections(java.util.Collections) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) User(uk.gov.gchq.gaffer.user.User) GetAdjacentIds(uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) Graph(uk.gov.gchq.gaffer.graph.Graph) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) Edge(uk.gov.gchq.gaffer.data.element.Edge) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 23 with ViewElementDefinition

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

the class AbstractElementFilter method init.

@Override
public void init(final SortedKeyValueIterator<Key, Value> source, final Map<String, String> options, final IteratorEnvironment env) throws IOException {
    super.init(source, options, env);
    schema = Schema.fromJson(StringUtil.toBytes(options.get(AccumuloStoreConstants.SCHEMA)));
    LOGGER.debug("Initialising AbstractElementFilter with Schema {}", schema);
    final String elementConverterClass = options.get(AccumuloStoreConstants.ACCUMULO_ELEMENT_CONVERTER_CLASS);
    try {
        elementConverter = Class.forName(elementConverterClass).asSubclass(AccumuloElementConverter.class).getConstructor(Schema.class).newInstance(schema);
        LOGGER.debug("Creating AccumuloElementConverter of class {}", elementConverterClass);
    } catch (final ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
        throw new ElementFilterException("Failed to create element converter of the class name provided (" + elementConverterClass + ")", e);
    }
    if (filterType == ElementValidator.FilterType.SCHEMA_VALIDATION) {
        updateSchemaGroupsWithoutFilters();
        elementPredicate = new ElementValidator(schema, false)::validateWithSchema;
    } else {
        final String viewJson = options.get(AccumuloStoreConstants.VIEW);
        if (null == viewJson) {
            throw new IllegalArgumentException("Must specify the " + AccumuloStoreConstants.VIEW);
        }
        final View view = View.fromJson(StringUtil.toBytes(viewJson));
        LOGGER.debug("Determining groups that don't need to be filtered based on view {}", view);
        if (filterType == ElementValidator.FilterType.PRE_AGGREGATION_FILTER) {
            updateViewGroupsWithoutFilters(view, ViewElementDefinition::hasPreAggregationFilters);
            elementPredicate = new ElementValidator(view)::validateInput;
        } else {
            updateViewGroupsWithoutFilters(view, ViewElementDefinition::hasPostAggregationFilters);
            elementPredicate = new ElementValidator(view)::validateAggregation;
        }
    }
}
Also used : ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) ElementValidator(uk.gov.gchq.gaffer.store.ElementValidator) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) InvocationTargetException(java.lang.reflect.InvocationTargetException) ElementFilterException(uk.gov.gchq.gaffer.accumulostore.key.exception.ElementFilterException)

Example 24 with ViewElementDefinition

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

the class FiltersToOperationConverter method applyPropertyFilters.

private Output<RDD<Element>> applyPropertyFilters(final View derivedView, final Output<RDD<Element>> operation) {
    final List<Set<String>> groupsRelatedToFilters = new ArrayList<>();
    for (final Filter filter : filters) {
        final Set<String> groupsRelatedToFilter = getGroupsFromFilter(filter);
        if (null != groupsRelatedToFilter && !groupsRelatedToFilter.isEmpty()) {
            groupsRelatedToFilters.add(groupsRelatedToFilter);
        }
        LOGGER.info("Groups {} are related to filter {}", StringUtils.join(groupsRelatedToFilter, ','), filter);
    }
    LOGGER.info("Groups related to filters are: {}", StringUtils.join(groupsRelatedToFilters, ','));
    // Take the intersection of this list of groups - only these groups can be related to the query
    final Set<String> intersection = new HashSet<>(derivedView.getEntityGroups());
    intersection.addAll(derivedView.getEdgeGroups());
    for (final Set<String> groupsRelatedToFilter : groupsRelatedToFilters) {
        intersection.retainAll(groupsRelatedToFilter);
    }
    LOGGER.info("Groups that can be returned are: {}", StringUtils.join(intersection, ','));
    // Update view with filters and add to operation
    final Map<String, List<TupleAdaptedPredicate<String, ?>>> groupToFunctions = new HashMap<>();
    for (final Filter filter : filters) {
        final Map<String, List<TupleAdaptedPredicate<String, ?>>> map = getFunctionsFromFilter(filter);
        for (final Entry<String, List<TupleAdaptedPredicate<String, ?>>> entry : map.entrySet()) {
            if (!groupToFunctions.containsKey(entry.getKey())) {
                groupToFunctions.put(entry.getKey(), new ArrayList<>());
            }
            groupToFunctions.get(entry.getKey()).addAll(entry.getValue());
        }
    }
    LOGGER.info("The following functions will be applied for the given group:");
    for (final Entry<String, List<TupleAdaptedPredicate<String, ?>>> entry : groupToFunctions.entrySet()) {
        LOGGER.info("Group = {}: ", entry.getKey());
        for (final TupleAdaptedPredicate<String, ?> cfc : entry.getValue()) {
            if (null != cfc.getSelection()) {
                LOGGER.info("\t{} {}", cfc.getSelection(), cfc.getPredicate());
            } else {
                LOGGER.info("\t{} {}", StringUtils.join(cfc.getSelection(), ','), cfc.getPredicate());
            }
        }
    }
    boolean updated = false;
    View.Builder builder = new View.Builder();
    for (final String group : derivedView.getEntityGroups()) {
        if (intersection.contains(group)) {
            if (null != groupToFunctions.get(group)) {
                final ViewElementDefinition ved = new ViewElementDefinition.Builder().merge(derivedView.getEntity(group)).postAggregationFilterFunctions(groupToFunctions.get(group)).build();
                LOGGER.info("Adding the following filter functions to the view for group {}:", group);
                for (final TupleAdaptedPredicate<String, ?> cfc : groupToFunctions.get(group)) {
                    if (null != cfc.getSelection()) {
                        LOGGER.info("\t{} {}", cfc.getSelection(), cfc.getPredicate());
                    } else {
                        LOGGER.info("\t{} {}", StringUtils.join(cfc.getSelection(), ','), cfc.getPredicate());
                    }
                }
                builder = builder.entity(group, ved);
                updated = true;
            } else {
                LOGGER.info("Not adding any filter functions to the view for group {}", group);
            }
        }
    }
    for (final String group : derivedView.getEdgeGroups()) {
        if (intersection.contains(group)) {
            if (null != groupToFunctions.get(group)) {
                final ViewElementDefinition ved = new ViewElementDefinition.Builder().merge(derivedView.getEdge(group)).postAggregationFilterFunctions(groupToFunctions.get(group)).build();
                LOGGER.info("Adding the following filter functions to the view for group {}:", group);
                for (final TupleAdaptedPredicate<String, ?> cfc : groupToFunctions.get(group)) {
                    if (null != cfc.getSelection()) {
                        LOGGER.info("\t{} {}", cfc.getSelection(), cfc.getPredicate());
                    } else {
                        LOGGER.info("\t{} {}", StringUtils.join(cfc.getSelection(), ','), cfc.getPredicate());
                    }
                }
                builder = builder.edge(group, ved);
                updated = true;
            } else {
                LOGGER.info("Not adding any filter functions to the view for group {}", group);
            }
        }
    }
    if (updated) {
        ((GraphFilters) operation).setView(builder.build());
    } else {
        ((GraphFilters) operation).setView(derivedView);
    }
    return operation;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) GraphFilters(uk.gov.gchq.gaffer.operation.graph.GraphFilters) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Filter(org.apache.spark.sql.sources.Filter) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 25 with ViewElementDefinition

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

the class HBaseRetriever method deserialiseAndTransform.

private Element deserialiseAndTransform(final Cell cell) {
    try {
        Element element = serialisation.getElement(cell, includeMatchedVertex);
        final ViewElementDefinition viewDef = operation.getView().getElement(element.getGroup());
        if (null != viewDef) {
            final ElementTransformer transformer = viewDef.getTransformer();
            if (null != transformer) {
                element = transformer.apply(element);
            }
        }
        return element;
    } catch (final SerialisationException e) {
        throw new RuntimeException(e);
    }
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) Element(uk.gov.gchq.gaffer.data.element.Element) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)

Aggregations

ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)27 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)15 Test (org.junit.jupiter.api.Test)10 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)6 HashMap (java.util.HashMap)5 GlobalViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.GlobalViewElementDefinition)5 Graph (uk.gov.gchq.gaffer.graph.Graph)5 Set (java.util.Set)4 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)4 Schema (uk.gov.gchq.gaffer.store.schema.Schema)4 SchemaEdgeDefinition (uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Element (uk.gov.gchq.gaffer.data.element.Element)3 TestStore (uk.gov.gchq.gaffer.integration.store.TestStore)3 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)3 OperationView (uk.gov.gchq.gaffer.operation.graph.OperationView)3 Store (uk.gov.gchq.gaffer.store.Store)3 StoreProperties (uk.gov.gchq.gaffer.store.StoreProperties)3