Search in sources :

Example 1 with DirectedType

use of uk.gov.gchq.gaffer.data.element.id.DirectedType in project Gaffer by gchq.

the class GetElementsUtil method getRelevantElements.

public static Set<Element> getRelevantElements(final MapImpl mapImpl, final ElementId elementId, final View view, final DirectedType directedType, final IncludeIncomingOutgoingType inOutType, final SeedMatchingType seedMatchingType) {
    final Set<Element> relevantElements;
    final Set<String> groups = view.getGroups();
    Predicate<Element> isFiltered = e -> !groups.contains(e.getGroup());
    if (elementId instanceof EntityId) {
        final Collection<Element> elements = mapImpl.lookup(new EntitySeed(((EntityId) elementId).getVertex()));
        if (elements.isEmpty()) {
            return Collections.emptySet();
        }
        relevantElements = new HashSet<>(elements);
        // Apply inOutType options - if option is EITHER then nothing to do
        if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
            isFiltered = isFiltered.or(e -> e instanceof Edge && ((Edge) e).isDirected() && (EdgeId.MatchedVertex.SOURCE == ((Edge) e).getMatchedVertex()));
        } else if (inOutType == IncludeIncomingOutgoingType.OUTGOING) {
            isFiltered = isFiltered.or(e -> e instanceof Edge && ((Edge) e).isDirected() && (EdgeId.MatchedVertex.DESTINATION == ((Edge) e).getMatchedVertex()));
        }
        // Apply seedMatching option - if option is RELATED then nothing to do
        if (seedMatchingType == SeedMatchingType.EQUAL) {
            isFiltered = isFiltered.or(e -> e instanceof Edge);
        }
    } else {
        relevantElements = new HashSet<>();
        final EdgeId edgeId = (EdgeId) elementId;
        if (DirectedType.isEither(edgeId.getDirectedType())) {
            relevantElements.addAll(mapImpl.lookup(new EdgeSeed(edgeId.getSource(), edgeId.getDestination(), false)));
            relevantElements.addAll(mapImpl.lookup(new EdgeSeed(edgeId.getSource(), edgeId.getDestination(), true)));
        } else {
            relevantElements.addAll(mapImpl.lookup(new EdgeSeed(edgeId.getSource(), edgeId.getDestination(), edgeId.getDirectedType())));
        }
        mapImpl.lookup(new EntitySeed(edgeId.getSource())).stream().filter(e -> e instanceof Entity).forEach(relevantElements::add);
        mapImpl.lookup(new EntitySeed(edgeId.getDestination())).stream().filter(e -> e instanceof Entity).forEach(relevantElements::add);
        // If option is RELATED then nothing to do
        if (seedMatchingType == SeedMatchingType.EQUAL) {
            isFiltered = isFiltered.or(e -> e instanceof Entity);
        }
    }
    // Apply directedType flag
    if (directedType == DirectedType.DIRECTED) {
        isFiltered = isFiltered.or(e -> e instanceof Edge && !((Edge) e).isDirected());
    } else if (directedType == DirectedType.UNDIRECTED) {
        isFiltered = isFiltered.or(e -> e instanceof Edge && ((Edge) e).isDirected());
    }
    relevantElements.removeIf(isFiltered);
    return relevantElements;
}
Also used : User(uk.gov.gchq.gaffer.user.User) LoggerFactory(org.slf4j.LoggerFactory) EdgeId(uk.gov.gchq.gaffer.data.element.id.EdgeId) Element(uk.gov.gchq.gaffer.data.element.Element) Authorisations(uk.gov.gchq.gaffer.commonutil.elementvisibilityutil.Authorisations) HashSet(java.util.HashSet) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) SeedMatchingType(uk.gov.gchq.gaffer.operation.SeedMatching.SeedMatchingType) VisibilityParseException(uk.gov.gchq.gaffer.commonutil.elementvisibilityutil.exception.VisibilityParseException) StreamSupport(java.util.stream.StreamSupport) Edge(uk.gov.gchq.gaffer.data.element.Edge) IncludeIncomingOutgoingType(uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) Logger(org.slf4j.Logger) ElementVisibility(uk.gov.gchq.gaffer.commonutil.elementvisibilityutil.ElementVisibility) DirectedType(uk.gov.gchq.gaffer.data.element.id.DirectedType) AggregatorUtil(uk.gov.gchq.gaffer.store.util.AggregatorUtil) VisibilityEvaluator(uk.gov.gchq.gaffer.commonutil.elementvisibilityutil.VisibilityEvaluator) Predicate(java.util.function.Predicate) Collection(java.util.Collection) EdgeSeed(uk.gov.gchq.gaffer.operation.data.EdgeSeed) Set(java.util.Set) Entity(uk.gov.gchq.gaffer.data.element.Entity) Collectors(java.util.stream.Collectors) Stream(java.util.stream.Stream) Schema(uk.gov.gchq.gaffer.store.schema.Schema) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) ElementId(uk.gov.gchq.gaffer.data.element.id.ElementId) Collections(java.util.Collections) Entity(uk.gov.gchq.gaffer.data.element.Entity) Element(uk.gov.gchq.gaffer.data.element.Element) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) EdgeId(uk.gov.gchq.gaffer.data.element.id.EdgeId) EdgeSeed(uk.gov.gchq.gaffer.operation.data.EdgeSeed) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Edge(uk.gov.gchq.gaffer.data.element.Edge)

Example 2 with DirectedType

use of uk.gov.gchq.gaffer.data.element.id.DirectedType in project Gaffer by gchq.

the class AbstractLoaderIT method getAllElements.

private void getAllElements(final List<Element> expectedElements) throws Exception {
    for (final boolean includeEntities : Arrays.asList(true, false)) {
        for (final boolean includeEdges : Arrays.asList(true, false)) {
            if (!includeEntities && !includeEdges) {
                // Cannot query for nothing!
                continue;
            }
            for (final DirectedType directedType : DirectedType.values()) {
                try {
                    final View.Builder viewBuilder = new View.Builder();
                    if (includeEntities) {
                        viewBuilder.entity(TestGroups.ENTITY);
                    }
                    if (includeEdges) {
                        viewBuilder.edge(TestGroups.EDGE);
                    }
                    getAllElements(expectedElements, directedType, viewBuilder.build());
                } catch (final AssertionError e) {
                    throw new AssertionError("GetAllElements failed with parameters: includeEntities=" + includeEntities + ", includeEdges=" + includeEdges + ", directedType=" + directedType.name(), e);
                }
            }
        }
    }
}
Also used : Builder(uk.gov.gchq.gaffer.data.elementdefinition.view.View.Builder) Builder(uk.gov.gchq.gaffer.data.elementdefinition.view.View.Builder) DirectedType(uk.gov.gchq.gaffer.data.element.id.DirectedType) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View)

Example 3 with DirectedType

use of uk.gov.gchq.gaffer.data.element.id.DirectedType in project Gaffer by gchq.

the class QueryGenerator method seedToPredicate.

private FilterPredicate seedToPredicate(final ParquetElementSeed seed, final SeededGraphFilters.IncludeIncomingOutgoingType includeIncomingOutgoingType, final SeedMatching.SeedMatchingType seedMatchingType, final String group, final boolean reversed) {
    final boolean isEntityGroup = schemaUtils.getEntityGroups().contains(group);
    FilterPredicate filter = null;
    final ElementId elementId = seed.getElementId();
    // Is it an entity group?
    if (isEntityGroup) {
        // EntityId case
        if (elementId instanceof EntityId) {
            filter = getIsEqualFilter(ParquetStore.VERTEX, ((ParquetEntitySeed) seed).getSeed(), group);
        } else {
            // EdgeId case
            // Does the seed type need to match the group type?
            final ParquetEdgeSeed edgeSeed = (ParquetEdgeSeed) seed;
            if (seedMatchingType != SeedMatching.SeedMatchingType.EQUAL) {
                // Vertex = source of edge seed or Vertex = destination of edge seed
                // look in partition 0 with filter src = A and partition 1 with filter src = B
                filter = getIsEqualFilter(ParquetStore.VERTEX, edgeSeed.getSource(), group);
                if (null != ((ParquetEdgeSeed) seed).getDestination()) {
                    filter = FilterPredicateUtils.or(filter, getIsEqualFilter(ParquetStore.VERTEX, edgeSeed.getDestination(), group));
                }
            }
        }
    } else {
        // EntityId case
        if (elementId instanceof EntityId) {
            // If seedMatchingType is EQUAL then we can't find anything in an edge group
            if (seedMatchingType != SeedMatching.SeedMatchingType.EQUAL) {
                if (includeIncomingOutgoingType == SeededGraphFilters.IncludeIncomingOutgoingType.INCOMING) {
                    if (reversed) {
                        // Dst is seed
                        filter = getIsEqualFilter(ParquetStore.DESTINATION, ((ParquetEntitySeed) seed).getSeed(), group);
                    } else {
                        // Src is seed and edge is undirected
                        filter = getIsEqualFilter(ParquetStore.SOURCE, ((ParquetEntitySeed) seed).getSeed(), group);
                        filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { false }, group));
                    }
                } else if (includeIncomingOutgoingType == SeededGraphFilters.IncludeIncomingOutgoingType.OUTGOING) {
                    if (reversed) {
                        // Dst is seed and edge is undirected
                        filter = getIsEqualFilter(ParquetStore.DESTINATION, ((ParquetEntitySeed) seed).getSeed(), group);
                        filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { false }, group));
                    } else {
                        // Src is seed
                        filter = getIsEqualFilter(ParquetStore.SOURCE, ((ParquetEntitySeed) seed).getSeed(), group);
                    }
                } else {
                    if (reversed) {
                        // Dst is seed
                        filter = getIsEqualFilter(ParquetStore.DESTINATION, ((ParquetEntitySeed) seed).getSeed(), group);
                    } else {
                        // Src is seed
                        filter = getIsEqualFilter(ParquetStore.SOURCE, ((ParquetEntitySeed) seed).getSeed(), group);
                    }
                }
            }
        } else {
            // EdgeId case
            final ParquetEdgeSeed edgeSeed = (ParquetEdgeSeed) seed;
            if (!reversed) {
                // Src is source of edge seed and destination is destination of edge seed
                filter = getIsEqualFilter(ParquetStore.SOURCE, edgeSeed.getSource(), group);
                // WRONG seed is already serialised source and dest - now fixed?
                filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DESTINATION, edgeSeed.getDestination(), group));
                final DirectedType directedType = edgeSeed.getDirectedType();
                if (directedType == DirectedType.DIRECTED) {
                    filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { true }, group));
                } else if (directedType == DirectedType.UNDIRECTED) {
                    filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { false }, group));
                }
            } else {
                // TODO Optimise this - there are times this is unnecessary
                filter = getIsEqualFilter(ParquetStore.DESTINATION, edgeSeed.getSource(), group);
                filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.SOURCE, edgeSeed.getDestination(), group));
                final DirectedType directedType = edgeSeed.getDirectedType();
                if (directedType == DirectedType.DIRECTED) {
                    filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { true }, group));
                } else if (directedType == DirectedType.UNDIRECTED) {
                    filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { false }, group));
                }
            }
        }
    }
    LOGGER.debug("Returning {} from seedToPredicate", filter);
    return filter;
}
Also used : EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) DirectedType(uk.gov.gchq.gaffer.data.element.id.DirectedType) FilterPredicate(org.apache.parquet.filter2.predicate.FilterPredicate) ElementId(uk.gov.gchq.gaffer.data.element.id.ElementId)

Example 4 with DirectedType

use of uk.gov.gchq.gaffer.data.element.id.DirectedType in project Gaffer by gchq.

the class GetWalksIT method shouldGetPathsWithEntities.

@Test
public void shouldGetPathsWithEntities() throws Exception {
    // Given
    final GetElements getEntities = new GetElements.Builder().directedType(DirectedType.DIRECTED).view(new View.Builder().entity(TestGroups.ENTITY).build()).build();
    final GetElements getElements = new GetElements.Builder().directedType(DirectedType.DIRECTED).inOutType(SeededGraphFilters.IncludeIncomingOutgoingType.OUTGOING).view(new View.Builder().entity(TestGroups.ENTITY).edge(TestGroups.EDGE).build()).build();
    final GetWalks op = new GetWalks.Builder().input(seedA).operations(getElements, getElements, getEntities).build();
    // When
    final List<Walk> results = Lists.newArrayList(graph.execute(op, getUser()));
    // Then
    assertThat(getPaths(results)).isEqualTo("AED,ABC");
    results.forEach(r -> r.getEntities().forEach(l -> {
        assertThat(l).isNotEmpty();
    }));
}
Also used : Walk(uk.gov.gchq.gaffer.data.graph.Walk) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ExtractProperty(uk.gov.gchq.gaffer.data.element.function.ExtractProperty) GraphConfig(uk.gov.gchq.gaffer.graph.GraphConfig) SchemaEntityDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) Edge(uk.gov.gchq.gaffer.data.element.Edge) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) TestGroups(uk.gov.gchq.gaffer.commonutil.TestGroups) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition) StringConcat(uk.gov.gchq.koryphe.impl.binaryoperator.StringConcat) Exists(uk.gov.gchq.koryphe.impl.predicate.Exists) Predicate(java.util.function.Predicate) TestPropertyNames(uk.gov.gchq.gaffer.commonutil.TestPropertyNames) Set(java.util.Set) Collectors(java.util.stream.Collectors) List(java.util.List) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) ForEach(uk.gov.gchq.gaffer.operation.impl.ForEach) AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) OperationException(uk.gov.gchq.gaffer.operation.OperationException) StoreTrait(uk.gov.gchq.gaffer.store.StoreTrait) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) AgeOff(uk.gov.gchq.koryphe.impl.predicate.AgeOff) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) Conditional(uk.gov.gchq.gaffer.operation.util.Conditional) SchemaEdgeDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition) Limit(uk.gov.gchq.gaffer.operation.impl.Limit) HashMap(java.util.HashMap) HashSet(java.util.HashSet) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Lists(com.google.common.collect.Lists) While(uk.gov.gchq.gaffer.operation.impl.While) Map(uk.gov.gchq.gaffer.operation.impl.Map) Max(uk.gov.gchq.koryphe.impl.binaryoperator.Max) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) Sum(uk.gov.gchq.koryphe.impl.binaryoperator.Sum) IsLessThan(uk.gov.gchq.koryphe.impl.predicate.IsLessThan) DirectedType(uk.gov.gchq.gaffer.data.element.id.DirectedType) Walk(uk.gov.gchq.gaffer.data.graph.Walk) AddOperationsToChain(uk.gov.gchq.gaffer.graph.hook.AddOperationsToChain) GetWalks(uk.gov.gchq.gaffer.operation.impl.GetWalks) CollectionContains(uk.gov.gchq.koryphe.impl.predicate.CollectionContains) Test(org.junit.Test) Entity(uk.gov.gchq.gaffer.data.element.Entity) KorypheFunction(uk.gov.gchq.koryphe.function.KorypheFunction) IterableConcat(uk.gov.gchq.koryphe.impl.function.IterableConcat) StoreProperties(uk.gov.gchq.gaffer.store.StoreProperties) ExtractWalkEntities(uk.gov.gchq.gaffer.data.graph.function.walk.ExtractWalkEntities) AbstractStoreIT(uk.gov.gchq.gaffer.integration.AbstractStoreIT) TraitRequirement(uk.gov.gchq.gaffer.integration.TraitRequirement) Operation(uk.gov.gchq.gaffer.operation.Operation) Schema(uk.gov.gchq.gaffer.store.schema.Schema) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) TestTypes(uk.gov.gchq.gaffer.store.TestTypes) Builder(uk.gov.gchq.gaffer.operation.impl.GetWalks.Builder) SeededGraphFilters(uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters) GetWalks(uk.gov.gchq.gaffer.operation.impl.GetWalks) Builder(uk.gov.gchq.gaffer.operation.impl.GetWalks.Builder) Builder(uk.gov.gchq.gaffer.operation.impl.GetWalks.Builder) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Test(org.junit.Test)

Example 5 with DirectedType

use of uk.gov.gchq.gaffer.data.element.id.DirectedType in project Gaffer by gchq.

the class GetElementsIT method shouldGetElements.

@Test
public void shouldGetElements() {
    final List<DirectedType> directedTypes = Lists.newArrayList(DirectedType.values());
    directedTypes.add(null);
    final List<IncludeIncomingOutgoingType> inOutTypes = Lists.newArrayList(IncludeIncomingOutgoingType.values());
    inOutTypes.add(null);
    for (final boolean includeEntities : Arrays.asList(true, false)) {
        for (final boolean includeEdges : Arrays.asList(true, false)) {
            if (!includeEntities && !includeEdges) {
                // Cannot query for nothing!
                continue;
            }
            for (final DirectedType directedType : directedTypes) {
                for (final IncludeIncomingOutgoingType inOutType : inOutTypes) {
                    try {
                        shouldGetElementsBySeed(includeEntities, includeEdges, directedType, inOutType);
                    } catch (final Throwable e) {
                        throw new AssertionError("GetElementsBySeed failed with parameters: \nincludeEntities=" + includeEntities + " \nincludeEdges=" + includeEdges + " \ndirectedType=" + directedType + " \ninOutType=" + inOutType, e);
                    }
                    try {
                        shouldGetRelatedElements(includeEntities, includeEdges, directedType, inOutType);
                    } catch (final Throwable e) {
                        throw new AssertionError("GetRelatedElements failed with parameters: \nincludeEntities=" + includeEntities + " \nincludeEdges=" + includeEdges + " \ndirectedType=" + directedType + " \ninOutType=" + inOutType, e);
                    }
                }
            }
        }
    }
}
Also used : IncludeIncomingOutgoingType(uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType) DirectedType(uk.gov.gchq.gaffer.data.element.id.DirectedType) Test(org.junit.Test)

Aggregations

DirectedType (uk.gov.gchq.gaffer.data.element.id.DirectedType)16 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)7 IncludeIncomingOutgoingType (uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType)7 ArrayList (java.util.ArrayList)6 Edge (uk.gov.gchq.gaffer.data.element.Edge)5 Entity (uk.gov.gchq.gaffer.data.element.Entity)5 EntityId (uk.gov.gchq.gaffer.data.element.id.EntityId)5 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)5 SeededGraphFilters (uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters)5 HashSet (java.util.HashSet)4 List (java.util.List)4 Set (java.util.Set)4 Test (org.junit.Test)4 TestGroups (uk.gov.gchq.gaffer.commonutil.TestGroups)4 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)4 Element (uk.gov.gchq.gaffer.data.element.Element)4 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)4 SeedMatching (uk.gov.gchq.gaffer.operation.SeedMatching)4 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)4 Collectors (java.util.stream.Collectors)3